[Liferay]liferay hooks exercise startup


EXERCISE: CREATE A CUSTOM FIELD

Let’s create the user’s custom field first:

  • With the server up and running, log in as an administrator.
  • Go to Control Panel → Portal → Custom Fields.
  • Click on the User link at the Custom Fields page.
  • Click on the button Add Custom Field.
  • Enter Interested-in-becoming-an-astronaut in the Key box.
  • Choose Selection of Text Values in the Type.
  • Click Save.
  • You should see the newly created field listed under the User page.
  • Click on the Interested_in_becoming_an_astronaut link again to return to the Edit view.
  • Change the Default Value to:
    • Not interested at all
    • Somewhat interested
    • Interested
    • Very interested
  • Change the Display Type to Radio.
  • Click Save.
  • Click My Account → Custom Fields under Miscellaneous. 
  • You should now see an Interested in Becoming an Astronaut option.

UPDATING CUSTOM FIELDS PERMISSIONS

  • You have now created a Custom Field that will be persisted to the database. 
  • By default, the field won't be available for all users.
  • We'll have to modify the permissions.
    • Go back to Control Panel → Portal → Custom Fields → User.  
    • Click the Actions button that corresponds to the Interested-in-becoming-an-astronaut custom field and selectPermissions.
    • Add a check for Update and View on the Guest and User roles. 
    • Click Save.
    • Now that guests can view and update this field, let's add this field to the Create Account page.

ADDING CUSTOM FIELDS PROGRAMMATICALLY

  • As we've seen, Custom Fields can be added through the user interface which can be useful for initial testing and set up.
  • Custom Fields can also be added programmatically using the Expando API.
  • Note: Custom Fields are called Expandos in the back end API.
  • This is useful when migrating Custom Fields from one environment to another.
  • The easiest way to do this is through a custom StartupAction implemented with aHook Plugin.

GET/ADD DEFAULT EXPANDO TABLE

  • First, we get the default Expando table for the User class if it exists, and add the default Expando table if it does not exist.
  • Use getDefaultTable and addDefaultTable when working with objects that have Custom Fields in theUI, such as the User and Organization objects (use getTable and addTable for custom Expando tables).
  • Javadocs: 点击打开链接
ExpandoTable table = null;
try {
 	table = ExpandoTableLocalServiceUtil.getDefaultTable(
 		companyId, User.class.getName());
}
catch(NoSuchTableException nste) {
 	table = ExpandoTableLocalServiceUtil.addDefaultTable(
 		companyId, User.class.getName());
}

ADD EXPANDO COLUMN

  • Since we want to programmatically add Expando columns to our environment on startup, we'll add thecomments-astronauts column first.
  • If the column already exists, we'll get the existing column instead.
  • Bonus: add an employeeid to the custom field.
ExpandoColumn column = null;
long tableId = table.getTableId();
try {
	column = ExpandoColumnLocalServiceUtil.addColumn(
		tableId, "Comments-astronauts", ExpandoColumnConstants.STRING);
	...
}


catch(DuplicateColumnNameException dcne) {
	column = ExpandoColumnLocalServiceUtil.getColumn(
		tableId, "Comments-astronauts");
}

ADD EXPANDO COLUMN PROPERTIES

  • Here, we make our Comments-astronauts column searchable by defining its INDEX_TYPEas TRUE:
column = ExpandoColumnLocalServiceUtil.addColumn(
	tableId, "Comments-astronauts", ExpandoColumnConstants.STRING);


UnicodeProperties properties = new UnicodeProperties();


properties.setProperty(
	ExpandoColumnConstants.INDEX_TYPE, Boolean.TRUE.toString());


column.setTypeSettingsProperties(properties);


ExpandoColumnLocalServiceUtil.updateExpandoColumn(column);

ADD/UPDATE EXPANDO VALUES (I)

  • What if we wanted to programmatically update the data in the library-id column for a specific user?
  • Use ExpandoValueLocalServiceUtil.addValue to add or update the value of theComment-astronauts Expando Column for the specific user.
String emailAddress = "test@liferay.com";
User user = UserLocalServiceUtil.getUserByEmailAddress(companyId, emailAddress);
long classNameId = table.getClassNameId();
long columnId = column.getColumnId();
long classPK = user.getUserId();
String data = "Love them since I was a child!";
ExpandoValue value = ExpandoValueLocalServiceUtil.addValue(
	classNameId, tableId, columnId, classPK, data);
  • classNameId: maps the value of User.class.getName() to an ID (the class name is mapped to an ID for performance, query based on an ID vs. a String)
  • tableId: the tableId of the default Expando Table for the User class
  • columnId: the columnId of the "library-id" Expando Column
  • classPK: the primary key of the class we want to perform an update on (update the data for the usertest@liferay.com, classPK = user.getUserId())
  • data: data / value to store in the library-id column

EXERCISE: CREATE HOOK PROJECT (I)

  • Go to File → New → Liferay Hook.
  • Hook plugin project: training-hook
  • Select Hook type(s) to create: Portal properties
  • Click Next.
  • Click the Add button next to the Define actions to be executed on portal events text box.
  • Events: Click the Select button.
    • Select application.startup.events and click OK.
  • Class: Click the New button.
    • Classname: ExpandoStartupAction
    • Java package: com.liferay.training.startupaction
  • Click Create → OK → Finish.
  • Open the portal.properties file under docroot/WEB-INF/src. You should find that Liferay Developer Studio has added the following:
    • application.startup.events=com.liferay.training.startupaction.ExpandoStartupAction
  • Open ExpandoStartupAction.java under com.liferay.training.startupaction.
  • Delete the contents of the file.
  • Add the contents in ExpandoStartupAction.java.
package com.liferay.training.startupaction;


import com.liferay.portal.kernel.events.SimpleAction;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.UnicodeProperties;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portlet.expando.DuplicateColumnNameException;
import com.liferay.portlet.expando.NoSuchTableException;
import com.liferay.portlet.expando.model.ExpandoColumn;
import com.liferay.portlet.expando.model.ExpandoColumnConstants;
import com.liferay.portlet.expando.model.ExpandoTable;
import com.liferay.portlet.expando.model.ExpandoValue;
import com.liferay.portlet.expando.service.ExpandoColumnLocalServiceUtil;
import com.liferay.portlet.expando.service.ExpandoTableLocalServiceUtil;
import com.liferay.portlet.expando.service.ExpandoValueLocalServiceUtil;




public class ExpandoStartupAction extends SimpleAction {


	public void run(String[] ids) {
		try {


			// Get a reference to the ExpandoTable (User class)


			ExpandoTable table = null;


			long companyId = Long.parseLong(ids[0]);


			try {
			 	table = ExpandoTableLocalServiceUtil.getDefaultTable(
			 		companyId, User.class.getName());
			}
			catch(NoSuchTableException nste) {
			 	table = ExpandoTableLocalServiceUtil.addDefaultTable(
			 		companyId, User.class.getName());
			}


			// Add the ExpandoColumn ("Comments-astronauts")


			ExpandoColumn column = null;


			long tableId = table.getTableId();


			try {
				column = ExpandoColumnLocalServiceUtil.addColumn(
					tableId, "Comments-astronauts", ExpandoColumnConstants.STRING);


				// Add Unicode Properties


				UnicodeProperties properties = new UnicodeProperties();


				properties.setProperty(
						ExpandoColumnConstants.INDEX_TYPE, Boolean.TRUE.toString());


				column.setTypeSettingsProperties(properties);


				ExpandoColumnLocalServiceUtil.updateExpandoColumn(column);
			}
			catch(DuplicateColumnNameException dcne) {


				// Get the ExpandoColumn ("Comments-astronauts")


				column = ExpandoColumnLocalServiceUtil.getColumn(
						tableId, "Comments-astronauts");
			}


			// Get the User object for "test@liferay.com"


			String emailAddress = "test@liferay.com";


			User user = UserLocalServiceUtil.getUserByEmailAddress(
				companyId, emailAddress);


			// Add or Update Comments Astronauts to "Love them since I was a child!"


			long classNameId = table.getClassNameId();
			long columnId = column.getColumnId();
			long classPK = user.getUserId();
			String data = "Love them since I was a child!";


			ExpandoValue value = ExpandoValueLocalServiceUtil.addValue(
				classNameId, tableId, columnId, classPK, data);


			System.out.println("Comments: " + value.getData());
		}
		catch(Exception e) {
			_log.error(e);
		}
	}


	private static Log _log = LogFactoryUtil.getLog(
ExpandoStartupAction.class);
}

  • After the hook is deployed, you should see the following information in the Console window:

CHECKPOINT

  • Open MySQL command line client.
  • Input your password.
  • Type: use lportal; to choose the database.
  • Type: select name from ExpandoColumn; to show the custom fields we just created.
  • Type: select data_ from ExpandoValue; to see the custom field's value. 
  • Alternatively, you could use the MySQL API to check this.
 
  • Log in as test@liferay.com.
  • Go to My Account page in Control Panel.
  • Click on Custom Fields. The Comments Astronauts field now contains the value“Loved them since I was a child!”
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值