001
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.StringPool;
022 import com.liferay.portal.kernel.util.Validator;
023
024 import java.text.DateFormat;
025
026 import java.util.Calendar;
027
028 import javax.portlet.PortletRequest;
029
030 import javax.servlet.http.HttpServletRequest;
031
032
035 public class DAOParamUtil {
036
037 public static boolean getBoolean(HttpServletRequest request, String param) {
038 return GetterUtil.getBoolean(getString(request, param));
039 }
040
041 public static boolean getBoolean(
042 PortletRequest portletRequest, String param) {
043
044 return GetterUtil.getBoolean(getString(portletRequest, param));
045 }
046
047 public static int getInteger(HttpServletRequest request, String param) {
048 return GetterUtil.getInteger(getString(request, param));
049 }
050
051 public static int getInteger(PortletRequest portletRequest, String param) {
052 return GetterUtil.getInteger(getString(portletRequest, param));
053 }
054
055 public static String getISODate(HttpServletRequest request, String param) {
056 int month = ParamUtil.getInteger(request, param + "Month");
057 int day = ParamUtil.getInteger(request, param + "Day");
058 int year = ParamUtil.getInteger(request, param + "Year");
059 int hour = ParamUtil.getInteger(request, param + "Hour", -1);
060 int minute = ParamUtil.getInteger(request, param + "Minute", -1);
061 int amPm = ParamUtil.getInteger(request, param + "AmPm");
062
063 if ((month >= 0) && (day > 0) && (year > 0)) {
064 Calendar cal = CalendarFactoryUtil.getCalendar();
065
066 if ((hour == -1) || (minute == -1)) {
067 cal.set(year, month, day);
068 }
069 else {
070 if (amPm == Calendar.PM) {
071 hour += 12;
072 }
073
074 cal.set(year, month, day, hour, minute, 0);
075 }
076
077 DateFormat isoFormat = DateUtil.getISOFormat();
078
079 return isoFormat.format(cal.getTime());
080 }
081 else {
082 return null;
083 }
084 }
085
086 public static String getISODate(
087 PortletRequest portletRequest, String param) {
088
089 int month = ParamUtil.getInteger(portletRequest, param + "Month");
090 int day = ParamUtil.getInteger(portletRequest, param + "Day");
091 int year = ParamUtil.getInteger(portletRequest, param + "Year");
092 int hour = ParamUtil.getInteger(portletRequest, param + "Hour", -1);
093 int minute = ParamUtil.getInteger(portletRequest, param + "Minute", -1);
094 int amPm = ParamUtil.getInteger(portletRequest, param + "AmPm");
095
096 if ((month >= 0) && (day > 0) && (year > 0)) {
097 Calendar cal = CalendarFactoryUtil.getCalendar();
098
099 if ((hour == -1) || (minute == -1)) {
100 cal.set(year, month, day);
101 }
102 else {
103 if (amPm == Calendar.PM) {
104 hour += 12;
105 }
106
107 cal.set(year, month, day, hour, minute, 0);
108 }
109
110 DateFormat isoFormat = DateUtil.getISOFormat();
111
112 return isoFormat.format(cal.getTime());
113 }
114 else {
115 return null;
116 }
117 }
118
119 public static String getLike(HttpServletRequest request, String param) {
120 return getLike(request, param, null, true);
121 }
122
123 public static String getLike(
124 HttpServletRequest request, String param, boolean toLowerCase) {
125
126 return getLike(request, param, null, toLowerCase);
127 }
128
129 public static String getLike(
130 HttpServletRequest request, String param, String defaultValue) {
131
132 return getLike(request, param, defaultValue, true);
133 }
134
135 public static String getLike(
136 HttpServletRequest request, String param, String defaultValue,
137 boolean toLowerCase) {
138
139 String value = request.getParameter(param);
140
141 if (value != null) {
142 value = value.trim();
143
144 if (toLowerCase) {
145 value = value.toLowerCase();
146 }
147 }
148
149 if (Validator.isNull(value)) {
150 value = defaultValue;
151 }
152 else {
153 value = StringPool.PERCENT + value + StringPool.PERCENT;
154 }
155
156 return value;
157 }
158
159 public static String getLike(PortletRequest portletRequest, String param) {
160 return getLike(portletRequest, param, null, true);
161 }
162
163 public static String getLike(
164 PortletRequest portletRequest, String param, boolean toLowerCase) {
165
166 return getLike(portletRequest, param, null, toLowerCase);
167 }
168
169 public static String getLike(
170 PortletRequest portletRequest, String param, String defaultValue) {
171
172 return getLike(portletRequest, param, defaultValue, true);
173 }
174
175 public static String getLike(
176 PortletRequest portletRequest, String param, String defaultValue,
177 boolean toLowerCase) {
178
179 String value = portletRequest.getParameter(param);
180
181 if (value != null) {
182 value = value.trim();
183
184 if (toLowerCase) {
185 value = value.toLowerCase();
186 }
187 }
188
189 if (Validator.isNull(value)) {
190 value = defaultValue;
191 }
192 else {
193 value = StringPool.PERCENT + value + StringPool.PERCENT;
194 }
195
196 return value;
197 }
198
199 public static long getLong(HttpServletRequest request, String param) {
200 return GetterUtil.getLong(getString(request, param));
201 }
202
203 public static long getLong(PortletRequest portletRequest, String param) {
204 return GetterUtil.getLong(getString(portletRequest, param));
205 }
206
207 public static short getShort(HttpServletRequest request, String param) {
208 return GetterUtil.getShort(getString(request, param));
209 }
210
211 public static short getShort(PortletRequest portletRequest, String param) {
212 return GetterUtil.getShort(getString(portletRequest, param));
213 }
214
215 public static String getString(HttpServletRequest request, String param) {
216 String value = ParamUtil.getString(request, param);
217
218 if (Validator.isNull(value)) {
219 return null;
220 }
221 else {
222 return value;
223 }
224 }
225
226 public static String getString(
227 PortletRequest portletRequest, String param) {
228
229 String value = ParamUtil.getString(portletRequest, param);
230
231 if (Validator.isNull(value)) {
232 return null;
233 }
234 else {
235 return value;
236 }
237 }
238
239 }