Liferayのカレンダーコンポーネントの使用


careendar Portletの例:Liferayが持っているポーターの中で、careendarはいい例です.
careendar Portletは7つのオプションを含みます.まとめて、日曜日、月、年、事項、輸出/輸入
デフォルトでは「まとめ」オプションのホームページが表示されます.ページの「追加事項」ボタンをクリックして、
edit_event.jspページで、ページの表示効果は以下の通りです.
[img]http://fantasyyong840205.iteye.com/upload/picture/pic/8110/8955b435-7b90-33ed-95ef-17d4d4e01439.jpg [img]
edit_event.jspページ:
<%
//        
String redirect = ParamUtil.getString(request, "redirect");

CalEvent event = (CalEvent)request.getAttribute(WebKeys.CALENDAR_EVENT);

long eventId = BeanParamUtil.getLong(event, request, "eventId");

Calendar startDate = CalendarUtil.roundByMinutes((Calendar)selCal.clone(), 15);

if (event != null) {
	if (!event.isTimeZoneSensitive()) {
		startDate = CalendarFactoryUtil.getCalendar();
	}

	startDate.setTime(event.getStartDate());
}

Calendar endDate = (Calendar)curCal.clone();

endDate.add(Calendar.YEAR, 1);

if (event != null) {
	if (!event.isTimeZoneSensitive()) {
		endDate = CalendarFactoryUtil.getCalendar();
	}

	if (event.getEndDate() != null) {
		endDate.setTime(event.getEndDate());
	}
}

%>

<!--        -->

<table class="liferay-table">
<tr>
	<td>
		<liferay-ui:message key="start-date" />
	</td>
	<td>
		<liferay-ui:input-field model="<%= CalEvent.class %>" bean="<%= event %>" field="startDate" defaultValue="<%= startDate %>" />
	</td>
</tr>
このように日付コントロールが表示され、上の図のようなスタイル効果が得られます.日付ごとに表示され、カレンダーボタンがあります.典型的なカレンダー画面が表示されます.
生成されたコードは主に以下の部分に分けられます.
1.該当するJavaScript文
このコードの一つは、Calendarオブジェクトを作成すると、カレンダーページがはじけます.
2.作成年月日のHTMLコード
年月日コードは、Liferayの中で、3つの単独のSelectボックスです.
3.カレンダーページをイジェクトした画像
画像を一つ使ってカレンダ.gifをクリックするとカレンダーページが表示されます.
4.生成時のHTMLコード
Liferayの中には、2つのSelectボックスがあり、それぞれ選択時と分、もう一つのAM、PMのSelectボックスがあります.
ここを見ると、ほとんどのinput入力ボックスは<liferay-ui:input-field>というTaglibを使って実現されています.また、JSPからは、このinput-fieldはどこで日付として表示されるべきかが分かりません.普通のinputウィンドウではなく、日付として表示されます.
これはソースコードを読むことによって説明できます.具体的なコードは:
potal\potal-web\docroot\html\taglib\ui\input_field\page.jsp


<%@ include file="/html/taglib/init.jsp" %>

<%@ page import="com.liferay.portal.model.impl.BaseModelImpl" %>
<%@ page import="com.liferay.util.XSSUtil" %>

<%
//   liferay-ui:input-field       model   
String model = (String)request.getAttribute("liferay-ui:input-field:model");
//   liferay-ui:input-field       bean   
Object bean = request.getAttribute("liferay-ui:input-field:bean");
//   liferay-ui:input-field       field   
String field = (String)request.getAttribute("liferay-ui:input-field:field");
//   liferay-ui:input-field       fieldParam   
String fieldParam = (String)request.getAttribute("liferay-ui:input-field:fieldParam");
//   liferay-ui:input-field       defaultValue   
Object defaultValue = request.getAttribute("liferay-ui:input-field:defaultValue");
//   liferay-ui:input-field       disabled   
boolean disabled = GetterUtil.getBoolean((String)request.getAttribute("liferay-ui:input-field:disabled"));

//           model  field   type  
String type = ModelHintsUtil.getType(model, field);
Map hints = ModelHintsUtil.getHints(model, field);
%>

<c:if test="<%= type != null %>">
	<c:choose>
		<c:when test='<%= type.equals("boolean") %>'>

			<%
			boolean defaultBoolean = GetterUtil.DEFAULT_BOOLEAN;

			if (defaultValue != null) {
				defaultBoolean = ((Boolean)defaultValue).booleanValue();
			}
			else {
				if (hints != null) {
					defaultBoolean = GetterUtil.getBoolean((String)hints.get("default-value"));
				}
			}

			boolean value = BeanParamUtil.getBoolean(bean, request, field, defaultBoolean);
			%>

			<liferay-ui:input-checkbox param="<%= field %>" defaultValue="<%= value %>" disabled="<%= disabled %>" />
		</c:when>
		
		<!--         model  field   type    “Date” -->
		<c:when test='<%= type.equals("Date") %>'>

			<%
			//   liferay-ui:input-field       fieldParam  ,fieldParam    field   
			if (fieldParam == null) {
				fieldParam = field;
			}
			// defaultValue                
			Calendar cal = (Calendar)defaultValue;

			int month = ParamUtil.getInteger(request, fieldParam + "Month", -1);

			if ((month == -1) && (cal != null)) {
				month = cal.get(Calendar.MONTH);
			}

			boolean monthNullable = false;

			if (hints != null) {
				monthNullable = GetterUtil.getBoolean((String)hints.get("month-nullable"), monthNullable);
			}

			int day = ParamUtil.getInteger(request, fieldParam + "Day", -1);

			if ((day == -1) && (cal != null)) {
				day = cal.get(Calendar.DATE);
			}

			boolean dayNullable = false;

			if (hints != null) {
				dayNullable = GetterUtil.getBoolean((String)hints.get("day-nullable"), dayNullable);
			}

			int year = ParamUtil.getInteger(request, fieldParam + "Year", -1);

			if ((year == -1) && (cal != null)) {
				year = cal.get(Calendar.YEAR);
			}

			boolean yearNullable = false;

			if (hints != null) {
				yearNullable = GetterUtil.getBoolean((String)hints.get("year-nullable"), yearNullable);
			}

			int yearRangeDelta = 5;

			if (hints != null) {
				yearRangeDelta = GetterUtil.getInteger((String)hints.get("year-range-delta"), yearRangeDelta);
			}

			int yearRangeStart = year - yearRangeDelta;
			int yearRangeEnd = year + yearRangeDelta;

			if (year == -1) {
				Calendar now = CalendarFactoryUtil.getCalendar(timeZone, locale);

				yearRangeStart = now.get(Calendar.YEAR) - yearRangeDelta;
				yearRangeEnd = now.get(Calendar.YEAR) + yearRangeDelta;
			}

			int firstDayOfWeek = Calendar.SUNDAY - 1;

			if (cal != null) {
				firstDayOfWeek = cal.getFirstDayOfWeek() - 1;
			}

			int hour = ParamUtil.getInteger(request, fieldParam + "Hour", -1);

			if ((hour == -1) && (cal != null)) {
				hour = cal.get(Calendar.HOUR);
			}

			int minute = ParamUtil.getInteger(request, fieldParam + "Minute", -1);

			if ((minute == -1) && (cal != null)) {
				minute = cal.get(Calendar.MINUTE);
			}

			int amPm = ParamUtil.getInteger(request, fieldParam + "AmPm", -1);

			if ((amPm == -1) && (cal != null)) {
				amPm = cal.get(Calendar.AM_PM);
			}

			boolean showTime = true;

			if (hints != null) {
				showTime = GetterUtil.getBoolean((String)hints.get("show-time"), showTime);
			}
			%>

			<liferay-ui:input-date
				monthParam='<%= fieldParam + "Month" %>'
				monthValue="<%= month %>"
				monthNullable="<%= monthNullable %>"
				dayParam='<%= fieldParam + "Day" %>'
				dayValue="<%= day %>"
				dayNullable="<%= dayNullable %>"
				yearParam='<%= fieldParam + "Year" %>'
				yearValue="<%= year %>"
				yearNullable="<%= yearNullable %>"
				yearRangeStart="<%= yearRangeStart %>"
				yearRangeEnd="<%= yearRangeEnd %>"
				firstDayOfWeek="<%= firstDayOfWeek %>"
				imageInputId='<%= fieldParam + "ImageInputId" %>'
				disabled="<%= disabled %>"
			/>

			<c:if test="<%= showTime %>">
				&nbsp;

				<liferay-ui:input-time
					hourParam='<%= fieldParam + "Hour" %>'
					hourValue="<%= hour %>"
					minuteParam='<%= fieldParam + "Minute" %>'
					minuteValue="<%= minute %>"
					minuteInterval="1"
					amPmParam='<%= fieldParam + "AmPm" %>'
					amPmValue="<%= amPm %>"
					disabled="<%= disabled %>"
				/>
			</c:if>
		</c:when>
		<c:when test='<%= type.equals("double") || type.equals("int") || type.equals("String") %>'>

			<%
			String defaultString = GetterUtil.DEFAULT_STRING;

			if (defaultValue != null) {
				defaultString = (String)defaultValue;
			}

			String value = null;

			if (fieldParam == null) {
				fieldParam = namespace + field;

				if (type.equals("double")) {
					value = String.valueOf(BeanParamUtil.getDouble(bean, request, field, GetterUtil.DEFAULT_DOUBLE));
				}
				else if (type.equals("int")) {
					value = String.valueOf(BeanParamUtil.getInteger(bean, request, field, GetterUtil.DEFAULT_INTEGER));
				}
				else {
					value = BeanParamUtil.getString(bean, request, field, defaultString);

					String httpValue = request.getParameter(field);

					if (httpValue != null) {
						boolean xssAllowByModel = GetterUtil.getBoolean(PropsUtil.get("xss.allow." + model), BaseModelImpl.XSS_ALLOW);
						boolean xssAllowByField = GetterUtil.getBoolean(PropsUtil.get("xss.allow." + model + "." + field), xssAllowByModel);

						value = XSSUtil.strip(httpValue);
					}
				}
			}
			else {
				fieldParam = namespace + fieldParam;
				value = defaultString;
			}

			String displayHeight = ModelHintsDefaults.TEXT_DISPLAY_HEIGHT;
			String displayWidth = ModelHintsDefaults.TEXT_DISPLAY_WIDTH;
			String maxLength = ModelHintsDefaults.TEXT_MAX_LENGTH;
			boolean upperCase = false;
			boolean checkTab = false;

			if (hints != null) {
				displayHeight = GetterUtil.getString((String)hints.get("display-height"), displayHeight);
				displayWidth = GetterUtil.getString((String)hints.get("display-width"), displayWidth);
				maxLength = GetterUtil.getString((String)hints.get("max-length"), maxLength);
				upperCase = GetterUtil.getBoolean((String)hints.get("upper-case"), upperCase);
				checkTab = GetterUtil.getBoolean((String)hints.get("check-tab"), checkTab);
			}
			%>

			<c:choose>
				<c:when test='<%= displayHeight.equals(ModelHintsDefaults.TEXT_DISPLAY_HEIGHT) %>'>

					<%
					if (Validator.isNotNull(value)) {
						int maxLengthInt = GetterUtil.getInteger(maxLength);

						if (value.length() > maxLengthInt) {
							value = value.substring(0, maxLengthInt);
						}
					}
					%>

					<input <%= disabled ? "disabled" : "" %> id="<%= fieldParam %>" name="<%= fieldParam %>" style="width: <%= displayWidth %>px; <%= upperCase ? "text-transform: uppercase;" : "" %>" type="text" value="<%= value %>" onKeyPress="Liferay.Util.checkMaxLength(this, <%= maxLength %>);">
				</c:when>
				<c:otherwise>
					<textarea <%= disabled ? "disabled" : "" %> id="<%= fieldParam %>" name="<%= fieldParam %>" style="height: <%= displayHeight %><%= Validator.isDigit(displayHeight) ? "px" : "" %>; width: <%= displayWidth %><%= Validator.isDigit(displayWidth) ? "px" : "" %>;" wrap="soft" onKeyDown="<%= checkTab ? "Liferay.Util.checkTab(this); " : "" %> Liferay.Util.disableEsc();" onKeyPress="Liferay.Util.checkMaxLength(this, <%= maxLength %>);"><%= value %></textarea>
				</c:otherwise>
			</c:choose>
		</c:when>
	</c:choose>
</c:if>
ModelHits Util.java:
package com.liferay.portal.model;

import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.util.InitUtil;
import com.liferay.portal.util.PropsUtil;
import com.liferay.util.CollectionFactory;
import com.liferay.util.ListUtil;

import java.io.StringReader;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * <a href="ModelHintsUtil.java.html"><b><i>View Source</i></b></a>
 * 
 * @author Brian Wing Shun Chan
 * 
 */
public class ModelHintsUtil {

	static {
		InitUtil.init();
	}

	public static Map getDefaultHints(String model) {
		return _instance._getDefaultHints(model);
	}

	public static Element getFieldsEl(String model, String field) {
		return _instance._getFieldsEl(model, field);
	}

	public static List getModels() {
		return _instance._getModels();
	}

	public static String getType(String model, String field) {
		return _instance._getType(model, field);
	}

	public static Map getHints(String model, String field) {
		return _instance._getHints(model, field);
	}

	public static String trimString(String model, String field, String value) {
		if (value == null) {
			return value;
		}

		Map hints = getHints(model, field);

		if (hints == null) {
			return value;
		}

		int maxLength = GetterUtil
				.getInteger(ModelHintsDefaults.TEXT_MAX_LENGTH);

		maxLength = GetterUtil.getInteger((String) hints.get("max-length"),
				maxLength);

		if (value.length() > maxLength) {
			return value.substring(0, maxLength);
		} else {
			return value;
		}
	}

	private ModelHintsUtil() {
		_hintCollections = CollectionFactory.getHashMap();
		_defaultHints = CollectionFactory.getHashMap();
		//  CollectionFactory     HashMap  
		_modelFields = CollectionFactory.getHashMap();
		_models = new TreeSet();

		try {
			ClassLoader classLoader = getClass().getClassLoader();

			String[] configs = StringUtil.split(PropsUtil
					.get(PropsUtil.MODEL_HINTS_CONFIGS));

			for (int i = 0; i < configs.length; i++) {
				_read(classLoader, configs[i]);
			}
		} catch (Exception e) {
			_log.error(e, e);
		}
	}

	private Map _getDefaultHints(String model) {
		return (Map) _defaultHints.get(model);
	}

	private Element _getFieldsEl(String model, String field) {
		Map fields = (Map) _modelFields.get(model);

		if (fields == null) {
			return null;
		} else {
			return (Element) fields.get(field + _ELEMENTS_SUFFIX);
		}
	}

	private List _getModels() {
		return ListUtil.fromCollection(_models);
	}

	/**
	 * Portal    portal-model-hints.xml    ,           。
	 * 
	 *       model  name     key      model    (Map  )    .
	 * 
	 *          field  name   + “_TYPE”       key      field     type   。
	 * 
	 */
	private String _getType(String model, String field) {
		Map fields = (Map) _modelFields.get(model);

		if (fields == null) {
			return null;
		} else {
			return (String) fields.get(field + _TYPE_SUFFIX);
		}
	}

	private Map _getHints(String model, String field) {
		Map fields = (Map) _modelFields.get(model);

		if (fields == null) {
			return null;
		} else {
			return (Map) fields.get(field + _HINTS_SUFFIX);
		}
	}

	private void _read(ClassLoader classLoader, String source) throws Exception {

		String xml = null;

		try {
			xml = StringUtil.read(classLoader, source);
		} catch (Exception e) {
			_log.warn("Cannot load " + source);
		}

		if (xml == null) {
			return;
		}

		if (_log.isDebugEnabled()) {
			_log.debug("Loading " + source);
		}

		SAXReader reader = new SAXReader();

		Document doc = reader.read(new StringReader(xml));

		Element root = doc.getRootElement();

		Iterator itr1 = root.elements("hint-collection").iterator();

		while (itr1.hasNext()) {
			Element hintCollection = (Element) itr1.next();

			String name = hintCollection.attributeValue("name");

			Map hints = (Map) _hintCollections.get(name);

			if (hints == null) {
				hints = CollectionFactory.getHashMap();

				_hintCollections.put(name, hints);
			}

			Iterator itr2 = hintCollection.elements("hint").iterator();

			while (itr2.hasNext()) {
				Element hint = (Element) itr2.next();

				String hintName = hint.attributeValue("name");
				String hintValue = hint.getText();

				hints.put(hintName, hintValue);
			}
		}

		itr1 = root.elements("model").iterator();

		while (itr1.hasNext()) {
			Element model = (Element) itr1.next();

			String name = model.attributeValue("name");

			Map defaultHints = CollectionFactory.getHashMap();

			_defaultHints.put(name, defaultHints);

			Element defaultHintsEl = model.element("default-hints");

			if (defaultHintsEl != null) {
				Iterator itr2 = defaultHintsEl.elements("hint").iterator();

				while (itr2.hasNext()) {
					Element hint = (Element) itr2.next();

					String hintName = hint.attributeValue("name");
					String hintValue = hint.getText();

					defaultHints.put(hintName, hintValue);
				}
			}

			Map fields = (Map) _modelFields.get(name);

			if (fields == null) {
				fields = CollectionFactory.getHashMap();

				_modelFields.put(name, fields);
			}

			_models.add(name);

			Iterator itr2 = model.elements("field").iterator();

			while (itr2.hasNext()) {
				Element field = (Element) itr2.next();

				String fieldName = field.attributeValue("name");
				String fieldType = field.attributeValue("type");

				Map fieldHints = CollectionFactory.getHashMap();

				fieldHints.putAll(defaultHints);

				Iterator itr3 = field.elements("hint-collection").iterator();

				while (itr3.hasNext()) {
					Element hintCollection = (Element) itr3.next();

					Map hints = (Map) _hintCollections.get(hintCollection
							.attributeValue("name"));

					fieldHints.putAll(hints);
				}

				itr3 = field.elements("hint").iterator();

				while (itr3.hasNext()) {
					Element hint = (Element) itr3.next();

					String hintName = hint.attributeValue("name");
					String hintValue = hint.getText();

					fieldHints.put(hintName, hintValue);
				}

				fields.put(fieldName + _ELEMENTS_SUFFIX, field);
				fields.put(fieldName + _TYPE_SUFFIX, fieldType);
				fields.put(fieldName + _HINTS_SUFFIX, fieldHints);
			}
		}
	}

	private static final String _ELEMENTS_SUFFIX = "_ELEMENTS";

	private static final String _TYPE_SUFFIX = "_TYPE";

	private static final String _HINTS_SUFFIX = "_HINTS";

	private static Log _log = LogFactory.getLog(ModelHintsUtil.class);

	private static ModelHintsUtil _instance = new ModelHintsUtil();

	private Map _hintCollections;
	private Map _defaultHints;
	private Map _modelFields;
	private Set _models;

}
このコードを読むことによって、異なるタイプのfieldは異なるHTMLコードを生成しますが、その根拠はプロファイルpotal-model-hints.xmlとext-model-hints.xmlです.この中にはFieldのタイプの定義があります.
例えば、potal\potal-inmpl\clases\META-INF\potal-model-hints.xmlの経路でpotal-model-hints.xmlファイルを見つけて開けます.
careendarの中のdisplayDateの定義は以下の通りです.
	<model name="com.liferay.portlet.calendar.model.CalEvent">
		<field name="eventId" type="long" />
		<field name="groupId" type="long" />
		<field name="companyId" type="long" />
		<field name="userId" type="long" />
		<field name="userName" type="String" />
		<field name="createDate" type="Date" />
		<field name="modifiedDate" type="Date" />
		<field name="title" type="String" />
		<field name="description" type="String">
			<hint-collection name="TEXTAREA" />
		</field>
		
		<!-- startDate field   -->
		<field name="startDate" type="Date" />
		
		<field name="endDate" type="Date" />
		<field name="durationHour" type="int" />
		<field name="durationMinute" type="int" />
		<field name="allDay" type="boolean" />
		<field name="timeZoneSensitive" type="boolean" />
		<field name="type" type="String" />
		<field name="repeating" type="boolean" />
		<field name="recurrence" type="String">
			<hint-collection name="CLOB" />
		</field>
		<field name="remindBy" type="String" />
		<field name="firstReminder" type="int" />
		<field name="secondReminder" type="int" />
	</model>
また、ソースコードでも見られますが、年月日だけを表示する場合は、表示されない場合は、
  <field name=" displayDate" type="Date">
   <hint name="show-time">false</hint>
  </field>