1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class DAOParamUtil {
46  
47      // Servlet Request
48  
49      public static boolean getBoolean(HttpServletRequest request, String param) {
50          return GetterUtil.getBoolean(getString(request, param));
51      }
52  
53      public static int getInteger(HttpServletRequest request, String param) {
54          return GetterUtil.getInteger(getString(request, 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 getLike(HttpServletRequest request, String param) {
89          return getLike(request, param, null, true);
90      }
91  
92      public static String getLike(
93          HttpServletRequest request, String param, String defaultValue) {
94  
95          return getLike(request, param, defaultValue, true);
96      }
97  
98      public static String getLike(
99          HttpServletRequest request, String param, boolean toLowerCase) {
100 
101         return getLike(request, param, null, toLowerCase);
102     }
103 
104     public static String getLike(
105         HttpServletRequest request, String param, String defaultValue,
106         boolean toLowerCase) {
107 
108         String value = request.getParameter(param);
109 
110         if (value != null) {
111             value = value.trim();
112 
113             if (toLowerCase) {
114                 value = value.toLowerCase();
115             }
116         }
117 
118         if (Validator.isNull(value)) {
119             value = defaultValue;
120         }
121         else {
122             value = StringPool.PERCENT + value + StringPool.PERCENT;
123         }
124 
125         return value;
126     }
127 
128     public static long getLong(HttpServletRequest request, String param) {
129         return GetterUtil.getLong(getString(request, param));
130     }
131 
132     public static short getShort(HttpServletRequest request, String param) {
133         return GetterUtil.getShort(getString(request, param));
134     }
135 
136     public static String getString(HttpServletRequest request, String param) {
137         String value = ParamUtil.getString(request, param);
138 
139         if (Validator.isNull(value)) {
140             return null;
141         }
142         else {
143             return value;
144         }
145     }
146 
147     // Portlet Request
148 
149     public static boolean getBoolean(
150         PortletRequest portletRequest, String param) {
151 
152         return GetterUtil.getBoolean(getString(portletRequest, param));
153     }
154 
155     public static int getInteger(PortletRequest portletRequest, String param) {
156         return GetterUtil.getInteger(getString(portletRequest, param));
157     }
158 
159     public static String getISODate(
160         PortletRequest portletRequest, String param) {
161 
162         int month = ParamUtil.getInteger(portletRequest, param + "Month");
163         int day = ParamUtil.getInteger(portletRequest, param + "Day");
164         int year = ParamUtil.getInteger(portletRequest, param + "Year");
165         int hour = ParamUtil.getInteger(portletRequest, param + "Hour", -1);
166         int minute = ParamUtil.getInteger(portletRequest, param + "Minute", -1);
167         int amPm = ParamUtil.getInteger(portletRequest, param + "AmPm");
168 
169         if ((month >= 0) && (day > 0) && (year > 0)) {
170             Calendar cal = CalendarFactoryUtil.getCalendar();
171 
172             if ((hour == -1) || (minute == -1)) {
173                 cal.set(year, month, day);
174             }
175             else {
176                 if (amPm == Calendar.PM) {
177                     hour += 12;
178                 }
179 
180                 cal.set(year, month, day, hour, minute, 0);
181             }
182 
183             DateFormat isoFormat = DateUtil.getISOFormat();
184 
185             return isoFormat.format(cal.getTime());
186         }
187         else {
188             return null;
189         }
190     }
191 
192     public static String getLike(PortletRequest portletRequest, String param) {
193         return getLike(portletRequest, param, null, true);
194     }
195 
196     public static String getLike(
197         PortletRequest portletRequest, String param, String defaultValue) {
198 
199         return getLike(portletRequest, param, defaultValue, true);
200     }
201 
202     public static String getLike(
203         PortletRequest portletRequest, String param, boolean toLowerCase) {
204 
205         return getLike(portletRequest, param, null, toLowerCase);
206     }
207 
208     public static String getLike(
209         PortletRequest portletRequest, String param, String defaultValue,
210         boolean toLowerCase) {
211 
212         String value = portletRequest.getParameter(param);
213 
214         if (value != null) {
215             value = value.trim();
216 
217             if (toLowerCase) {
218                 value = value.toLowerCase();
219             }
220         }
221 
222         if (Validator.isNull(value)) {
223             value = defaultValue;
224         }
225         else {
226             value = StringPool.PERCENT + value + StringPool.PERCENT;
227         }
228 
229         return value;
230     }
231 
232     public static long getLong(PortletRequest portletRequest, String param) {
233         return GetterUtil.getLong(getString(portletRequest, param));
234     }
235 
236     public static short getShort(PortletRequest portletRequest, String param) {
237         return GetterUtil.getShort(getString(portletRequest, param));
238     }
239 
240     public static String getString(
241         PortletRequest portletRequest, String param) {
242 
243         String value = ParamUtil.getString(portletRequest, param);
244 
245         if (Validator.isNull(value)) {
246             return null;
247         }
248         else {
249             return value;
250         }
251     }
252 
253 }