如何以编程方式创建 Liferay 7 结构?

How to create a Liferay 7 structure programmatically?

在 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 table:

中创建

不幸的是,它没有出现在该站点的 Liferay UI:

如何让它显示出来?

您忘记在调用 DDMStructureLocalServiceUtil 之前初始化 ServiceContext。在 ServiceContext 实例中,您应该添加默认权限:

    ServiceContext context = new ServiceContext();
    context.setAddGroupPermissions(true);
    context.setAddGuestPermissions(true); 

通过之前执行此操作,您可以确保以后可以查看该结构。

DDMStructureLocalServiceUtil.addStructure 方法的 ServiceContext 参数有问题。根据您尝试添加结构的位置,您可以通过两种方式设置上下文:

如果您从有权访问 portlet 请求的 servlet 调用,请使用以下方法:

   ServiceContextFactory.getInstance(className, portletRequest);

这将处理所有必要的范围和权限。

如果您从其他任何地方实施它,最好的方法是实例化 ServiceContext 并至少设置 scopeGroupId:

ServiceContext serviceContext = new ServiceContext();
serviceContext.setScopeGroupId(myGroupId);
serviceContext.setAddGroupPermissions(true);

这是一个 classNameId 问题。将 DDLRecordSet 替换为 JournalArticle 解决了问题,使结构在 Liferay 的结构 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);
}