1
22
23 package com.liferay.util;
24
25 import com.liferay.portal.kernel.util.GetterUtil;
26
27 import org.apache.commons.beanutils.PropertyUtils;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31
37 public class BeanUtil {
38
39 public static boolean getBoolean(Object bean, String param) {
40 return getBoolean(bean, param, GetterUtil.DEFAULT_BOOLEAN);
41 }
42
43 public static boolean getBoolean(
44 Object bean, String param, boolean defaultValue) {
45
46 Boolean beanValue = null;
47
48 if (bean != null) {
49 try {
50 beanValue =
51 (Boolean)PropertyUtils.getSimpleProperty(bean, param);
52 }
53 catch (Exception e) {
54 _log.error(e);
55 }
56 }
57
58 if (beanValue == null) {
59 return defaultValue;
60 }
61 else {
62 return beanValue.booleanValue();
63 }
64 }
65
66 public static double getDouble(Object bean, String param) {
67 return getDouble(bean, param, GetterUtil.DEFAULT_DOUBLE);
68 }
69
70 public static double getDouble(
71 Object bean, String param, double defaultValue) {
72
73 Double beanValue = null;
74
75 if (bean != null) {
76 try {
77 beanValue =
78 (Double)PropertyUtils.getSimpleProperty(bean, param);
79 }
80 catch (Exception e) {
81 _log.error(e);
82 }
83 }
84
85 if (beanValue == null) {
86 return defaultValue;
87 }
88 else {
89 return beanValue.doubleValue();
90 }
91 }
92
93 public static int getInteger(Object bean, String param) {
94 return getInteger(bean, param, GetterUtil.DEFAULT_INTEGER);
95 }
96
97 public static int getInteger(
98 Object bean, String param, int defaultValue) {
99
100 Integer beanValue = null;
101
102 if (bean != null) {
103 try {
104 beanValue =
105 (Integer)PropertyUtils.getSimpleProperty(bean, param);
106 }
107 catch (Exception e) {
108 _log.error(e);
109 }
110 }
111
112 if (beanValue == null) {
113 return defaultValue;
114 }
115 else {
116 return beanValue.intValue();
117 }
118 }
119
120 public static long getLong(Object bean, String param) {
121 return getLong(bean, param, GetterUtil.DEFAULT_LONG);
122 }
123
124 public static long getLong(
125 Object bean, String param, long defaultValue) {
126
127 Long beanValue = null;
128
129 if (bean != null) {
130 try {
131 beanValue =
132 (Long)PropertyUtils.getSimpleProperty(bean, param);
133 }
134 catch (Exception e) {
135 _log.error(e);
136 }
137 }
138
139 if (beanValue == null) {
140 return defaultValue;
141 }
142 else {
143 return beanValue.longValue();
144 }
145 }
146
147 public static Object getObject(Object bean, String param) {
148 Object beanValue = null;
149
150 if (bean != null) {
151 try {
152 beanValue = PropertyUtils.getSimpleProperty(bean, param);
153 }
154 catch (Exception e) {
155 _log.error(e);
156 }
157 }
158
159 return beanValue;
160 }
161
162 public static String getString(Object bean, String param) {
163 return getString(bean, param, GetterUtil.DEFAULT_STRING);
164 }
165
166 public static String getString(
167 Object bean, String param, String defaultValue) {
168
169 String beanValue = null;
170
171 if (bean != null) {
172 try {
173 beanValue =
174 (String)PropertyUtils.getSimpleProperty(bean, param);
175 }
176 catch (Exception e) {
177 _log.error(e);
178 }
179 }
180
181 if (beanValue == null) {
182 return defaultValue;
183 }
184 else {
185 return beanValue;
186 }
187 }
188
189 private static Log _log = LogFactory.getLog(BeanUtil.class);
190
191 }