1   /**
2    * Copyright (c) 2000-2007 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 String getISODate(ServletRequest req, String param) {
51          int month = ParamUtil.getInteger(req, param + "Month");
52          int day = ParamUtil.getInteger(req, param + "Day");
53          int year = ParamUtil.getInteger(req, param + "Year");
54          int hour = ParamUtil.getInteger(req, param + "Hour", -1);
55          int minute = ParamUtil.getInteger(req, param + "Minute", -1);
56          int amPm = ParamUtil.getInteger(req, param + "AmPm");
57  
58          if ((month >= 0) && (day > 0) && (year > 0)) {
59              Calendar cal = CalendarFactoryUtil.getCalendar();
60  
61              if ((hour == -1) || (minute == -1)) {
62                  cal.set(year, month, day);
63              }
64              else {
65                  if (amPm == Calendar.PM) {
66                      hour += 12;
67                  }
68  
69                  cal.set(year, month, day, hour, minute, 0);
70              }
71  
72              DateFormat isoFormat = DateUtil.getISOFormat();
73  
74              return isoFormat.format(cal.getTime());
75          }
76          else {
77              return null;
78          }
79      }
80  
81      public static String getLike(ServletRequest req, String param) {
82          return getLike(req, param, null, true);
83      }
84  
85      public static String getLike(
86          ServletRequest req, String param, String defaultValue) {
87  
88          return getLike(req, param, defaultValue, true);
89      }
90  
91      public static String getLike(
92          ServletRequest req, String param, boolean toLowerCase) {
93  
94          return getLike(req, param, null, toLowerCase);
95      }
96  
97      public static String getLike(
98          ServletRequest req, String param, String defaultValue,
99          boolean toLowerCase) {
100 
101         String value = req.getParameter(param);
102 
103         if (value != null) {
104             value = value.trim();
105 
106             if (toLowerCase) {
107                 value = value.toLowerCase();
108             }
109         }
110 
111         if (Validator.isNull(value)) {
112             value = defaultValue;
113         }
114         else {
115             value = StringPool.PERCENT + value + StringPool.PERCENT;
116         }
117 
118         return value;
119     }
120 
121     public static long getLong(ServletRequest req, String param) {
122         return GetterUtil.getLong(getString(req, param));
123     }
124 
125     public static String getString(ServletRequest req, String param) {
126         String value = ParamUtil.getString(req, param);
127 
128         if (Validator.isNull(value)) {
129             return null;
130         }
131         else {
132             return value;
133         }
134     }
135 
136     // Portlet Request
137 
138     public static String getISODate(PortletRequest req, String param) {
139         int month = ParamUtil.getInteger(req, param + "Month");
140         int day = ParamUtil.getInteger(req, param + "Day");
141         int year = ParamUtil.getInteger(req, param + "Year");
142         int hour = ParamUtil.getInteger(req, param + "Hour", -1);
143         int minute = ParamUtil.getInteger(req, param + "Minute", -1);
144         int amPm = ParamUtil.getInteger(req, param + "AmPm");
145 
146         if ((month >= 0) && (day > 0) && (year > 0)) {
147             Calendar cal = CalendarFactoryUtil.getCalendar();
148 
149             if ((hour == -1) || (minute == -1)) {
150                 cal.set(year, month, day);
151             }
152             else {
153                 if (amPm == Calendar.PM) {
154                     hour += 12;
155                 }
156 
157                 cal.set(year, month, day, hour, minute, 0);
158             }
159 
160             DateFormat isoFormat = DateUtil.getISOFormat();
161 
162             return isoFormat.format(cal.getTime());
163         }
164         else {
165             return null;
166         }
167     }
168 
169     public static String getLike(PortletRequest req, String param) {
170         return getLike(req, param, null, true);
171     }
172 
173     public static String getLike(
174         PortletRequest req, String param, String defaultValue) {
175 
176         return getLike(req, param, defaultValue, true);
177     }
178 
179     public static String getLike(
180         PortletRequest req, String param, boolean toLowerCase) {
181 
182         return getLike(req, param, null, toLowerCase);
183     }
184 
185     public static String getLike(
186         PortletRequest req, String param, String defaultValue,
187         boolean toLowerCase) {
188 
189         String value = req.getParameter(param);
190 
191         if (value != null) {
192             value = value.trim();
193 
194             if (toLowerCase) {
195                 value = value.toLowerCase();
196             }
197         }
198 
199         if (Validator.isNull(value)) {
200             value = defaultValue;
201         }
202         else {
203             value = StringPool.PERCENT + value + StringPool.PERCENT;
204         }
205 
206         return value;
207     }
208 
209     public static long getLong(PortletRequest req, String param) {
210         return GetterUtil.getLong(getString(req, param));
211     }
212 
213     public static String getString(PortletRequest req, String param) {
214         String value = ParamUtil.getString(req, param);
215 
216         if (Validator.isNull(value)) {
217             return null;
218         }
219         else {
220             return value;
221         }
222     }
223 
224 }