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