1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.docsearch;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.core.api.search.SearchOperator;
20 import org.kuali.rice.core.framework.persistence.jdbc.sql.SQLUtils;
21 import org.kuali.rice.kew.api.KewApiConstants;
22 import org.kuali.rice.kew.api.document.attribute.DocumentAttributeDecimal;
23 import org.kuali.rice.kew.api.document.attribute.DocumentAttributeFactory;
24
25 import javax.persistence.AttributeOverride;
26 import javax.persistence.AttributeOverrides;
27 import javax.persistence.Column;
28 import javax.persistence.Entity;
29 import javax.persistence.Inheritance;
30 import javax.persistence.InheritanceType;
31 import javax.persistence.NamedQueries;
32 import javax.persistence.NamedQuery;
33 import javax.persistence.Table;
34 import java.io.Serializable;
35 import java.math.BigDecimal;
36 import java.sql.ResultSet;
37 import java.sql.SQLException;
38 import java.text.DecimalFormat;
39 import java.text.NumberFormat;
40 import java.util.Arrays;
41 import java.util.List;
42 import java.util.regex.Matcher;
43 import java.util.regex.Pattern;
44
45
46
47
48
49 @Entity
50 @Inheritance(strategy= InheritanceType.TABLE_PER_CLASS)
51 @Table(name="KREW_DOC_HDR_EXT_FLT_T")
52 @NamedQueries({
53 @NamedQuery(name="SearchableAttributeFloatValue.FindByDocumentId", query="select s from "
54 + "SearchableAttributeFloatValue as s where s.documentId = :documentId"),
55 @NamedQuery(name="SearchableAttributeFloatValue.FindByKey", query="select s from "
56 + "SearchableAttributeFloatValue as s where s.documentId = :documentId and "
57 + "s.searchableAttributeKey = :searchableAttributeKey")
58 })
59 @AttributeOverrides({
60 @AttributeOverride(name="searchableAttributeValueId", column=@Column(name="DOC_HDR_EXT_FLT_ID"))
61 })
62 public class SearchableAttributeFloatValue extends SearchableAttributeBase implements SearchableAttributeValue, Serializable {
63
64 private static final long serialVersionUID = -6682101853805320760L;
65
66 private static final String ATTRIBUTE_DATABASE_TABLE_NAME = "KREW_DOC_HDR_EXT_FLT_T";
67 private static final boolean DEFAULT_WILDCARD_ALLOWANCE_POLICY = false;
68 private static final boolean ALLOWS_RANGE_SEARCH = true;
69 private static final boolean ALLOWS_CASE_INSENSITIVE_SEARCH = false;
70 private static final String DEFAULT_VALIDATION_REGEX_EXPRESSION = "[-+]?[0-9]*\\.?[0-9]+";
71 private static final String ATTRIBUTE_XML_REPRESENTATION = KewApiConstants.SearchableAttributeConstants.DATA_TYPE_FLOAT;
72 private static final String DEFAULT_FORMAT_PATTERN = "";
73
74 @Column(name="VAL")
75 private BigDecimal searchableAttributeValue;
76
77
78
79
80 public SearchableAttributeFloatValue() {
81 super();
82 this.ojbConcreteClass = this.getClass().getName();
83 }
84
85 @Override
86 public void setupAttributeValue(String value) {
87 this.setSearchableAttributeValue(convertStringToBigDecimal(value));
88 }
89
90 private BigDecimal convertStringToBigDecimal(String value) {
91 if (org.apache.commons.lang.StringUtils.isEmpty(value)) {
92 return null;
93 } else {
94 return new BigDecimal(value);
95 }
96 }
97
98 @Override
99 public void setupAttributeValue(ResultSet resultSet, String columnName) throws SQLException {
100 this.setSearchableAttributeValue(resultSet.getBigDecimal(columnName));
101 }
102
103 @Override
104 public String getSearchableAttributeDisplayValue() {
105 NumberFormat format = DecimalFormat.getInstance();
106 ((DecimalFormat)format).toPattern();
107 ((DecimalFormat)format).applyPattern(DEFAULT_FORMAT_PATTERN);
108 return format.format(getSearchableAttributeValue().doubleValue());
109 }
110
111 @Override
112 public String getAttributeDataType() {
113 return ATTRIBUTE_XML_REPRESENTATION;
114 }
115
116 @Override
117 public String getAttributeTableName() {
118 return ATTRIBUTE_DATABASE_TABLE_NAME;
119 }
120
121 @Override
122 public boolean allowsWildcards() {
123 return DEFAULT_WILDCARD_ALLOWANCE_POLICY;
124 }
125
126 @Override
127 public boolean allowsCaseInsensitivity() {
128 return ALLOWS_CASE_INSENSITIVE_SEARCH;
129 }
130
131 @Override
132 public boolean allowsRangeSearches() {
133 return ALLOWS_RANGE_SEARCH;
134 }
135
136 @Override
137 public boolean isPassesDefaultValidation(String valueEntered) {
138
139 boolean bRet = true;
140 boolean bSplit = false;
141
142 if (StringUtils.contains(valueEntered, SearchOperator.BETWEEN.op())) {
143 List<String> l = Arrays.asList(valueEntered.split("\\.\\."));
144 for(String value : l){
145 bSplit = true;
146 if(!isPassesDefaultValidation(value)){
147 bRet = false;
148 }
149 }
150 }
151 if (StringUtils.contains(valueEntered, SearchOperator.OR.op())) {
152
153 List<String> l = Arrays.asList(StringUtils.split(valueEntered, SearchOperator.OR.op()));
154 for(String value : l){
155 bSplit = true;
156 if(!isPassesDefaultValidation(value)){
157 bRet = false;
158 }
159 }
160 }
161 if (StringUtils.contains(valueEntered, SearchOperator.AND.op())) {
162
163 List<String> l = Arrays.asList(StringUtils.split(valueEntered, SearchOperator.AND.op()));
164 for(String value : l){
165 bSplit = true;
166 if(!isPassesDefaultValidation(value)){
167 bRet = false;
168 }
169 }
170 }
171
172 if(bSplit){
173 return bRet;
174 }
175
176 Pattern pattern = Pattern.compile(DEFAULT_VALIDATION_REGEX_EXPRESSION);
177 Matcher matcher = pattern.matcher(SQLUtils.cleanNumericOfValidOperators(valueEntered).trim());
178 if(!matcher.matches()){
179 bRet = false;
180 }
181
182 return bRet;
183
184 }
185
186 @Override
187 public Boolean isRangeValid(String lowerValue, String upperValue) {
188 if (allowsRangeSearches()) {
189 BigDecimal lower = null;
190 BigDecimal upper = null;
191 try{
192 lower = convertStringToBigDecimal(lowerValue);
193 upper = convertStringToBigDecimal(upperValue);
194 }catch(NumberFormatException ex){
195 return false;
196 }
197 if ( (lower != null) && (upper != null) ) {
198 return (lower.compareTo(upper) <= 0);
199 }
200 return true;
201 }
202 return null;
203 }
204
205 @Override
206 public BigDecimal getSearchableAttributeValue() {
207 return searchableAttributeValue;
208 }
209
210 public void setSearchableAttributeValue(BigDecimal searchableAttributeValue) {
211 this.searchableAttributeValue = searchableAttributeValue;
212 }
213
214
215
216
217 @Deprecated
218 public void setSearchableAttributeValue(Float floatValueToTranslate) {
219 this.searchableAttributeValue = null;
220 if (floatValueToTranslate != null) {
221 this.searchableAttributeValue = new BigDecimal(floatValueToTranslate.toString());
222 }
223 }
224
225 @Override
226 public DocumentAttributeDecimal toDocumentAttribute() {
227 return DocumentAttributeFactory.createDecimalAttribute(getSearchableAttributeKey(), getSearchableAttributeValue());
228 }
229
230 }
231