Search in sources :

Example 1 with DDMForm

use of com.liferay.dynamic.data.mapping.model.DDMForm in project liferay-db-setup-core by ableneo.

the class SetupArticles method addDDMStructure.

public static void addDDMStructure(final StructureType structure, final long groupId, final long classNameId) throws PortalException {
    LOG.info(String.format("Adding Article structure %1$s", structure.getName()));
    Map<Locale, String> nameMap = new HashMap<>();
    Locale siteDefaultLocale = null;
    try {
        siteDefaultLocale = PortalUtil.getSiteDefaultLocale(groupId);
    } catch (PortalException e) {
        LOG.error(e);
    }
    String name = getStructureNameOrKey(structure);
    // when default site locale is not 'en_us', then LocaleUtil.getSiteDefault still returns en_us.. we are not IN the site yet..
    // so an exception follows: Name is null (for en_us locale). so:
    nameMap.put(LocaleUtil.US, name);
    nameMap.put(siteDefaultLocale, name);
    Map<Locale, String> descMap = new HashMap<>();
    String content = null;
    DDMForm ddmForm = null;
    DDMFormLayout ddmFormLayout = null;
    try {
        content = ResourcesUtil.getFileContent(structure.getPath());
        ddmForm = DDMUtil.getDDMForm(content);
        if (ddmForm == null) {
            LOG.error("Can not parse given structure JSON content into Liferay DDMForm.");
            return;
        }
        ddmFormLayout = DDMUtil.getDefaultDDMFormLayout(ddmForm);
    } catch (IOException e) {
        LOG.error(String.format("Error Reading Structure File content for: %1$s", structure.getName()));
        return;
    } catch (PortalException e) {
        LOG.error("Can not parse given structure JSON content into Liferay DDMForm.", e);
        return;
    } catch (Exception e) {
        LOG.error(String.format("Other error while trying to get content of the structure file. Possibly wrong filesystem path (%1$s)?", structure.getPath()), e);
        return;
    }
    Locale contentDefaultLocale = ddmForm.getDefaultLocale();
    if (!contentDefaultLocale.equals(siteDefaultLocale)) {
        nameMap.put(contentDefaultLocale, name);
    }
    DDMStructure ddmStructure = DDMStructureLocalServiceUtil.fetchStructure(groupId, classNameId, structure.getKey());
    long runAsUserId = SetupConfigurationThreadLocal.getRunAsUserId();
    long companyId = SetupConfigurationThreadLocal.getRunInCompanyId();
    if (ddmStructure != null) {
        LOG.info("Structure already exists and will be overwritten.");
        if (structure.getParent() != null && !structure.getParent().isEmpty()) {
            LOG.info(String.format("Setting up parent structure: %1$s", structure.getName()));
            DDMStructure parentStructure = DDMStructureLocalServiceUtil.fetchStructure(groupId, classNameId, structure.getParent(), true);
            if (parentStructure != null) {
                ddmStructure.setParentStructureId(parentStructure.getStructureId());
            } else {
                LOG.info(String.format("Parent structure not found: %1$s", structure.getName()));
            }
        }
        DDMStructure ddmStructureSaved = DDMStructureLocalServiceUtil.updateStructure(runAsUserId, ddmStructure.getStructureId(), ddmStructure.getParentStructureId(), nameMap, descMap, ddmForm, ddmFormLayout, new ServiceContext());
        LOG.info(String.format("Template successfully updated: %1$s", structure.getName()));
        SetupPermissions.updatePermission(String.format("Structure %1$s", structure.getKey()), companyId, ddmStructureSaved.getStructureId(), DDMStructure.class.getName() + "-" + JournalArticle.class.getName(), structure.getRolePermissions(), DEFAULT_DDM_PERMISSIONS);
        return;
    }
    DDMStructure newStructure = DDMStructureLocalServiceUtil.addStructure(runAsUserId, groupId, structure.getParent(), classNameId, structure.getKey(), nameMap, descMap, ddmForm, ddmFormLayout, StorageType.JSON.getValue(), 0, new ServiceContext());
    SetupPermissions.updatePermission(String.format("Structure %1$s", structure.getKey()), companyId, newStructure.getStructureId(), DDMStructure.class.getName() + "-" + JournalArticle.class.getName(), structure.getRolePermissions(), DEFAULT_DDM_PERMISSIONS);
    LOG.info(String.format("Added Article structure: %1$s", newStructure.getName()));
}
Also used : Locale(java.util.Locale) DDMStructure(com.liferay.dynamic.data.mapping.model.DDMStructure) HashMap(java.util.HashMap) ServiceContext(com.liferay.portal.kernel.service.ServiceContext) PortalException(com.liferay.portal.kernel.exception.PortalException) IOException(java.io.IOException) DDMForm(com.liferay.dynamic.data.mapping.model.DDMForm) DDMFormLayout(com.liferay.dynamic.data.mapping.model.DDMFormLayout) TemplateDuplicateTemplateKeyException(com.liferay.dynamic.data.mapping.exception.TemplateDuplicateTemplateKeyException) PortalException(com.liferay.portal.kernel.exception.PortalException) SystemException(com.liferay.portal.kernel.exception.SystemException) IOException(java.io.IOException)

Example 2 with DDMForm

use of com.liferay.dynamic.data.mapping.model.DDMForm in project liferay-imex by jpdacunha.

the class WcDDMImporter method doImportStructure.

private DDMStructure doImportStructure(Properties config, ServiceContext serviceContext, boolean debug, Group group, User user, Locale locale, File structureDir) {
    DDMStructure structure = null;
    String structurefileBegin = FileNames.getStructureFileNameBegin();
    ServiceContext serviceContextStr = (ServiceContext) serviceContext.clone();
    File[] structureFiles = FileUtil.listFiles(structureDir, structurefileBegin);
    if (structureFiles != null && structureFiles.length == 1) {
        String structureFileName = structureFiles[0].getName();
        File structureFile = new File(structureDir, structureFileName);
        ImexOperationEnum operation = ImexOperationEnum.UPDATE;
        try {
            ImExStructure imexStructure = (ImExStructure) processor.read(ImExStructure.class, structureFile);
            Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(imexStructure.getName());
            Map<Locale, String> descriptionMap = LocalizationUtil.getLocalizationMap(imexStructure.getDescription());
            long userId = user.getUserId();
            long groupId = group.getGroupId();
            serviceContextStr.setAddGroupPermissions(true);
            serviceContextStr.setAddGuestPermissions(true);
            String content = imexStructure.getData();
            DDMFormDeserializerDeserializeRequest dDMFormDeserializerDeserializeRequest = DDMFormDeserializerDeserializeRequest.Builder.newBuilder(content).build();
            DDMFormDeserializerDeserializeResponse deserialized = _ddmFormDeserializer.deserialize(dDMFormDeserializerDeserializeRequest);
            DDMForm ddmForm = deserialized.getDDMForm();
            DDMFormLayout ddmFormLayout = _ddm.getDefaultDDMFormLayout(ddmForm);
            try {
                structure = dDMStructureLocalService.getDDMStructureByUuidAndGroupId(imexStructure.getUuid(), group.getGroupId());
                long parentStructureId = structure.getParentStructureId();
                long classNameId = structure.getClassNameId();
                String structureKey = structure.getStructureKey();
                structure = dDMStructureLocalService.updateStructure(userId, groupId, parentStructureId, classNameId, structureKey, nameMap, descriptionMap, ddmForm, ddmFormLayout, serviceContextStr);
            } catch (NoSuchStructureException e) {
                long classNameId = classNameLocalService.getClassNameId(JournalArticle.class);
                long parentStructureId = DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID;
                String structureKey = imexStructure.getKey();
                String storageType = imexStructure.getStorageType();
                int type = imexStructure.getStructureType();
                // uuid is set only for creation
                serviceContextStr.setUuid(imexStructure.getUuid());
                try {
                    structure = dDMStructureLocalService.addStructure(userId, groupId, parentStructureId, classNameId, structureKey, nameMap, descriptionMap, ddmForm, ddmFormLayout, storageType, type, serviceContextStr);
                    operation = ImexOperationEnum.CREATE;
                } catch (StructureDuplicateStructureKeyException e1) {
                    // In case of different structure with same key exists => apply custom behavior or not
                    String behaviorValue = GetterUtil.getString(config.get(ImExWCDDmImporterPropsKeys.IMPORT_STRUCTURE_ON_KEY_EXISTS));
                    OnExistsStructureMethodEnum duplicateMethod = OnExistsStructureMethodEnum.fromValue(behaviorValue);
                    if (duplicateMethod != null && duplicateMethod.getValue().equals(OnExistsStructureMethodEnum.UPDATE_EXISTING_STRUCTURE_BY_KEY.getValue())) {
                        structure = dDMStructureLocalService.getStructure(groupId, classNameId, structureKey);
                        parentStructureId = structure.getParentStructureId();
                        classNameId = structure.getClassNameId();
                        structureKey = structure.getStructureKey();
                        structure = dDMStructureLocalService.updateStructure(userId, groupId, parentStructureId, classNameId, structureKey, nameMap, descriptionMap, ddmForm, ddmFormLayout, serviceContextStr);
                    } else {
                        reportService.getError(_log, operation.getValue(), " a structure [" + structureKey + "] with the same key [" + structureKey + "] already exists. Please see [" + ImExWCDDmImporterPropsKeys.IMPORT_STRUCTURE_ON_KEY_EXISTS + "] parameter to customize this behavior.");
                        throw new StructureDuplicateStructureKeyException(e1);
                    }
                }
            }
            String groupName = GroupUtil.getGroupName(group, locale);
            reportService.getOK(_log, groupName, "STRUCTURE : " + structure.getName(locale), structureFile, operation);
        } catch (Exception e) {
            reportService.getError(_log, structureFile.getName(), e);
            if (debug) {
                _log.error(e, e);
            }
        }
    } else {
        reportService.getError(_log, "Wrong number of files", "[" + structureDir.getPath() + "] cannot contain more than one file matching [" + structurefileBegin + "]");
    }
    return structure;
}
Also used : Locale(java.util.Locale) DDMFormDeserializerDeserializeResponse(com.liferay.dynamic.data.mapping.io.DDMFormDeserializerDeserializeResponse) OnExistsStructureMethodEnum(com.liferay.imex.wcddm.model.OnExistsStructureMethodEnum) ServiceContext(com.liferay.portal.kernel.service.ServiceContext) StructureDuplicateStructureKeyException(com.liferay.dynamic.data.mapping.exception.StructureDuplicateStructureKeyException) DDMFormDeserializerDeserializeRequest(com.liferay.dynamic.data.mapping.io.DDMFormDeserializerDeserializeRequest) DDMFormLayout(com.liferay.dynamic.data.mapping.model.DDMFormLayout) NoSuchStructureException(com.liferay.dynamic.data.mapping.exception.NoSuchStructureException) StructureDuplicateStructureKeyException(com.liferay.dynamic.data.mapping.exception.StructureDuplicateStructureKeyException) TemplateDuplicateTemplateKeyException(com.liferay.dynamic.data.mapping.exception.TemplateDuplicateTemplateKeyException) NoSuchTemplateException(com.liferay.dynamic.data.mapping.exception.NoSuchTemplateException) NoSuchStructureException(com.liferay.dynamic.data.mapping.exception.NoSuchStructureException) DDMStructure(com.liferay.dynamic.data.mapping.model.DDMStructure) ImexOperationEnum(com.liferay.imex.core.api.report.model.ImexOperationEnum) ImExStructure(com.liferay.imex.wcddm.model.ImExStructure) File(java.io.File) DDMForm(com.liferay.dynamic.data.mapping.model.DDMForm) JournalArticle(com.liferay.journal.model.JournalArticle)

Aggregations

TemplateDuplicateTemplateKeyException (com.liferay.dynamic.data.mapping.exception.TemplateDuplicateTemplateKeyException)2 DDMForm (com.liferay.dynamic.data.mapping.model.DDMForm)2 DDMFormLayout (com.liferay.dynamic.data.mapping.model.DDMFormLayout)2 DDMStructure (com.liferay.dynamic.data.mapping.model.DDMStructure)2 ServiceContext (com.liferay.portal.kernel.service.ServiceContext)2 Locale (java.util.Locale)2 NoSuchStructureException (com.liferay.dynamic.data.mapping.exception.NoSuchStructureException)1 NoSuchTemplateException (com.liferay.dynamic.data.mapping.exception.NoSuchTemplateException)1 StructureDuplicateStructureKeyException (com.liferay.dynamic.data.mapping.exception.StructureDuplicateStructureKeyException)1 DDMFormDeserializerDeserializeRequest (com.liferay.dynamic.data.mapping.io.DDMFormDeserializerDeserializeRequest)1 DDMFormDeserializerDeserializeResponse (com.liferay.dynamic.data.mapping.io.DDMFormDeserializerDeserializeResponse)1 ImexOperationEnum (com.liferay.imex.core.api.report.model.ImexOperationEnum)1 ImExStructure (com.liferay.imex.wcddm.model.ImExStructure)1 OnExistsStructureMethodEnum (com.liferay.imex.wcddm.model.OnExistsStructureMethodEnum)1 JournalArticle (com.liferay.journal.model.JournalArticle)1 PortalException (com.liferay.portal.kernel.exception.PortalException)1 SystemException (com.liferay.portal.kernel.exception.SystemException)1 File (java.io.File)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1