Example #1
0
  public static FileVersion getLatestFileVersion(FileEntry fileEntry, boolean trusted)
      throws PortalException, SystemException {

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

      DLFileEntry dlFileEntry = liferayFileEntry.getDLFileEntry();

      return new LiferayFileVersion(dlFileEntry.getLatestFileVersion(trusted));
    }

    return fileEntry.getLatestFileVersion();
  }
  protected void importMetaData(
      PortletDataContext portletDataContext,
      Element fileEntryElement,
      FileEntry fileEntry,
      ServiceContext serviceContext)
      throws Exception {

    LiferayFileEntry liferayFileEntry = (LiferayFileEntry) fileEntry;

    DLFileEntry dlFileEntry = liferayFileEntry.getDLFileEntry();

    Map<Long, Long> dlFileEntryTypeIds =
        (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(DLFileEntryType.class);

    long dlFileEntryTypeId =
        MapUtil.getLong(
            dlFileEntryTypeIds, dlFileEntry.getFileEntryTypeId(), dlFileEntry.getFileEntryTypeId());

    DLFileEntryType existingDLFileEntryType =
        DLFileEntryTypeLocalServiceUtil.fetchDLFileEntryType(dlFileEntryTypeId);

    if (existingDLFileEntryType == null) {
      serviceContext.setAttribute("fileEntryTypeId", -1);

      return;
    }

    serviceContext.setAttribute("fileEntryTypeId", existingDLFileEntryType.getFileEntryTypeId());

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

    for (DDMStructure ddmStructure : ddmStructures) {
      Element structureFieldsElement =
          (Element)
              fileEntryElement.selectSingleNode(
                  "structure-fields[@structureUuid='".concat(ddmStructure.getUuid()).concat("']"));

      if (structureFieldsElement == null) {
        continue;
      }

      String path = structureFieldsElement.attributeValue("path");

      DDMFormValues ddmFormValues = (DDMFormValues) portletDataContext.getZipEntryAsObject(path);

      serviceContext.setAttribute(
          DDMFormValues.class.getName() + ddmStructure.getStructureId(), ddmFormValues);
    }
  }
  protected void exportMetaData(
      PortletDataContext portletDataContext, Element fileEntryElement, FileEntry fileEntry)
      throws Exception {

    LiferayFileEntry liferayFileEntry = (LiferayFileEntry) fileEntry;

    DLFileEntry dlFileEntry = liferayFileEntry.getDLFileEntry();

    long fileEntryTypeId = dlFileEntry.getFileEntryTypeId();

    DLFileEntryType dlFileEntryType =
        DLFileEntryTypeLocalServiceUtil.fetchFileEntryType(fileEntryTypeId);

    if ((dlFileEntryType == null) || !dlFileEntryType.isExportable()) {
      return;
    }

    StagedModelDataHandlerUtil.exportReferenceStagedModel(
        portletDataContext, fileEntry, dlFileEntryType, PortletDataContext.REFERENCE_TYPE_STRONG);

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

    for (DDMStructure ddmStructure : ddmStructures) {
      FileVersion fileVersion = fileEntry.getFileVersion();

      DLFileEntryMetadata dlFileEntryMetadata =
          DLFileEntryMetadataLocalServiceUtil.fetchFileEntryMetadata(
              ddmStructure.getStructureId(), fileVersion.getFileVersionId());

      if (dlFileEntryMetadata == null) {
        continue;
      }

      Element structureFields = fileEntryElement.addElement("structure-fields");

      String path =
          ExportImportPathUtil.getModelPath(
              ddmStructure, String.valueOf(dlFileEntryMetadata.getDDMStorageId()));

      structureFields.addAttribute("path", path);

      structureFields.addAttribute("structureUuid", ddmStructure.getUuid());

      DDMFormValues ddmFormValues =
          StorageEngineUtil.getDDMFormValues(dlFileEntryMetadata.getDDMStorageId());

      portletDataContext.addZipEntry(path, ddmFormValues);
    }
  }
  @Before
  public void setUp() throws Exception {
    FinderCacheUtil.clearCache();

    _liveGroup = GroupTestUtil.addGroup();
    _stagingGroup = GroupTestUtil.addGroup();

    _fileEntry =
        DLAppTestUtil.addFileEntry(
            _stagingGroup.getGroupId(),
            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
            ServiceTestUtil.randomString() + ".txt",
            ServiceTestUtil.randomString(),
            true);

    LiferayFileEntry liferayFileEntry = (LiferayFileEntry) _fileEntry;

    DLFileEntry dlFileEntry = liferayFileEntry.getDLFileEntry();

    dlFileEntry.setLargeImageId(dlFileEntry.getFileEntryId());

    DLFileEntryLocalServiceUtil.updateDLFileEntry(dlFileEntry);

    TestReaderWriter testReaderWriter = new TestReaderWriter();

    _portletDataContextExport =
        PortletDataContextFactoryUtil.createExportPortletDataContext(
            _stagingGroup.getCompanyId(),
            _stagingGroup.getGroupId(),
            new HashMap<String, String[]>(),
            new Date(System.currentTimeMillis() - Time.HOUR),
            new Date(),
            testReaderWriter);

    Element rootElement = SAXReaderUtil.createElement("root");

    _portletDataContextExport.setExportDataRootElement(rootElement);

    _stagingPrivateLayout =
        LayoutTestUtil.addLayout(_stagingGroup.getGroupId(), ServiceTestUtil.randomString(), true);
    _stagingPublicLayout =
        LayoutTestUtil.addLayout(_stagingGroup.getGroupId(), ServiceTestUtil.randomString(), false);

    _portletDataContextExport.setPlid(_stagingPublicLayout.getPlid());

    _portletDataContextImport =
        PortletDataContextFactoryUtil.createImportPortletDataContext(
            _stagingGroup.getCompanyId(),
            _stagingGroup.getGroupId(),
            new HashMap<String, String[]>(),
            new CurrentUserIdStrategy(TestPropsValues.getUser()),
            testReaderWriter);

    _portletDataContextImport.setImportDataRootElement(rootElement);

    _livePublicLayout =
        LayoutTestUtil.addLayout(_liveGroup.getGroupId(), ServiceTestUtil.randomString(), false);

    _portletDataContextImport.setPlid(_livePublicLayout.getPlid());

    _portletDataContextImport.setSourceGroupId(_stagingGroup.getGroupId());

    rootElement.addElement("entry");

    _referrerStagedModel =
        JournalTestUtil.addArticle(
            _stagingGroup.getGroupId(),
            ServiceTestUtil.randomString(),
            ServiceTestUtil.randomString());
  }
  protected static Object[] setUp(
      long repositoryId, String titleSuffix, ServiceContext serviceContext) throws Exception {

    Folder folder =
        DLAppLocalServiceUtil.addFolder(
            TestPropsValues.getUserId(),
            repositoryId,
            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
            "Folder A",
            StringPool.BLANK,
            serviceContext);

    DLAppLocalServiceUtil.addFolder(
        TestPropsValues.getUserId(),
        repositoryId,
        folder.getFolderId(),
        "Folder B",
        StringPool.BLANK,
        serviceContext);

    Folder folderC =
        DLAppLocalServiceUtil.addFolder(
            TestPropsValues.getUserId(),
            repositoryId,
            folder.getFolderId(),
            "Folder C",
            StringPool.BLANK,
            serviceContext);

    DLTrashServiceUtil.moveFolderToTrash(folderC.getFolderId());

    FileEntry fileEntry =
        addFileEntry(
            _user.getUserId(),
            repositoryId,
            folder.getFolderId(),
            "FE1.txt",
            titleSuffix,
            ContentTypes.TEXT_PLAIN,
            DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT);

    LiferayFileEntry liferayFileEntry = (LiferayFileEntry) fileEntry;

    DLFileEntry dlFileEntry = liferayFileEntry.getDLFileEntry();

    dlFileEntry.setExtraSettings("hello=world");
    dlFileEntry.setSmallImageId(_SMALL_IMAGE_ID);

    dlFileEntry = DLFileEntryLocalServiceUtil.updateDLFileEntry(dlFileEntry);

    DLFileVersion dlFileVersion = dlFileEntry.getFileVersion();

    addFileEntry(
        TestPropsValues.getUserId(),
        repositoryId,
        folder.getFolderId(),
        "FE2.pdf",
        titleSuffix,
        ContentTypes.APPLICATION_PDF,
        DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL);

    fileEntry =
        addFileEntry(
            TestPropsValues.getUserId(),
            repositoryId,
            folder.getFolderId(),
            "FE3.txt",
            titleSuffix,
            ContentTypes.TEXT_PLAIN,
            DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_ALL);

    fileEntry =
        DLAppServiceUtil.updateFileEntry(
            fileEntry.getFileEntryId(),
            "FE3.txt",
            ContentTypes.TEXT_PLAIN,
            "FE3.txt".concat(titleSuffix),
            StringPool.BLANK,
            StringPool.BLANK,
            false,
            RandomTestUtil.randomBytes(TikaSafeRandomizerBumper.INSTANCE),
            serviceContext);

    dlFileEntry = ((LiferayFileEntry) fileEntry).getDLFileEntry();

    dlFileEntry.setDescription("FE3.txt");

    DLFileEntryLocalServiceUtil.updateDLFileEntry(dlFileEntry);

    DLFileVersion dlFileVersion3 = dlFileEntry.getFileVersion();

    dlFileVersion3.setExtraSettings("hello=world");

    DLFileVersionLocalServiceUtil.updateDLFileVersion(dlFileVersion3);

    DLTrashServiceUtil.moveFileEntryToTrash(fileEntry.getFileEntryId());

    return new Object[] {folder, dlFileVersion};
  }