01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: QueryHelper.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.database.queries;
09:
10: import com.uwyn.rife.database.Datasource;
11: import com.uwyn.rife.database.exceptions.BeanException;
12: import com.uwyn.rife.database.exceptions.DbQueryException;
13: import com.uwyn.rife.database.types.SqlNull;
14: import com.uwyn.rife.tools.BeanUtils;
15: import com.uwyn.rife.tools.exceptions.BeanUtilsException;
16: import java.util.LinkedHashMap;
17: import java.util.Map;
18: import java.util.Set;
19:
20: public abstract class QueryHelper {
21: public static Set<String> getBeanPropertyNames(Class beanClass,
22: String[] excludedFields) throws DbQueryException {
23: try {
24: return BeanUtils.getPropertyNames(beanClass, null,
25: excludedFields, null);
26: } catch (BeanUtilsException e) {
27: throw new BeanException(
28: "Error while obtaining bean property names.",
29: beanClass, e);
30: }
31: }
32:
33: public static Map<String, String> getBeanPropertyValues(
34: Object bean, String[] includedFields,
35: String[] excludedFields, Datasource datasource)
36: throws DbQueryException {
37: Map<String, String> property_values_sql = new LinkedHashMap<String, String>();
38:
39: if (bean != null) {
40: Map<String, Object> property_values = null;
41: Set<Map.Entry<String, Object>> property_values_entries = null;
42:
43: try {
44: property_values = BeanUtils.getPropertyValues(bean,
45: includedFields, excludedFields, null);
46: } catch (BeanUtilsException e) {
47: throw new BeanException(
48: "Error while obtaining bean property values.",
49: bean.getClass(), e);
50: }
51: property_values_entries = property_values.entrySet();
52:
53: // convert the property values to strings that are usable in sql statements
54: for (Map.Entry<String, Object> property_value_entry : property_values_entries) {
55: // exclude null values
56: if (property_value_entry.getValue() != null) {
57: String value = datasource.getSqlConversion()
58: .getSqlValue(
59: property_value_entry.getValue());
60: // exclude some more null values
61: if (!value.equals(SqlNull.NULL.toString())) {
62: property_values_sql.put(property_value_entry
63: .getKey(), value);
64: }
65: }
66: }
67: }
68:
69: assert property_values_sql != null;
70:
71: return property_values_sql;
72: }
73:
74: public static Map<String, Class> getBeanPropertyTypes(
75: Class beanClass, String[] includedFields,
76: String[] excludedFields) throws DbQueryException {
77: try {
78: return BeanUtils.getPropertyTypes(beanClass,
79: includedFields, excludedFields, null);
80: } catch (BeanUtilsException e) {
81: throw new BeanException(
82: "Error while obtaining bean property types.",
83: beanClass, e);
84: }
85: }
86: }
|