问题描述
我需要通过Java代码以编程方式创建结构和模板.我使用了以下代码段.
I need to create the Structure and Template progrmatically through java code.I used following code snippets.
结构:
public void createStructure(String userName,long userId){
log_.info("Inside create structure ");
long structureId=115203;
DDMStructure ddmStructure=DDMStructureLocalServiceUtil.createDDMStructure(structureId);
ddmStructure.setName("MigrationStructure");
ddmStructure.setDescription("This Structure created programatically");
ddmStructure.setUserId(userId);
ddmStructure.setUserName(userName);
File fXmlFile = new File("D:/FilesDataMigration/structure.xml");
try {
Document document = SAXReaderUtil.read(fXmlFile);
ddmStructure.setDocument(document);
DDMStructureLocalServiceUtil.addDDMStructure(ddmStructure);
}catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
log_.info("Inside create structure done");
}
模板:
public void createTemplate(String userName,long userId){
log_.info("Inside create template ");
long templateId=12504;
DDMTemplate ddmTemplate=DDMTemplateLocalServiceUtil.createDDMTemplate(templateId);
ddmTemplate.setName("MigrationTemplate");
ddmTemplate.setDescription("This Template created programatically");
ddmTemplate.setUserId(userId);
ddmTemplate.setUserName(userName);
try {
BufferedReader br = new BufferedReader(new FileReader("D:/FilesDataMigration/template.txt"));
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String script = sb.toString();
ddmTemplate.setScript(script);
DDMTemplateLocalServiceUtil.addDDMTemplate(ddmTemplate);
}catch(IOException e){
e.printStackTrace();
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
log_.info("Inside create template done");
}
以上代码段均正常运行,没有任何异常,但无法在控制面板"的内容部分中看到.如果有任何问题,建议我
The above snippets are executing properly with out any exceptions But unable to see in the content section of Control Panel.Suggest me if anything wrong
推荐答案
您的代码有几个问题:
-
您没有设置所有必需的属性,例如
groupId, companyId, classNameId, structureKey, dates
等.
对于DDMStructure
或DDMTemplate
,没有任何setName
和setDescription
方法接受String
自变量(Liferay 6.2 GA2
).相反,只有setNameMap
和setDescriptionMap
方法都可以接受Map<Locale, String>
.
There isn't any setName
and setDescription
method for DDMStructure
or DDMTemplate
accepting String
argument (Liferay 6.2 GA2
). Instead, there are only setNameMap
and setDescriptionMap
methods for both accepting Map<Locale, String>
.
使用动态ID(structureId
和templateId
)代替硬编码ID,如下所示:DDMStructure ddmStructure = DDMStructureUtil.create(CounterLocalServiceUtil.increment());
和DDMTemplate ddmTemplate = DDMTemplateUtil.create(CounterLocalServiceUtil.increment());
Use dynamic ids (structureId
and templateId
) in place of hard-coded ids, as following:DDMStructure ddmStructure = DDMStructureUtil.create(CounterLocalServiceUtil.increment());
andDDMTemplate ddmTemplate = DDMTemplateUtil.create(CounterLocalServiceUtil.increment());
对于classNameId
,您可以使用其值来获取它,例如:ClassName className = ClassNameLocalServiceUtil.getClassName("com.liferay.portlet.journal.model.JournalArticle");long classNameId = className.getClassNameId();
For classNameId
, you can get it using it's value, like:ClassName className = ClassNameLocalServiceUtil.getClassName("com.liferay.portlet.journal.model.JournalArticle");long classNameId = className.getClassNameId();
此外,最好在填充的对象上使用update
代替添加:DDMStructureUtil.update(ddmStructure);
和DDMTemplateUtil.update(ddmTemplate);
Also, better to use update
over populated object in place of adding:DDMStructureUtil.update(ddmStructure);
andDDMTemplateUtil.update(ddmTemplate);
此外,如果您有权访问ThemeDisplay
对象,则可以从中获取groupId, companyId, userId, userFullName
.另外,为createDate
和modifiedDate
属性设置new Date()
.
Additionally, if you have access to the ThemeDisplay
object, you can get groupId, companyId, userId, userFullName
from it. Also, set new Date()
for createDate
and modifiedDate
properties.
这篇关于如何创建结构和结构在Liferay 6中以编程方式编写模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!