Search in sources :

Example 1 with ImexOperationEnum

use of com.liferay.imex.core.api.report.model.ImexOperationEnum 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)

Example 2 with ImexOperationEnum

use of com.liferay.imex.core.api.report.model.ImexOperationEnum 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)

Example 3 with ImexOperationEnum

use of com.liferay.imex.core.api.report.model.ImexOperationEnum in project liferay-imex by jpdacunha.

the class AdtImporter method doImportAdt.

private DDMTemplate doImportAdt(Bundle bundle, ServiceContext serviceContext, boolean debug, Group group, User user, Locale locale, File adtDir, Properties config) {
    DDMTemplate template = null;
    String adtfileBegin = FileNames.getAdtFileNameBegin();
    ServiceContext serviceContextTem = (ServiceContext) serviceContext.clone();
    String groupName = GroupUtil.getGroupName(group, locale);
    File[] adtFiles = FileUtil.listFiles(adtDir, adtfileBegin);
    if (adtFiles != null || (adtFiles != null && adtFiles.length == 0)) {
        for (File matchingAdtFile : Arrays.asList(adtFiles)) {
            String templateFileName = matchingAdtFile.getName();
            File adtFile = new File(adtDir, templateFileName);
            ImexOperationEnum operation = ImexOperationEnum.UPDATE;
            try {
                ImExAdt imexAdt = (ImExAdt) processor.read(ImExAdt.class, adtFile);
                long classNameId = classNameLocalService.getClassNameId(imexAdt.getClassName());
                // Liferay trap here :-) : the ressourceClassNameId is the name of the portlet. Potential problem here if the class is moved in future Liferay versions.
                long resourceClassNameId = classNameLocalService.getClassNameId(RESOURCE_CLASSNAME);
                Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(imexAdt.getName());
                Map<Locale, String> descriptionMap = LocalizationUtil.getLocalizationMap(imexAdt.getDescription());
                long userId = user.getUserId();
                String type = imexAdt.getTemplateType();
                String mode = null;
                String language = imexAdt.getLangType();
                String script = imexAdt.getData();
                boolean cacheable = imexAdt.isCacheable();
                long classPK = 0;
                serviceContextTem.setUuid(imexAdt.getUuid());
                serviceContextTem.setAddGroupPermissions(false);
                serviceContextTem.setAddGuestPermissions(false);
                try {
                    // Searching for existing template
                    template = dDMTemplateLocalService.getDDMTemplateByUuidAndGroupId(imexAdt.getUuid(), group.getGroupId());
                    long templateId = template.getTemplateId();
                    dDMTemplateLocalService.updateTemplate(userId, templateId, classPK, nameMap, descriptionMap, type, mode, language, script, cacheable, serviceContextTem);
                } catch (NoSuchTemplateException e) {
                    long groupId = group.getGroupId();
                    String templateKey = "imex-" + imexAdt.getKey() + "-" + counterLocalService.increment();
                    boolean smallImage = false;
                    String smallImageURL = null;
                    File smallImageFile = null;
                    template = dDMTemplateLocalService.addTemplate(userId, groupId, classNameId, classPK, resourceClassNameId, templateKey, nameMap, descriptionMap, type, mode, language, script, cacheable, smallImage, smallImageURL, smallImageFile, serviceContextTem);
                    operation = ImexOperationEnum.CREATE;
                }
                // Setting ADT permissions
                Resource resource = resourceLocalService.getResource(template.getCompanyId(), DDMTemplate.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, template.getTemplateId() + "");
                permissionSetter.setPermissions(config, bundle, resource);
                reportService.getOK(_log, groupName, "ADT : " + template.getName(locale), adtFile, operation);
            } catch (Exception e) {
                reportService.getError(_log, adtFile.getName(), e);
                if (debug) {
                    _log.error(e, e);
                }
            }
        }
    } else {
        reportService.getMessage(_log, "Missing templates", "No files found matching [" + adtfileBegin + "]");
    }
    return template;
}
Also used : Locale(java.util.Locale) ServiceContext(com.liferay.portal.kernel.service.ServiceContext) Resource(com.liferay.portal.kernel.model.Resource) DDMTemplate(com.liferay.dynamic.data.mapping.model.DDMTemplate) NoSuchTemplateException(com.liferay.dynamic.data.mapping.exception.NoSuchTemplateException) ImExAdt(com.liferay.imex.adt.model.ImExAdt) NoSuchTemplateException(com.liferay.dynamic.data.mapping.exception.NoSuchTemplateException) ImexOperationEnum(com.liferay.imex.core.api.report.model.ImexOperationEnum) File(java.io.File)

Aggregations

NoSuchTemplateException (com.liferay.dynamic.data.mapping.exception.NoSuchTemplateException)3 ImexOperationEnum (com.liferay.imex.core.api.report.model.ImexOperationEnum)3 ServiceContext (com.liferay.portal.kernel.service.ServiceContext)3 File (java.io.File)3 Locale (java.util.Locale)3 NoSuchStructureException (com.liferay.dynamic.data.mapping.exception.NoSuchStructureException)2 StructureDuplicateStructureKeyException (com.liferay.dynamic.data.mapping.exception.StructureDuplicateStructureKeyException)2 TemplateDuplicateTemplateKeyException (com.liferay.dynamic.data.mapping.exception.TemplateDuplicateTemplateKeyException)2 DDMStructure (com.liferay.dynamic.data.mapping.model.DDMStructure)2 DDMTemplate (com.liferay.dynamic.data.mapping.model.DDMTemplate)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 DDMForm (com.liferay.dynamic.data.mapping.model.DDMForm)1 DDMFormLayout (com.liferay.dynamic.data.mapping.model.DDMFormLayout)1 ImExAdt (com.liferay.imex.adt.model.ImExAdt)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