1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.kew.docsearch; |
18 | |
|
19 | |
import java.math.BigDecimal; |
20 | |
import java.sql.ResultSet; |
21 | |
import java.sql.SQLException; |
22 | |
import java.text.DecimalFormat; |
23 | |
import java.text.NumberFormat; |
24 | |
import java.util.Arrays; |
25 | |
import java.util.List; |
26 | |
import java.util.regex.Matcher; |
27 | |
import java.util.regex.Pattern; |
28 | |
|
29 | |
import javax.persistence.CascadeType; |
30 | |
import javax.persistence.Column; |
31 | |
import javax.persistence.Entity; |
32 | |
import javax.persistence.FetchType; |
33 | |
import javax.persistence.Id; |
34 | |
import javax.persistence.JoinColumn; |
35 | |
import javax.persistence.ManyToOne; |
36 | |
import javax.persistence.NamedQueries; |
37 | |
import javax.persistence.NamedQuery; |
38 | |
import javax.persistence.PrePersist; |
39 | |
import javax.persistence.Table; |
40 | |
import javax.persistence.Transient; |
41 | |
|
42 | |
import org.apache.commons.lang.StringUtils; |
43 | |
import org.kuali.rice.core.jdbc.SqlBuilder; |
44 | |
import org.kuali.rice.core.jpa.annotations.Sequence; |
45 | |
import org.kuali.rice.core.util.OrmUtils; |
46 | |
import org.kuali.rice.kew.bo.WorkflowPersistable; |
47 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
48 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
49 | |
import org.kuali.rice.kew.util.Utilities; |
50 | |
import org.kuali.rice.kns.util.KNSConstants; |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | 0 | @Entity |
57 | |
@Table(name="KREW_DOC_HDR_EXT_FLT_T") |
58 | |
@Sequence(name="KREW_SRCH_ATTR_S",property="searchableAttributeValueId") |
59 | |
@NamedQueries({ |
60 | |
@NamedQuery(name="SearchableAttributeFloatValue.FindByRouteHeaderId", query="select s from SearchableAttributeFloatValue as s where s.routeHeaderId = :routeHeaderId"), |
61 | |
@NamedQuery(name="SearchableAttributeFloatValue.FindByKey", query="select s from SearchableAttributeFloatValue as s where s.routeHeaderId = :routeHeaderId and s.searchableAttributeKey = :searchableAttributeKey") |
62 | |
}) |
63 | |
public class SearchableAttributeFloatValue implements WorkflowPersistable, SearchableAttributeValue { |
64 | |
|
65 | |
private static final long serialVersionUID = -6682101853805320760L; |
66 | |
|
67 | |
private static final String ATTRIBUTE_DATABASE_TABLE_NAME = "KREW_DOC_HDR_EXT_FLT_T"; |
68 | |
private static final boolean DEFAULT_WILDCARD_ALLOWANCE_POLICY = false; |
69 | |
private static final boolean ALLOWS_RANGE_SEARCH = true; |
70 | |
private static final boolean ALLOWS_CASE_INSENSITIVE_SEARCH = false; |
71 | |
private static final String DEFAULT_VALIDATION_REGEX_EXPRESSION = "[-+]?[0-9]*\\.?[0-9]+"; |
72 | |
private static final String ATTRIBUTE_XML_REPRESENTATION = SearchableAttribute.DATA_TYPE_FLOAT; |
73 | |
private static final String DEFAULT_FORMAT_PATTERN = ""; |
74 | |
|
75 | |
@Id |
76 | |
@Column(name="DOC_HDR_EXT_FLT_ID") |
77 | |
private Long searchableAttributeValueId; |
78 | |
@Column(name="KEY_CD") |
79 | |
private String searchableAttributeKey; |
80 | |
@Column(name="VAL") |
81 | |
private BigDecimal searchableAttributeValue; |
82 | |
@Transient |
83 | |
protected String ojbConcreteClass; |
84 | |
|
85 | |
@Column(name="DOC_HDR_ID") |
86 | |
private Long routeHeaderId; |
87 | |
@ManyToOne(fetch=FetchType.EAGER, cascade={CascadeType.PERSIST}) |
88 | |
@JoinColumn(name="DOC_HDR_ID", insertable=false, updatable=false) |
89 | |
private DocumentRouteHeaderValue routeHeader; |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
public SearchableAttributeFloatValue() { |
95 | 0 | super(); |
96 | 0 | this.ojbConcreteClass = this.getClass().getName(); |
97 | 0 | } |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
public void setupAttributeValue(String value) { |
103 | 0 | this.setSearchableAttributeValue(convertStringToBigDecimal(value)); |
104 | 0 | } |
105 | |
|
106 | |
private BigDecimal convertStringToBigDecimal(String value) { |
107 | 0 | if (Utilities.isEmpty(value)) { |
108 | 0 | return null; |
109 | |
} else { |
110 | 0 | return new BigDecimal(value); |
111 | |
} |
112 | |
} |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
public void setupAttributeValue(ResultSet resultSet, String columnName) throws SQLException { |
118 | 0 | this.setSearchableAttributeValue(resultSet.getBigDecimal(columnName)); |
119 | 0 | } |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
public String getSearchableAttributeDisplayValue() { |
125 | 0 | NumberFormat format = DecimalFormat.getInstance(); |
126 | 0 | ((DecimalFormat)format).toPattern(); |
127 | 0 | ((DecimalFormat)format).applyPattern(DEFAULT_FORMAT_PATTERN); |
128 | 0 | return format.format(getSearchableAttributeValue().doubleValue()); |
129 | |
} |
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
public String getAttributeDataType() { |
135 | 0 | return ATTRIBUTE_XML_REPRESENTATION; |
136 | |
} |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
public String getAttributeTableName() { |
142 | 0 | return ATTRIBUTE_DATABASE_TABLE_NAME; |
143 | |
} |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
public boolean allowsWildcards() { |
149 | 0 | return DEFAULT_WILDCARD_ALLOWANCE_POLICY; |
150 | |
} |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
public boolean allowsCaseInsensitivity() { |
156 | 0 | return ALLOWS_CASE_INSENSITIVE_SEARCH; |
157 | |
} |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
public boolean allowsRangeSearches() { |
163 | 0 | return ALLOWS_RANGE_SEARCH; |
164 | |
} |
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
public boolean isPassesDefaultValidation(String valueEntered) { |
170 | |
|
171 | 0 | boolean bRet = true; |
172 | 0 | boolean bSplit = false; |
173 | |
|
174 | 0 | if (StringUtils.contains(valueEntered, KNSConstants.BETWEEN_OPERATOR)) { |
175 | 0 | List<String> l = Arrays.asList(valueEntered.split("\\.\\.")); |
176 | 0 | for(String value : l){ |
177 | 0 | bSplit = true; |
178 | 0 | if(!isPassesDefaultValidation(value)){ |
179 | 0 | bRet = false; |
180 | |
} |
181 | |
} |
182 | |
} |
183 | 0 | if (StringUtils.contains(valueEntered, KNSConstants.OR_LOGICAL_OPERATOR)) { |
184 | |
|
185 | 0 | List<String> l = Arrays.asList(StringUtils.split(valueEntered, KNSConstants.OR_LOGICAL_OPERATOR)); |
186 | 0 | for(String value : l){ |
187 | 0 | bSplit = true; |
188 | 0 | if(!isPassesDefaultValidation(value)){ |
189 | 0 | bRet = false; |
190 | |
} |
191 | |
} |
192 | |
} |
193 | 0 | if (StringUtils.contains(valueEntered, KNSConstants.AND_LOGICAL_OPERATOR)) { |
194 | |
|
195 | 0 | List<String> l = Arrays.asList(StringUtils.split(valueEntered, KNSConstants.AND_LOGICAL_OPERATOR)); |
196 | 0 | for(String value : l){ |
197 | 0 | bSplit = true; |
198 | 0 | if(!isPassesDefaultValidation(value)){ |
199 | 0 | bRet = false; |
200 | |
} |
201 | |
} |
202 | |
} |
203 | |
|
204 | 0 | if(bSplit){ |
205 | 0 | return bRet; |
206 | |
} |
207 | |
|
208 | 0 | Pattern pattern = Pattern.compile(DEFAULT_VALIDATION_REGEX_EXPRESSION); |
209 | 0 | Matcher matcher = pattern.matcher(SqlBuilder.cleanNumericOfValidOperators(valueEntered).trim()); |
210 | 0 | if(!matcher.matches()){ |
211 | 0 | bRet = false; |
212 | |
} |
213 | |
|
214 | 0 | return bRet; |
215 | |
|
216 | |
} |
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
public Boolean isRangeValid(String lowerValue, String upperValue) { |
222 | 0 | if (allowsRangeSearches()) { |
223 | 0 | BigDecimal lower = null; |
224 | 0 | BigDecimal upper = null; |
225 | |
try{ |
226 | 0 | lower = convertStringToBigDecimal(lowerValue); |
227 | 0 | upper = convertStringToBigDecimal(upperValue); |
228 | 0 | }catch(NumberFormatException ex){ |
229 | 0 | return false; |
230 | 0 | } |
231 | 0 | if ( (lower != null) && (upper != null) ) { |
232 | 0 | return (lower.compareTo(upper) <= 0); |
233 | |
} |
234 | 0 | return true; |
235 | |
} |
236 | 0 | return null; |
237 | |
} |
238 | |
|
239 | |
public String getOjbConcreteClass() { |
240 | 0 | return ojbConcreteClass; |
241 | |
} |
242 | |
|
243 | |
public void setOjbConcreteClass(String ojbConcreteClass) { |
244 | 0 | this.ojbConcreteClass = ojbConcreteClass; |
245 | 0 | } |
246 | |
|
247 | |
public DocumentRouteHeaderValue getRouteHeader() { |
248 | 0 | return routeHeader; |
249 | |
} |
250 | |
|
251 | |
public void setRouteHeader(DocumentRouteHeaderValue routeHeader) { |
252 | 0 | this.routeHeader = routeHeader; |
253 | 0 | } |
254 | |
|
255 | |
public Long getRouteHeaderId() { |
256 | 0 | return routeHeaderId; |
257 | |
} |
258 | |
|
259 | |
public void setRouteHeaderId(Long routeHeaderId) { |
260 | 0 | this.routeHeaderId = routeHeaderId; |
261 | 0 | } |
262 | |
|
263 | |
public String getSearchableAttributeKey() { |
264 | 0 | return searchableAttributeKey; |
265 | |
} |
266 | |
|
267 | |
public void setSearchableAttributeKey(String searchableAttributeKey) { |
268 | 0 | this.searchableAttributeKey = searchableAttributeKey; |
269 | 0 | } |
270 | |
|
271 | |
public BigDecimal getSearchableAttributeValue() { |
272 | 0 | return searchableAttributeValue; |
273 | |
} |
274 | |
|
275 | |
public void setSearchableAttributeValue(BigDecimal searchableAttributeValue) { |
276 | 0 | this.searchableAttributeValue = searchableAttributeValue; |
277 | 0 | } |
278 | |
|
279 | |
|
280 | |
|
281 | |
|
282 | |
public void setSearchableAttributeValue(Float floatValueToTranslate) { |
283 | 0 | this.searchableAttributeValue = null; |
284 | 0 | if (floatValueToTranslate != null) { |
285 | 0 | this.searchableAttributeValue = new BigDecimal(floatValueToTranslate.toString()); |
286 | |
} |
287 | 0 | } |
288 | |
|
289 | |
public Long getSearchableAttributeValueId() { |
290 | 0 | return searchableAttributeValueId; |
291 | |
} |
292 | |
|
293 | |
public void setSearchableAttributeValueId(Long searchableAttributeValueId) { |
294 | 0 | this.searchableAttributeValueId = searchableAttributeValueId; |
295 | 0 | } |
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
@Deprecated |
301 | |
public Object copy(boolean preserveKeys) { |
302 | 0 | return null; |
303 | |
} |
304 | |
|
305 | |
@PrePersist |
306 | |
public void beforeInsert(){ |
307 | 0 | OrmUtils.populateAutoIncValue(this, KEWServiceLocator.getEntityManagerFactory().createEntityManager()); |
308 | 0 | } |
309 | |
|
310 | |
} |
311 | |
|