Wednesday, January 9, 2013

Liferay Expando Services


Liferay provides a mechanism to create tables, columns, rows and values dynamically. It provides these features through the Expando services. The mechanism is supported by using four database tables: ExpandoTable, ExpandoColumn, ExpandoRow and ExpandoValue.

Creating tables dynamically

The class com.liferay.portlet.expando.service.ExpandoTableLocalServiceUtil allows creation of dynamic tables.
To create a new table, call:
ExpandoTable table = ExpandoTableLocalServiceUtil.addTable(<companyId>, <className>, <tableName>);
Parameters:
<companyId> (Type: long) – The company id for the table. To get the company id, call
long companyId = CompanyLocalServiceUtil.getCompanies().get(0).getCompanyId();
<className> (Type: String) – The class name should be the fully qualified class name of the java class that you are trying to save to database.
<tableName> (Type: String) – The name of the dynamic table to create.
A new row is added to the ExpandoTable table.

Creating columns dynamically

The class com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil allows creation of dynamic columns.
To create a new column, call:
ExpandoColumn column = ExpandoColumnLocalServiceUtil.addColumn(<tableId>, <columnName>, <type>);
Parameters:
<tableId> (Type: long) – The table id for which to create column.
<columnName> (Type: String) – The name of the column.
<type> (Type: int) – The data type of the column. Use the ExpandoColumnConstants to specify the data type. For example, ExpandoColumnConstants.STRING;
A new row is added to the ExpandoColumn table.

Creating rows dynamically

The class com.liferay.portlet.expando.service.ExpandoRowLocalServiceUtil allows creation of dynamic rows.
To create a new row, call:
ExpandoRow row = ExpandoRowLocalServiceUtil.addRow(<tableId>, <classPK>);
Parameters:
<tableId> (Type: long) – The table id for which to create row.
<classPK> (Type: long) – The classPK .
A new row is added to the ExpandoRow table.

Storing values in a dynamic table

The class com.liferay.portlet.expando.service.ExpandoValueLocalServiceUtil allows storing values for dynamic tables.
To store a value, call:
ExpandoValue
value = ExpandoValueLocalServiceUtil.addValue(<classNameId>,
<tableId>, <columnId>, <classPK>, <value>);
Parameters:
<classNameId> (Type: long) – The class name id.
<tableId> (Type: long) – The table id for which to store value.
<columnId> (Type: long) – The column id of the table for which to store value.
<classPK> (Type: long) – The classPK .
<value> (Type: String) – The value to be stored .
A new row is added to the ExpandoValue table. There are several variations of the addValue API to store different data types.

Removing dynamic entities

Delete Table:
ExpandoTableLocalServiceUtil.deleteTable(long <tableId>)
Delete Column:
ExpandoColumnLocalServiceUtil.deleteColumn(long <columnId>)
Delete Row:
ExpandoRowLocalServiceUtil.deleteRow(long <rowId>)
Delete Value:
ExpandoValueLocalServiceUtil.deleteValue(long<valueId>)
The above APIs for each entity is the simplest one. There are other variations depending on the parameters. The above APIs only removes the corresponding entity. It does not remove the related entities. For example, deleting table does not automatically delete the columns, rows and values.

Updating dynamic entities

Update Table:
ExpandoTableLocalServiceUtil.updateTable(long <tableId>, String <name>)
ExpandoTableLocalServiceUtil.updateExpandoTable(ExpandoTable <table>)
Update Column:
ExpandoColumnLocalServiceUtil.updateColumn(<...>)
ExpandoColumnLocalServiceUtil.updateExpandoColumn(<...>)
Update Row:
ExpandoRowLocalServiceUtil.updateExpandoRow(<...>)
Update Value:
ExpandoValueLocalServiceUtil.updateExpandoValue(<...>)

Searching for entities

Search Table:
ExpandoTableLocalServiceUtil.getTable(<...>)
ExpandoTableLocalServiceUtil.getExpandoTable(long <tableId>)
Search Column:
ExpandoColumnLocalServiceUtil.getColumn(<...>)
ExpandoColumnLocalServiceUtil.getExpandoColumn(long <columnId>)
Search Row:
ExpandoRowLocalServiceUtil.getRow(<...>)
ExpandoRowLocalServiceUtil.getExpandoRow(long <rowId>)
Search Value:
ExpandoValueLocalServiceUtil.getValue(<...>)
ExpandoValueLocalServiceUtil.getExpandoValue(long <valueId>)
ExpandoValueLocalServiceUtil.getData(<...>)

Dynamic queries

For dynamic queries, all the Local service util provide 2 APIs:
Expando[Table|Column|Row|Value]LocalServiceUtil.dynamicQuery(<...>)
Expando[Table|Column|Row|Value]LocalServiceUtil.dynamicQueryCount(DynamicQuery <dynamicQuery>)

No comments:

Post a Comment