EXTENDING THE LIFERAY FUNCTIONALITY - A CLASSICAL APPROACH
Problem statement:
Add two new fields - "Education" and "Occupation" in the user registration form.
Corresponding changes to be done in the "My Account" page.
This is the best example to know how to do customization of the existing liferay functionality and "extend" it according to our requirement.
---------------------------------------------------------------------------------------
Step 1: Create the underlying tables.
---------------------------------------------------------------------------------------
Run the script from the "mysql" prompt to create the table in the database.
create table UserExtra (
userId BIGINT(20) not null primary key
,education VARCHAR(75) null
,occupation VARCHAR(75) null
);
---------------------------------------------------------------------------------------
Step 2: Create the service layer for the new table
---------------------------------------------------------------------------------------
(create service.xml file under "ext/ext-impl/src/com/liferay/portlet/login")
UserExtra
Generate the service layer by running the command from "ext-impl"
ant build-service -Dservice.file=src/com/liferay/portlet/login/service.xml
Just verify everything is intact by running "ant compile"
Notes:
1. The new table will have reference to the User entity
2. we have made remote-service to false (default)
---------------------------------------------------------------------------------------
Step 3: Modifying the UI
---------------------------------------------------------------------------------------
Edit the jsp page responsible for displaying the registartion page.
It is create_account.jsp under "ext-web/tmp/html/portlet/login"
Copy this file into your "ext" envt "ext-web/docroot/html/portlet/login"
Add the new fields inside the form
Add these rows after "email address" field
Do "ant deploy-fast" to move this file to the server.
Refresh the registration page and confirm that your changes are there.
---------------------------------------------------------------------------------------
Step 4: Changing the action class in struts-config file
---------------------------------------------------------------------------------------
Now, let us find out the action class that is called when the "save" button clicked.
By looking into the HTML form element, we find that it is invoking a struts action "/login/create_account"
Now open the original "struts-config.xml" file to find which is the action class that is being called.
You'll find the original "struts-config.xml" file under "ext-web/tmp/WEB-INF"
Make a copy of this entry in "struts-config.xml" under "ext-web/docroot/WEB-INF"
Modify the type to the new action class.
Note: you have to modify the type attribute, giving the correct path of the new action class.
After the last two steps, please make sure to run "ant deploy" from "ext-web" to move all our changes to server.
---------------------------------------------------------------------------------------
Step 5: Modifying the action class
---------------------------------------------------------------------------------------
Copy the original CreateAccountAction.java from "portal-src" and paste it into "ext-impl/src/com/liferay/portlet/login/action"
Change the file name and class name to CreateAccountAction2.java, making it to extend CreateAccountAction.java
Retaining the "addUser" method remove all other methods and declarations.
Go to "ext/ext-impl" and give "ant compile" and confirm the build successful.
// Additional logic to store user extra info
String education = ParamUtil.getString(actionRequest, "education");
String occupation = ParamUtil.getString(actionRequest, "occupation");
UserExtra userExtra = new UserExtraImpl();
userExtra.setEducation(education);
userExtra.setOccupation(occupation);
Dont forget to put the following import statement
import com.liferay.portlet.login.model.UserExtra;
import com.liferay.portlet.login.model.impl.UserExtraImpl;
Remove unwanted imports (by pressing CTRL+SHIFT+o in eclipse). Do the same thing for other classes we'll modify later.
Do "ant compile" and confirm the build is successful.
If you really want to test if the new action class is working fine, put some SOP statements and check.
---------------------------------------------------------------------------------------
Step 6: Further changes to the action class
---------------------------------------------------------------------------------------
comment out the statement
/*
User user = UserServiceUtil.addUser(
company.getCompanyId(), autoPassword, password1, password2,
autoScreenName, screenName, emailAddress, openId,
themeDisplay.getLocale(), firstName, middleName, lastName, prefixId,
suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
groupIds, organizationIds, roleIds, userGroupIds, sendEmail,
serviceContext);
*/
and replace it with,
User user = UserExtraLocalServiceUtil.addUser(
company.getCompanyId(), autoPassword, password1, password2,
autoScreenName, screenName, emailAddress, openId,
themeDisplay.getLocale(), firstName, middleName, lastName, prefixId,
suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
groupIds, organizationIds, roleIds, userGroupIds, sendEmail,
serviceContext, userExtra);
---------------------------------------------------------------------------------------
Step 7: Implementing the actual logic to UserExtraLocalServiceImpl.java
---------------------------------------------------------------------------------------
You'll find this file under src/com/liferay/portlet/login/service/impl after the service layer is generated.
add a new method,
public User addUser(
long companyId, boolean autoPassword, String password1,
String password2, boolean autoScreenName, String screenName,
String emailAddress, String openId, Locale locale, String firstName,
String middleName, String lastName, int prefixId, int suffixId,
boolean male, int birthdayMonth, int birthdayDay, int birthdayYear,
String jobTitle, long[] groupIds, long[] organizationIds, long[] roleIds, long[] userGroupIds,
boolean sendEmail, ServiceContext serviceContext, UserExtra userExtra)
throws PortalException, SystemException, RemoteException {
User user = UserServiceUtil.addUser(
companyId, autoPassword, password1, password2,
autoScreenName, screenName, emailAddress, openId,
locale, firstName, middleName, lastName, prefixId,
suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
groupIds, organizationIds, roleIds, userGroupIds, sendEmail,
serviceContext);
// create the user extra object
userExtra.setUserId(user.getUserId());
userExtraLocalService.addUserExtra(userExtra);
return user;
}
make the necessary imports
import java.rmi.RemoteException;
import java.util.Locale;
import com.liferay.portlet.login.model.UserExtra;
import com.liferay.portal.PortalException;
import com.liferay.portal.SystemException;
import com.liferay.portal.model.User;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.UserServiceUtil;
---------------------------------------------------------------------------------------
Step 9: Deploy and verify
---------------------------------------------------------------------------------------
Once a new method is added to this class, run "ant build-service" in order to update the corresponding interfaces
Do "ant compile" from ext-impl to compile all the files we have changed so far.
Once the compilation is successful
Do "ant deploy" from ext-impl to move all our changes to the server.
Restart the server and check the changes are taken effect in the user registration page.
---------------------------------------------------------------------------------------
Step 10: Using Liferay taglib for the input 2 new input fields
---------------------------------------------------------------------------------------
replace,
with,
Do similar change for "occupation".
Define a new variable,
UserExtra userExtra = null;
Make the necessary import at the beginning of the create_account.jsp
<%@ page import="com.liferay.portlet.login.model.UserExtra" %>
Do "ant deploy-fast" from ext-web to move the changes to the server.
---------------------------------------------------------------------------------------
Step 11: Modifiying the "My Account" - displaying the additional values
---------------------------------------------------------------------------------------
DO IT YOURSELF.
---------------------------------------------------------------------------------------
Step 12: Write the update functionality
---------------------------------------------------------------------------------------
DO IT YOURSELF.