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);
    }
  }
  protected String extractDDMContent(DLFileVersion dlFileVersion, Locale locale) throws Exception {

    List<DLFileEntryMetadata> dlFileEntryMetadatas =
        DLFileEntryMetadataLocalServiceUtil.getFileVersionFileEntryMetadatas(
            dlFileVersion.getFileVersionId());

    StringBundler sb = new StringBundler(dlFileEntryMetadatas.size());

    for (DLFileEntryMetadata dlFileEntryMetadata : dlFileEntryMetadatas) {
      Fields fields = null;

      try {
        fields = StorageEngineUtil.getFields(dlFileEntryMetadata.getDDMStorageId());
      } catch (Exception e) {
      }

      if (fields != null) {
        DDMStructure ddmStructure =
            DDMStructureLocalServiceUtil.getStructure(dlFileEntryMetadata.getDDMStructureId());

        sb.append(DDMIndexerUtil.extractAttributes(ddmStructure, fields, locale));
      }
    }

    return sb.toString();
  }
  protected void addFileEntryTypeAttributes(Document document, DLFileVersion dlFileVersion)
      throws PortalException, SystemException {

    DLFileEntryType dlFileEntryType =
        DLFileEntryTypeLocalServiceUtil.getDLFileEntryType(dlFileVersion.getFileEntryTypeId());

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

    for (DDMStructure ddmStructure : ddmStructures) {
      Fields fields = null;

      try {
        DLFileEntryMetadata fileEntryMetadata =
            DLFileEntryMetadataLocalServiceUtil.getFileEntryMetadata(
                ddmStructure.getStructureId(), dlFileVersion.getFileVersionId());

        fields = StorageEngineUtil.getFields(fileEntryMetadata.getDDMStorageId());
      } catch (Exception e) {
      }

      if (fields != null) {
        DDMIndexerUtil.addAttributes(document, ddmStructure, fields);
      }
    }
  }
  protected void sendDDMRecordFile(
      HttpServletRequest request, HttpServletResponse response, String[] pathArray)
      throws Exception {

    if (pathArray.length == 4) {
      String className = GetterUtil.getString(pathArray[1]);
      long classPK = GetterUtil.getLong(pathArray[2]);
      String fieldName = GetterUtil.getString(pathArray[3]);

      Field field = null;

      if (className.equals(DDLRecord.class.getName())) {
        DDLRecord ddlRecord = DDLRecordLocalServiceUtil.getRecord(classPK);

        field = ddlRecord.getField(fieldName);
      } else if (className.equals(DLFileEntryMetadata.class.getName())) {
        DLFileEntryMetadata fileEntryMetadata =
            DLFileEntryMetadataLocalServiceUtil.getDLFileEntryMetadata(classPK);

        Fields fields = StorageEngineUtil.getFields(fileEntryMetadata.getDDMStorageId());

        field = fields.get(fieldName);
      }

      DDMUtil.sendFieldFile(request, response, field);
    }
  }
  @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;
  }
  @Test
  public void testUpdateExisting() throws Exception {
    long pk = ServiceTestUtil.nextLong();

    DLFileEntryMetadata newDLFileEntryMetadata = _persistence.create(pk);

    newDLFileEntryMetadata.setUuid(ServiceTestUtil.randomString());

    newDLFileEntryMetadata.setDDMStorageId(ServiceTestUtil.nextLong());

    newDLFileEntryMetadata.setDDMStructureId(ServiceTestUtil.nextLong());

    newDLFileEntryMetadata.setFileEntryTypeId(ServiceTestUtil.nextLong());

    newDLFileEntryMetadata.setFileEntryId(ServiceTestUtil.nextLong());

    newDLFileEntryMetadata.setFileVersionId(ServiceTestUtil.nextLong());

    _persistence.update(newDLFileEntryMetadata, false);

    DLFileEntryMetadata existingDLFileEntryMetadata =
        _persistence.findByPrimaryKey(newDLFileEntryMetadata.getPrimaryKey());

    Assert.assertEquals(existingDLFileEntryMetadata.getUuid(), newDLFileEntryMetadata.getUuid());
    Assert.assertEquals(
        existingDLFileEntryMetadata.getFileEntryMetadataId(),
        newDLFileEntryMetadata.getFileEntryMetadataId());
    Assert.assertEquals(
        existingDLFileEntryMetadata.getDDMStorageId(), newDLFileEntryMetadata.getDDMStorageId());
    Assert.assertEquals(
        existingDLFileEntryMetadata.getDDMStructureId(),
        newDLFileEntryMetadata.getDDMStructureId());
    Assert.assertEquals(
        existingDLFileEntryMetadata.getFileEntryTypeId(),
        newDLFileEntryMetadata.getFileEntryTypeId());
    Assert.assertEquals(
        existingDLFileEntryMetadata.getFileEntryId(), newDLFileEntryMetadata.getFileEntryId());
    Assert.assertEquals(
        existingDLFileEntryMetadata.getFileVersionId(), newDLFileEntryMetadata.getFileVersionId());
  }
  protected void addFileEntryTypeAttributes(Document document, DLFileVersion dlFileVersion)
      throws PortalException {

    List<DLFileEntryMetadata> dlFileEntryMetadatas =
        DLFileEntryMetadataLocalServiceUtil.getFileVersionFileEntryMetadatas(
            dlFileVersion.getFileVersionId());

    for (DLFileEntryMetadata dlFileEntryMetadata : dlFileEntryMetadatas) {
      Fields fields = null;

      try {
        fields = StorageEngineUtil.getFields(dlFileEntryMetadata.getDDMStorageId());
      } catch (Exception e) {
      }

      if (fields != null) {
        DDMStructure ddmStructure =
            DDMStructureLocalServiceUtil.getStructure(dlFileEntryMetadata.getDDMStructureId());

        DDMIndexerUtil.addAttributes(document, ddmStructure, fields);
      }
    }
  }