protected void cascadeFileEntryTypes(
      long groupId,
      long folderId,
      long defaultFileEntryTypeId,
      List<Long> fileEntryTypeIds,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    List<DLFileEntry> dlFileEntries = dlFileEntryPersistence.findByG_F(groupId, folderId);

    for (DLFileEntry dlFileEntry : dlFileEntries) {
      Long fileEntryTypeId = dlFileEntry.getFileEntryTypeId();

      if (fileEntryTypeIds.contains(fileEntryTypeId)) {
        continue;
      }

      DLFileVersion dlFileVersion =
          dlFileVersionLocalService.getLatestFileVersion(dlFileEntry.getFileEntryId(), true);

      if (dlFileVersion.isPending()) {
        workflowInstanceLinkLocalService.deleteWorkflowInstanceLink(
            dlFileVersion.getCompanyId(),
            groupId,
            DLFileEntry.class.getName(),
            dlFileVersion.getFileVersionId());
      }

      dlFileEntryService.updateFileEntry(
          dlFileEntry.getFileEntryId(),
          null,
          null,
          null,
          null,
          null,
          false,
          defaultFileEntryTypeId,
          null,
          null,
          null,
          0,
          serviceContext);
    }

    List<DLFolder> subFolders = dlFolderPersistence.findByG_M_P_H(groupId, false, folderId, false);

    for (DLFolder subFolder : subFolders) {
      long subFolderId = subFolder.getFolderId();

      if (subFolder.isOverrideFileEntryTypes()) {
        continue;
      }

      cascadeFileEntryTypes(
          groupId, subFolderId, defaultFileEntryTypeId, fileEntryTypeIds, serviceContext);
    }
  }
Exemple #2
0
  public static SyncDLObject toSyncDLObject(
      DLFileEntry dlFileEntry, String event, boolean excludeWorkingCopy)
      throws PortalException, SystemException {

    DLFileVersion dlFileVersion = null;

    Date lockExpirationDate = null;
    long lockUserId = 0;
    String lockUserName = StringPool.BLANK;
    String type = null;

    Lock lock = dlFileEntry.getLock();

    if ((lock == null) || excludeWorkingCopy) {
      dlFileVersion =
          DLFileVersionLocalServiceUtil.getFileVersion(
              dlFileEntry.getFileEntryId(), dlFileEntry.getVersion());

      type = SyncConstants.TYPE_FILE;
    } else {
      dlFileVersion =
          DLFileVersionLocalServiceUtil.getFileVersion(
              dlFileEntry.getFileEntryId(), DLFileEntryConstants.PRIVATE_WORKING_COPY_VERSION);

      lockExpirationDate = lock.getExpirationDate();
      lockUserId = lock.getUserId();
      lockUserName = lock.getUserName();
      type = SyncConstants.TYPE_PRIVATE_WORKING_COPY;
    }

    SyncDLObject syncDLObject = new SyncDLObjectImpl();

    syncDLObject.setCompanyId(dlFileVersion.getCompanyId());
    syncDLObject.setCreateDate(dlFileVersion.getCreateDate());
    syncDLObject.setModifiedDate(dlFileVersion.getModifiedDate());
    syncDLObject.setRepositoryId(dlFileVersion.getRepositoryId());
    syncDLObject.setParentFolderId(dlFileVersion.getFolderId());
    syncDLObject.setName(dlFileVersion.getTitle());
    syncDLObject.setExtension(dlFileVersion.getExtension());
    syncDLObject.setMimeType(dlFileVersion.getMimeType());
    syncDLObject.setDescription(dlFileVersion.getDescription());
    syncDLObject.setChangeLog(dlFileVersion.getChangeLog());
    syncDLObject.setExtraSettings(dlFileVersion.getExtraSettings());
    syncDLObject.setVersion(dlFileVersion.getVersion());
    syncDLObject.setSize(dlFileVersion.getSize());
    syncDLObject.setChecksum(getChecksum(dlFileVersion));
    syncDLObject.setEvent(event);
    syncDLObject.setLockExpirationDate(lockExpirationDate);
    syncDLObject.setLockUserId(lockUserId);
    syncDLObject.setLockUserName(lockUserName);
    syncDLObject.setType(type);
    syncDLObject.setTypePK(dlFileEntry.getFileEntryId());
    syncDLObject.setTypeUuid(dlFileEntry.getUuid());

    return syncDLObject;
  }
  protected void cascadeFileEntryTypes(
      long userId,
      long groupId,
      long folderId,
      long defaultFileEntryTypeId,
      List<Long> fileEntryTypeIds,
      ServiceContext serviceContext)
      throws PortalException {

    List<DLFileEntry> dlFileEntries = dlFileEntryPersistence.findByG_F(groupId, folderId);

    for (DLFileEntry dlFileEntry : dlFileEntries) {
      Long fileEntryTypeId = dlFileEntry.getFileEntryTypeId();

      if (fileEntryTypeIds.contains(fileEntryTypeId)) {
        continue;
      }

      DLFileVersion dlFileVersion =
          dlFileVersionLocalService.getLatestFileVersion(dlFileEntry.getFileEntryId(), true);

      if (dlFileVersion.isPending()) {
        workflowInstanceLinkLocalService.deleteWorkflowInstanceLink(
            dlFileVersion.getCompanyId(),
            groupId,
            DLFileEntry.class.getName(),
            dlFileVersion.getFileVersionId());
      }

      dlFileEntryLocalService.updateFileEntryType(
          userId, dlFileEntry.getFileEntryId(), defaultFileEntryTypeId, serviceContext);

      dlAppHelperLocalService.updateAsset(
          userId,
          new LiferayFileEntry(dlFileEntry),
          new LiferayFileVersion(dlFileVersion),
          serviceContext.getAssetCategoryIds(),
          serviceContext.getAssetTagNames(),
          serviceContext.getAssetLinkEntryIds());
    }

    List<DLFolder> subFolders = dlFolderPersistence.findByG_M_P_H(groupId, false, folderId, false);

    for (DLFolder subFolder : subFolders) {
      long subFolderId = subFolder.getFolderId();

      if (subFolder.getRestrictionType() == DLFolderConstants.RESTRICTION_TYPE_INHERIT) {

        continue;
      }

      cascadeFileEntryTypes(
          userId, groupId, subFolderId, defaultFileEntryTypeId, fileEntryTypeIds, serviceContext);
    }
  }
  @Override
  public DLFileEntry updateFileEntryFileEntryType(
      DLFileEntry dlFileEntry, ServiceContext serviceContext) throws PortalException {

    long groupId = serviceContext.getScopeGroupId();
    long folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;

    DLFolder dlFolder = dlFolderPersistence.fetchByPrimaryKey(dlFileEntry.getFolderId());

    if (dlFolder != null) {
      groupId = dlFolder.getGroupId();
      folderId = dlFolder.getFolderId();
    }

    List<DLFileEntryType> dlFileEntryTypes =
        getFolderFileEntryTypes(
            PortalUtil.getCurrentAndAncestorSiteGroupIds(groupId), folderId, true);

    List<Long> fileEntryTypeIds = getFileEntryTypeIds(dlFileEntryTypes);

    if (fileEntryTypeIds.contains(dlFileEntry.getFileEntryTypeId())) {
      return dlFileEntry;
    }

    long defaultFileEntryTypeId = getDefaultFileEntryTypeId(folderId);

    DLFileVersion dlFileVersion =
        dlFileVersionLocalService.getLatestFileVersion(dlFileEntry.getFileEntryId(), true);

    if (dlFileVersion.isPending()) {
      workflowInstanceLinkLocalService.deleteWorkflowInstanceLink(
          dlFileVersion.getCompanyId(), dlFileEntry.getGroupId(),
          DLFileEntry.class.getName(), dlFileVersion.getFileVersionId());
    }

    return dlFileEntryLocalService.updateFileEntry(
        serviceContext.getUserId(),
        dlFileEntry.getFileEntryId(),
        null,
        null,
        null,
        null,
        null,
        false,
        defaultFileEntryTypeId,
        null,
        null,
        null,
        0,
        serviceContext);
  }
  public DLFileVersion updateDLFileVersion(DLFileVersion model) throws SystemException {
    DLFileVersion dlFileVersion = new DLFileVersionImpl();

    dlFileVersion.setNew(false);

    dlFileVersion.setFileVersionId(model.getFileVersionId());
    dlFileVersion.setCompanyId(model.getCompanyId());
    dlFileVersion.setUserId(model.getUserId());
    dlFileVersion.setUserName(model.getUserName());
    dlFileVersion.setCreateDate(model.getCreateDate());
    dlFileVersion.setFolderId(model.getFolderId());
    dlFileVersion.setName(model.getName());
    dlFileVersion.setVersion(model.getVersion());
    dlFileVersion.setSize(model.getSize());

    return dlFileVersionPersistence.update(dlFileVersion);
  }
  public void addFileEntry(
      long userId, FileEntry fileEntry, FileVersion fileVersion, ServiceContext serviceContext)
      throws PortalException, SystemException {

    updateAsset(
        userId,
        fileEntry,
        fileVersion,
        serviceContext.getAssetCategoryIds(),
        serviceContext.getAssetTagNames(),
        serviceContext.getAssetLinkEntryIds());

    if (PropsValues.DL_FILE_ENTRY_COMMENTS_ENABLED) {
      mbMessageLocalService.addDiscussionMessage(
          fileEntry.getUserId(), fileEntry.getUserName(),
          fileEntry.getGroupId(), DLFileEntryConstants.getClassName(),
          fileEntry.getFileEntryId(), WorkflowConstants.ACTION_PUBLISH);
    }

    if (fileVersion instanceof LiferayFileVersion) {
      DLFileVersion dlFileVersion = (DLFileVersion) fileVersion.getModel();

      Map<String, Serializable> workflowContext = new HashMap<String, Serializable>();

      workflowContext.put("event", DLSyncConstants.EVENT_ADD);

      WorkflowHandlerRegistryUtil.startWorkflowInstance(
          dlFileVersion.getCompanyId(),
          dlFileVersion.getGroupId(),
          userId,
          DLFileEntryConstants.getClassName(),
          dlFileVersion.getFileVersionId(),
          dlFileVersion,
          serviceContext,
          workflowContext);
    }

    registerDLProcessorCallback(fileEntry, null);
  }
  @Test
  public void testUpdateExisting() throws Exception {
    long pk = RandomTestUtil.nextLong();

    DLFileVersion newDLFileVersion = _persistence.create(pk);

    newDLFileVersion.setUuid(RandomTestUtil.randomString());

    newDLFileVersion.setGroupId(RandomTestUtil.nextLong());

    newDLFileVersion.setCompanyId(RandomTestUtil.nextLong());

    newDLFileVersion.setUserId(RandomTestUtil.nextLong());

    newDLFileVersion.setUserName(RandomTestUtil.randomString());

    newDLFileVersion.setCreateDate(RandomTestUtil.nextDate());

    newDLFileVersion.setModifiedDate(RandomTestUtil.nextDate());

    newDLFileVersion.setRepositoryId(RandomTestUtil.nextLong());

    newDLFileVersion.setFolderId(RandomTestUtil.nextLong());

    newDLFileVersion.setFileEntryId(RandomTestUtil.nextLong());

    newDLFileVersion.setTreePath(RandomTestUtil.randomString());

    newDLFileVersion.setExtension(RandomTestUtil.randomString());

    newDLFileVersion.setMimeType(RandomTestUtil.randomString());

    newDLFileVersion.setTitle(RandomTestUtil.randomString());

    newDLFileVersion.setDescription(RandomTestUtil.randomString());

    newDLFileVersion.setChangeLog(RandomTestUtil.randomString());

    newDLFileVersion.setExtraSettings(RandomTestUtil.randomString());

    newDLFileVersion.setFileEntryTypeId(RandomTestUtil.nextLong());

    newDLFileVersion.setVersion(RandomTestUtil.randomString());

    newDLFileVersion.setSize(RandomTestUtil.nextLong());

    newDLFileVersion.setChecksum(RandomTestUtil.randomString());

    newDLFileVersion.setStatus(RandomTestUtil.nextInt());

    newDLFileVersion.setStatusByUserId(RandomTestUtil.nextLong());

    newDLFileVersion.setStatusByUserName(RandomTestUtil.randomString());

    newDLFileVersion.setStatusDate(RandomTestUtil.nextDate());

    _persistence.update(newDLFileVersion);

    DLFileVersion existingDLFileVersion =
        _persistence.findByPrimaryKey(newDLFileVersion.getPrimaryKey());

    Assert.assertEquals(existingDLFileVersion.getUuid(), newDLFileVersion.getUuid());
    Assert.assertEquals(
        existingDLFileVersion.getFileVersionId(), newDLFileVersion.getFileVersionId());
    Assert.assertEquals(existingDLFileVersion.getGroupId(), newDLFileVersion.getGroupId());
    Assert.assertEquals(existingDLFileVersion.getCompanyId(), newDLFileVersion.getCompanyId());
    Assert.assertEquals(existingDLFileVersion.getUserId(), newDLFileVersion.getUserId());
    Assert.assertEquals(existingDLFileVersion.getUserName(), newDLFileVersion.getUserName());
    Assert.assertEquals(
        Time.getShortTimestamp(existingDLFileVersion.getCreateDate()),
        Time.getShortTimestamp(newDLFileVersion.getCreateDate()));
    Assert.assertEquals(
        Time.getShortTimestamp(existingDLFileVersion.getModifiedDate()),
        Time.getShortTimestamp(newDLFileVersion.getModifiedDate()));
    Assert.assertEquals(
        existingDLFileVersion.getRepositoryId(), newDLFileVersion.getRepositoryId());
    Assert.assertEquals(existingDLFileVersion.getFolderId(), newDLFileVersion.getFolderId());
    Assert.assertEquals(existingDLFileVersion.getFileEntryId(), newDLFileVersion.getFileEntryId());
    Assert.assertEquals(existingDLFileVersion.getTreePath(), newDLFileVersion.getTreePath());
    Assert.assertEquals(existingDLFileVersion.getExtension(), newDLFileVersion.getExtension());
    Assert.assertEquals(existingDLFileVersion.getMimeType(), newDLFileVersion.getMimeType());
    Assert.assertEquals(existingDLFileVersion.getTitle(), newDLFileVersion.getTitle());
    Assert.assertEquals(existingDLFileVersion.getDescription(), newDLFileVersion.getDescription());
    Assert.assertEquals(existingDLFileVersion.getChangeLog(), newDLFileVersion.getChangeLog());
    Assert.assertEquals(
        existingDLFileVersion.getExtraSettings(), newDLFileVersion.getExtraSettings());
    Assert.assertEquals(
        existingDLFileVersion.getFileEntryTypeId(), newDLFileVersion.getFileEntryTypeId());
    Assert.assertEquals(existingDLFileVersion.getVersion(), newDLFileVersion.getVersion());
    Assert.assertEquals(existingDLFileVersion.getSize(), newDLFileVersion.getSize());
    Assert.assertEquals(existingDLFileVersion.getChecksum(), newDLFileVersion.getChecksum());
    Assert.assertEquals(existingDLFileVersion.getStatus(), newDLFileVersion.getStatus());
    Assert.assertEquals(
        existingDLFileVersion.getStatusByUserId(), newDLFileVersion.getStatusByUserId());
    Assert.assertEquals(
        existingDLFileVersion.getStatusByUserName(), newDLFileVersion.getStatusByUserName());
    Assert.assertEquals(
        Time.getShortTimestamp(existingDLFileVersion.getStatusDate()),
        Time.getShortTimestamp(newDLFileVersion.getStatusDate()));
  }
  public void updateStatuses(User user, List<Object> dlFileEntriesAndDLFolders, int status)
      throws PortalException, SystemException {

    for (Object object : dlFileEntriesAndDLFolders) {
      if (object instanceof DLFileEntry) {
        DLFileEntry dlFileEntry = (DLFileEntry) object;

        List<DLFileVersion> dlFileVersions =
            dlFileVersionLocalService.getFileVersions(
                dlFileEntry.getFileEntryId(), WorkflowConstants.STATUS_ANY);

        dlFileVersions = ListUtil.copy(dlFileVersions);

        Collections.sort(dlFileVersions, new FileVersionVersionComparator());

        DLFileVersion latestDlFileVersion = dlFileVersions.get(0);

        if ((status == WorkflowConstants.STATUS_APPROVED)
            && (latestDlFileVersion.getStatus() == WorkflowConstants.STATUS_IN_TRASH)) {

          continue;
        }

        // Asset

        if (status == WorkflowConstants.STATUS_APPROVED) {
          if (latestDlFileVersion.isApproved()) {
            assetEntryLocalService.updateVisible(
                DLFileEntryConstants.getClassName(), dlFileEntry.getFileEntryId(), true);
          }
        } else {
          assetEntryLocalService.moveEntryToTrash(
              DLFileEntryConstants.getClassName(), dlFileEntry.getFileEntryId());
        }

        // Social

        if (status == WorkflowConstants.STATUS_APPROVED) {
          socialActivityCounterLocalService.enableActivityCounters(
              DLFileEntryConstants.getClassName(), dlFileEntry.getFileEntryId());

          socialActivityLocalService.addActivity(
              user.getUserId(),
              dlFileEntry.getGroupId(),
              DLFileEntryConstants.getClassName(),
              dlFileEntry.getFileEntryId(),
              SocialActivityConstants.TYPE_RESTORE_FROM_TRASH,
              StringPool.BLANK,
              0);
        } else if (latestDlFileVersion.getStatus() == WorkflowConstants.STATUS_APPROVED) {

          socialActivityLocalService.addActivity(
              user.getUserId(),
              dlFileEntry.getGroupId(),
              DLFileEntryConstants.getClassName(),
              dlFileEntry.getFileEntryId(),
              SocialActivityConstants.TYPE_MOVE_TO_TRASH,
              StringPool.BLANK,
              0);
        }

        // Index

        Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(DLFileEntry.class);

        if (status == WorkflowConstants.STATUS_APPROVED) {
          indexer.reindex(dlFileEntry);
        } else {
          indexer.delete(dlFileEntry);
        }

        // Workflow

        if (status != WorkflowConstants.STATUS_APPROVED) {
          for (DLFileVersion dlFileVersion : dlFileVersions) {
            if (!dlFileVersion.isPending()) {
              continue;
            }

            dlFileVersion.setStatus(WorkflowConstants.STATUS_DRAFT);

            dlFileVersionPersistence.update(dlFileVersion, false);

            workflowInstanceLinkLocalService.deleteWorkflowInstanceLink(
                dlFileVersion.getCompanyId(),
                dlFileVersion.getGroupId(),
                DLFileEntryConstants.getClassName(),
                dlFileVersion.getFileVersionId());
          }
        }
      } else if (object instanceof DLFolder) {
        DLFolder dlFolder = (DLFolder) object;

        if (dlFolder.isInTrash()) {
          continue;
        }

        QueryDefinition queryDefinition = new QueryDefinition(WorkflowConstants.STATUS_ANY);

        List<Object> foldersAndFileEntriesAndFileShortcuts =
            dlFolderLocalService.getFoldersAndFileEntriesAndFileShortcuts(
                dlFolder.getGroupId(), dlFolder.getFolderId(), null, false, queryDefinition);

        updateStatuses(user, foldersAndFileEntriesAndFileShortcuts, status);
      }
    }
  }