Wednesday, 20 August 2014

Using Custom Fields/Custom Attributes in Liferay 6.1


Some time we may come across the need for adding a property or storing data associated with OOTB portlets. We dont need to create any new service. It can be done with the help of Custom Attributes or Custom Fields. Liferay provides this feature through Expando services.

The mechanism is supported by using 4 database tables: ExpandoTable, ExpandoColumn, ExpandoRow and ExpandoValue.

Expando values can be added to liferay objects like: Users, Documents and Media, Web Contents, Bookmarks, etc. through control panel and programmatically to the custom portlets as well.

For Custom-Portlets:

Create custom-fields for custom-table:
Suppose we want to create the custom fields into custom-table. e.g Let us create a custom field named
PrimaryTag and associate it with a custom portlet named VideoManagement.
Example code:
ExpandoBridge expandoBridge = vManag.getExpandoBridge(); //vManag is obj of class
if (!expandoBridge.hasAttribute("PrimaryTag")) {
expandoBridge.addAttribute("PrimaryTag");
}
view raw Expando1.java hosted with ❤ by GitHub

It will add an entry in all 4 expando-tables mapped with VideoManagement classname.


Display custom-fields into your webpage(for custom portlet)
Liferay provides below code to display custom fields on page for all the OOTB portlets.
Same code can be used in custom-portlet to display them as text fields.

<liferay-ui:custom-attributes-available className="<%= VideoManagement.class.getName() %>">
<aui:fieldset>
<liferay-ui:custom-attribute-list
className="<%= VideoManagement.class.getName() %>"
classPK="<%= (vManag != null) ? vManag.getPrimaryKey() : 0 %>"
editable="<%= true %>"
label="<%= false %>"
/>
</aui:fieldset>
</liferay-ui:custom-attributes-available>
view raw Expandos1.jsp hosted with ❤ by GitHub

In LocalServiceImpl add following code for storing expandovalue:


The way of setting value:

private void addCustomAttribute(long _companyId, String _className, String name, long _classPK, String value){
try {
ExpandoValueLocalServiceUtil.addValue( _companyId, _className, ExpandoTableConstants.DEFAULT_TABLE_NAME, name, _classPK,value);
}
catch (Exception e) {
// e.printStackTrace();
}
}

e.g addCustomAttribute(_companyId, _className, PrimaryTag", _classPK, “primaryTagValue”);
i.e. ExpandoValueLocalServiceUtil.addValue(classNameId, tableId, columnId, classPK, data);


The way of getting value:


Expando Table:
long videoClassNameId= ClassNameLocalServiceUtil.getClassNameId(VideoManagement.class.getName());
ExpandoTable table = ExpandoTableLocalServiceUtil.getDefaultTable(companyId, videoClassNameId );

Expando Column:
ExpandoColumn column = ExpandoColumnLocalServiceUtil.getColumn(tableId, name);


Expando Value:
ExpandoValueLocalServiceUtil.getValue(tableId, columnId, classPK);

where tableId is the id of default table, columnId the id of retrieved column and classPK the primary key of entity.

For OOTB portlets e.g journalArticle, we have to first create custom attributes from control panel.

The following code sets attributes for particular article using service context.

//Fetch Article
ExpandoBridge expandoBridge = article.getExpandoBridge();
expandoBridge.setAttributes(serviceContext);
com.liferay.portal.service.ServiceContext serviceContext = null;
try {
serviceContext = ServiceContextFactory.getInstance(JournalArticle.class.getName(), actionRequest);
} catch (PortalException e1) {
e1.printStackTrace();
} catch (SystemException e1) {
e1.printStackTrace();
}

Thanks for reading! Happy Learning!

1 comment: