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;
}
Aggregations