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