@Override
  public void deleteStagedModel(String uuid, long groupId, String className, String extraData)
      throws PortalException {

    DLFileEntryType dlFileEntryType =
        DLFileEntryTypeLocalServiceUtil.fetchDLFileEntryTypeByUuidAndGroupId(uuid, groupId);

    if (dlFileEntryType != null) {
      DLFileEntryTypeLocalServiceUtil.deleteFileEntryType(dlFileEntryType);
    }
  }
  protected DLFileEntryType fetchExistingFileEntryType(
      String uuid, long groupId, String fileEntryTypeKey, boolean preloaded) {

    DLFileEntryType existingDLFileEntryType = null;

    if (!preloaded) {
      existingDLFileEntryType =
          DLFileEntryTypeLocalServiceUtil.fetchDLFileEntryTypeByUuidAndGroupId(uuid, groupId);
    } else {
      existingDLFileEntryType =
          DLFileEntryTypeLocalServiceUtil.fetchFileEntryType(groupId, fileEntryTypeKey);
    }

    return existingDLFileEntryType;
  }
  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);
      }
    }
  }
  @Test
  public void testLocalizedSiteAddFileEntryType() throws Exception {
    Group group = GroupTestUtil.addGroup();

    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId());

    Locale locale = LocaleThreadLocal.getSiteDefaultLocale();

    try {
      LocaleThreadLocal.setSiteDefaultLocale(LocaleUtil.SPAIN);

      String name = RandomTestUtil.randomString();
      String description = RandomTestUtil.randomString();
      DDMStructure ddmStructure =
          DDMStructureTestUtil.addStructure(
              DLFileEntry.class.getName(), new Locale[] {LocaleUtil.SPAIN}, LocaleUtil.SPAIN);

      DLFileEntryType dlFileEntryType =
          DLFileEntryTypeLocalServiceUtil.addFileEntryType(
              TestPropsValues.getUserId(),
              group.getGroupId(),
              name,
              description,
              new long[] {ddmStructure.getStructureId()},
              serviceContext);

      Assert.assertEquals(name, dlFileEntryType.getName(LocaleUtil.US, true));
      Assert.assertEquals(description, dlFileEntryType.getDescription(LocaleUtil.US, true));
    } finally {
      LocaleThreadLocal.setSiteDefaultLocale(locale);
    }
  }
  @Override
  protected void validateImport(
      Map<String, List<StagedModel>> dependentStagedModelsMap, Group group) throws Exception {

    List<StagedModel> ddmStructureDependentStagedModels =
        dependentStagedModelsMap.get(DDMStructure.class.getSimpleName());

    Assert.assertEquals(1, ddmStructureDependentStagedModels.size());

    DDMStructure ddmStructure = (DDMStructure) ddmStructureDependentStagedModels.get(0);

    DDMStructureLocalServiceUtil.getDDMStructureByUuidAndGroupId(
        ddmStructure.getUuid(), group.getGroupId());

    List<StagedModel> dlFileEntryTypesDependentStagedModels =
        dependentStagedModelsMap.get(DLFileEntryType.class.getSimpleName());

    Assert.assertEquals(1, dlFileEntryTypesDependentStagedModels.size());

    DLFileEntryType dlFileEntryType =
        (DLFileEntryType) dlFileEntryTypesDependentStagedModels.get(0);

    DLFileEntryTypeLocalServiceUtil.getDLFileEntryTypeByUuidAndGroupId(
        dlFileEntryType.getUuid(), group.getGroupId());

    List<StagedModel> folderDependentStagedModels =
        dependentStagedModelsMap.get(DLFolder.class.getSimpleName());

    Assert.assertEquals(1, folderDependentStagedModels.size());

    Folder parentFolder = (Folder) folderDependentStagedModels.get(0);

    DLFolderLocalServiceUtil.getDLFolderByUuidAndGroupId(
        parentFolder.getUuid(), group.getGroupId());
  }
  protected void validateCompanyDependenciesImport(
      Map<String, List<StagedModel>> dependentStagedModelsMap, Group group) throws Exception {

    List<StagedModel> ddmStructureDependentStagedModels =
        dependentStagedModelsMap.get(DDMStructure.class.getSimpleName());

    Assert.assertEquals(1, ddmStructureDependentStagedModels.size());

    DDMStructure ddmStructure = (DDMStructure) ddmStructureDependentStagedModels.get(0);

    Assert.assertNull(
        "Company DDM structure dependency should not be imported",
        DDMStructureLocalServiceUtil.fetchDDMStructureByUuidAndGroupId(
            ddmStructure.getUuid(), group.getGroupId()));

    List<StagedModel> dlFileEntryTypesDependentStagedModels =
        dependentStagedModelsMap.get(DLFileEntryType.class.getSimpleName());

    Assert.assertEquals(1, dlFileEntryTypesDependentStagedModels.size());

    DLFileEntryType dlFileEntryType =
        (DLFileEntryType) dlFileEntryTypesDependentStagedModels.get(0);

    Assert.assertNull(
        "Company DL file entry should not be imported",
        DLFileEntryTypeLocalServiceUtil.fetchDLFileEntryTypeByUuidAndGroupId(
            dlFileEntryType.getUuid(), group.getGroupId()));
  }
  @Before
  public void setUp() throws Exception {
    SimpleAction simpleAction = new AddDefaultDocumentLibraryStructuresAction();

    String companyIdString = String.valueOf(TestPropsValues.getCompanyId());

    simpleAction.run(new String[] {companyIdString});

    _group = GroupTestUtil.addGroup();

    _folder =
        DLAppLocalServiceUtil.addFolder(
            TestPropsValues.getUserId(),
            _group.getGroupId(),
            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
            "Folder A",
            "",
            ServiceContextTestUtil.getServiceContext(_group.getGroupId()));

    _subfolder =
        DLAppLocalServiceUtil.addFolder(
            TestPropsValues.getUserId(),
            _group.getGroupId(),
            _folder.getFolderId(),
            "SubFolder AA",
            "",
            ServiceContextTestUtil.getServiceContext(_group.getGroupId()));

    _basicDocumentDLFileEntryType =
        DLFileEntryTypeLocalServiceUtil.getFileEntryType(
            DLFileEntryTypeConstants.FILE_ENTRY_TYPE_ID_BASIC_DOCUMENT);

    _dlFileEntryTypes =
        DLFileEntryTypeLocalServiceUtil.getFileEntryTypes(
            PortalUtil.getCurrentAndAncestorSiteGroupIds(_group.getGroupId()));

    for (DLFileEntryType dlFileEntryType : _dlFileEntryTypes) {
      String name = dlFileEntryType.getName(LocaleUtil.getSiteDefault());

      if (name.equals(DLFileEntryTypeConstants.NAME_CONTRACT)) {
        _contractDLFileEntryType = dlFileEntryType;
      } else if (name.equals(DLFileEntryTypeConstants.NAME_MARKETING_BANNER)) {

        _marketingBannerDLFileEntryType = dlFileEntryType;
      }
    }
  }
 @Override
 protected StagedModel getStagedModel(String uuid, Group group) {
   try {
     return DLFileEntryTypeLocalServiceUtil.getDLFileEntryTypeByUuidAndGroupId(
         uuid, group.getGroupId());
   } catch (Exception e) {
     return null;
   }
 }
  @Override
  public void postProcessSearchQuery(BooleanQuery searchQuery, SearchContext searchContext)
      throws Exception {

    Set<DDMStructure> ddmStructuresSet = new TreeSet<DDMStructure>();

    long[] groupIds = searchContext.getGroupIds();

    if ((groupIds != null) && (groupIds.length > 0)) {
      List<DLFileEntryType> dlFileEntryTypes =
          DLFileEntryTypeLocalServiceUtil.getFileEntryTypes(groupIds);

      for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
        ddmStructuresSet.addAll(dlFileEntryType.getDDMStructures());
      }
    }

    Group group = GroupLocalServiceUtil.getCompanyGroup(searchContext.getCompanyId());

    DDMStructure tikaRawMetadataStructure =
        DDMStructureLocalServiceUtil.fetchStructure(
            group.getGroupId(),
            PortalUtil.getClassNameId(RawMetadataProcessor.class),
            "TikaRawMetadata");

    if (tikaRawMetadataStructure != null) {
      ddmStructuresSet.add(tikaRawMetadataStructure);
    }

    for (DDMStructure ddmStructure : ddmStructuresSet) {
      addSearchDDMStruture(searchQuery, searchContext, ddmStructure);
    }

    String keywords = searchContext.getKeywords();

    if (Validator.isNull(keywords)) {
      addSearchTerm(searchQuery, searchContext, Field.DESCRIPTION, false);
      addSearchTerm(searchQuery, searchContext, Field.TITLE, false);
      addSearchTerm(searchQuery, searchContext, Field.USER_NAME, false);
    }

    addSearchTerm(searchQuery, searchContext, "extension", false);
    addSearchTerm(searchQuery, searchContext, "fileEntryTypeId", false);
    addSearchTerm(searchQuery, searchContext, "path", false);

    LinkedHashMap<String, Object> params =
        (LinkedHashMap<String, Object>) searchContext.getAttribute("params");

    if (params != null) {
      String expandoAttributes = (String) params.get("expandoAttributes");

      if (Validator.isNotNull(expandoAttributes)) {
        addSearchExpando(searchQuery, searchContext, expandoAttributes);
      }
    }
  }
  @Override
  public List<DLFileEntryType> fetchStagedModelsByUuidAndCompanyId(String uuid, long companyId) {

    return DLFileEntryTypeLocalServiceUtil.getDLFileEntryTypesByUuidAndCompanyId(
        uuid,
        companyId,
        QueryUtil.ALL_POS,
        QueryUtil.ALL_POS,
        new StagedModelModifiedDateComparator<DLFileEntryType>());
  }
  @Test
  public void testAddFileEntryType() throws Exception {
    ServiceContext serviceContext = new ServiceContext();

    byte[] testFileBytes = FileUtil.getBytes(getClass(), _TEST_DDM_STRUCTURE);

    serviceContext.setAttribute("xsd", new String(testFileBytes));

    User user = TestPropsValues.getUser();

    serviceContext.setLanguageId(LocaleUtil.toLanguageId(user.getLocale()));

    DLFileEntryType dlFileEntryType =
        DLFileEntryTypeLocalServiceUtil.addFileEntryType(
            TestPropsValues.getUserId(),
            _group.getGroupId(),
            "Test Structure",
            StringPool.BLANK,
            new long[0],
            serviceContext);

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

    Assert.assertEquals(1, ddmStructures.size());

    DDMStructure ddmStructure = ddmStructures.get(0);

    Locale[] availableLocales = LocaleUtil.fromLanguageIds(ddmStructure.getAvailableLanguageIds());

    boolean hasDefaultLocale = ArrayUtil.contains(availableLocales, LocaleUtil.getSiteDefault());

    Assert.assertTrue(hasDefaultLocale);

    boolean hasHungarianLocale = ArrayUtil.contains(availableLocales, LocaleUtil.HUNGARY);

    Assert.assertTrue(hasHungarianLocale);

    boolean hasUserLocale = ArrayUtil.contains(availableLocales, user.getLocale());

    Assert.assertTrue(hasUserLocale);

    DLFileEntryTypeLocalServiceUtil.deleteDLFileEntryType(dlFileEntryType);
  }
  @Override
  public String getTypeName(Locale locale, long subtypeId) {
    try {
      DLFileEntryType dlFileEntryType = DLFileEntryTypeLocalServiceUtil.getFileEntryType(subtypeId);

      return dlFileEntryType.getName(locale);
    } catch (Exception e) {
      return super.getTypeName(locale, subtypeId);
    }
  }
  @Before
  @Override
  public void setUp() throws Exception {
    super.setUp();

    SimpleAction simpleAction = new AddDefaultDocumentLibraryStructuresAction();

    String companyIdString = String.valueOf(TestPropsValues.getCompanyId());

    simpleAction.run(new String[] {companyIdString});

    List<DLFileEntryType> dlFileEntryTypes =
        DLFileEntryTypeLocalServiceUtil.getFileEntryTypes(
            PortalUtil.getCurrentAndAncestorSiteGroupIds(group.getGroupId()));

    for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
      String name = dlFileEntryType.getName(LocaleUtil.getSiteDefault());

      if (name.equals(DLFileEntryTypeConstants.NAME_CONTRACT)) {
        _contractDLFileEntryTypeId = dlFileEntryType.getFileEntryTypeId();
      }
    }

    ExpandoTable expandoTable =
        ExpandoTableLocalServiceUtil.addDefaultTable(
            PortalUtil.getDefaultCompanyId(), DLFileEntry.class.getName());

    ExpandoColumnLocalServiceUtil.addColumn(
        expandoTable.getTableId(),
        _EXPANDO_ATTRIBUTE_NAME,
        ExpandoColumnConstants.STRING,
        StringPool.BLANK);

    _serviceContext = getServiceContext();

    FileEntry fileEntry =
        DLAppServiceUtil.addFileEntry(
            group.getGroupId(),
            parentFolder.getFolderId(),
            _SOURCE_FILE_NAME,
            ContentTypes.APPLICATION_OCTET_STREAM,
            _TITLE,
            StringPool.BLANK,
            StringPool.BLANK,
            _DATA_VERSION_1,
            _serviceContext);

    _fileVersion =
        DLFileVersionLocalServiceUtil.getFileVersion(
            fileEntry.getFileEntryId(), DLFileEntryConstants.VERSION_DEFAULT);
  }
  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);
    }
  }
  @Override
  public int getClassTypeFieldNamesCount(long classTypeId, Locale locale) throws Exception {

    List<Tuple> classTypeFieldNames = new ArrayList<Tuple>();

    DLFileEntryType dlFileEntryType =
        DLFileEntryTypeLocalServiceUtil.getDLFileEntryType(classTypeId);

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

    for (DDMStructure ddmStructure : ddmStructures) {
      classTypeFieldNames.addAll(getDDMStructureFieldNames(ddmStructure, locale));
    }

    return classTypeFieldNames.size();
  }
  @Override
  public List<Tuple> getClassTypeFieldNames(long classTypeId, Locale locale, int start, int end)
      throws Exception {

    List<Tuple> classTypeFieldNames = new ArrayList<Tuple>();

    DLFileEntryType dlFileEntryType =
        DLFileEntryTypeLocalServiceUtil.getDLFileEntryType(classTypeId);

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

    for (DDMStructure ddmStructure : ddmStructures) {
      classTypeFieldNames.addAll(getDDMStructureFieldNames(ddmStructure, locale));
    }

    return ListUtil.subList(classTypeFieldNames, start, end);
  }
  protected HashMap<String, Fields> getFieldsMap(
      ServiceContext serviceContext, long fileEntryTypeId) throws PortalException, SystemException {

    HashMap<String, Fields> fieldsMap = new HashMap<String, Fields>();

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

    DLFileEntryType fileEntryType =
        DLFileEntryTypeLocalServiceUtil.getFileEntryType(fileEntryTypeId);

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

    for (DDMStructure ddmStructure : ddmStructures) {
      String namespace = String.valueOf(ddmStructure.getStructureId());

      Set<String> fieldNames = ddmStructure.getFieldNames();

      Fields fields =
          (Fields)
              serviceContext.getAttribute(Fields.class.getName() + ddmStructure.getStructureId());

      if (fields == null) {
        fields = new Fields();

        for (String name : fieldNames) {
          Field field = new Field();

          field.setName(name);

          String value = ParamUtil.getString(serviceContext, namespace + name);

          field.setValue(value);

          fields.put(field);
        }
      }

      fieldsMap.put(ddmStructure.getStructureKey(), fields);
    }

    return fieldsMap;
  }
  @Override
  public void postProcessSearchQuery(BooleanQuery searchQuery, SearchContext searchContext)
      throws Exception {

    Set<DDMStructure> ddmStructuresSet = new TreeSet<DDMStructure>();

    long[] groupIds = searchContext.getGroupIds();

    if ((groupIds != null) && (groupIds.length > 0)) {
      for (long groupId : searchContext.getGroupIds()) {
        List<DLFileEntryType> dlFileEntryTypes =
            DLFileEntryTypeLocalServiceUtil.getFileEntryTypes(groupId);

        for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
          ddmStructuresSet.addAll(dlFileEntryType.getDDMStructures());
        }
      }
    }

    for (DDMStructure ddmStructure : ddmStructuresSet) {
      addSearchDDMStruture(searchQuery, searchContext, ddmStructure);
    }

    addSearchTerm(searchQuery, searchContext, Field.USER_NAME, true);

    addSearchTerm(searchQuery, searchContext, "extension", true);
    addSearchTerm(searchQuery, searchContext, "fileEntryTypeId", false);
    addSearchTerm(searchQuery, searchContext, "path", true);

    LinkedHashMap<String, Object> params =
        (LinkedHashMap<String, Object>) searchContext.getAttribute("params");

    if (params != null) {
      String expandoAttributes = (String) params.get("expandoAttributes");

      if (Validator.isNotNull(expandoAttributes)) {
        addSearchExpando(searchQuery, searchContext, expandoAttributes);
      }
    }
  }
  protected void updateServiceContext(String expando, long fileEntryTypeId, String metadata)
      throws PortalException {

    Map<String, Serializable> expandoBridgeAttributes =
        _serviceContext.getExpandoBridgeAttributes();

    expandoBridgeAttributes.put(_EXPANDO_ATTRIBUTE_NAME, expando);

    _serviceContext.setExpandoBridgeAttributes(expandoBridgeAttributes);

    _serviceContext.setAttribute("fileEntryTypeId", fileEntryTypeId);

    if (fileEntryTypeId <= 0) {
      return;
    }

    DLFileEntryType fileEntryType =
        DLFileEntryTypeLocalServiceUtil.getFileEntryType(fileEntryTypeId);

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

    for (DDMStructure ddmStructure : ddmStructures) {
      Fields fields =
          (Fields)
              _serviceContext.getAttribute(Fields.class.getName() + ddmStructure.getStructureId());

      for (Field field : fields) {
        String type = field.getType();

        if (!field.isPrivate() && type.equals("text")) {
          field.setValue(metadata);
        }
      }

      _serviceContext.setAttribute(Fields.class.getName() + ddmStructure.getStructureId(), fields);
    }
  }
  protected ServiceContext getServiceContext() throws Exception {
    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId());

    serviceContext.setAttribute("fileEntryTypeId", _contractDLFileEntryTypeId);

    Map<String, Serializable> expandoBridgeAttributes = serviceContext.getExpandoBridgeAttributes();

    expandoBridgeAttributes.put(_EXPANDO_ATTRIBUTE_NAME, StringPool.BLANK);

    serviceContext.setExpandoBridgeAttributes(expandoBridgeAttributes);

    DLFileEntryType fileEntryType =
        DLFileEntryTypeLocalServiceUtil.getFileEntryType(_contractDLFileEntryTypeId);

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

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

      Set<String> names = ddmStructure.getFieldNames();

      for (String name : names) {
        Field field = new Field(ddmStructure.getStructureId(), name, StringPool.BLANK);

        if (ddmStructure.isFieldPrivate(name)) {
          field.setValue(RandomTestUtil.randomString());
        }

        fields.put(field);
      }

      serviceContext.setAttribute(Fields.class.getName() + ddmStructure.getStructureId(), fields);
    }

    return serviceContext;
  }
  @Override
  public DLFileEntryType fetchStagedModelByUuidAndGroupId(String uuid, long groupId) {

    return DLFileEntryTypeLocalServiceUtil.fetchDLFileEntryTypeByUuidAndGroupId(uuid, groupId);
  }
 @Override
 public DLFileEntryType getDLFileEntryType() throws PortalException {
   return DLFileEntryTypeLocalServiceUtil.getDLFileEntryType(getFileEntryTypeId());
 }
  @Override
  protected void doImportStagedModel(
      PortletDataContext portletDataContext, DLFileEntryType fileEntryType) throws Exception {

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

    List<Element> ddmStructureElements =
        portletDataContext.getReferenceDataElements(fileEntryType, DDMStructure.class);

    for (Element ddmStructureElement : ddmStructureElements) {
      StagedModelDataHandlerUtil.importStagedModel(portletDataContext, ddmStructureElement);
    }

    List<Element> ddmStructureReferenceElements =
        portletDataContext.getReferenceElements(fileEntryType, DDMStructure.class);

    long[] ddmStructureIdsArray = new long[ddmStructureReferenceElements.size()];

    Map<Long, Long> ddmStructureIds =
        (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(DDMStructure.class);

    for (int i = 0; i < ddmStructureReferenceElements.size(); i++) {
      Element ddmStructureReferenceElement = ddmStructureReferenceElements.get(i);

      long ddmStructureId =
          GetterUtil.getLong(ddmStructureReferenceElement.attributeValue("class-pk"));

      ddmStructureIdsArray[i] = MapUtil.getLong(ddmStructureIds, ddmStructureId);
    }

    ServiceContext serviceContext =
        portletDataContext.createServiceContext(fileEntryType, DLPortletDataHandler.NAMESPACE);

    DLFileEntryType importedDLFileEntryType = null;

    if (portletDataContext.isDataStrategyMirror()) {
      DLFileEntryType existingDLFileEntryType =
          DLFileEntryTypeLocalServiceUtil.fetchDLFileEntryTypeByUuidAndGroupId(
              fileEntryType.getUuid(), portletDataContext.getScopeGroupId());

      if (existingDLFileEntryType == null) {
        existingDLFileEntryType =
            DLFileEntryTypeLocalServiceUtil.fetchDLFileEntryTypeByUuidAndGroupId(
                fileEntryType.getUuid(), portletDataContext.getCompanyGroupId());
      }

      if (existingDLFileEntryType == null) {
        serviceContext.setUuid(fileEntryType.getUuid());

        importedDLFileEntryType =
            DLFileEntryTypeLocalServiceUtil.addFileEntryType(
                userId,
                portletDataContext.getScopeGroupId(),
                fileEntryType.getFileEntryTypeKey(),
                fileEntryType.getNameMap(),
                fileEntryType.getDescriptionMap(),
                ddmStructureIdsArray,
                serviceContext);
      } else if (portletDataContext.isCompanyStagedGroupedModel(existingDLFileEntryType)) {

        return;
      } else {
        DLFileEntryTypeLocalServiceUtil.updateFileEntryType(
            userId,
            existingDLFileEntryType.getFileEntryTypeId(),
            fileEntryType.getNameMap(),
            fileEntryType.getDescriptionMap(),
            ddmStructureIdsArray,
            serviceContext);
      }
    } else {
      importedDLFileEntryType =
          DLFileEntryTypeLocalServiceUtil.addFileEntryType(
              userId,
              portletDataContext.getScopeGroupId(),
              fileEntryType.getFileEntryTypeKey(),
              fileEntryType.getNameMap(),
              fileEntryType.getDescriptionMap(),
              ddmStructureIdsArray,
              serviceContext);
    }

    portletDataContext.importClassedModel(
        fileEntryType, importedDLFileEntryType, DLPortletDataHandler.NAMESPACE);

    String importedDLFileEntryDDMStructureKey = DLUtil.getDDMStructureKey(importedDLFileEntryType);

    List<DDMStructure> importedDDMStructures = importedDLFileEntryType.getDDMStructures();

    for (DDMStructure importedDDMStructure : importedDDMStructures) {
      String ddmStructureKey = importedDDMStructure.getStructureKey();

      if (!DLUtil.isAutoGeneratedDLFileEntryTypeDDMStructureKey(ddmStructureKey)) {

        continue;
      }

      if (ddmStructureKey.equals(importedDLFileEntryDDMStructureKey)) {
        continue;
      }

      importedDDMStructure.setStructureKey(importedDLFileEntryDDMStructureKey);

      DDMStructureLocalServiceUtil.updateDDMStructure(importedDDMStructure);
    }
  }
  @Override
  protected void doImportStagedModel(
      PortletDataContext portletDataContext, DLFileEntryType fileEntryType) throws Exception {

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

    List<Element> ddmStructureReferenceElements =
        portletDataContext.getReferenceElements(
            fileEntryType, com.liferay.dynamic.data.mapping.model.DDMStructure.class);

    long[] ddmStructureIdsArray = new long[ddmStructureReferenceElements.size()];

    Map<Long, Long> ddmStructureIds =
        (Map<Long, Long>)
            portletDataContext.getNewPrimaryKeysMap(
                com.liferay.dynamic.data.mapping.model.DDMStructure.class);

    for (int i = 0; i < ddmStructureReferenceElements.size(); i++) {
      Element ddmStructureReferenceElement = ddmStructureReferenceElements.get(i);

      long ddmStructureId =
          GetterUtil.getLong(ddmStructureReferenceElement.attributeValue("class-pk"));

      ddmStructureIdsArray[i] = MapUtil.getLong(ddmStructureIds, ddmStructureId);
    }

    ServiceContext serviceContext = portletDataContext.createServiceContext(fileEntryType);

    DLFileEntryType importedDLFileEntryType = null;

    Element element = portletDataContext.getImportDataStagedModelElement(fileEntryType);

    boolean preloaded = GetterUtil.getBoolean(element.attributeValue("preloaded"));

    if (portletDataContext.isDataStrategyMirror()) {
      DLFileEntryType existingDLFileEntryType =
          fetchExistingFileEntryType(
              fileEntryType.getUuid(),
              portletDataContext.getScopeGroupId(),
              fileEntryType.getFileEntryTypeKey(),
              preloaded);

      if (existingDLFileEntryType == null) {
        serviceContext.setUuid(fileEntryType.getUuid());

        importedDLFileEntryType =
            DLFileEntryTypeLocalServiceUtil.addFileEntryType(
                userId,
                portletDataContext.getScopeGroupId(),
                fileEntryType.getFileEntryTypeKey(),
                fileEntryType.getNameMap(),
                fileEntryType.getDescriptionMap(),
                ddmStructureIdsArray,
                serviceContext);
      } else {
        DLFileEntryTypeLocalServiceUtil.updateFileEntryType(
            userId,
            existingDLFileEntryType.getFileEntryTypeId(),
            fileEntryType.getNameMap(),
            fileEntryType.getDescriptionMap(),
            ddmStructureIdsArray,
            serviceContext);

        importedDLFileEntryType =
            DLFileEntryTypeLocalServiceUtil.fetchDLFileEntryType(
                existingDLFileEntryType.getFileEntryTypeId());
      }
    } else {
      importedDLFileEntryType =
          DLFileEntryTypeLocalServiceUtil.addFileEntryType(
              userId,
              portletDataContext.getScopeGroupId(),
              fileEntryType.getFileEntryTypeKey(),
              fileEntryType.getNameMap(),
              fileEntryType.getDescriptionMap(),
              ddmStructureIdsArray,
              serviceContext);
    }

    portletDataContext.importClassedModel(fileEntryType, importedDLFileEntryType);

    if (preloaded) {
      return;
    }

    String importedDLFileEntryDDMStructureKey = DLUtil.getDDMStructureKey(importedDLFileEntryType);

    List<DDMStructure> importedDDMStructures = importedDLFileEntryType.getDDMStructures();

    for (DDMStructure importedDDMStructure : importedDDMStructures) {
      String ddmStructureKey = importedDDMStructure.getStructureKey();

      if (!DLUtil.isAutoGeneratedDLFileEntryTypeDDMStructureKey(ddmStructureKey)) {

        continue;
      }

      if (ddmStructureKey.equals(importedDLFileEntryDDMStructureKey)) {
        continue;
      }

      com.liferay.dynamic.data.mapping.model.DDMStructure ddmStructure =
          _ddmStructureLocalService.getDDMStructure(importedDDMStructure.getStructureId());

      ddmStructure.setStructureKey(importedDLFileEntryDDMStructureKey);

      _ddmStructureLocalService.updateDDMStructure(ddmStructure);
    }
  }
  @Override
  public void deleteStagedModel(DLFileEntryType fileEntryType) throws PortalException {

    DLFileEntryTypeLocalServiceUtil.deleteFileEntryType(fileEntryType);
  }