Search in sources :

Example 1 with DDMStructure

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

the class ResolverUtil method getStructureUUID.

public static String getStructureUUID(final String structureKey, final long groupId, final Class clazz) throws PortalException {
    long classNameId = ClassNameLocalServiceUtil.getClassNameId(clazz);
    DDMStructure structure = DDMStructureLocalServiceUtil.getStructure(groupId, classNameId, structureKey);
    return structure.getUuid();
}
Also used : DDMStructure(com.liferay.dynamic.data.mapping.model.DDMStructure)

Example 2 with DDMStructure

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

the class ResolverUtil method getStructureId.

// CHECKSTYLE:ON
public static long getStructureId(final String structureKey, final long groupId, final String className, boolean includeAncestorStructures) throws PortalException {
    long classNameId = ClassNameLocalServiceUtil.getClassNameId(className);
    DDMStructure structure = DDMStructureLocalServiceUtil.getStructure(groupId, classNameId, structureKey, includeAncestorStructures);
    return structure.getStructureId();
}
Also used : DDMStructure(com.liferay.dynamic.data.mapping.model.DDMStructure)

Example 3 with DDMStructure

use of com.liferay.dynamic.data.mapping.model.DDMStructure 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 4 with DDMStructure

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

the class WcDDMExporter method doExport.

public void doExport(Properties config, Group group, File groupsDir, Locale locale, List<ExporterRawContent> rawContentToExport, boolean debug) throws ImexException {
    if (group != null) {
        // String groupName = GroupUtil.getGroupName(group, locale);
        long classNameId = classNameLocalService.getClassNameId(JournalArticle.class);
        List<DDMStructure> ddmStructures = dDMStructureLocalService.getStructures(group.getGroupId(), classNameId);
        if (ddmStructures != null && ddmStructures.size() > 0) {
            File groupDir = initializeSingleGroupDirectory(groupsDir, group);
            if (groupDir != null) {
                if (groupDir.exists()) {
                    // Iterate over structures
                    for (DDMStructure ddmStructure : ddmStructures) {
                        File structureDir = initializeSingleWCDDMExportDirectory(groupDir, ddmStructure);
                        if (structureDir != null) {
                            if (structureDir.exists()) {
                                try {
                                    writeStructure(group, locale, rawContentToExport, ddmStructure, structureDir);
                                    String groupName = GroupUtil.getGroupName(group, locale);
                                    reportService.getOK(_log, groupName, "STRUCTURE : " + ddmStructure.getName(locale));
                                    List<DDMTemplate> ddmTemplates = dDMTemplateLocalService.getTemplatesByClassPK(ddmStructure.getGroupId(), ddmStructure.getPrimaryKey());
                                    // Iterate over templates
                                    for (DDMTemplate ddmTemplate : ddmTemplates) {
                                        writeTemplate(group, locale, rawContentToExport, structureDir, groupName, ddmTemplate);
                                        reportService.getOK(_log, groupName, "TEMPLATE : " + ddmTemplate.getName(locale));
                                    }
                                    reportService.getSeparator(_log);
                                } catch (Exception e) {
                                    reportService.getError(_log, ddmStructure.getName(locale), e);
                                    if (debug) {
                                        _log.error(e, e);
                                    }
                                }
                            } else {
                                reportService.getDNE(_log, structureDir.getAbsolutePath());
                            }
                        } else {
                            _log.error("structureDir is null ...");
                        }
                    }
                } else {
                    reportService.getDNE(_log, groupDir.getAbsolutePath());
                }
            } else {
                _log.error("groupDir is null ...");
            }
        } else {
            reportService.getEmpty(_log, group, locale);
        }
    } else {
        _log.error("Skipping null group ...");
    }
}
Also used : DDMStructure(com.liferay.dynamic.data.mapping.model.DDMStructure) DDMTemplate(com.liferay.dynamic.data.mapping.model.DDMTemplate) File(java.io.File) ImexException(com.liferay.imex.core.util.exception.ImexException)

Example 5 with DDMStructure

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

the class WcDDMImporter method doImportTemplate.

private DDMTemplate doImportTemplate(Properties config, ServiceContext serviceContext, boolean debug, Group group, User user, Locale locale, File structureDir, DDMStructure structure) {
    DDMTemplate template = null;
    String templatefileBegin = FileNames.getTemplateFileNameBegin();
    File[] templateFiles = FileUtil.listFiles(structureDir, templatefileBegin);
    String groupName = GroupUtil.getGroupName(group, locale);
    ServiceContext serviceContextTem = (ServiceContext) serviceContext.clone();
    if (templateFiles != null || (templateFiles != null && templateFiles.length == 0)) {
        for (File matchingTemplateFile : Arrays.asList(templateFiles)) {
            String templateFileName = matchingTemplateFile.getName();
            File templateFile = new File(structureDir, templateFileName);
            ImexOperationEnum operation = ImexOperationEnum.UPDATE;
            try {
                ImExTemplate imexTemplate = (ImExTemplate) processor.read(ImExTemplate.class, templateFile);
                Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(imexTemplate.getName());
                Map<Locale, String> descriptionMap = LocalizationUtil.getLocalizationMap(imexTemplate.getDescription());
                long userId = user.getUserId();
                String type = imexTemplate.getTemplateType();
                String mode = null;
                String language = imexTemplate.getLangType();
                String script = imexTemplate.getData();
                boolean cacheable = imexTemplate.isCacheable();
                long classPK = 0;
                if (structure != null) {
                    classPK = structure.getStructureId();
                }
                serviceContextTem.setAddGroupPermissions(true);
                serviceContextTem.setAddGuestPermissions(true);
                long groupId = group.getGroupId();
                try {
                    // Searching for existing template
                    template = dDMTemplateLocalService.getDDMTemplateByUuidAndGroupId(imexTemplate.getUuid(), groupId);
                    long templateId = template.getTemplateId();
                    dDMTemplateLocalService.updateTemplate(userId, templateId, classPK, nameMap, descriptionMap, type, mode, language, script, cacheable, serviceContextTem);
                } catch (NoSuchTemplateException e) {
                    long classNameId = classNameLocalService.getClassNameId(DDMStructure.class);
                    long resourceClassNameId = classNameLocalService.getClassNameId(JournalArticle.class);
                    String templateKey = imexTemplate.getKey();
                    boolean smallImage = false;
                    String smallImageURL = null;
                    File smallImageFile = null;
                    try {
                        template = dDMTemplateLocalService.addTemplate(userId, groupId, classNameId, classPK, resourceClassNameId, templateKey, nameMap, descriptionMap, type, mode, language, script, cacheable, smallImage, smallImageURL, smallImageFile, serviceContextTem);
                        operation = ImexOperationEnum.CREATE;
                    } catch (TemplateDuplicateTemplateKeyException e1) {
                        // In case of different template with same key exists => apply custom behavior or not
                        String behaviorValue = GetterUtil.getString(config.get(ImExWCDDmImporterPropsKeys.IMPORT_TEMPLATE_ON_KEY_EXISTS));
                        OnExistsTemplateMethodEnum duplicateMethod = OnExistsTemplateMethodEnum.fromValue(behaviorValue);
                        if (duplicateMethod.getValue().equals(OnExistsTemplateMethodEnum.UPDATE_EXISTING_TEMPLATE_BY_KEY.getValue())) {
                            template = dDMTemplateLocalService.getTemplate(groupId, classNameId, templateKey);
                            long templateId = template.getTemplateId();
                            dDMTemplateLocalService.updateTemplate(userId, templateId, classPK, nameMap, descriptionMap, type, mode, language, script, cacheable, serviceContextTem);
                        } else {
                            reportService.getError(_log, templateFile.getName(), " a template with the same key [" + templateKey + "] already exists. Please see [" + ImExWCDDmImporterPropsKeys.IMPORT_STRUCTURE_ON_KEY_EXISTS + "] parameter to customize this behavior.");
                            throw new TemplateDuplicateTemplateKeyException(e1);
                        }
                    }
                }
                reportService.getOK(_log, groupName, "[TEMPLATE : " + template.getName(locale) + " => STRUCTURE : " + structure.getName(locale) + "]", templateFile, operation);
            } catch (Exception e) {
                reportService.getError(_log, templateFile.getName(), e);
                if (debug) {
                    _log.error(e, e);
                }
            }
        }
    } else {
        reportService.getMessage(_log, "Missing templates", "No files found matching [" + templatefileBegin + "]");
    }
    return template;
}
Also used : Locale(java.util.Locale) ServiceContext(com.liferay.portal.kernel.service.ServiceContext) DDMTemplate(com.liferay.dynamic.data.mapping.model.DDMTemplate) ImExTemplate(com.liferay.imex.wcddm.model.ImExTemplate) 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) NoSuchTemplateException(com.liferay.dynamic.data.mapping.exception.NoSuchTemplateException) DDMStructure(com.liferay.dynamic.data.mapping.model.DDMStructure) ImexOperationEnum(com.liferay.imex.core.api.report.model.ImexOperationEnum) TemplateDuplicateTemplateKeyException(com.liferay.dynamic.data.mapping.exception.TemplateDuplicateTemplateKeyException) OnExistsTemplateMethodEnum(com.liferay.imex.wcddm.model.OnExistsTemplateMethodEnum) File(java.io.File) JournalArticle(com.liferay.journal.model.JournalArticle)

Aggregations

DDMStructure (com.liferay.dynamic.data.mapping.model.DDMStructure)7 File (java.io.File)4 TemplateDuplicateTemplateKeyException (com.liferay.dynamic.data.mapping.exception.TemplateDuplicateTemplateKeyException)3 ServiceContext (com.liferay.portal.kernel.service.ServiceContext)3 Locale (java.util.Locale)3 NoSuchStructureException (com.liferay.dynamic.data.mapping.exception.NoSuchStructureException)2 NoSuchTemplateException (com.liferay.dynamic.data.mapping.exception.NoSuchTemplateException)2 StructureDuplicateStructureKeyException (com.liferay.dynamic.data.mapping.exception.StructureDuplicateStructureKeyException)2 DDMForm (com.liferay.dynamic.data.mapping.model.DDMForm)2 DDMFormLayout (com.liferay.dynamic.data.mapping.model.DDMFormLayout)2 DDMTemplate (com.liferay.dynamic.data.mapping.model.DDMTemplate)2 ImexOperationEnum (com.liferay.imex.core.api.report.model.ImexOperationEnum)2 JournalArticle (com.liferay.journal.model.JournalArticle)2 DDMFormDeserializerDeserializeRequest (com.liferay.dynamic.data.mapping.io.DDMFormDeserializerDeserializeRequest)1 DDMFormDeserializerDeserializeResponse (com.liferay.dynamic.data.mapping.io.DDMFormDeserializerDeserializeResponse)1 ImexException (com.liferay.imex.core.util.exception.ImexException)1 ImExStructure (com.liferay.imex.wcddm.model.ImExStructure)1 ImExTemplate (com.liferay.imex.wcddm.model.ImExTemplate)1 OnExistsStructureMethodEnum (com.liferay.imex.wcddm.model.OnExistsStructureMethodEnum)1 OnExistsTemplateMethodEnum (com.liferay.imex.wcddm.model.OnExistsTemplateMethodEnum)1