在Liferay 7中,如何从Java模块创建structure? 这是我的尝试:
Map<Locale, String> nameMap = new HashMap<Locale, String>();
nameMap.put(Locale.JAPAN, "The name");
Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
descriptionMap.put(Locale.JAPAN, "The description");
DDMForm ddmForm = DDMUtil.getDDMForm("<here goes my real JSON form>");
DDMFormLayout ddmFormLayout = DDMUtil.getDefaultDDMFormLayout(ddmForm);
DDMStructureLocalServiceUtil.addStructure(
20156, // userId
33421, // groupId
DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID, // parentStructureId
PortalUtil.getPortal().getClassNameId(DDLRecordSet.class), // classNameId
new Long(CounterLocalServiceUtil.increment()).toString(), // structureKey
nameMap,
descriptionMap,
ddmForm,
ddmFormLayout,
StorageType.JSON.toString(),
0, // type
new ServiceContext()
);
结构在数据库的DDMStructure
表中创建:
不幸的是,它没有出现在该网站的Liferay UI中:
如何让它出现?
ResourcePermission
表中...在Java中创建结构时,我是否还要创建这3个对象?答案 0 :(得分:0)
您忘记在调用DDMStructureLocalServiceUtil之前初始化ServiceContext。在ServiceContext实例中,您应该添加默认权限:
ServiceContext context = new ServiceContext();
context.setAddGroupPermissions(true);
context.setAddGuestPermissions(true);
通过以前执行此操作,您可以确保以后可以查看结构。
答案 1 :(得分:0)
ServiceContext
方法的DDMStructureLocalServiceUtil.addStructure
参数存在问题。您可以通过两种方式设置上下文,具体取决于您尝试添加结构的位置:
如果要从有权访问portlet请求的servlet调用,请使用以下方法:
ServiceContextFactory.getInstance(className, portletRequest);
这将考虑所有必要的范围和许可。
如果您从其他任何地方实现它,最好的方法是实例化ServiceContext并至少设置scopeGroupId
:
ServiceContext serviceContext = new ServiceContext();
serviceContext.setScopeGroupId(myGroupId);
serviceContext.setAddGroupPermissions(true);
答案 2 :(得分:0)
这是一个classNameId问题。用DDLRecordSet
替换JournalArticle
可以解决问题,使结构在Liferay的Structures UI中正确显示。
有效的代码:
ServiceContext serviceContext = new ServiceContext();
serviceContext.setScopeGroupId(group.getGroupId());
serviceContext.setAddGroupPermissions(true);
serviceContext.setAddGuestPermissions(true);
serviceContext.setWorkflowAction(WorkflowConstants.ACTION_PUBLISH);
Map<Locale, String> nameMap = new HashMap<Locale, String>();
nameMap.put(Locale.JAPAN, "The name");
Map<Locale, String> descriptionMap = new HashMap<Locale, String>();
descriptionMap.put(Locale.JAPAN, "The description");
DDMForm ddmForm = null;
try {
ddmForm = DDMUtil.getDDMForm(json);
} catch (PortalException e) {
log.error("Exception when parsing structure JSON", e);
}
DDMFormLayout ddmFormLayout = DDMUtil.getDefaultDDMFormLayout(ddmForm);
long scopeClassNameId = PortalUtil.getPortal().getClassNameId(JournalArticle.class);
long parentStructureId = DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID;
String storageType = StorageType.JSON.toString();
String structureKey = "my structure";
try {
DDMStructure ddmStructure = DDMStructureLocalServiceUtil.addStructure(
user.getUserId(), group.getGroupId(), parentStructureId,
scopeClassNameId, structureKey,
nameMap, descriptionMap, ddmForm, ddmFormLayout, storageType,
DDMStructureConstants.TYPE_DEFAULT, serviceContext);
} catch (StructureDuplicateStructureKeyException e) {
log.info("Skipping creation of structure that already exists");
} catch (PortalException e) {
log.error("Exception when creating structure: " + structureDefinitionFilePath, e);
}