Site icon Pro Liferay

Liferay Expando API

ExpandoAim of this article:

Understand the Liferay Expando API. In this article we will dynamically add different attributes for User Entity using Liferay Expando services. 

Whats Expando:

Expando represents objects whose member can be added and removed dynamically at run time. In context of Liferay expando is always associated with Entities. Add custom field or add custom attribute simply means that we are adding some extra field to a particular entity. Liferay provides services (API) for doing all the expando operations like add,update, delete …etc.

Lets consider the User_ table in Liferay. If its default column is not sufficient for you then you can add field as well as field value  in run time. Liferay expando related information are persisted in 4 tables.These are

Expando Table & Row
Expando Column & Value

 

In this article we will do all the expando related operations on User entity. we are going to use 4 LocalServices for all the operations

 


 

1. Add Expando Table

Use the method ExpandoTableLocalServiceUtil.addTable(long companyId, long classNameId, String name)

companyId : From themeDisplay object you can get companyId

classNameId :

Every entity in Liferay has an ID. Have a look in classname_ table in Liferay. So how to get classNameId by code for an entity. Use this method

//Return classNameId for User entity

long classNameId = ClassNameLocalServiceUtil.getClassNameId(User.class.getName());

name: Its the table name that we are going to create

Final Code:

long classNameId = ClassNameLocalServiceUtil.getClassNameId(User.class.getName());
//Here we are adding expando table with name MyUser
ExpandoTableLocalServiceUtil.addTable(themeDisplay.getCompanyId(),classNameId, "MyUser");

Check Point:

Go to Liferay database and check expandotable. One new row should be added with table name as MyUser


2. Add Expando Row

Use the method  ExpandoRowLocalServiceUtil.addRow(long tableId, long classPK) to add expando row.

tableId:This is the id of the table that we generated in the Step 1.

classPK: This is generally primary key of particular record of the Entity for which we are adding row. In our case userId is the classPK

Final code:

long classNameId = ClassNameLocalServiceUtil.getClassNameId(User.class.getName());
ExpandoTable expandoTable = ExpandoTableLocalServiceUtil.getTable(themeDisplay.getCompanyId(), classNameId, "MyUser");
ExpandoRowLocalServiceUtil.addRow(expandoTable.getTableId(), user.getUserId());

Check Point:

Check database and look into expandorow table. One new row should be added


3. Add Expando Column

Use the method ExpandoColumnLocalServiceUtil.addColumn(long tableId, String name, int type) to add expando column.

tableId: This is the id for the table which is generated in Step 1.

name : The name of the column

type: While adding column we have to tell whats the type of the column.Various  Column type can be get from ExpandoColumnConstants.java

Final code:

long classNameId = ClassNameLocalServiceUtil.getClassNameId(User.class.getName());
ExpandoTable expandoTable = ExpandoTableLocalServiceUtil.getTable(themeDisplay.getCompanyId(), classNameId, "MyUser");
//We are adding a column. The name of the column MyUserColumn and its type is String
ExpandoColumnLocalServiceUtil.addColumn(expandoTable.getTableId(), "MyUserColumn", ExpandoColumnConstants.STRING);



4. Add Expando Value
Use the method

ExpandoValueLocalServiceUtil.addValue(long classNameId, long tableId, long columnId, long classPK, String data)

classNameId : In our case the classNameId is the ID for the class User

tableId: Its the table ID which is created in Step 1

columnId : Its ID for the column which is generated in Step 3

classPK : Its the primary key of the record in User. In our user.getUserId is the classPK

data : Its the actual expando value

Final code:

long classNameId = ClassNameLocalServiceUtil.getClassNameId(User.class.getName());

//Get the table MyUser

ExpandoTable expandoTable = ExpandoTableLocalServiceUtil.getTable(themeDisplay.getCompanyId(), classNameId, "MyUser");

ExpandoColumn expandoColumn =  ExpandoColumnLocalServiceUtil.getColumn(themeDisplay.getCompanyId(), classNameId, expandoTable.getName(), "MyUserColumn");

//In this we are adding MyUserColumnData for the column MyUserColumn. See the above line

ExpandoValueLocalServiceUtil.addValue(classNameId, expandoTable.getTableId(),expandoColumn.getColumnId(), user.getUserId(), "MyUserColumnData");

Check Point:
In Liferay database check the table expandovalue. A new row should be added with the value  MyUserColumnData


Exit mobile version