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:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ExpandoBridge expandoBridge = vManag.getExpandoBridge(); //vManag is obj of class | |
if (!expandoBridge.hasAttribute("PrimaryTag")) { | |
expandoBridge.addAttribute("PrimaryTag"); | |
} |
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
In LocalServiceImpl add following code for storing expandovalue:
The way of setting value:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
long videoClassNameId= ClassNameLocalServiceUtil.getClassNameId(VideoManagement.class.getName()); | |
ExpandoTable table = ExpandoTableLocalServiceUtil.getDefaultTable(companyId, videoClassNameId ); |
Expando Column:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ExpandoColumn column = ExpandoColumnLocalServiceUtil.getColumn(tableId, name); |
Expando Value:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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!
A huge explanation about the attributes and custom fields. Thanks for the sharing.
ReplyDeletePortal solutions in coimbatore | Best e-commerce service provider