@Override
  @SystemEvent(action = SystemEventConstants.ACTION_SKIP, type = SystemEventConstants.TYPE_DELETE)
  public void deleteFileEntryType(DLFileEntryType dlFileEntryType) throws PortalException {

    if (dlFileEntryPersistence.countByFileEntryTypeId(dlFileEntryType.getFileEntryTypeId()) > 0) {

      throw new RequiredFileEntryTypeException();
    }

    DDMStructure ddmStructure =
        ddmStructureLocalService.fetchStructure(
            dlFileEntryType.getGroupId(),
            classNameLocalService.getClassNameId(DLFileEntryMetadata.class),
            DLUtil.getDDMStructureKey(dlFileEntryType));

    if (ddmStructure == null) {
      ddmStructure =
          ddmStructureLocalService.fetchStructure(
              dlFileEntryType.getGroupId(),
              classNameLocalService.getClassNameId(DLFileEntryMetadata.class),
              DLUtil.getDeprecatedDDMStructureKey(dlFileEntryType));
    }

    if (ddmStructure != null) {
      ddmStructureLocalService.deleteStructure(ddmStructure.getStructureId());
    }

    dlFileEntryTypePersistence.remove(dlFileEntryType);
  }
  protected void fixDDMStructureKey(String fileEntryTypeUuid, long fileEntryTypeId, long groupId) {

    DDMStructure ddmStructure =
        ddmStructureLocalService.fetchStructure(
            groupId,
            classNameLocalService.getClassNameId(DLFileEntryMetadata.class),
            DLUtil.getDeprecatedDDMStructureKey(fileEntryTypeId));

    if (ddmStructure != null) {
      ddmStructure.setStructureKey(DLUtil.getDDMStructureKey(fileEntryTypeUuid));

      ddmStructureLocalService.updateDDMStructure(ddmStructure);
    }
  }
  public void deleteFileEntryType(DLFileEntryType dlFileEntryType)
      throws PortalException, SystemException {

    DDMStructure ddmStructure =
        ddmStructureLocalService.fetchStructure(
            dlFileEntryType.getGroupId(),
            PortalUtil.getClassNameId(DLFileEntryMetadata.class),
            DLUtil.getDDMStructureKey(dlFileEntryType));

    if (ddmStructure == null) {
      ddmStructure =
          ddmStructureLocalService.fetchStructure(
              dlFileEntryType.getGroupId(),
              PortalUtil.getClassNameId(DLFileEntryMetadata.class),
              DLUtil.getDeprecatedDDMStructureKey(dlFileEntryType));
    }

    if (ddmStructure != null) {
      ddmStructureLocalService.deleteStructure(ddmStructure.getStructureId());
    }

    dlFileEntryTypePersistence.remove(dlFileEntryType);
  }
  protected long updateDDMStructure(
      long userId,
      String fileEntryTypeUuid,
      long fileEntryTypeId,
      long groupId,
      String name,
      String description,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    fixDDMStructureKey(fileEntryTypeUuid, fileEntryTypeId, groupId);

    String ddmStructureKey = DLUtil.getDDMStructureKey(fileEntryTypeUuid);

    Map<Locale, String> nameMap = new HashMap<Locale, String>();

    Locale locale = serviceContext.getLocale();

    nameMap.put(locale, name);

    Locale defaultLocale = LocaleUtil.getDefault();

    nameMap.put(defaultLocale, name);

    Map<Locale, String> descriptionMap = new HashMap<Locale, String>();

    descriptionMap.put(locale, description);
    descriptionMap.put(defaultLocale, description);

    String xsd = ParamUtil.getString(serviceContext, "xsd");

    DDMStructure ddmStructure =
        ddmStructureLocalService.fetchStructure(
            groupId, PortalUtil.getClassNameId(DLFileEntryMetadata.class), ddmStructureKey);

    if ((ddmStructure != null) && Validator.isNull(xsd)) {
      xsd = ddmStructure.getXsd();
    }

    Locale[] contentAvailableLocales =
        LocaleUtil.fromLanguageIds(LocalizationUtil.getAvailableLocales(xsd));

    for (Locale contentAvailableLocale : contentAvailableLocales) {
      nameMap.put(contentAvailableLocale, name);

      descriptionMap.put(contentAvailableLocale, description);
    }

    try {
      if (ddmStructure == null) {
        ddmStructure =
            ddmStructureLocalService.addStructure(
                userId,
                groupId,
                DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID,
                PortalUtil.getClassNameId(DLFileEntryMetadata.class),
                ddmStructureKey,
                nameMap,
                descriptionMap,
                xsd,
                "xml",
                DDMStructureConstants.TYPE_AUTO,
                serviceContext);
      } else {
        ddmStructure =
            ddmStructureLocalService.updateStructure(
                ddmStructure.getStructureId(),
                ddmStructure.getParentStructureId(),
                nameMap,
                descriptionMap,
                xsd,
                serviceContext);
      }

      return ddmStructure.getStructureId();
    } catch (StructureXsdException sxe) {
      if (ddmStructure != null) {
        ddmStructureLocalService.deleteStructure(ddmStructure.getStructureId());
      }
    }

    return 0;
  }
  @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);
    }
  }
  protected long updateDDMStructure(
      long userId,
      String fileEntryTypeUuid,
      long fileEntryTypeId,
      long groupId,
      Map<Locale, String> nameMap,
      Map<Locale, String> descriptionMap,
      ServiceContext serviceContext)
      throws PortalException {

    fixDDMStructureKey(fileEntryTypeUuid, fileEntryTypeId, groupId);

    String ddmStructureKey = DLUtil.getDDMStructureKey(fileEntryTypeUuid);

    DDMForm ddmForm = (DDMForm) serviceContext.getAttribute("ddmForm");

    DDMStructure ddmStructure =
        ddmStructureLocalService.fetchStructure(
            groupId,
            classNameLocalService.getClassNameId(DLFileEntryMetadata.class),
            ddmStructureKey);

    if ((ddmStructure != null) && (ddmForm == null)) {
      ddmForm = ddmStructure.getDDMForm();
    }

    if (ddmForm == null) {
      return 0;
    }

    try {
      DDMFormLayout ddmFormLayout = DDMUtil.getDefaultDDMFormLayout(ddmForm);

      if (ddmStructure == null) {
        ddmStructure =
            ddmStructureLocalService.addStructure(
                userId,
                groupId,
                DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID,
                classNameLocalService.getClassNameId(DLFileEntryMetadata.class),
                ddmStructureKey,
                nameMap,
                descriptionMap,
                ddmForm,
                ddmFormLayout,
                StorageType.JSON.toString(),
                DDMStructureConstants.TYPE_AUTO,
                serviceContext);
      } else {
        ddmStructure =
            ddmStructureLocalService.updateStructure(
                userId,
                ddmStructure.getStructureId(),
                ddmStructure.getParentStructureId(),
                nameMap,
                descriptionMap,
                ddmForm,
                ddmFormLayout,
                serviceContext);
      }

      return ddmStructure.getStructureId();
    } catch (StructureDefinitionException sde) {
      if (ddmStructure != null) {
        ddmStructureLocalService.deleteStructure(ddmStructure.getStructureId());
      }
    }

    return 0;
  }
  @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);
    }
  }