2

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

Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
  • First of all, what sort of structures or templates do you want to create? – Parkash Kumar Apr 06 '16 at 14:46
  • 1
    You are not setting all the required properties, like `classNameId`, `language` etc – Parkash Kumar Apr 06 '16 at 14:48
  • 2
    Also, better to use `DDMStructure ddmStructure = DDMStructureUtil.create(CounterLocalServiceUtil.increment());` and `DDMTemplate ddmTemplate = DDMTemplateUtil.create(CounterLocalServiceUtil.increment());` in place of hard-coded ids. – Parkash Kumar Apr 06 '16 at 14:59
  • @ParkashKumar Thanks a lot it works for me.But i have doubt how to set classNameId and ClassName without hard coding like as you suggested for strcture id. – Kona Laxman Deepak Apr 07 '16 at 11:51
  • ddmStructure.setClassName("com.liferay.portlet.journal.model.JournalArticle");ddmStructure.setClassNameId(10109); This is not the correct way of doing.Please can you suggest me – Kona Laxman Deepak Apr 07 '16 at 11:54
  • You can get `ClassName` object using `ClassName className = ClassNameLocalServiceUtil.getClassName(""com.liferay.portlet.journal.model.JournalArticle"");`, then use `className.getClassNameId()` for setting `classNameId` for structure / template. – Parkash Kumar Apr 07 '16 at 13:08
  • Also, better to use `DDMStructureUtil.update(ddmStructure);` instead of `DDMStructureLocalServiceUtil.addDDMStructure(ddmStructure);` when you are finished setting required properties. Same goes for `DDMTemplate` as well. – Parkash Kumar Apr 07 '16 at 13:11
  • Don't forget to associate your structure to a site: `ddmStructure.setGroupId(groupId);` – Nicolas Raoul May 29 '17 at 09:58
  • and also the company (instance id): `ddmStructure.setCompanyId(PortalUtil.getDefaultCompanyId());` – Nicolas Raoul May 29 '17 at 11:14

1 Answers1

3

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.

Parkash Kumar
  • 4,710
  • 3
  • 23
  • 39
  • Thanks a lot for your time and patience.I set some of the things but i didn't update here.You explained very well here and understood very clearly and learnt some of the best practices. – Kona Laxman Deepak Apr 11 '16 at 05:31
  • I able to set all the things but what about structure key as it is expecting Stirng.How to avoid hard coding of this one. – Kona Laxman Deepak Apr 11 '16 at 05:49
  • Well, I am not pretty much sure about that, but, looking at the records for web content structures in database, I have observed that `structureKey` is always `-1` of `structureId`. Therefore you can set it like `String structureKey = Long.toString(ddmStructure.getStrucutureId() - 1);` – Parkash Kumar Apr 11 '16 at 06:09
  • ok.Thank You.But i didn't see in the DB as you said.If i create directly without setting StructureKey,It is getting populated automatically in DB – Kona Laxman Deepak Apr 12 '16 at 10:07
  • That is fine, this is what I have observed in my database, when creating structures from control panel. – Parkash Kumar Apr 12 '16 at 10:10