Problem : While working with the Liferay projects, you may need to create a configuration from the control panel like custom fields, structures, templates, roles, permissions, and many more in order to make portlet/implementation work properly.

Solution : You can create the BundleActivator component to create the above configurations programmatically.

Benefits : 

  • We don’t need remember/prepare steps to create configurations from Control Panel.
  • This will remove overhead of creating configurations in all environments (Dev, Test, Production) you have for your project.

Prerequisites

  • Java 
  • Liferay portal 7/7.x
  • Basic knowledge of Liferay

Environment Requirements

  • JDK 8
  • Eclipse
  • MySQL
  • Liferay Portal
  • Gradle

To create BundleActivator for the Liferay project, you need to perform the below steps. In this example, we will explain to you to create custom fields, structure, templates and roles.

1. Create a Liferay module project using the following steps.
  1. Go to Liferay workspace project modules new
  2. Select other Liferay Liferay Module Project and Click on “Next”.
  3. Enter the project name.
  4. Select “Project Template Name” as “ MVC-portlet” and Click on “Next”.
  5. Enter a Package name and click on “Finish”. The necessary file structure for the MVC-portlet automatically gets created as below.
Configuration-Creator-in-Liferay-7.2
2. Create a package with the name com.ignek.action.
3. Create one class with the name ConfigurationCreator into this package.
4. Enter the following line into the bnd.bnd.

Bundle-Activator: com.ignek.action.ConfigurationCreator.

5. Enter the following lines into the build.gradle.

compileOnly group: “org.osgi”, name: “org.osgi.core”

compileOnly group: “com.liferay”, name: “com.liferay.dynamic.data.mapping.api”

compileOnly group: “com.liferay”, name: “com.liferay.journal.api”

compileOnly group: “com.liferay”, name: “com.liferay.petra.string”

6. Open ConfigurationCreator.java & add the following lines before the declaration of the class name.

7. Implement the BundleActivator in ConfigurationCreator.

8. Override the following methods inside the class ConfigurationCreator.

Note : Start() method will call automatically when a module is deployed or Liferay server is started. We will add our code to create custom fields, structures, templates, and roles in this method.

1. Create custom fields

Write the below code in the start() method to create custom fields.

ExpandoBridge allows you to add custom fields in Liferay programmatically.

Notes : 

1. You can choose different inputType for Custom fields.

For Text and numberint inputType = 15;
For Selectionint inputType = 16;
For Mapint inputType = 21;
For Dateint inputType = 3;
For true/false (boolean)int inputType = 1;

2. You can choose different className to generate custom fields for sites, organizations, roles, users, and several more.

For sitesGroup.class.getName()
For OrganizationOrganization.class.getName()
For RoleRole.class.getName()

3. You can check generated custom fields from control-panel configuration custom-fields (refer below image).

Configuration-Creator-in-Liferay-7.2

 2. Create Structure

Perform the below steps to create Structure programmatically.

  1. Create a folder inside the src/main/resources with name structures.
  2. Create a file with the name student.json in this folder.
  3. Add the source of structure into this file (You can copy the source from the Liferay control panel).

Add the following code into the start() method to generate structures.

Output :

Configuration-Creator-in-Liferay-7.2

3. Create Template

To generate a template for an existing structure, perform below steps.

  1. Create one folder in src/main/resources with the name templates.
  2. Create one file with the name student.ftl in this folder.
  3. Add the source of the template into this file (You can copy the source from the Liferay admin panel).
  4.  Add the following code into the start() method to create a template.

Output:

Configuration-Creator-in-Liferay-7.2

4. Create Roles

Write the below code (Inside the start() method) to generate roles in Liferay.

As you see, the Head of Organization role is created under control-panel → users → roles.

Configuration-Creator-in-Liferay-7.2

Now the structure of the Configuration Creator project will look like the structure below.

Configuration-Creator-in-Liferay-7.2

Now build/deploy this using the following command.

  •  blade gw deploy

You can download the source code from the below link.

configuration creator source code