use of com.liferay.dynamic.data.mapping.model.DDMForm 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()));
}
use of com.liferay.dynamic.data.mapping.model.DDMForm 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