问题
I need to create the Structure and Template progrmatically through java code.I used following code snippets.
Structure:
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");
}
Template:
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
回答1:
There are couple of issues with your code:
You are not setting all the required properties, like
groupId, companyId, classNameId, structureKey, datesetc.There isn't any
setNameandsetDescriptionmethod forDDMStructureorDDMTemplateacceptingStringargument (Liferay 6.2 GA2). Instead, there are onlysetNameMapandsetDescriptionMapmethods for both acceptingMap<Locale, String>.Use dynamic ids (
structureIdandtemplateId) in place of hard-coded ids, as following:DDMStructure ddmStructure = DDMStructureUtil.create(CounterLocalServiceUtil.increment());andDDMTemplate ddmTemplate = DDMTemplateUtil.create(CounterLocalServiceUtil.increment());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();Also, better to use
updateover populated object in place of adding:DDMStructureUtil.update(ddmStructure);andDDMTemplateUtil.update(ddmTemplate);
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.
来源:https://stackoverflow.com/questions/36442540/how-to-create-structure-template-programmatically-in-liferay-6