How to create Structure & Template programmatically in Liferay 6

我怕爱的太早我们不能终老 提交于 2019-12-01 08:54:28

问题


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:

  1. You are not setting all the required properties, like groupId, companyId, classNameId, structureKey, dates etc.

  2. 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>.

  3. Use dynamic ids (structureId and templateId) in place of hard-coded ids, as following: DDMStructure ddmStructure = DDMStructureUtil.create(CounterLocalServiceUtil.increment());and DDMTemplate ddmTemplate = DDMTemplateUtil.create(CounterLocalServiceUtil.increment());

  4. For classNameId, you can get it using it's value, like:
    ClassName className = ClassNameLocalServiceUtil.getClassName("com.liferay.portlet.journal.model.Journ‌​alArticle"); long classNameId = className.getClassNameId();

  5. Also, better to use update over populated object in place of adding: DDMStructureUtil.update(ddmStructure); and DDMTemplateUtil.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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!