1   /**
2    * Copyright (c) 2000-2008 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;
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.ServletRequest;
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(ServletRequest req, String param) {
51          return GetterUtil.getBoolean(getString(req, param));
52      }
53  
54      public static int getInteger(ServletRequest req, String param) {
55          return GetterUtil.getInteger(getString(req, param));
56      }
57  
58      public static String getISODate(ServletRequest req, String param) {
59          int month = ParamUtil.getInteger(req, param + "Month");
60          int day = ParamUtil.getInteger(req, param + "Day");
61          int year = ParamUtil.getInteger(req, param + "Year");
62          int hour = ParamUtil.getInteger(req, param + "Hour", -1);
63          int minute = ParamUtil.getInteger(req, param + "Minute", -1);
64          int amPm = ParamUtil.getInteger(req, 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(ServletRequest req, String param) {
90          return getLike(req, param, null, true);
91      }
92  
93      public static String getLike(
94          ServletRequest req, String param, String defaultValue) {
95  
96          return getLike(req, param, defaultValue, true);
97      }
98  
99      public static String getLike(
100         ServletRequest req, String param, boolean toLowerCase) {
101 
102         return getLike(req, param, null, toLowerCase);
103     }
104 
105     public static String getLike(
106         ServletRequest req, String param, String defaultValue,
107         boolean toLowerCase) {
108 
109         String value = req.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(ServletRequest req, String param) {
130         return GetterUtil.getLong(getString(req, param));
131     }
132 
133     public static short getShort(ServletRequest req, String param) {
134         return GetterUtil.getShort(getString(req, param));
135     }
136 
137     public static String getString(ServletRequest req, String param) {
138         String value = ParamUtil.getString(req, 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(PortletRequest req, String param) {
151         return GetterUtil.getBoolean(getString(req, param));
152     }
153 
154     public static int getInteger(PortletRequest req, String param) {
155         return GetterUtil.getInteger(getString(req, param));
156     }
157 
158     public static String getISODate(PortletRequest req, String param) {
159         int month = ParamUtil.getInteger(req, param + "Month");
160         int day = ParamUtil.getInteger(req, param + "Day");
161         int year = ParamUtil.getInteger(req, param + "Year");
162         int hour = ParamUtil.getInteger(req, param + "Hour", -1);
163         int minute = ParamUtil.getInteger(req, param + "Minute", -1);
164         int amPm = ParamUtil.getInteger(req, param + "AmPm");
165 
166         if ((month >= 0) && (day > 0) && (year > 0)) {
167             Calendar cal = CalendarFactoryUtil.getCalendar();
168 
169             if ((hour == -1) || (minute == -1)) {
170                 cal.set(year, month, day);
171             }
172             else {
173                 if (amPm == Calendar.PM) {
174                     hour += 12;
175                 }
176 
177                 cal.set(year, month, day, hour, minute, 0);
178             }
179 
180             DateFormat isoFormat = DateUtil.getISOFormat();
181 
182             return isoFormat.format(cal.getTime());
183         }
184         else {
185             return null;
186         }
187     }
188 
189     public static String getLike(PortletRequest req, String param) {
190         return getLike(req, param, null, true);
191     }
192 
193     public static String getLike(
194         PortletRequest req, String param, String defaultValue) {
195 
196         return getLike(req, param, defaultValue, true);
197     }
198 
199     public static String getLike(
200         PortletRequest req, String param, boolean toLowerCase) {
201 
202         return getLike(req, param, null, toLowerCase);
203     }
204 
205     public static String getLike(
206         PortletRequest req, String param, String defaultValue,
207         boolean toLowerCase) {
208 
209         String value = req.getParameter(param);
210 
211         if (value != null) {
212             value = value.trim();
213 
214             if (toLowerCase) {
215                 value = value.toLowerCase();
216             }
217         }
218 
219         if (Validator.isNull(value)) {
220             value = defaultValue;
221         }
222         else {
223             value = StringPool.PERCENT + value + StringPool.PERCENT;
224         }
225 
226         return value;
227     }
228 
229     public static long getLong(PortletRequest req, String param) {
230         return GetterUtil.getLong(getString(req, param));
231     }
232 
233     public static short getShort(PortletRequest req, String param) {
234         return GetterUtil.getShort(getString(req, param));
235     }
236 
237     public static String getString(PortletRequest req, String param) {
238         String value = ParamUtil.getString(req, param);
239 
240         if (Validator.isNull(value)) {
241             return null;
242         }
243         else {
244             return value;
245         }
246     }
247 
248 }