protected void importAssetLink(AssetLink assetLink, long entryId1)
      throws PortalException, SystemException {

    long entryId2 = assetLink.getEntryId2();

    AssetEntry assetEntry2 = assetEntryPersistence.findByPrimaryKey(entryId2);

    if (assetEntry2.getClassNameId() == classNameLocalService.getClassNameId(CalEvent.class)) {

      CalEvent calEvent = calEventPersistence.findByPrimaryKey(assetEntry2.getClassPK());

      importCalEvent(calEvent);

      assetEntry2 = assetEntryPersistence.findByG_CU(calEvent.getGroupId(), calEvent.getUuid());

      entryId2 = assetEntry2.getEntryId();
    }

    long linkId = counterLocalService.increment();

    addAssetLink(
        linkId,
        assetLink.getCompanyId(),
        assetLink.getUserId(),
        assetLink.getUserName(),
        assetLink.getCreateDate(),
        entryId1,
        entryId2,
        assetLink.getType(),
        assetLink.getWeight());
  }
  public AssetEntry getNextEntry(long entryId) throws PortalException, SystemException {

    try {
      getParentEntry(entryId);
    } catch (NoSuchEntryException nsee) {
      List<AssetEntry> childEntries = getChildEntries(entryId);

      if (childEntries.isEmpty()) {
        throw new NoSuchEntryException();
      }

      return childEntries.get(0);
    }

    List<AssetLink> links =
        assetLinkLocalService.getDirectLinks(entryId, AssetLinkConstants.TYPE_CHILD);

    for (int i = 0; i < links.size(); i++) {
      AssetLink link = links.get(i);

      if (link.getEntryId2() == entryId) {
        if ((i + 1) >= links.size()) {
          throw new NoSuchEntryException();
        } else {
          AssetLink nextLink = links.get(i + 1);

          return getEntry(nextLink.getEntryId2());
        }
      }
    }

    throw new NoSuchEntryException();
  }
  protected void addAssetLink(
      long linkId,
      long companyId,
      long userId,
      String userName,
      Date createDate,
      long entryId1,
      long entryId2,
      int type,
      int weight)
      throws SystemException {

    AssetLink assetLink = assetLinkPersistence.create(linkId);

    assetLink.setCompanyId(companyId);
    assetLink.setUserId(userId);
    assetLink.setUserName(userName);
    assetLink.setCreateDate(createDate);
    assetLink.setEntryId1(entryId1);
    assetLink.setEntryId2(entryId2);
    assetLink.setType(type);
    assetLink.setWeight(weight);

    assetLinkPersistence.update(assetLink);
  }
  public void addAssetLinks(Class<?> clazz, long classPK) throws PortalException, SystemException {

    AssetEntry assetEntry = null;

    try {
      assetEntry = AssetEntryLocalServiceUtil.getEntry(clazz.getName(), classPK);
    } catch (NoSuchEntryException nsee) {
      return;
    }

    List<AssetLink> directAssetLinks =
        AssetLinkLocalServiceUtil.getDirectLinks(assetEntry.getEntryId());

    if (directAssetLinks.isEmpty()) {
      return;
    }

    Map<Integer, List<AssetLink>> assetLinksMap = new HashMap<Integer, List<AssetLink>>();

    for (AssetLink assetLink : directAssetLinks) {
      List<AssetLink> assetLinks = assetLinksMap.get(assetLink.getType());

      if (assetLinks == null) {
        assetLinks = new ArrayList<AssetLink>();

        assetLinksMap.put(assetLink.getType(), assetLinks);
      }

      assetLinks.add(assetLink);
    }

    for (Map.Entry<Integer, List<AssetLink>> entry : assetLinksMap.entrySet()) {

      int assetLinkType = entry.getKey();
      List<AssetLink> assetLinks = entry.getValue();

      List<String> assetLinkUuids = new ArrayList<String>(directAssetLinks.size());

      for (AssetLink assetLink : assetLinks) {
        try {
          AssetEntry assetLinkEntry = AssetEntryLocalServiceUtil.getEntry(assetLink.getEntryId2());

          assetLinkUuids.add(assetLinkEntry.getClassUuid());
        } catch (NoSuchEntryException nsee) {
        }
      }

      _assetLinkUuidsMap.put(
          getPrimaryKeyString(assetEntry.getClassUuid(), String.valueOf(assetLinkType)),
          assetLinkUuids.toArray(new String[assetLinkUuids.size()]));
    }
  }
  public AssetEntry getParentEntry(long entryId) throws PortalException, SystemException {

    List<AssetLink> links =
        assetLinkLocalService.getReverseLinks(entryId, AssetLinkConstants.TYPE_CHILD);

    if (links.isEmpty()) {
      throw new NoSuchEntryException();
    }

    AssetLink link = links.get(0);

    return getEntry(link.getEntryId1());
  }
  public List<AssetEntry> getChildEntries(long entryId) throws PortalException, SystemException {

    List<AssetEntry> entries = new ArrayList<AssetEntry>();

    List<AssetLink> links =
        assetLinkLocalService.getDirectLinks(entryId, AssetLinkConstants.TYPE_CHILD);

    for (AssetLink link : links) {
      AssetEntry curAsset = getEntry(link.getEntryId2());

      entries.add(curAsset);
    }

    return entries;
  }
  public int compareTo(AssetLink assetLink) {
    int value = 0;

    if (getWeight() < assetLink.getWeight()) {
      value = -1;
    } else if (getWeight() > assetLink.getWeight()) {
      value = 1;
    } else {
      value = 0;
    }

    if (value != 0) {
      return value;
    }

    return 0;
  }
  @Test
  public void testDeleteLinksByAssetEntryGroupId() throws Exception {

    // Add link between entries in group 1

    AssetEntry assetEntry1 = AssetTestUtil.addAssetEntry(_group1.getGroupId());
    AssetEntry assetEntry2 = AssetTestUtil.addAssetEntry(_group1.getGroupId());

    AssetLinkLocalServiceUtil.addLink(
        TestPropsValues.getUserId(), assetEntry1.getEntryId(), assetEntry2.getEntryId(), 0, 0);

    // Add link between entries in different groups

    assetEntry1 = AssetTestUtil.addAssetEntry(_group1.getGroupId());
    assetEntry2 = AssetTestUtil.addAssetEntry(_group2.getGroupId());

    AssetLinkLocalServiceUtil.addLink(
        TestPropsValues.getUserId(), assetEntry1.getEntryId(), assetEntry2.getEntryId(), 0, 0);

    // Add link between entries in group 2

    assetEntry1 = AssetTestUtil.addAssetEntry(_group2.getGroupId());
    assetEntry2 = AssetTestUtil.addAssetEntry(_group2.getGroupId());

    AssetLinkLocalServiceUtil.addLink(
        TestPropsValues.getUserId(), assetEntry1.getEntryId(), assetEntry2.getEntryId(), 0, 0);

    AssetLinkLocalServiceUtil.deleteGroupLinks(_group1.getGroupId());

    List<AssetLink> assetLinks =
        AssetLinkLocalServiceUtil.getAssetLinks(QueryUtil.ALL_POS, QueryUtil.ALL_POS);

    Assert.assertNotNull(assetLinks);
    Assert.assertTrue(assetLinks.size() > 0);

    for (AssetLink assetLink : assetLinks) {
      AssetEntry assetEntry = AssetEntryLocalServiceUtil.getEntry(assetLink.getEntryId1());

      Assert.assertTrue(assetEntry.getGroupId() != _group1.getGroupId());

      assetEntry = AssetEntryLocalServiceUtil.getEntry(assetLink.getEntryId2());

      Assert.assertTrue(assetEntry.getGroupId() != _group1.getGroupId());
    }
  }
  public boolean equals(Object obj) {
    if (obj == null) {
      return false;
    }

    AssetLink assetLink = null;

    try {
      assetLink = (AssetLink) obj;
    } catch (ClassCastException cce) {
      return false;
    }

    long pk = assetLink.getPrimaryKey();

    if (getPrimaryKey() == pk) {
      return true;
    } else {
      return false;
    }
  }
  public AssetEntry getPreviousEntry(long entryId) throws PortalException, SystemException {

    getParentEntry(entryId);

    List<AssetLink> links =
        assetLinkLocalService.getDirectLinks(entryId, AssetLinkConstants.TYPE_CHILD);

    for (int i = 0; i < links.size(); i++) {
      AssetLink link = links.get(i);

      if (link.getEntryId2() == entryId) {
        if (i == 0) {
          throw new NoSuchEntryException();
        } else {
          AssetLink nextAssetLink = links.get(i - 1);

          return getEntry(nextAssetLink.getEntryId2());
        }
      }
    }

    throw new NoSuchEntryException();
  }
  public AssetLink addLink(long userId, long entryId1, long entryId2, int type, int weight)
      throws PortalException, SystemException {

    User user = userLocalService.getUser(userId);
    Date now = new Date();

    long linkId = counterLocalService.increment();

    AssetLink link = assetLinkPersistence.create(linkId);

    link.setCompanyId(user.getCompanyId());
    link.setUserId(user.getUserId());
    link.setUserName(user.getFullName());
    link.setCreateDate(now);
    link.setEntryId1(entryId1);
    link.setEntryId2(entryId2);
    link.setType(type);
    link.setWeight(weight);

    assetLinkPersistence.update(link, false);

    return link;
  }
  /**
   * Adds the asset link to the database. Also notifies the appropriate model listeners.
   *
   * @param assetLink the asset link
   * @return the asset link that was added
   * @throws SystemException if a system exception occurred
   */
  public AssetLink addAssetLink(AssetLink assetLink) throws SystemException {
    assetLink.setNew(true);

    assetLink = assetLinkPersistence.update(assetLink, false);

    Indexer indexer = IndexerRegistryUtil.getIndexer(getModelClassName());

    if (indexer != null) {
      try {
        indexer.reindex(assetLink);
      } catch (SearchException se) {
        if (_log.isWarnEnabled()) {
          _log.warn(se, se);
        }
      }
    }

    return assetLink;
  }
  protected void readAssetLinks(PortletDataContext portletDataContext) throws Exception {

    String xml =
        portletDataContext.getZipEntryAsString(
            ExportImportPathUtil.getSourceRootPath(portletDataContext) + "/links.xml");

    if (xml == null) {
      return;
    }

    Document document = SAXReaderUtil.read(xml);

    Element rootElement = document.getRootElement();

    List<Element> assetLinkGroupElements = rootElement.elements("asset-link-group");

    for (Element assetLinkGroupElement : assetLinkGroupElements) {
      String sourceUuid = assetLinkGroupElement.attributeValue("source-uuid");

      AssetEntry sourceAssetEntry =
          AssetEntryLocalServiceUtil.fetchEntry(portletDataContext.getScopeGroupId(), sourceUuid);

      if (sourceAssetEntry == null) {
        sourceAssetEntry =
            AssetEntryLocalServiceUtil.fetchEntry(
                portletDataContext.getCompanyGroupId(), sourceUuid);
      }

      if (sourceAssetEntry == null) {
        if (_log.isWarnEnabled()) {
          _log.warn("Unable to find asset entry with uuid " + sourceUuid);
        }

        continue;
      }

      List<Element> assetLinksElements = assetLinkGroupElement.elements("asset-link");

      for (Element assetLinkElement : assetLinksElements) {
        String path = assetLinkElement.attributeValue("path");

        if (!portletDataContext.isPathNotProcessed(path)) {
          continue;
        }

        String targetUuid = assetLinkElement.attributeValue("target-uuid");

        AssetEntry targetAssetEntry =
            AssetEntryLocalServiceUtil.fetchEntry(portletDataContext.getScopeGroupId(), targetUuid);

        if (targetAssetEntry == null) {
          targetAssetEntry =
              AssetEntryLocalServiceUtil.fetchEntry(
                  portletDataContext.getCompanyGroupId(), targetUuid);
        }

        if (targetAssetEntry == null) {
          if (_log.isWarnEnabled()) {
            _log.warn("Unable to find asset entry with uuid " + targetUuid);
          }

          continue;
        }

        AssetLink assetLink = (AssetLink) portletDataContext.getZipEntryAsObject(path);

        long userId = portletDataContext.getUserId(assetLink.getUserUuid());

        AssetLinkLocalServiceUtil.updateLink(
            userId,
            sourceAssetEntry.getEntryId(),
            targetAssetEntry.getEntryId(),
            assetLink.getType(),
            assetLink.getWeight());
      }
    }
  }