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