在Liferay 7中,我有一个结构,一些FTL代码,我想在Java Liferay模块(没有portlet)中创建一个模板。就像人类会使用下面的UI一样,但是以编程方式:
注意:How to create Structure & Template programmatically in Liferay 6提议的代码不起作用,导致Someone may be trying to circumvent the permission checker
例外。
答案 0 :(得分:1)
此代码采用structure
(DDMStructure)并成功创建模板:
ServiceContext serviceContext = new ServiceContext();
serviceContext.setScopeGroupId(group.getGroupId());
serviceContext.setAddGroupPermissions(true);
serviceContext.setAddGuestPermissions(true);
serviceContext.setWorkflowAction(WorkflowConstants.ACTION_PUBLISH);
String templateKey = "my template"; // Any name you want.
long classNameId = PortalUtil.getPortal().getClassNameId(DDMStructure.class);
long classPK = structure.getPrimaryKey();
long resourceClassNameId = PortalUtil.getPortal().getClassNameId(JournalArticle.class);
nameMap = Utils.createLocalesMap(templateKey);
descriptionMap = nameMap; // Use the same.
String type = "display";
String mode = "create";
String language = "ftl";
String script = null;
try {
script = new String(Files.readAllBytes(Paths.get("/path/to/my/template.ftl")));
} catch (IOException e) {
log.error("Exception when reading template: " + templateDefinitionFilePath, e);
}
try {
DDMTemplate template = DDMTemplateLocalServiceUtil.addTemplate(
user.getUserId(), group.getGroupId(), classNameId, classPK,
resourceClassNameId, nameMap, descriptionMap,
type, mode, language, script, serviceContext);
} catch (PortalException e) {
log.error("Exception when creating template: " + templateDefinitionFilePath, e);
return false;
}