Wednesday, May 11, 2011

Adding language selection portlet to the Dockbar using custom jsp hook plugin

The Liferay Language portlet provide an option to select from different localizations. It’s a common requirement in web application to display language selection option as flags in the header area.

In Liferay we can accomplish this through hook plugin as following:

1. Create a Liferay hook plugin project create sample-hook "Sample Dockbar Hook"

2. In the /docroot/WEB-INF/ folder open liferay-hook.xml file and add the following entry

<hook>

<custom-jsp-dir>/WEB-INF/jsps</custom-jsp-dir>

</hook>

3. Copy ${PORTAL_ROOT_HOME}htmlportletdockbarview.jsp to ${liferay.plugins.sdk}sample-hookdocrootWEB-INFjspshtmlportletdockbar folder.

4. Add the following code after Toggle Edit Control

<liferay-ui:language displayStyle="0" languageIds='<%= new string[] {"en_US","nl_NL"} %>' />

This will add English and Dutch Language to the dockbar.

5. Build and package the hook WAR file and deploy it on your Liferay server.

Friday, May 6, 2011

Adding Portlet Preferences to Custom Portlet

The portlet preferences are intended to store basic configuration data for portlets. The PortletPreferences interface allows the portlet to store configuration data, not to replace the general-purpose databases.
Normally, there are two different types of preferences: Modifiable and Read Only.
Modifiable preferences can be changed by the portlet in any standard portlet mode (for example, EDIT, HELP, and VIEW). While Read-Only preferences cannot be changed by the portlet in any standard portlet mode, they may be changed by administrative modes.

By default, preference is modifiable.

Following are the steps to add portlet preferences (configurations) to custom portlet:

1. Add a configuration.jsp file:


2. Create an ConfigurationActionImpl class :


3. Made an entry in liferay-portlet.xml file:

<configuration-action-class>ConfigurationActionImpl</configuration-action-class>

4. Just Fetch and use the preferences:


5.Adding Preferences Default Value  to Portlet.xml:



In this way you can add any configuration options to the portlet and persist them portlet preferences.

If you have any questions or query, Please comment.....

Friday, April 29, 2011

Suppressing Default Success Message

Each time user request is processed successfully Liferay Portal display the following message-

“Your request has been processed successfully”

To remove this message Add an entry to portlet.xml file:
<add-process-action-success-action >
false
</add-process-action-success-action >

Wednesday, April 27, 2011

Removing Portlet Permission Error Message

If any user don’t have permission to access a portlet, Liferay Portal display the following permission error message

"you don't have required permission to access this portlet"

Instead of displaying the error message we can make that portlet invisible for that particular user by setting the following property in portal-ext.properties file –

layout.show.portlet.access.denied=false

Saturday, April 9, 2011

Display Journal Articles based on Tags

1. Have a long[] array containing Tag Ids for which you want to serach Journal Articles

long[] tagIds = {tagId1,tagId2,…..};

2. Create an AssetEntryQuery and set the tagIds as the criteria

AssetEntryQuery assetEntryQuery = new AssetEntryQuery();
assetEntryQuery.setAllTagIds(tagIds);

3. Call the getEntries(AssetEntryQuery ob) method on AssetEntryLocalServiceUtil class to get the list of assetEntries

ListentryList = new ArrayList();
try {
entryList = AssetEntryLocalServiceUtil.getEntries(assetEntryQuery);
} catch (SystemException e) {
e.printStackTrace();
}

4. Now iterate the list and pick classPK attribute for each entry and from that query JournalArticle Table for corresponding articleId.

Wednesday, February 23, 2011

Creating and Applying Different Liferay themes to Pages

Here is an example of how to apply different themes on pages in Liferay. Here we have created two themes-

1. First of all in the liferay-look-and-feel.xml file, create two different theme entries that refer to the same theme but have a different value for the header-type setting:


Both the theme entries refer to same theme but they different value for their header-type setting. We can access the settings programmatically in our theme template and then take action based on the settings we have defined.

When this theme is deployed to Liferay, it will display to the user as two different themes.

2. Accessing the settings in portal_normal.vm template file




In the above code we accessed the settings defined in the liferay-look-and-feel.xml file and based on the value of the setting we are parsing different template files.

Custom Sql in Liferay

1. Create a folder named custom-sql under the src folder.

2. Create a default.xml file in the custom-sql folder.

3. Create another xml file in the custom-sql folder, this file contains the custom sql query.

4. Create a class say SampleFinderImpl in the persistent package. This class will extends the BasePersistenceImpl<Sample> and implements SampleFinder.

5. Now build the service, it will generate the SampleFinder Interface.

6. Now define methods to execute custom sql in the SampleFinderImpl class.

7. Build the service; it will generate the SampleFinderUtil class with the method defined in the SampleFinderImpl class.

8. Copy the generated method definition from SampleFinderUtil and place it in the SampleLocalServiceImpl.

9. Build the service; it will place the methods definition in the SampleLocalServiceUtil from EntryLocalServiceImpl.



1. Create a folder named custom-sql under the src folder.


2. Create a default.xml file in the custom-sql folder.


3. Create another xml file in the custom-sql folder, this file contains the custom sql query.


4. Create a class named (EntryFinderImpl) in the persistent package. This class will extends the BasePersistenceImpl<Entry> and implements EntryFinder.


5. Now build the service, it will generate the EntryFinder Interface.


6. Now define methods to execute custom sql in the EntryFinderImpl class.


7. Build the service; it will generate the EntryFinderUtil class with the method defined in the EntryFinderImpl class.


8. Copy the generated method definition from EntryFinderUtil and place it in the EntryLocalServiceImpl.


9. Build the service; it will place the methods definition in the EntryLocalServiceUtil from EntryLocalServiceImpl.


10. Now the method is available to call from any portlet class.