001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util;
022:
023: import com.liferay.portal.kernel.util.GetterUtil;
024: import com.liferay.portal.kernel.util.ParamUtil;
025:
026: import javax.servlet.ServletRequest;
027:
028: import org.apache.commons.beanutils.PropertyUtils;
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031:
032: /**
033: * <a href="BeanParamUtil.java.html"><b><i>View Source</i></b></a>
034: *
035: * @author Brian Wing Shun Chan
036: *
037: */
038: public class BeanParamUtil {
039:
040: public static boolean getBoolean(Object bean, ServletRequest req,
041: String param) {
042:
043: return getBoolean(bean, req, param, GetterUtil.DEFAULT_BOOLEAN);
044: }
045:
046: public static boolean getBoolean(Object bean, ServletRequest req,
047: String param, boolean defaultValue) {
048:
049: Boolean beanValue = null;
050:
051: if (bean != null) {
052: try {
053: beanValue = (Boolean) PropertyUtils.getSimpleProperty(
054: bean, param);
055: } catch (Exception e) {
056: _log.error(param + " " + e.getMessage());
057: }
058: }
059:
060: if (beanValue == null) {
061: return ParamUtil.get(req, param, defaultValue);
062: } else {
063: return ParamUtil.get(req, param, beanValue.booleanValue());
064: }
065: }
066:
067: public static double getDouble(Object bean, ServletRequest req,
068: String param) {
069:
070: return getDouble(bean, req, param, GetterUtil.DEFAULT_DOUBLE);
071: }
072:
073: public static double getDouble(Object bean, ServletRequest req,
074: String param, double defaultValue) {
075:
076: Double beanValue = null;
077:
078: if (bean != null) {
079: try {
080: beanValue = (Double) PropertyUtils.getSimpleProperty(
081: bean, param);
082: } catch (Exception e) {
083: _log.error(param + " " + e.getMessage());
084: }
085: }
086:
087: if (beanValue == null) {
088: return ParamUtil.get(req, param, defaultValue);
089: } else {
090: return ParamUtil.get(req, param, beanValue.doubleValue());
091: }
092: }
093:
094: public static int getInteger(Object bean, ServletRequest req,
095: String param) {
096:
097: return getInteger(bean, req, param, GetterUtil.DEFAULT_INTEGER);
098: }
099:
100: public static int getInteger(Object bean, ServletRequest req,
101: String param, int defaultValue) {
102:
103: Integer beanValue = null;
104:
105: if (bean != null) {
106: try {
107: beanValue = (Integer) PropertyUtils.getSimpleProperty(
108: bean, param);
109: } catch (Exception e) {
110: _log.error(param + " " + e.getMessage());
111: }
112: }
113:
114: if (beanValue == null) {
115: return ParamUtil.get(req, param, defaultValue);
116: } else {
117: return ParamUtil.get(req, param, beanValue.intValue());
118: }
119: }
120:
121: public static long getLong(Object bean, ServletRequest req,
122: String param) {
123: return getLong(bean, req, param, GetterUtil.DEFAULT_LONG);
124: }
125:
126: public static long getLong(Object bean, ServletRequest req,
127: String param, long defaultValue) {
128:
129: Long beanValue = null;
130:
131: if (bean != null) {
132: try {
133: beanValue = (Long) PropertyUtils.getSimpleProperty(
134: bean, param);
135: } catch (Exception e) {
136: _log.error(param + " " + e.getMessage());
137: }
138: }
139:
140: if (beanValue == null) {
141: return ParamUtil.get(req, param, defaultValue);
142: } else {
143: return ParamUtil.get(req, param, beanValue.longValue());
144: }
145: }
146:
147: public static String getString(Object bean, ServletRequest req,
148: String param) {
149:
150: return getString(bean, req, param, GetterUtil.DEFAULT_STRING);
151: }
152:
153: public static String getString(Object bean, ServletRequest req,
154: String param, String defaultValue) {
155:
156: String beanValue = null;
157:
158: if (bean != null) {
159: try {
160: beanValue = (String) PropertyUtils.getSimpleProperty(
161: bean, param);
162: } catch (Exception e) {
163: _log.error(param + " " + e.getMessage());
164: }
165: }
166:
167: if (beanValue == null) {
168: return ParamUtil.get(req, param, defaultValue);
169: } else {
170: return ParamUtil.get(req, param, beanValue);
171: }
172: }
173:
174: private static Log _log = LogFactory.getLog(BeanParamUtil.class);
175:
176: }
|