1 /*
2 * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3 * the spring framework, http://el4j.sf.net
4 * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5 * 1000 Lausanne, Switzerland, http://www.elca.ch
6 *
7 * EL4J is published under the GNU Lesser General Public License (LGPL)
8 * Version 2.1. See http://www.gnu.org/licenses/
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * For alternative licensing, please contact info@elca.ch
16 */
17 package ch.elca.el4j.services.search.criterias;
18
19 import org.springframework.util.StringUtils;
20
21 import ch.elca.el4j.util.codingsupport.Reject;
22
23 /**
24 * Criteria to compare fields to values.
25 *
26 * @svnLink $Revision: 3873 $;$Date: 2009-08-04 13:59:45 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/main/java/ch/elca/el4j/services/search/criterias/ComparisonCriteria.java $
27 *
28 * @author Martin Zeltner (MZE)
29 */
30 public class ComparisonCriteria extends AbstractCriteria {
31 /**
32 * Is the type prefix.
33 */
34 public static final String TYPE_PREFIX = "comparison";
35
36 /**
37 * Is the compare operator.
38 */
39 private String m_operator;
40
41 /**
42 * Is the type of this criteria.
43 */
44 private String m_type;
45
46 /**
47 * Default constructor for remoting protocols like hessian added.
48 */
49 protected ComparisonCriteria() { }
50
51 /**
52 * Constructor to create new special comparison Criteria objects.
53 *
54 * @param field Is the field the criteria is made for.
55 * @param value Is the value of this criteria.
56 * @param operator Is the compare operator.
57 * @param typeSuffix is the type suffix of this criteria.
58 * (we use the unqualified class name of the basic Java types (
59 * Boolean,Integer, ...))
60 */
61 public ComparisonCriteria(String field, Object value, String operator,
62 String typeSuffix) {
63 super(field, value);
64 Reject.ifEmpty(field);
65 m_type = TYPE_PREFIX + StringUtils.capitalize(typeSuffix);
66 m_operator = operator;
67 }
68
69 /**
70 * @param field Is the field the criteria is made for.
71 * @param value Is the value of this criteria.
72 * @return Returns an equals comparison criteria.
73 */
74 public static ComparisonCriteria equals(String field, boolean value) {
75 return new ComparisonCriteria(
76 field, Boolean.valueOf(value), "=", "Boolean");
77 }
78
79 /**
80 * @param field Is the field the criteria is made for.
81 * @param value Is the value of this criteria.
82 * @return Returns an equals comparison criteria.
83 */
84 public static ComparisonCriteria equals(String field, int value) {
85 return new ComparisonCriteria(
86 field, Integer.valueOf(value), "=", "Integer");
87 }
88
89 /**
90 * @param field Is the field the criteria is made for.
91 * @param value Is the value of this criteria.
92 * @return Returns an equals comparison criteria.
93 */
94 public static ComparisonCriteria equals(String field, long value) {
95 return new ComparisonCriteria(
96 field, Long.valueOf(value), "=", "Long");
97 }
98
99 /**
100 * @param field Is the field the criteria is made for.
101 * @param value Is the value of this criteria.
102 * @return Returns an equals comparison criteria.
103 */
104 public static ComparisonCriteria equals(String field, short value) {
105 return new ComparisonCriteria(
106 field, Short.valueOf(value), "=", "Short");
107 }
108
109 /**
110 * @param field Is the field the criteria is made for.
111 * @param value Is the value of this criteria.
112 * @return Returns an equals comparison criteria.
113 */
114 public static ComparisonCriteria equals(String field, byte value) {
115 return new ComparisonCriteria(
116 field, Byte.valueOf(value), "=", "Byte");
117 }
118
119 /**
120 * @param field Is the field the criteria is made for.
121 * @param value Is the value of this criteria.
122 * @return Returns an equals comparison criteria.
123 */
124 public static ComparisonCriteria equals(String field, double value) {
125 return new ComparisonCriteria(
126 field, new Double(value), "=", "Double");
127 }
128
129 /**
130 * @param field Is the field the criteria is made for.
131 * @param value Is the value of this criteria.
132 * @return Returns an equals comparison criteria.
133 */
134 public static ComparisonCriteria equals(String field, Enum<?> value) {
135 return new ComparisonCriteria(
136 field, value, "=", "Enum");
137 }
138
139 /**
140 * @param field Is the field the criteria is made for.
141 * @param value Is the value of this criteria.
142 * @return Returns an equals comparison criteria.
143 */
144 public static ComparisonCriteria equals(String field, float value) {
145 return new ComparisonCriteria(
146 field, new Float(value), "=", "Float");
147 }
148
149 /**
150 * @param field Is the field the criteria is made for.
151 * @param value Is the value of this criteria.
152 * @return Returns an equals comparison criteria.
153 */
154 public static ComparisonCriteria equals(String field, String value) {
155 return new ComparisonCriteria(
156 field, value, "=", "String");
157 }
158
159 /**
160 * @param field Is the field the criteria is made for.
161 * @param value Is the value of this criteria.
162 * @return Returns an equals comparison criteria.
163 */
164 public static ComparisonCriteria equalsObject(String field, Object value) {
165 return new ComparisonCriteria(
166 field, value, "=", "Object");
167 }
168
169 /**
170 * @return Returns the compare operator.
171 */
172 public String getOperator() {
173 return m_operator;
174 }
175
176 /**
177 * {@inheritDoc}
178 */
179 public String getType() {
180 return m_type;
181 }
182
183 /**
184 * {@inheritDoc}
185 */
186 @Override
187 public String toString() {
188 return getSqlWhereCondition();
189 }
190
191 /**
192 * {@inheritDoc}
193 */
194 public String getSqlWhereCondition() {
195 return " ( "+getField()+" "+getOperator()+" "+getValue()+ " ) ";
196 }
197
198 }