Esempio n. 1
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 migrateDLFileEntry(
      long companyId, String portletId, long groupId, long repositoryId, DLFileEntry fileEntry)
      throws Exception {

    String fileName = fileEntry.getName();
    long fileEntryId = fileEntry.getFileEntryId();
    String properties = fileEntry.getLuceneProperties();

    List<DLFileVersion> dlFileVersions =
        DLFileVersionLocalServiceUtil.getFileVersions(
            groupId, repositoryId, fileName, WorkflowConstants.STATUS_ANY);

    if (dlFileVersions.isEmpty()) {
      String versionNumber = Hook.DEFAULT_VERSION;
      Date modifiedDate = fileEntry.getModifiedDate();

      migrateFile(
          companyId,
          portletId,
          groupId,
          repositoryId,
          fileName,
          versionNumber,
          fileEntryId,
          properties,
          modifiedDate);

      return;
    }

    for (DLFileVersion dlFileVersion : dlFileVersions) {
      String versionNumber = dlFileVersion.getVersion();
      Date modifiedDate = dlFileVersion.getCreateDate();

      migrateFile(
          companyId,
          portletId,
          groupId,
          repositoryId,
          fileName,
          versionNumber,
          fileEntryId,
          properties,
          modifiedDate);
    }
  }
  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);
  }
  @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 AssetEntry updateAsset(
      long userId,
      FileEntry fileEntry,
      FileVersion fileVersion,
      long[] assetCategoryIds,
      String[] assetTagNames,
      long[] assetLinkEntryIds)
      throws PortalException, SystemException {

    AssetEntry assetEntry = null;

    boolean visible = false;

    boolean addDraftAssetEntry = false;

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

      if (dlFileVersion.isApproved()) {
        visible = true;
      } else {
        String version = dlFileVersion.getVersion();

        if (!version.equals(DLFileEntryConstants.VERSION_DEFAULT)) {
          addDraftAssetEntry = true;
        }
      }
    } else {
      visible = true;
    }

    long fileEntryTypeId = getFileEntryTypeId(fileEntry);

    if (addDraftAssetEntry) {
      assetEntry =
          assetEntryLocalService.updateEntry(
              userId,
              fileEntry.getGroupId(),
              DLFileEntryConstants.getClassName(),
              fileVersion.getFileVersionId(),
              fileEntry.getUuid(),
              fileEntryTypeId,
              assetCategoryIds,
              assetTagNames,
              false,
              null,
              null,
              null,
              fileEntry.getMimeType(),
              fileEntry.getTitle(),
              fileEntry.getDescription(),
              null,
              null,
              null,
              0,
              0,
              null,
              false);
    } else {
      assetEntry =
          assetEntryLocalService.updateEntry(
              userId,
              fileEntry.getGroupId(),
              DLFileEntryConstants.getClassName(),
              fileEntry.getFileEntryId(),
              fileEntry.getUuid(),
              fileEntryTypeId,
              assetCategoryIds,
              assetTagNames,
              visible,
              null,
              null,
              null,
              fileEntry.getMimeType(),
              fileEntry.getTitle(),
              fileEntry.getDescription(),
              null,
              null,
              null,
              0,
              0,
              null,
              false);

      List<DLFileShortcut> dlFileShortcuts =
          dlFileShortcutPersistence.findByToFileEntryId(fileEntry.getFileEntryId());

      for (DLFileShortcut dlFileShortcut : dlFileShortcuts) {
        assetEntryLocalService.updateEntry(
            userId,
            dlFileShortcut.getGroupId(),
            DLFileShortcut.class.getName(),
            dlFileShortcut.getFileShortcutId(),
            dlFileShortcut.getUuid(),
            fileEntryTypeId,
            assetCategoryIds,
            assetTagNames,
            true,
            null,
            null,
            null,
            fileEntry.getMimeType(),
            fileEntry.getTitle(),
            fileEntry.getDescription(),
            null,
            null,
            null,
            0,
            0,
            null,
            false);
      }
    }

    assetLinkLocalService.updateLinks(
        userId, assetEntry.getEntryId(), assetLinkEntryIds, AssetLinkConstants.TYPE_RELATED);

    return assetEntry;
  }
  @Override
  public SyncDLObject patchFileEntry(
      long fileEntryId,
      long sourceVersionId,
      String sourceFileName,
      String mimeType,
      String title,
      String description,
      String changeLog,
      boolean majorVersion,
      File deltaFile,
      String checksum,
      ServiceContext serviceContext)
      throws PortalException {

    File patchedFile = null;

    try {
      FileEntry fileEntry = dlAppLocalService.getFileEntry(fileEntryId);

      SyncUtil.checkSyncEnabled(fileEntry.getGroupId());

      DLFileVersion dlFileVersion = dlFileVersionLocalService.getDLFileVersion(sourceVersionId);

      File sourceFile =
          dlFileEntryLocalService.getFile(fileEntryId, dlFileVersion.getVersion(), false);

      patchedFile = FileUtil.createTempFile();

      SyncUtil.patchFile(sourceFile, deltaFile, patchedFile);

      SyncDLObject syncDLObject =
          updateFileEntry(
              fileEntryId,
              sourceFileName,
              mimeType,
              title,
              description,
              changeLog,
              majorVersion,
              patchedFile,
              checksum,
              serviceContext);

      if (PortletPropsValues.SYNC_FILE_DIFF_CACHE_ENABLED
          && (sourceVersionId != syncDLObject.getVersionId())) {

        DLFileVersion targetDLFileVersion =
            dlFileVersionLocalService.getFileVersion(syncDLObject.getVersionId());

        syncDLFileVersionDiffLocalService.addSyncDLFileVersionDiff(
            fileEntryId, sourceVersionId, targetDLFileVersion.getFileVersionId(), deltaFile);
      }

      return syncDLObject;
    } catch (PortalException pe) {
      throw new PortalException(SyncUtil.buildExceptionMessage(pe), pe);
    } finally {
      FileUtil.delete(patchedFile);
    }
  }