1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.dao.search;
24  
25  import com.liferay.portal.kernel.util.CalendarFactoryUtil;
26  import com.liferay.portal.kernel.util.DateUtil;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.Validator;
31  
32  import java.text.DateFormat;
33  
34  import java.util.Calendar;
35  
36  import javax.portlet.PortletRequest;
37  
38  import javax.servlet.http.HttpServletRequest;
39  
40  /**
41   * <a href="DAOParamUtil.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class DAOParamUtil {
47  
48      // Servlet Request
49  
50      public static boolean getBoolean(HttpServletRequest request, String param) {
51          return GetterUtil.getBoolean(getString(request, param));
52      }
53  
54      public static int getInteger(HttpServletRequest request, String param) {
55          return GetterUtil.getInteger(getString(request, param));
56      }
57  
58      public static String getISODate(HttpServletRequest request, String param) {
59          int month = ParamUtil.getInteger(request, param + "Month");
60          int day = ParamUtil.getInteger(request, param + "Day");
61          int year = ParamUtil.getInteger(request, param + "Year");
62          int hour = ParamUtil.getInteger(request, param + "Hour", -1);
63          int minute = ParamUtil.getInteger(request, param + "Minute", -1);
64          int amPm = ParamUtil.getInteger(request, param + "AmPm");
65  
66          if ((month >= 0) && (day > 0) && (year > 0)) {
67              Calendar cal = CalendarFactoryUtil.getCalendar();
68  
69              if ((hour == -1) || (minute == -1)) {
70                  cal.set(year, month, day);
71              }
72              else {
73                  if (amPm == Calendar.PM) {
74                      hour += 12;
75                  }
76  
77                  cal.set(year, month, day, hour, minute, 0);
78              }
79  
80              DateFormat isoFormat = DateUtil.getISOFormat();
81  
82              return isoFormat.format(cal.getTime());
83          }
84          else {
85              return null;
86          }
87      }
88  
89      public static String getLike(HttpServletRequest request, String param) {
90          return getLike(request, param, null, true);
91      }
92  
93      public static String getLike(
94          HttpServletRequest request, String param, String defaultValue) {
95  
96          return getLike(request, param, defaultValue, true);
97      }
98  
99      public static String getLike(
100         HttpServletRequest request, String param, boolean toLowerCase) {
101 
102         return getLike(request, param, null, toLowerCase);
103     }
104 
105     public static String getLike(
106         HttpServletRequest request, String param, String defaultValue,
107         boolean toLowerCase) {
108 
109         String value = request.getParameter(param);
110 
111         if (value != null) {
112             value = value.trim();
113 
114             if (toLowerCase) {
115                 value = value.toLowerCase();
116             }
117         }
118 
119         if (Validator.isNull(value)) {
120             value = defaultValue;
121         }
122         else {
123             value = StringPool.PERCENT + value + StringPool.PERCENT;
124         }
125 
126         return value;
127     }
128 
129     public static long getLong(HttpServletRequest request, String param) {
130         return GetterUtil.getLong(getString(request, param));
131     }
132 
133     public static short getShort(HttpServletRequest request, String param) {
134         return GetterUtil.getShort(getString(request, param));
135     }
136 
137     public static String getString(HttpServletRequest request, String param) {
138         String value = ParamUtil.getString(request, param);
139 
140         if (Validator.isNull(value)) {
141             return null;
142         }
143         else {
144             return value;
145         }
146     }
147 
148     // Portlet Request
149 
150     public static boolean getBoolean(
151         PortletRequest portletRequest, String param) {
152 
153         return GetterUtil.getBoolean(getString(portletRequest, param));
154     }
155 
156     public static int getInteger(PortletRequest portletRequest, String param) {
157         return GetterUtil.getInteger(getString(portletRequest, param));
158     }
159 
160     public static String getISODate(
161         PortletRequest portletRequest, String param) {
162 
163         int month = ParamUtil.getInteger(portletRequest, param + "Month");
164         int day = ParamUtil.getInteger(portletRequest, param + "Day");
165         int year = ParamUtil.getInteger(portletRequest, param + "Year");
166         int hour = ParamUtil.getInteger(portletRequest, param + "Hour", -1);
167         int minute = ParamUtil.getInteger(portletRequest, param + "Minute", -1);
168         int amPm = ParamUtil.getInteger(portletRequest, param + "AmPm");
169 
170         if ((month >= 0) && (day > 0) && (year > 0)) {
171             Calendar cal = CalendarFactoryUtil.getCalendar();
172 
173             if ((hour == -1) || (minute == -1)) {
174                 cal.set(year, month, day);
175             }
176             else {
177                 if (amPm == Calendar.PM) {
178                     hour += 12;
179                 }
180 
181                 cal.set(year, month, day, hour, minute, 0);
182             }
183 
184             DateFormat isoFormat = DateUtil.getISOFormat();
185 
186             return isoFormat.format(cal.getTime());
187         }
188         else {
189             return null;
190         }
191     }
192 
193     public static String getLike(PortletRequest portletRequest, String param) {
194         return getLike(portletRequest, param, null, true);
195     }
196 
197     public static String getLike(
198         PortletRequest portletRequest, String param, String defaultValue) {
199 
200         return getLike(portletRequest, param, defaultValue, true);
201     }
202 
203     public static String getLike(
204         PortletRequest portletRequest, String param, boolean toLowerCase) {
205 
206         return getLike(portletRequest, param, null, toLowerCase);
207     }
208 
209     public static String getLike(
210         PortletRequest portletRequest, String param, String defaultValue,
211         boolean toLowerCase) {
212 
213         String value = portletRequest.getParameter(param);
214 
215         if (value != null) {
216             value = value.trim();
217 
218             if (toLowerCase) {
219                 value = value.toLowerCase();
220             }
221         }
222 
223         if (Validator.isNull(value)) {
224             value = defaultValue;
225         }
226         else {
227             value = StringPool.PERCENT + value + StringPool.PERCENT;
228         }
229 
230         return value;
231     }
232 
233     public static long getLong(PortletRequest portletRequest, String param) {
234         return GetterUtil.getLong(getString(portletRequest, param));
235     }
236 
237     public static short getShort(PortletRequest portletRequest, String param) {
238         return GetterUtil.getShort(getString(portletRequest, param));
239     }
240 
241     public static String getString(
242         PortletRequest portletRequest, String param) {
243 
244         String value = ParamUtil.getString(portletRequest, param);
245 
246         if (Validator.isNull(value)) {
247             return null;
248         }
249         else {
250             return value;
251         }
252     }
253 
254 }