如何创建结构和结构在Liferay 6中以编程方式编写模板 [英] How to create Structure & Template programmatically in Liferay 6

查看:97
本文介绍了如何创建结构和结构在Liferay 6中以编程方式编写模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过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

推荐答案

您的代码有几个问题:

  1. 您没有设置所有必需的属性,例如groupId, companyId, classNameId, structureKey, dates等.

对于DDMStructureDDMTemplate,没有任何setNamesetDescription方法接受String自变量(Liferay 6.2 GA2).相反,只有setNameMapsetDescriptionMap方法都可以接受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(structureIdtemplateId)代替硬编码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());and DDMTemplate ddmTemplate = DDMTemplateUtil.create(CounterLocalServiceUtil.increment());

对于classNameId,您可以使用其值来获取它,例如:
ClassName className = ClassNameLocalServiceUtil.getClassName("com.liferay.portlet.journal.model.Journ‌​alArticle"); long classNameId = className.getClassNameId();

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();

此外,最好在填充的对象上使用update代替添加: DDMStructureUtil.update(ddmStructure);DDMTemplateUtil.update(ddmTemplate);

Also, better to use update over populated object in place of adding: DDMStructureUtil.update(ddmStructure); and DDMTemplateUtil.update(ddmTemplate);

此外,如果您有权访问ThemeDisplay对象,则可以从中获取groupId, companyId, userId, userFullName.另外,为createDatemodifiedDate属性设置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中以编程方式编写模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆