Search in sources :

Example 1 with TemplateDuplicateTemplateKeyException

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

the class SetupArticles method setupSiteStructuresAndTemplates.

public static void setupSiteStructuresAndTemplates(final Site site, long groupId) throws PortalException {
    List<StructureType> articleStructures = site.getArticleStructure();
    if (articleStructures != null && false == articleStructures.isEmpty()) {
        long classNameId = ClassNameLocalServiceUtil.getClassNameId(JournalArticle.class);
        for (StructureType structure : articleStructures) {
            addDDMStructure(structure, groupId, classNameId);
        }
    }
    List<StructureType> ddlStructures = site.getDdlStructure();
    if (ddlStructures != null && false == ddlStructures.isEmpty()) {
        long classNameId = ClassNameLocalServiceUtil.getClassNameId(DDLRecordSet.class);
        for (StructureType structure : ddlStructures) {
            LOG.info(String.format("Adding DDL structure %1$s", structure.getName()));
            addDDMStructure(structure, groupId, classNameId);
        }
    }
    List<ArticleTemplate> articleTemplates = site.getArticleTemplate();
    if (articleTemplates != null) {
        for (ArticleTemplate template : articleTemplates) {
            try {
                addDDMTemplate(template, groupId);
            } catch (TemplateDuplicateTemplateKeyException e) {
                LOG.error(e);
            }
        }
    }
}
Also used : StructureType(com.ableneo.liferay.portal.setup.domain.StructureType) ArticleTemplate(com.ableneo.liferay.portal.setup.domain.ArticleTemplate) TemplateDuplicateTemplateKeyException(com.liferay.dynamic.data.mapping.exception.TemplateDuplicateTemplateKeyException)

Example 2 with TemplateDuplicateTemplateKeyException

use of com.liferay.dynamic.data.mapping.exception.TemplateDuplicateTemplateKeyException 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

TemplateDuplicateTemplateKeyException (com.liferay.dynamic.data.mapping.exception.TemplateDuplicateTemplateKeyException)2 ArticleTemplate (com.ableneo.liferay.portal.setup.domain.ArticleTemplate)1 StructureType (com.ableneo.liferay.portal.setup.domain.StructureType)1 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 DDMStructure (com.liferay.dynamic.data.mapping.model.DDMStructure)1 DDMTemplate (com.liferay.dynamic.data.mapping.model.DDMTemplate)1 ImexOperationEnum (com.liferay.imex.core.api.report.model.ImexOperationEnum)1 ImExTemplate (com.liferay.imex.wcddm.model.ImExTemplate)1 OnExistsTemplateMethodEnum (com.liferay.imex.wcddm.model.OnExistsTemplateMethodEnum)1 JournalArticle (com.liferay.journal.model.JournalArticle)1 ServiceContext (com.liferay.portal.kernel.service.ServiceContext)1 File (java.io.File)1 Locale (java.util.Locale)1