Example #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;
  }
  @Override
  public Map<String, DDMFormValues> getDDMFormValuesMap(long fileVersionId) throws PortalException {

    Map<String, DDMFormValues> ddmFormValuesMap = new HashMap<>();

    DLFileVersion dlFileVersion = DLFileVersionLocalServiceUtil.getFileVersion(fileVersionId);

    long fileEntryTypeId = dlFileVersion.getFileEntryTypeId();

    if (fileEntryTypeId <= 0) {
      return ddmFormValuesMap;
    }

    DLFileEntryType dlFileEntryType = getDLFileEntryType();

    List<DDMStructure> ddmStructures = dlFileEntryType.getDDMStructures();

    for (DDMStructure ddmStructure : ddmStructures) {
      DLFileEntryMetadata dlFileEntryMetadata =
          DLFileEntryMetadataLocalServiceUtil.fetchFileEntryMetadata(
              ddmStructure.getStructureId(), fileVersionId);

      if (dlFileEntryMetadata != null) {
        DDMFormValues ddmFormValues =
            StorageEngineUtil.getDDMFormValues(dlFileEntryMetadata.getDDMStorageId());

        ddmFormValuesMap.put(ddmStructure.getStructureKey(), ddmFormValues);
      }
    }

    return ddmFormValuesMap;
  }
  @Override
  public DLFileVersion getLatestFileVersion(boolean trusted) throws PortalException {

    if (trusted) {
      return DLFileVersionLocalServiceUtil.getLatestFileVersion(getFileEntryId(), false);
    } else {
      return DLFileVersionServiceUtil.getLatestFileVersion(getFileEntryId());
    }
  }
  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);
    }
  }
  @Test
  public void testActionableDynamicQuery() throws Exception {
    final IntegerWrapper count = new IntegerWrapper();

    ActionableDynamicQuery actionableDynamicQuery =
        DLFileVersionLocalServiceUtil.getActionableDynamicQuery();

    actionableDynamicQuery.setPerformActionMethod(
        new ActionableDynamicQuery.PerformActionMethod() {
          @Override
          public void performAction(Object object) {
            DLFileVersion dlFileVersion = (DLFileVersion) object;

            Assert.assertNotNull(dlFileVersion);

            count.increment();
          }
        });

    actionableDynamicQuery.performActions();

    Assert.assertEquals(count.getValue(), _persistence.countAll());
  }
 @Override
 public int getFileVersionsCount(int status) {
   return DLFileVersionLocalServiceUtil.getFileVersionsCount(getFileEntryId(), status);
 }
 @Override
 public List<DLFileVersion> getFileVersions(int status) {
   return DLFileVersionLocalServiceUtil.getFileVersions(getFileEntryId(), status);
 }
 @Override
 public DLFileVersion getFileVersion(String version) throws PortalException {
   return DLFileVersionLocalServiceUtil.getFileVersion(getFileEntryId(), version);
 }
  @Override
  protected void doImportStagedModel(PortletDataContext portletDataContext, FileEntry fileEntry)
      throws Exception {

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

    if (!fileEntry.isDefaultRepository()) {

      // References has been automatically imported, nothing to do here

      return;
    }

    Map<Long, Long> folderIds =
        (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(Folder.class);

    long folderId = MapUtil.getLong(folderIds, fileEntry.getFolderId(), fileEntry.getFolderId());

    long[] assetCategoryIds =
        portletDataContext.getAssetCategoryIds(DLFileEntry.class, fileEntry.getFileEntryId());
    String[] assetTagNames =
        portletDataContext.getAssetTagNames(DLFileEntry.class, fileEntry.getFileEntryId());

    ServiceContext serviceContext =
        portletDataContext.createServiceContext(fileEntry, DLFileEntry.class);

    serviceContext.setAttribute("sourceFileName", "A." + fileEntry.getExtension());
    serviceContext.setUserId(userId);

    Element fileEntryElement = portletDataContext.getImportDataElement(fileEntry);

    String binPath = fileEntryElement.attributeValue("bin-path");

    InputStream is = null;

    if (Validator.isNull(binPath) && portletDataContext.isPerformDirectBinaryImport()) {

      try {
        is = FileEntryUtil.getContentStream(fileEntry);
      } catch (Exception e) {
        if (_log.isWarnEnabled()) {
          _log.warn("Unable to retrieve content for file entry " + fileEntry.getFileEntryId(), e);
        }

        return;
      }
    } else {
      is = portletDataContext.getZipEntryAsInputStream(binPath);
    }

    if (is == null) {
      if (_log.isWarnEnabled()) {
        _log.warn("No file found for file entry " + fileEntry.getFileEntryId());
      }

      return;
    }

    importMetaData(portletDataContext, fileEntryElement, fileEntry, serviceContext);

    FileEntry importedFileEntry = null;

    String titleWithExtension = DLUtil.getTitleWithExtension(fileEntry);
    String extension = fileEntry.getExtension();

    String periodAndExtension = StringPool.PERIOD.concat(extension);

    if (portletDataContext.isDataStrategyMirror()) {
      FileEntry existingFileEntry =
          fetchStagedModelByUuidAndGroupId(
              fileEntry.getUuid(), portletDataContext.getScopeGroupId());

      FileVersion fileVersion = fileEntry.getFileVersion();

      if (existingFileEntry == null) {
        String fileEntryTitle = fileEntry.getTitle();

        FileEntry existingTitleFileEntry =
            FileEntryUtil.fetchByR_F_T(
                portletDataContext.getScopeGroupId(), folderId, fileEntryTitle);

        if (existingTitleFileEntry != null) {
          if ((fileEntry.getGroupId() == portletDataContext.getSourceGroupId())
              && portletDataContext.isDataStrategyMirrorWithOverwriting()) {

            DLAppLocalServiceUtil.deleteFileEntry(existingTitleFileEntry.getFileEntryId());
          } else {
            boolean titleHasExtension = false;

            if (fileEntryTitle.endsWith(periodAndExtension)) {
              fileEntryTitle = FileUtil.stripExtension(fileEntryTitle);

              titleHasExtension = true;
            }

            for (int i = 1; ; i++) {
              fileEntryTitle += StringPool.SPACE + i;

              titleWithExtension = fileEntryTitle + periodAndExtension;

              existingTitleFileEntry =
                  FileEntryUtil.fetchByR_F_T(
                      portletDataContext.getScopeGroupId(), folderId, titleWithExtension);

              if (existingTitleFileEntry == null) {
                if (titleHasExtension) {
                  fileEntryTitle += periodAndExtension;
                }

                break;
              }
            }
          }
        }

        serviceContext.setAttribute("fileVersionUuid", fileVersion.getUuid());
        serviceContext.setUuid(fileEntry.getUuid());

        importedFileEntry =
            DLAppLocalServiceUtil.addFileEntry(
                userId,
                portletDataContext.getScopeGroupId(),
                folderId,
                titleWithExtension,
                fileEntry.getMimeType(),
                fileEntryTitle,
                fileEntry.getDescription(),
                null,
                is,
                fileEntry.getSize(),
                serviceContext);

        if (fileEntry.isInTrash()) {
          importedFileEntry =
              DLAppServiceUtil.moveFileEntryToTrash(importedFileEntry.getFileEntryId());
        }
      } else {
        FileVersion latestExistingFileVersion = existingFileEntry.getLatestFileVersion(true);

        boolean indexEnabled = serviceContext.isIndexingEnabled();

        boolean deleteFileEntry = false;
        boolean updateFileEntry = false;

        if (!Validator.equals(fileVersion.getUuid(), latestExistingFileVersion.getUuid())) {

          deleteFileEntry = true;
          updateFileEntry = true;
        } else {
          InputStream existingFileVersionInputStream = null;

          try {
            existingFileVersionInputStream = latestExistingFileVersion.getContentStream(false);
          } catch (Exception e) {
            if (_log.isDebugEnabled()) {
              _log.debug(e, e);
            }
          } finally {
            if (existingFileVersionInputStream != null) {
              existingFileVersionInputStream.close();
            }
          }

          if (existingFileVersionInputStream == null) {
            updateFileEntry = true;
          }
        }

        try {
          serviceContext.setIndexingEnabled(false);

          if (updateFileEntry) {
            DLFileVersion alreadyExistingFileVersion =
                DLFileVersionLocalServiceUtil.getFileVersionByUuidAndGroupId(
                    fileVersion.getUuid(), existingFileEntry.getGroupId());

            if (alreadyExistingFileVersion != null) {
              serviceContext.setAttribute(
                  "existingDLFileVersionId", alreadyExistingFileVersion.getFileVersionId());
            }

            serviceContext.setUuid(fileVersion.getUuid());

            importedFileEntry =
                DLAppLocalServiceUtil.updateFileEntry(
                    userId,
                    existingFileEntry.getFileEntryId(),
                    titleWithExtension,
                    fileEntry.getMimeType(),
                    fileEntry.getTitle(),
                    fileEntry.getDescription(),
                    null,
                    false,
                    is,
                    fileEntry.getSize(),
                    serviceContext);
          } else {
            DLAppLocalServiceUtil.updateAsset(
                userId, existingFileEntry,
                latestExistingFileVersion, assetCategoryIds,
                assetTagNames, null);

            importedFileEntry = existingFileEntry;
          }

          if (importedFileEntry.getFolderId() != folderId) {
            importedFileEntry =
                DLAppLocalServiceUtil.moveFileEntry(
                    userId, importedFileEntry.getFileEntryId(), folderId, serviceContext);
          }

          if (importedFileEntry instanceof LiferayFileEntry) {
            LiferayFileEntry liferayFileEntry = (LiferayFileEntry) importedFileEntry;

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

            indexer.reindex((DLFileEntry) liferayFileEntry.getModel());
          }

          if (deleteFileEntry && ExportImportThreadLocal.isStagingInProcess()) {

            DLAppServiceUtil.deleteFileVersion(
                latestExistingFileVersion.getFileEntryId(), latestExistingFileVersion.getVersion());
          }
        } finally {
          serviceContext.setIndexingEnabled(indexEnabled);
        }
      }
    } else {
      try {
        importedFileEntry =
            DLAppLocalServiceUtil.addFileEntry(
                userId,
                portletDataContext.getScopeGroupId(),
                folderId,
                titleWithExtension,
                fileEntry.getMimeType(),
                fileEntry.getTitle(),
                fileEntry.getDescription(),
                null,
                is,
                fileEntry.getSize(),
                serviceContext);
      } catch (DuplicateFileException dfe) {
        String title = fileEntry.getTitle();

        String[] titleParts = title.split("\\.", 2);

        title = titleParts[0] + StringUtil.randomString();

        if (titleParts.length > 1) {
          title += StringPool.PERIOD + titleParts[1];
        }

        if (!title.endsWith(periodAndExtension)) {
          title += periodAndExtension;
        }

        importedFileEntry =
            DLAppLocalServiceUtil.addFileEntry(
                userId,
                portletDataContext.getScopeGroupId(),
                folderId,
                title,
                fileEntry.getMimeType(),
                title,
                fileEntry.getDescription(),
                null,
                is,
                fileEntry.getSize(),
                serviceContext);
      }
    }

    if (portletDataContext.getBooleanParameter(
        DLPortletDataHandler.NAMESPACE, "previews-and-thumbnails")) {

      DLProcessorRegistryUtil.importGeneratedFiles(
          portletDataContext, fileEntry, importedFileEntry, fileEntryElement);
    }

    portletDataContext.importClassedModel(fileEntry, importedFileEntry, DLFileEntry.class);

    Map<Long, Long> fileEntryIds =
        (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(FileEntry.class);

    fileEntryIds.put(fileEntry.getFileEntryId(), importedFileEntry.getFileEntryId());
  }