001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.dao.search;
016    
017    import com.liferay.portal.kernel.util.CalendarFactoryUtil;
018    import com.liferay.portal.kernel.util.DateUtil;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    
023    import java.text.DateFormat;
024    
025    import java.util.Calendar;
026    
027    import javax.portlet.PortletRequest;
028    
029    import javax.servlet.http.HttpServletRequest;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class DAOParamUtil {
035    
036            public static boolean getBoolean(HttpServletRequest request, String param) {
037                    return GetterUtil.getBoolean(getString(request, param));
038            }
039    
040            public static boolean getBoolean(
041                    PortletRequest portletRequest, String param) {
042    
043                    return GetterUtil.getBoolean(getString(portletRequest, param));
044            }
045    
046            public static int getInteger(HttpServletRequest request, String param) {
047                    return GetterUtil.getInteger(getString(request, param));
048            }
049    
050            public static int getInteger(PortletRequest portletRequest, String param) {
051                    return GetterUtil.getInteger(getString(portletRequest, param));
052            }
053    
054            public static String getISODate(HttpServletRequest request, String param) {
055                    int month = ParamUtil.getInteger(request, param + "Month");
056                    int day = ParamUtil.getInteger(request, param + "Day");
057                    int year = ParamUtil.getInteger(request, param + "Year");
058                    int hour = ParamUtil.getInteger(request, param + "Hour", -1);
059                    int minute = ParamUtil.getInteger(request, param + "Minute", -1);
060                    int amPm = ParamUtil.getInteger(request, param + "AmPm");
061    
062                    if ((month >= 0) && (day > 0) && (year > 0)) {
063                            Calendar cal = CalendarFactoryUtil.getCalendar();
064    
065                            if ((hour == -1) || (minute == -1)) {
066                                    cal.set(year, month, day);
067                            }
068                            else {
069                                    if (amPm == Calendar.PM) {
070                                            hour += 12;
071                                    }
072    
073                                    cal.set(year, month, day, hour, minute, 0);
074                            }
075    
076                            DateFormat isoFormat = DateUtil.getISOFormat();
077    
078                            return isoFormat.format(cal.getTime());
079                    }
080                    else {
081                            return null;
082                    }
083            }
084    
085            public static String getISODate(
086                    PortletRequest portletRequest, String param) {
087    
088                    int month = ParamUtil.getInteger(portletRequest, param + "Month");
089                    int day = ParamUtil.getInteger(portletRequest, param + "Day");
090                    int year = ParamUtil.getInteger(portletRequest, param + "Year");
091                    int hour = ParamUtil.getInteger(portletRequest, param + "Hour", -1);
092                    int minute = ParamUtil.getInteger(portletRequest, param + "Minute", -1);
093                    int amPm = ParamUtil.getInteger(portletRequest, param + "AmPm");
094    
095                    if ((month >= 0) && (day > 0) && (year > 0)) {
096                            Calendar cal = CalendarFactoryUtil.getCalendar();
097    
098                            if ((hour == -1) || (minute == -1)) {
099                                    cal.set(year, month, day);
100                            }
101                            else {
102                                    if (amPm == Calendar.PM) {
103                                            hour += 12;
104                                    }
105    
106                                    cal.set(year, month, day, hour, minute, 0);
107                            }
108    
109                            DateFormat isoFormat = DateUtil.getISOFormat();
110    
111                            return isoFormat.format(cal.getTime());
112                    }
113                    else {
114                            return null;
115                    }
116            }
117    
118            public static long getLong(HttpServletRequest request, String param) {
119                    return GetterUtil.getLong(getString(request, param));
120            }
121    
122            public static long getLong(PortletRequest portletRequest, String param) {
123                    return GetterUtil.getLong(getString(portletRequest, param));
124            }
125    
126            public static short getShort(HttpServletRequest request, String param) {
127                    return GetterUtil.getShort(getString(request, param));
128            }
129    
130            public static short getShort(PortletRequest portletRequest, String param) {
131                    return GetterUtil.getShort(getString(portletRequest, param));
132            }
133    
134            public static String getString(HttpServletRequest request, String param) {
135                    String value = ParamUtil.getString(request, param);
136    
137                    if (Validator.isNull(value)) {
138                            return null;
139                    }
140                    else {
141                            return value;
142                    }
143            }
144    
145            public static String getString(
146                    PortletRequest portletRequest, String param) {
147    
148                    String value = ParamUtil.getString(portletRequest, param);
149    
150                    if (Validator.isNull(value)) {
151                            return null;
152                    }
153                    else {
154                            return value;
155                    }
156            }
157    
158    }