Search in sources :

Example 1 with NoSuchStructureException

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

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 TemplateDuplicateTemplateKeyException (com.liferay.dynamic.data.mapping.exception.TemplateDuplicateTemplateKeyException)1 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 DDMStructure (com.liferay.dynamic.data.mapping.model.DDMStructure)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 ServiceContext (com.liferay.portal.kernel.service.ServiceContext)1 File (java.io.File)1 Locale (java.util.Locale)1