Friday, November 28, 2014

[Liferay] [Maven] [Service Builder] Building Liferay Service using Maven - "The Correct Way"

I found many posts which explain and show the way to build services using maven by modifying the pom.xml and ask you to include plugins with groupId : org.codehaus.mojo and artifactId : build-helper-maven-plugin.
Even though that works just fine, I could not take it as a right way to do. So what maven and liferay actually offer in this case?

We know that our portlets just use the service jar file they need. That makes service an independent project. That's exactly what maven makes it..

Try this with me.. lets build a brand new portlet. Portlet name is : mvn-service-builder-portlet [You could just name as mvn-service-builder. By mistake, I added portlet to artifactId and now portlet comes two times in the project name.]

1. mvn archetype:generate

2. You will now see a folder named mvn-service-builder-portlet but inside there would be two more folders

3. Now you can load these two projects in eclipse and see there is a service.xml file in mvn-service-builder-portlet-portlet > src > main > webapp > WEB-INF. In short where it should be :) 

4. Add your services.

5. Now navigate to the folder inside mvn-service-builder-portlet using command prompt. You can now run the commands to build service
mvn clean liferay:build-service install

6. liferay:build-service is going to build service and install is going to install the jar file to m2 repository in your local machine. 

7. If you see below error in eclipse or any other IDE where you imported the projects-
The hierarchy of the type 'Class name' is inconsistent

Then make sure the version of your service in pom of service is same as in portlet pom. If they are different, change the version to be the same. Usually it is ${pom.version}which you can replace to e.g. 1.0.0.0 or a version of your choice.

Now also choose a proper namespace for your service builder.

After this, re-run the service builder and install again. Refresh the project in IDE and you're done.

You don't need to add plugins manually to your pom.xml, this I feel the clear and correct way to develop services in portlets :)

Hope this helps someone.

Get the source here Maven Service Builder Sample Portlet Project

Until next time.. keep building services and enjoy :)

Tuesday, November 25, 2014

[Liferay] Data Migration Tab Loading Issue Fix


If you have many files in document library of Liferay portal, this problem will occur. Data migration tab will not get loaded. Each uploaded/updated file creates entries to DLFileEntry and DLFileVersion tables in database.

When we load data migration tab in Control Panel > Server Administration > Data Migration, Liferay tries to get all the ConvertProcess types which can be used to migrate data. One of which is com.liferay.portal.convert.ConvertDocumentLibraryExtraSettings for which the query below runs. This query takes a lot of time to complete and depends on number of rows in both tables. This query may take almost 10 minutes for just 10000+ documents in your document library.

Usually the result is zero and the method call convertProcess.isEnabled() returns false. And if it is false, ConvertDocumentLibraryExtraSettings is not going to appear in the dropdown which lists all available conversion process. In this case, it should be safe to use this hook.

We do not have opportunity to always change the db and optimize indexes and queries. This query is in Liferay core code and we can not modify this. The good thing is that we do not always load ConvertDocumentLibraryExtraSettings to migrate data and if you can skip that, you are free to use this hook.


Once the hook is deployed, it will always load properly the Data Migration tab.

Hope this helps someone in need :)

[Liferay] Ajax with JQuery, AUI with Action URL / Resource URL

Though its pretty simple and has been there for a long time now, I am writing this post to help new users.

Check the code below with three links. We will use these links to call our data using ajax calls.

To make an ajax call using JQuery with actionURL

To make an ajax call using JQuery with resourceURL

To make an ajax call using AlloyUI and resourceURL

Hope this helps someone in need.

You can get the source of the portlet here on github
Ajax JQuery Portlet
This portlet is for 6.1.20 version of Liferay and for previous versions you can check here.
Ajax JQuery Portlet

Until next time. Keep Ajax -ing :-)

Wednesday, November 19, 2014

[Liferay] [Hook] Fix for Special Characters in Friendly URL of Web Content

Friends,

In the last post [Liferay] Show Web Content in a different page using Asset Publisher we learned how to use asset publisher to display web content in specific portlet and make it a fit for most of the requirements.

That time, I also mentioned that a problem exists if we use special characters in title of web content, it creates some problem. Problem is that, Liferay replaces most of the special characters while generating unique url for the web content but fails to recognize all. That leads to existence of special characters in title and Liferay fails to show that web content in specific portlet.

Solution was simple, replace all special characters which Liferay code missed. Liferay takes care of these characters in FriendlyURLNormalizerImpl.
Changing FriendlyURLNormalizerImpl was not easier as per our project requirements so I fixed JournalContentLocalServiceImpl instead. I created a service wrapper hook and changed the code for addArticle(...)
This code replaces the special characters from existing title and updates article. This hook must be present before we create a web content.

That's it for now.

Until next time. :)

Tuesday, November 18, 2014

[Liferay] Show Web Content in a different page using Asset Publisher

Guys,

Sometimes, we get requirements to list all news on one page and then if clicked on title, show their details on another page. We can surely do this using Asset publisher. Simple steps below.

  1. Create a Page for listing - Suppose that page is News.
  2. Add Asset Publisher to the page. In the configuration of Asset publisher - Set "Asset Link Behavior"  to  "View in a Specific Portlet

  3. Now create a new page for details - Suppose that page is News Details
  4. Add Asset publisher to the page. In the configuration of Asset Publisher - Check "Set as the Default Asset Publisher for This Page".

  5. Make sure you select Display style as Full content.
  6. Now create a web content. 
  7. In the create screen, add a display page. In the list of display pages, you should be able to see News Details page enabled.
  8. News Details page appears because we followed step 4.

And that should be it. Now go to your news page and click on title of newly created web content. It should display the web content in news details page. You should be able to see the friendly url in browser navigation bar.

That's it for now. Next post will cover a problem with the above. We could not use special characters in title of web content because then friendly url will contain that special character and browser won't understand where to redirect, so it would either go to home page or stay on the same page.

Until next time. :)