Liferay is a Gartner Magic Quadrant Leader for the Sixth Year! Find out why
« Back to Installation

Data Migration Using Liferay API

Introduction #

This article describes the steps to migrate data using Liferay API. I would use the "Add Page" functionality as an example. The migration code has been tested in Liferay 5.1.2 with tomcat 6.0.

I will insert my data migration code in a Liferay class, compile the code and replace the original Liferay class with the class modified by me. When I click on the "Add Page" button on the "Manage Page" page, both the original Liferay code and my added code will run. This accomplishes the data migration. After the data migration is done, restore the original Liferay class.

I used this method because it works and it is easy to implement. It may not be an elegant way to use the Liferay API.

Locating the API #

Go to the "Manage Page" page. Find the url that is activated by clicking on the "Add Page" button. This url is "http://projectname.cignex.com/web/guest/home?p_p_id=88&p_p_lifecycle=1&p_p_state=maximized&p_p_mode=view&_88_struts_action=%2Flayout_management%2Fedit_pages."

Following this url I find that the class for this "Add Page" feature is "com.liferay.portlet.communities.action.EditPagesAction." Because the value of "cmd" parameter is "add," the section of code that concerns is the following method:

updateLayout(actionRequest, actionResponse);

If my data migration code is a "migrateProductPages(...)" method, I can insert it after the following line:

if (cmd.equals(Constants.ADD)) {

The "migrateProductPages(...)" includes all the code that is necessary when the "Add Page" button has been clicked on. So I gathered all the related code and put it in this "migrateProductPages(...)" method.

Preparation of Custom Data for Adding Pages #

I use some parameter values that I can get from UI and get my custom values for other parameters from a database table. The database table is as follows:

CUSTOM_PAGES table

ID PLID PRODUCT_ID PAGE_NAME FRIENDLY_URL COPY_PAGE

-- ----- ----------- ---------- ------------- -----------

1 0 17368 video-game /video/game general-page

2 0 16389 landing-page /landing/page general-page

The PAGE_NAME is my custom values for the "name_en_US" parameter. I read its values from the above database table and generate all the pages in a "while(rs.next())" loop. After the application generates a page (layout), it also gives that page a PLID. I use this PLID value to update the "PLID" field in the above talbe.

Why do I need this "PLID" field in the above table? Please note that I actually could not do much on the "Add Page" page -- I could simply add some empty pages. I need to do more when updating these pages. This is why I need the PLID, FRIENDLY_URL and COPY_PAGE fields.

I also add a "updateProductPages(...)" method in the "updateLayout(actionRequest, actionResponse)" method at an appropriate location. After I have generated the migrated pages, I click on the name of one dummy page at the same level as that of the migrated pages. I click on the "Page" tab. Then I input some parameter values. I click on the "Save" button. My data migration code will run and update the friendly urls of my product pages. At the same time it copies the content from a sampel copy page to my product pages. When this process is done, I simply need to go to a migrated page and add the custom content. In this way I minimize my work with UI.

How to Modify the Liferay API #

After I modify the code, I compile it. I find that the compiled "com.liferay.portlet.communities.action.EditPagesAction.class" goes to tomcat-6.0-5.1.2/webapps/ROOT/WEB-INF/lib/portal-impl.jar. So I replace the original "EditPagesAction.class" with my version of "EditPagesAction.class."

Data Migration #

Start liferay-tomcat. First add pages by clicking on the "Add Page" button. Then update the pages by clicking on the name of a dummy page, entering some values and clicking on the "Save" button.

Next steps #

After the data migration is done, shut down liferay-tomcat, restore the "portal-impl.jar" file and re-start liferay-tomcat.

Troubleshooting #

During updating pages, Liferay may complain about some special characters in the friendly urls. Delete those pages and update again.

References #

Liferay 5.1.2 source code

0 Attachments
22917 Views
Average (0 Votes)
The average rating is 0.0 stars out of 5.
Comments
Threaded Replies Author Date
This article has nothing to do with it's parent... petar banicevic May 24, 2009 4:57 PM
Hi, Petar, Thank you for your comments! "This... Xinsheng Chen May 27, 2009 2:07 PM

This article has nothing to do with it's parent article. Consider making it stand alone.

I don't understand what's wrong with classic approach of making a portlet for data migration or adding a startup class in liferay for performing the same.

Can you need to elaborate problem and steps better.

BTW:

- overwriting a class in portal-impl.jar is not needed. You may put your class in a jar and place it in lib/ext. This will cause class overwriting by class loader and as the result tomcat won't load original liferay class, but yours. Deleting added jars cancels the behaviour.
Posted on 5/24/09 4:57 PM.
Hi, Petar,

Thank you for your comments!

"This article has nothing to do with it's parent article. Consider making it stand alone."

My answer: I will try to make it stand-alone.

"I don't understand what's wrong with classic approach of making a portlet for data migration or adding a startup class in liferay for performing the same. "

My answer: When I migrate pages from an original Web site, I need to define a parent page for this pages. If I insert my migration code into the existing "EditPagesAction.java," I can take advantage of the existing parameters on the "Manage Page" page. This also guarantees the integrity of promary keys generation. However, I am also interested in the approach of making a portlet for data migration. Could you tell me a link to that Web page?

"overwriting a class in portal-impl.jar is not needed. You may put your class in a jar and place it in lib/ext. This will cause class overwriting by class loader and as the result tomcat won't load original liferay class, but yours. Deleting added jars cancels the behaviour. "

Thank you for this trick. It will save me quite same time.

Xinsheng (Robert) Chen
Posted on 5/27/09 2:07 PM in reply to petar banicevic.