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      public static boolean getBoolean(HttpServletRequest request, String param) {
48          return GetterUtil.getBoolean(getString(request, param));
49      }
50  
51      public static boolean getBoolean(
52          PortletRequest portletRequest, String param) {
53  
54          return GetterUtil.getBoolean(getString(portletRequest, param));
55      }
56  
57      public static int getInteger(HttpServletRequest request, String param) {
58          return GetterUtil.getInteger(getString(request, param));
59      }
60  
61      public static int getInteger(PortletRequest portletRequest, String param) {
62          return GetterUtil.getInteger(getString(portletRequest, param));
63      }
64  
65      public static String getISODate(HttpServletRequest request, String param) {
66          int month = ParamUtil.getInteger(request, param + "Month");
67          int day = ParamUtil.getInteger(request, param + "Day");
68          int year = ParamUtil.getInteger(request, param + "Year");
69          int hour = ParamUtil.getInteger(request, param + "Hour", -1);
70          int minute = ParamUtil.getInteger(request, param + "Minute", -1);
71          int amPm = ParamUtil.getInteger(request, param + "AmPm");
72  
73          if ((month >= 0) && (day > 0) && (year > 0)) {
74              Calendar cal = CalendarFactoryUtil.getCalendar();
75  
76              if ((hour == -1) || (minute == -1)) {
77                  cal.set(year, month, day);
78              }
79              else {
80                  if (amPm == Calendar.PM) {
81                      hour += 12;
82                  }
83  
84                  cal.set(year, month, day, hour, minute, 0);
85              }
86  
87              DateFormat isoFormat = DateUtil.getISOFormat();
88  
89              return isoFormat.format(cal.getTime());
90          }
91          else {
92              return null;
93          }
94      }
95  
96      public static String getISODate(
97          PortletRequest portletRequest, String param) {
98  
99          int month = ParamUtil.getInteger(portletRequest, param + "Month");
100         int day = ParamUtil.getInteger(portletRequest, param + "Day");
101         int year = ParamUtil.getInteger(portletRequest, param + "Year");
102         int hour = ParamUtil.getInteger(portletRequest, param + "Hour", -1);
103         int minute = ParamUtil.getInteger(portletRequest, param + "Minute", -1);
104         int amPm = ParamUtil.getInteger(portletRequest, param + "AmPm");
105 
106         if ((month >= 0) && (day > 0) && (year > 0)) {
107             Calendar cal = CalendarFactoryUtil.getCalendar();
108 
109             if ((hour == -1) || (minute == -1)) {
110                 cal.set(year, month, day);
111             }
112             else {
113                 if (amPm == Calendar.PM) {
114                     hour += 12;
115                 }
116 
117                 cal.set(year, month, day, hour, minute, 0);
118             }
119 
120             DateFormat isoFormat = DateUtil.getISOFormat();
121 
122             return isoFormat.format(cal.getTime());
123         }
124         else {
125             return null;
126         }
127     }
128 
129     public static String getLike(HttpServletRequest request, String param) {
130         return getLike(request, param, null, true);
131     }
132 
133     public static String getLike(
134         HttpServletRequest request, String param, boolean toLowerCase) {
135 
136         return getLike(request, param, null, toLowerCase);
137     }
138 
139     public static String getLike(
140         HttpServletRequest request, String param, String defaultValue) {
141 
142         return getLike(request, param, defaultValue, true);
143     }
144 
145     public static String getLike(
146         HttpServletRequest request, String param, String defaultValue,
147         boolean toLowerCase) {
148 
149         String value = request.getParameter(param);
150 
151         if (value != null) {
152             value = value.trim();
153 
154             if (toLowerCase) {
155                 value = value.toLowerCase();
156             }
157         }
158 
159         if (Validator.isNull(value)) {
160             value = defaultValue;
161         }
162         else {
163             value = StringPool.PERCENT + value + StringPool.PERCENT;
164         }
165 
166         return value;
167     }
168 
169     public static String getLike(PortletRequest portletRequest, String param) {
170         return getLike(portletRequest, param, null, true);
171     }
172 
173     public static String getLike(
174         PortletRequest portletRequest, String param, boolean toLowerCase) {
175 
176         return getLike(portletRequest, param, null, toLowerCase);
177     }
178 
179     public static String getLike(
180         PortletRequest portletRequest, String param, String defaultValue) {
181 
182         return getLike(portletRequest, param, defaultValue, true);
183     }
184 
185     public static String getLike(
186         PortletRequest portletRequest, String param, String defaultValue,
187         boolean toLowerCase) {
188 
189         String value = portletRequest.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(HttpServletRequest request, String param) {
210         return GetterUtil.getLong(getString(request, param));
211     }
212 
213     public static long getLong(PortletRequest portletRequest, String param) {
214         return GetterUtil.getLong(getString(portletRequest, param));
215     }
216 
217     public static short getShort(HttpServletRequest request, String param) {
218         return GetterUtil.getShort(getString(request, param));
219     }
220 
221     public static short getShort(PortletRequest portletRequest, String param) {
222         return GetterUtil.getShort(getString(portletRequest, param));
223     }
224 
225     public static String getString(HttpServletRequest request, String param) {
226         String value = ParamUtil.getString(request, param);
227 
228         if (Validator.isNull(value)) {
229             return null;
230         }
231         else {
232             return value;
233         }
234     }
235 
236     public static String getString(
237         PortletRequest portletRequest, String param) {
238 
239         String value = ParamUtil.getString(portletRequest, param);
240 
241         if (Validator.isNull(value)) {
242             return null;
243         }
244         else {
245             return value;
246         }
247     }
248 
249 }