Thursday, July 21, 2011

Localization Specific Logo Image : Liferay Portal

Often there are requirements to show customized logo image according to portal's current locale. In Liferay Portal there is no way achieve this but locale specific logo can applied by using the theme.

In theme, portal_normal.vm contains the following statement to display logo -

<a href="$company_url" title="#language("go-to") $company_name">
<span>$company_name</span>
</a>

Here you can place check for the locale and accordingly change the logo image -

<a href="$company_url" title="#language("go-to") $company_name">
#if($locale == "en_US")
<img src="englishlogoimage" />
#else
<img src="spanishlogoimage" />
#end
</a>

but now you can't manage the logo from the community settings as image urls are hardcoded in the theme. To make it manageable you can use web content to render the logo image.

Any comments or suggestions will be appreciated ....

 

Saturday, July 9, 2011

Community Specific Locale Liferay Portal (Per Community BasisLocalization)

Liferay provides us with fine grained built-in localization features. In Liferay Portal one can apply localization on per user basis. But how to apply default locale to a Community, Liferay doesn’t provide any such functionality but it can be implemented using Portal Properties Hook.

So case is, portal instance have more than one Community and we need to apply a default language setting for one Community that is different from other Communities. To achieve this implement an Event Hook for servlet.service.events.pre.

Following is the step by step description to achieve this functionality-

1.  Create a portal properties hook and choose the servlet.service.events.pre Event

2.  Create a new class say SampleClass by extending the com.liferay.portal.kernel.events.Action


3.  Now en_US locale will be applied to LONG_GROUP_ID Community.

Any queries or comments.........

Sunday, June 26, 2011

Extending Web Form Portlet – Adding File Upload Control

In Liferay Portal, Web Form Portlet is used to create custom forms but the types of field that can be created are limited. One such missing field type is File Upload Control. It’s a common requirement to have file upload control in forms.


Following are the steps to add File Upload Control to Web Form Portlet -


1.  Add a new field type to edit_field.jsp

<aui:option selected='<%= fieldType.equals("file") %>' value="file"><liferay-ui:message key="file" /></aui:option>

2.  Accordingly update the view.jsp to render the your new field

<c:when test='<%= fieldType.equals("file") %>'>

<aui:input cssClass='<%= fieldOptional ? "optional" : StringPool.BLANK %>' label=" <%=HtmlUtil.escape(fieldLabelDisplay) %>" name="<%= fieldName %>" type="file" value="<%=HtmlUtil.escape(fieldValue) %>"/>

</c:when>

3.  Set the preferences for this field in ConfigurationActionImpl.java

boolean isFileUpload = false;

if ("file".equals(fieldType)) {

isFileUpload = true;

}

preferences.setValue("isFileUpload", String.valueOf(isFileUpload));

4.  Finally get the file in WebFormPortlet.java and proceed with your implementation…

File file = uploadRequest.getFile(paramName.toString());

Proceed we the implementation with file object for example save to document library, send in mail etc.

 

 

Thursday, June 23, 2011

liferay Ext Plugin

In Liferay Portal, when working with Ext Plugin the server need to be rebooted after deployment.

But sometimes even after rebooting the server changes are not reflected, because some  jar files and xml files related to ext plugin project don't get updated.

To make sure all the files get updated, stop the server and remove the old files listed below-

./webapps/project-name
./lib/ext/ext-project-name-service.jar
./webapps/ROOT/WEB-INF/lib/ext-project-name-util-bridges.jar
./webapps/ROOT/WEB-INF/lib/ext-project-name-util-taglib.jar
./webapps/ROOT/WEB-INF/lib/ext-project-name-util-java.jar
./webapps/ROOT/WEB-INF/lib/ext-project-name-impl.jar
./webapps/ROOT/WEB-INF/ext-project-name.xml
./temp/liferay/com/liferay/portal/deploy/dependencies/ext-project-name-util-bridges.jar
./temp/liferay/com/liferay/portal/deploy/dependencies/ext-project-name-util-taglib.jar
./temp/liferay/com/liferay/portal/deploy/dependencies/ext-project-name-util-java.jar

After this deploy the ext project and reboot the server.

Enjoy working with Liferay Portal Ext Plugin ...

Sunday, June 12, 2011

Communities in Liferay Portal

Community is the group of users who shares the same interests. Each community have its own private and public pages in liferay portal.

Types of Communities:

There are 3 types of communities Liferay Portal. These are as follows-

  1. Open: Open Community allows users to join and leave a Community whenever they want to.

  2. Restricted: In Restricted Community user first have to make request for the membership. It means Restricted Community requires administrator or owner approval to add users to the community or to remove users from the community but users have the ability to leave the community.

  3. Private: In case of Private Community there is no way to join or make request for membership.Both  Community Owner and Community administrator can assign a user to the Community.


So basic difference between Restricted and Private Community is that, in case of Restricted Community membership is subject to administrator approval but in case of Private Community there is no way of joining the Community.

Thursday, June 9, 2011

Liferay Service Builder : Part 1 (Finder methods in liferay portal)

In liferay portal if we want to find records from table then we need finder methods. To create finder method we need to specify the finder tag in the service.xml file.

Assume that we have a database table named customer having fields like name, city, age etc. We want to find all the customers belonging to a particular city.

Following are the steps to create finder method for the above requirement-

1.  In the service.xml file add the following line after database columns declarations,

<finder name="City" return-type="Collection">
<finder-column name="city"></finder-column>
</finder>


Name – method name , a method named findByCity will be generated

Collection – return type

Finder column name – name of the corresponding column in the customer table

2.  Run the ant target "ant build-service". In the SampleUtil.java you will find the following method –

public static java.util.List<com.sample.model.Sample> findByCity(
java.lang.String city) throws com.liferay.portal.SystemException {
return getPersistence().findByCity(city);
}


3.  Copy the finder method from this class and paste it inside the SampleLocalServiceImpl class and modify the method definition as following

-       Remove the static modifier

-       Replace getPersistence() with SampleUtil.

4.  Run the target "ant build-service" again. After build finder will be available in your SampleLocalServiceUtil class.

List<Sample> customerList = SampleLocalServiceUtil.findByCity("cityname");

NOTE: Replace Sample with your EntityName.

Saturday, June 4, 2011

Adding a Plugin Portlet to Liferay Control Panel

Sometimes situations arises when we want to add our custom made plugin portlet to Liferay Control Panel.

To do so we need to made some entries in liferay-portlet.xml .  These are as follows-

1. control-panel-entry-category: It spefies the 'category' where your portlet will appear.

2. control-panel-entry-weight: weight determines the position among the other portlet listed in that category.

3. control-panel-entry-class: The name of a class that implements the ControlPanelEntry interface.

It’s not necessary to specify the class but depends on our requirement.

Here is an example-

<control-panel-entry-category>content</control-panel-entry-category>

<control-panel-entry-weight>100</control-panel-entry-weight>

NOTE- Make sure you provide a unique weight for your plugin portlet otherwise any liferay out of the box portlet with the same weight will override your portlet.

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.



Job Scheduling in Liferay 6

Following are the steps to schedule the job in Liferay 6.

1. Implement a class that will implement MessageListener Interface.



2. Then we need to tell liferay to run this job. So you add the following to your liferay-portlet.xml:


3. We need an actual portlet to attach this "scheduler-entry" to. So we also need the portlet.xml as usual.

Its Done!!!