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:
025: import org.apache.commons.beanutils.PropertyUtils;
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028:
029: /**
030: * <a href="BeanUtil.java.html"><b><i>View Source</i></b></a>
031: *
032: * @author Brian Wing Shun Chan
033: *
034: */
035: public class BeanUtil {
036:
037: public static boolean getBoolean(Object bean, String param) {
038: return getBoolean(bean, param, GetterUtil.DEFAULT_BOOLEAN);
039: }
040:
041: public static boolean getBoolean(Object bean, String param,
042: boolean defaultValue) {
043:
044: Boolean beanValue = null;
045:
046: if (bean != null) {
047: try {
048: beanValue = (Boolean) PropertyUtils.getSimpleProperty(
049: bean, param);
050: } catch (Exception e) {
051: _log.error(e);
052: }
053: }
054:
055: if (beanValue == null) {
056: return defaultValue;
057: } else {
058: return beanValue.booleanValue();
059: }
060: }
061:
062: public static double getDouble(Object bean, String param) {
063: return getDouble(bean, param, GetterUtil.DEFAULT_DOUBLE);
064: }
065:
066: public static double getDouble(Object bean, String param,
067: double defaultValue) {
068:
069: Double beanValue = null;
070:
071: if (bean != null) {
072: try {
073: beanValue = (Double) PropertyUtils.getSimpleProperty(
074: bean, param);
075: } catch (Exception e) {
076: _log.error(e);
077: }
078: }
079:
080: if (beanValue == null) {
081: return defaultValue;
082: } else {
083: return beanValue.doubleValue();
084: }
085: }
086:
087: public static int getInteger(Object bean, String param) {
088: return getInteger(bean, param, GetterUtil.DEFAULT_INTEGER);
089: }
090:
091: public static int getInteger(Object bean, String param,
092: int defaultValue) {
093:
094: Integer beanValue = null;
095:
096: if (bean != null) {
097: try {
098: beanValue = (Integer) PropertyUtils.getSimpleProperty(
099: bean, param);
100: } catch (Exception e) {
101: _log.error(e);
102: }
103: }
104:
105: if (beanValue == null) {
106: return defaultValue;
107: } else {
108: return beanValue.intValue();
109: }
110: }
111:
112: public static long getLong(Object bean, String param) {
113: return getLong(bean, param, GetterUtil.DEFAULT_LONG);
114: }
115:
116: public static long getLong(Object bean, String param,
117: long defaultValue) {
118:
119: Long beanValue = null;
120:
121: if (bean != null) {
122: try {
123: beanValue = (Long) PropertyUtils.getSimpleProperty(
124: bean, param);
125: } catch (Exception e) {
126: _log.error(e);
127: }
128: }
129:
130: if (beanValue == null) {
131: return defaultValue;
132: } else {
133: return beanValue.longValue();
134: }
135: }
136:
137: public static Object getObject(Object bean, String param) {
138: Object beanValue = null;
139:
140: if (bean != null) {
141: try {
142: beanValue = PropertyUtils
143: .getSimpleProperty(bean, param);
144: } catch (Exception e) {
145: _log.error(e);
146: }
147: }
148:
149: return beanValue;
150: }
151:
152: public static String getString(Object bean, String param) {
153: return getString(bean, param, GetterUtil.DEFAULT_STRING);
154: }
155:
156: public static String getString(Object bean, String param,
157: String defaultValue) {
158:
159: String beanValue = null;
160:
161: if (bean != null) {
162: try {
163: beanValue = (String) PropertyUtils.getSimpleProperty(
164: bean, param);
165: } catch (Exception e) {
166: _log.error(e);
167: }
168: }
169:
170: if (beanValue == null) {
171: return defaultValue;
172: } else {
173: return beanValue;
174: }
175: }
176:
177: private static Log _log = LogFactory.getLog(BeanUtil.class);
178:
179: }
|