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