liferayliferay-6liferay-themeliferay-auiliferay-velocity

How to create Structure & Template programmatically in Liferay 6


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


Solution

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