1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.dao.search;
16  
17  import com.liferay.portal.kernel.util.CalendarFactoryUtil;
18  import com.liferay.portal.kernel.util.DateUtil;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.ParamUtil;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.Validator;
23  
24  import java.text.DateFormat;
25  
26  import java.util.Calendar;
27  
28  import javax.portlet.PortletRequest;
29  
30  import javax.servlet.http.HttpServletRequest;
31  
32  /**
33   * <a href="DAOParamUtil.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   */
37  public class DAOParamUtil {
38  
39      public static boolean getBoolean(HttpServletRequest request, String param) {
40          return GetterUtil.getBoolean(getString(request, param));
41      }
42  
43      public static boolean getBoolean(
44          PortletRequest portletRequest, String param) {
45  
46          return GetterUtil.getBoolean(getString(portletRequest, param));
47      }
48  
49      public static int getInteger(HttpServletRequest request, String param) {
50          return GetterUtil.getInteger(getString(request, param));
51      }
52  
53      public static int getInteger(PortletRequest portletRequest, String param) {
54          return GetterUtil.getInteger(getString(portletRequest, param));
55      }
56  
57      public static String getISODate(HttpServletRequest request, String param) {
58          int month = ParamUtil.getInteger(request, param + "Month");
59          int day = ParamUtil.getInteger(request, param + "Day");
60          int year = ParamUtil.getInteger(request, param + "Year");
61          int hour = ParamUtil.getInteger(request, param + "Hour", -1);
62          int minute = ParamUtil.getInteger(request, param + "Minute", -1);
63          int amPm = ParamUtil.getInteger(request, param + "AmPm");
64  
65          if ((month >= 0) && (day > 0) && (year > 0)) {
66              Calendar cal = CalendarFactoryUtil.getCalendar();
67  
68              if ((hour == -1) || (minute == -1)) {
69                  cal.set(year, month, day);
70              }
71              else {
72                  if (amPm == Calendar.PM) {
73                      hour += 12;
74                  }
75  
76                  cal.set(year, month, day, hour, minute, 0);
77              }
78  
79              DateFormat isoFormat = DateUtil.getISOFormat();
80  
81              return isoFormat.format(cal.getTime());
82          }
83          else {
84              return null;
85          }
86      }
87  
88      public static String getISODate(
89          PortletRequest portletRequest, String param) {
90  
91          int month = ParamUtil.getInteger(portletRequest, param + "Month");
92          int day = ParamUtil.getInteger(portletRequest, param + "Day");
93          int year = ParamUtil.getInteger(portletRequest, param + "Year");
94          int hour = ParamUtil.getInteger(portletRequest, param + "Hour", -1);
95          int minute = ParamUtil.getInteger(portletRequest, param + "Minute", -1);
96          int amPm = ParamUtil.getInteger(portletRequest, param + "AmPm");
97  
98          if ((month >= 0) && (day > 0) && (year > 0)) {
99              Calendar cal = CalendarFactoryUtil.getCalendar();
100 
101             if ((hour == -1) || (minute == -1)) {
102                 cal.set(year, month, day);
103             }
104             else {
105                 if (amPm == Calendar.PM) {
106                     hour += 12;
107                 }
108 
109                 cal.set(year, month, day, hour, minute, 0);
110             }
111 
112             DateFormat isoFormat = DateUtil.getISOFormat();
113 
114             return isoFormat.format(cal.getTime());
115         }
116         else {
117             return null;
118         }
119     }
120 
121     public static String getLike(HttpServletRequest request, String param) {
122         return getLike(request, param, null, true);
123     }
124 
125     public static String getLike(
126         HttpServletRequest request, String param, boolean toLowerCase) {
127 
128         return getLike(request, param, null, toLowerCase);
129     }
130 
131     public static String getLike(
132         HttpServletRequest request, String param, String defaultValue) {
133 
134         return getLike(request, param, defaultValue, true);
135     }
136 
137     public static String getLike(
138         HttpServletRequest request, String param, String defaultValue,
139         boolean toLowerCase) {
140 
141         String value = request.getParameter(param);
142 
143         if (value != null) {
144             value = value.trim();
145 
146             if (toLowerCase) {
147                 value = value.toLowerCase();
148             }
149         }
150 
151         if (Validator.isNull(value)) {
152             value = defaultValue;
153         }
154         else {
155             value = StringPool.PERCENT + value + StringPool.PERCENT;
156         }
157 
158         return value;
159     }
160 
161     public static String getLike(PortletRequest portletRequest, String param) {
162         return getLike(portletRequest, param, null, true);
163     }
164 
165     public static String getLike(
166         PortletRequest portletRequest, String param, boolean toLowerCase) {
167 
168         return getLike(portletRequest, param, null, toLowerCase);
169     }
170 
171     public static String getLike(
172         PortletRequest portletRequest, String param, String defaultValue) {
173 
174         return getLike(portletRequest, param, defaultValue, true);
175     }
176 
177     public static String getLike(
178         PortletRequest portletRequest, String param, String defaultValue,
179         boolean toLowerCase) {
180 
181         String value = portletRequest.getParameter(param);
182 
183         if (value != null) {
184             value = value.trim();
185 
186             if (toLowerCase) {
187                 value = value.toLowerCase();
188             }
189         }
190 
191         if (Validator.isNull(value)) {
192             value = defaultValue;
193         }
194         else {
195             value = StringPool.PERCENT + value + StringPool.PERCENT;
196         }
197 
198         return value;
199     }
200 
201     public static long getLong(HttpServletRequest request, String param) {
202         return GetterUtil.getLong(getString(request, param));
203     }
204 
205     public static long getLong(PortletRequest portletRequest, String param) {
206         return GetterUtil.getLong(getString(portletRequest, param));
207     }
208 
209     public static short getShort(HttpServletRequest request, String param) {
210         return GetterUtil.getShort(getString(request, param));
211     }
212 
213     public static short getShort(PortletRequest portletRequest, String param) {
214         return GetterUtil.getShort(getString(portletRequest, param));
215     }
216 
217     public static String getString(HttpServletRequest request, String param) {
218         String value = ParamUtil.getString(request, param);
219 
220         if (Validator.isNull(value)) {
221             return null;
222         }
223         else {
224             return value;
225         }
226     }
227 
228     public static String getString(
229         PortletRequest portletRequest, String param) {
230 
231         String value = ParamUtil.getString(portletRequest, param);
232 
233         if (Validator.isNull(value)) {
234             return null;
235         }
236         else {
237             return value;
238         }
239     }
240 
241 }