1 /*
2 * Copyright 2003 - 2013 The eFaps Team
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * Revision: $Rev$
17 * Last Changed: $Date$
18 * Last Changed By: $Author$
19 */
20
21 package org.efaps.admin.datamodel.attributetype;
22
23 import java.sql.SQLException;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.efaps.admin.datamodel.Attribute;
28 import org.efaps.db.wrapper.AbstractSQLInsertUpdate;
29
30 /**
31 * Implements the mapping between values in the database and {@link Boolean}
32 * values in eFaps. Internally in the database the boolean value could be
33 * implemented native as boolean or as a number value. If the database uses
34 * number values all values which are not <code>null</code> and not zero are
35 * interpreted from eFaps as <i>true</i>.
36 *
37 * @author The eFaps Team
38 * @version $Id$
39 */
40 public class BooleanType
41 extends AbstractType
42 {
43 /**
44 * Needed for serialization.
45 */
46 private static final long serialVersionUID = 1L;
47
48 /**
49 * Converts given <code>_value</code> into a {@link Boolean} value and set
50 * for the {@link Attribute#getSqlColNames() SQL columns} in
51 * <code>_attribute</code> this value in <code>_insertUpdate</code> SQL
52 * statement.
53 *
54 * @param _insertUpdate SQL insert / update statement
55 * @param _attribute SQL update statement
56 * @param _value new object value to set; values are localized and
57 * are coming from the user interface
58 * @throws SQLException if size of the SQL columns of
59 * <code>_attribute</code> is not correct
60 */
61 @Override
62 public void prepare(final AbstractSQLInsertUpdate<?> _insertUpdate,
63 final Attribute _attribute,
64 final Object... _value)
65 throws SQLException
66 {
67 checkSQLColumnSize(_attribute, 1);
68 _insertUpdate.column(_attribute.getSqlColNames().get(0), eval(_value));
69 }
70
71 /**
72 * Evaluates given <code>_value</code> and converts them to a
73 * {@link Boolean} value.
74 *
75 * @param _value value to evaluate
76 * @return related evaluated boolean value
77 */
78 protected Boolean eval(final Object... _value)
79 {
80 final Boolean ret;
81 if (_value == null) {
82 ret = null;
83 } else if (_value[0] instanceof String) {
84 ret = Boolean.valueOf((String) _value[0]);
85 } else if (_value[0] instanceof Boolean) {
86 ret = (Boolean) _value[0];
87 } else {
88 ret = false;
89 }
90 return ret;
91 }
92
93 /**
94 * Evaluates the <code>_objectList</code> and interprets this value as
95 * boolean.
96 *
97 * @param _attribute related eFaps attribute definition; not used
98 * @param _objectList list of values for the attribute definition
99 * @return related interpreted boolean value if object list is
100 * {@link Boolean} or {@link Number}; otherwise <code>null</code>
101 * is returned
102 */
103 public Object readValue(final Attribute _attribute,
104 final List<Object> _objectList)
105 {
106 Object ret = null;
107 if (_objectList.size() < 1) {
108 ret = null;
109 } else if (_objectList.size() > 1) {
110 final List<Object> list = new ArrayList<Object>();
111 for (final Object object : _objectList) {
112 Object obj = Boolean.FALSE;
113 if (object instanceof Boolean) {
114 obj = object;
115 } else if (object instanceof Number) {
116 final Integer intvalue = ((Number) object).intValue();
117 if ((intvalue != null) && (intvalue != 0)) {
118 obj = Boolean.TRUE;
119 } else {
120 obj = Boolean.FALSE;
121 }
122 }
123 list.add(obj);
124 }
125 ret = list;
126 } else {
127 final Object object = _objectList.get(0);
128 if (object instanceof Boolean) {
129 ret = object;
130 } else if (object instanceof Number) {
131 final Integer intvalue = ((Number) object).intValue();
132 if ((intvalue != null) && (intvalue != 0)) {
133 ret = Boolean.TRUE;
134 } else {
135 ret = Boolean.FALSE;
136 }
137 }
138 }
139 return ret;
140 }
141 }