001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.kernel.dao;
022:
023: import com.liferay.portal.kernel.util.CalendarFactoryUtil;
024: import com.liferay.portal.kernel.util.DateUtil;
025: import com.liferay.portal.kernel.util.GetterUtil;
026: import com.liferay.portal.kernel.util.ParamUtil;
027: import com.liferay.portal.kernel.util.StringPool;
028: import com.liferay.portal.kernel.util.Validator;
029:
030: import java.text.DateFormat;
031:
032: import java.util.Calendar;
033:
034: import javax.portlet.PortletRequest;
035:
036: import javax.servlet.ServletRequest;
037:
038: /**
039: * <a href="DAOParamUtil.java.html"><b><i>View Source</i></b></a>
040: *
041: * @author Brian Wing Shun Chan
042: *
043: */
044: public class DAOParamUtil {
045:
046: // Servlet Request
047:
048: public static boolean getBoolean(ServletRequest req, String param) {
049: return GetterUtil.getBoolean(getString(req, param));
050: }
051:
052: public static int getInteger(ServletRequest req, String param) {
053: return GetterUtil.getInteger(getString(req, param));
054: }
055:
056: public static String getISODate(ServletRequest req, String param) {
057: int month = ParamUtil.getInteger(req, param + "Month");
058: int day = ParamUtil.getInteger(req, param + "Day");
059: int year = ParamUtil.getInteger(req, param + "Year");
060: int hour = ParamUtil.getInteger(req, param + "Hour", -1);
061: int minute = ParamUtil.getInteger(req, param + "Minute", -1);
062: int amPm = ParamUtil.getInteger(req, param + "AmPm");
063:
064: if ((month >= 0) && (day > 0) && (year > 0)) {
065: Calendar cal = CalendarFactoryUtil.getCalendar();
066:
067: if ((hour == -1) || (minute == -1)) {
068: cal.set(year, month, day);
069: } else {
070: if (amPm == Calendar.PM) {
071: hour += 12;
072: }
073:
074: cal.set(year, month, day, hour, minute, 0);
075: }
076:
077: DateFormat isoFormat = DateUtil.getISOFormat();
078:
079: return isoFormat.format(cal.getTime());
080: } else {
081: return null;
082: }
083: }
084:
085: public static String getLike(ServletRequest req, String param) {
086: return getLike(req, param, null, true);
087: }
088:
089: public static String getLike(ServletRequest req, String param,
090: String defaultValue) {
091:
092: return getLike(req, param, defaultValue, true);
093: }
094:
095: public static String getLike(ServletRequest req, String param,
096: boolean toLowerCase) {
097:
098: return getLike(req, param, null, toLowerCase);
099: }
100:
101: public static String getLike(ServletRequest req, String param,
102: String defaultValue, boolean toLowerCase) {
103:
104: String value = req.getParameter(param);
105:
106: if (value != null) {
107: value = value.trim();
108:
109: if (toLowerCase) {
110: value = value.toLowerCase();
111: }
112: }
113:
114: if (Validator.isNull(value)) {
115: value = defaultValue;
116: } else {
117: value = StringPool.PERCENT + value + StringPool.PERCENT;
118: }
119:
120: return value;
121: }
122:
123: public static long getLong(ServletRequest req, String param) {
124: return GetterUtil.getLong(getString(req, param));
125: }
126:
127: public static short getShort(ServletRequest req, String param) {
128: return GetterUtil.getShort(getString(req, param));
129: }
130:
131: public static String getString(ServletRequest req, String param) {
132: String value = ParamUtil.getString(req, param);
133:
134: if (Validator.isNull(value)) {
135: return null;
136: } else {
137: return value;
138: }
139: }
140:
141: // Portlet Request
142:
143: public static boolean getBoolean(PortletRequest req, String param) {
144: return GetterUtil.getBoolean(getString(req, param));
145: }
146:
147: public static int getInteger(PortletRequest req, String param) {
148: return GetterUtil.getInteger(getString(req, param));
149: }
150:
151: public static String getISODate(PortletRequest req, String param) {
152: int month = ParamUtil.getInteger(req, param + "Month");
153: int day = ParamUtil.getInteger(req, param + "Day");
154: int year = ParamUtil.getInteger(req, param + "Year");
155: int hour = ParamUtil.getInteger(req, param + "Hour", -1);
156: int minute = ParamUtil.getInteger(req, param + "Minute", -1);
157: int amPm = ParamUtil.getInteger(req, param + "AmPm");
158:
159: if ((month >= 0) && (day > 0) && (year > 0)) {
160: Calendar cal = CalendarFactoryUtil.getCalendar();
161:
162: if ((hour == -1) || (minute == -1)) {
163: cal.set(year, month, day);
164: } else {
165: if (amPm == Calendar.PM) {
166: hour += 12;
167: }
168:
169: cal.set(year, month, day, hour, minute, 0);
170: }
171:
172: DateFormat isoFormat = DateUtil.getISOFormat();
173:
174: return isoFormat.format(cal.getTime());
175: } else {
176: return null;
177: }
178: }
179:
180: public static String getLike(PortletRequest req, String param) {
181: return getLike(req, param, null, true);
182: }
183:
184: public static String getLike(PortletRequest req, String param,
185: String defaultValue) {
186:
187: return getLike(req, param, defaultValue, true);
188: }
189:
190: public static String getLike(PortletRequest req, String param,
191: boolean toLowerCase) {
192:
193: return getLike(req, param, null, toLowerCase);
194: }
195:
196: public static String getLike(PortletRequest req, String param,
197: String defaultValue, boolean toLowerCase) {
198:
199: String value = req.getParameter(param);
200:
201: if (value != null) {
202: value = value.trim();
203:
204: if (toLowerCase) {
205: value = value.toLowerCase();
206: }
207: }
208:
209: if (Validator.isNull(value)) {
210: value = defaultValue;
211: } else {
212: value = StringPool.PERCENT + value + StringPool.PERCENT;
213: }
214:
215: return value;
216: }
217:
218: public static long getLong(PortletRequest req, String param) {
219: return GetterUtil.getLong(getString(req, param));
220: }
221:
222: public static short getShort(PortletRequest req, String param) {
223: return GetterUtil.getShort(getString(req, param));
224: }
225:
226: public static String getString(PortletRequest req, String param) {
227: String value = ParamUtil.getString(req, param);
228:
229: if (Validator.isNull(value)) {
230: return null;
231: } else {
232: return value;
233: }
234: }
235:
236: }
|