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 | 0 | @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 | 0 | super(); |
100 | 0 | this.ojbConcreteClass = this.getClass().getName(); |
101 | 0 | } |
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
public void setupAttributeValue(String value) { |
107 | 0 | this.setSearchableAttributeValue(convertStringToLong(value)); |
108 | 0 | } |
109 | |
|
110 | |
private Long convertStringToLong(String value) { |
111 | 0 | if (org.apache.commons.lang.StringUtils.isEmpty(value)) { |
112 | 0 | return null; |
113 | |
} else { |
114 | 0 | return Long.valueOf(value); |
115 | |
} |
116 | |
} |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
public void setupAttributeValue(ResultSet resultSet, String columnName) throws SQLException { |
122 | 0 | this.setSearchableAttributeValue(resultSet.getLong(columnName)); |
123 | 0 | } |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
public String getSearchableAttributeDisplayValue() { |
129 | 0 | NumberFormat format = DecimalFormat.getInstance(); |
130 | 0 | ((DecimalFormat)format).applyPattern(DEFAULT_FORMAT_PATTERN); |
131 | 0 | return format.format(getSearchableAttributeValue().longValue()); |
132 | |
} |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public String getAttributeDataType() { |
138 | 0 | return ATTRIBUTE_XML_REPRESENTATION; |
139 | |
} |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
public String getAttributeTableName() { |
145 | 0 | return ATTRIBUTE_DATABASE_TABLE_NAME; |
146 | |
} |
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
public boolean allowsWildcards() { |
152 | 0 | return DEFAULT_WILDCARD_ALLOWANCE_POLICY; |
153 | |
} |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
public boolean allowsCaseInsensitivity() { |
159 | 0 | return ALLOWS_CASE_INSENSITIVE_SEARCH; |
160 | |
} |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
public boolean allowsRangeSearches() { |
166 | 0 | return ALLOWS_RANGE_SEARCH; |
167 | |
} |
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
public boolean isPassesDefaultValidation(String valueEntered) { |
173 | |
|
174 | 0 | boolean bRet = true; |
175 | 0 | boolean bSplit = false; |
176 | |
|
177 | 0 | if (StringUtils.contains(valueEntered, SearchOperator.BETWEEN.op())) { |
178 | 0 | List<String> l = Arrays.asList(valueEntered.split("\\.\\.")); |
179 | 0 | for(String value : l){ |
180 | 0 | bSplit = true; |
181 | 0 | if(!isPassesDefaultValidation(value)){ |
182 | 0 | bRet = false; |
183 | |
} |
184 | |
} |
185 | |
} |
186 | 0 | if (StringUtils.contains(valueEntered, SearchOperator.OR.op())) { |
187 | |
|
188 | 0 | List<String> l = Arrays.asList(StringUtils.split(valueEntered, SearchOperator.OR.op())); |
189 | 0 | for(String value : l){ |
190 | 0 | bSplit = true; |
191 | 0 | if(!isPassesDefaultValidation(value)){ |
192 | 0 | bRet = false; |
193 | |
} |
194 | |
} |
195 | |
} |
196 | 0 | if (StringUtils.contains(valueEntered, SearchOperator.AND.op())) { |
197 | |
|
198 | 0 | List<String> l = Arrays.asList(StringUtils.split(valueEntered, SearchOperator.AND.op())); |
199 | 0 | for(String value : l){ |
200 | 0 | bSplit = true; |
201 | 0 | if(!isPassesDefaultValidation(value)){ |
202 | 0 | bRet = false; |
203 | |
} |
204 | |
} |
205 | |
} |
206 | |
|
207 | 0 | if(bSplit){ |
208 | 0 | return bRet; |
209 | |
} |
210 | |
|
211 | 0 | Pattern pattern = Pattern.compile(DEFAULT_VALIDATION_REGEX_EXPRESSION); |
212 | 0 | Matcher matcher = pattern.matcher(SQLUtils.cleanNumericOfValidOperators(valueEntered).trim()); |
213 | 0 | if(!matcher.matches()){ |
214 | 0 | bRet = false; |
215 | |
} |
216 | |
|
217 | 0 | return bRet; |
218 | |
|
219 | |
} |
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
public Boolean isRangeValid(String lowerValue, String upperValue) { |
226 | 0 | if (allowsRangeSearches()) { |
227 | 0 | Long lower = convertStringToLong(lowerValue); |
228 | 0 | Long upper = convertStringToLong(upperValue); |
229 | 0 | if ( (lower != null) && (upper != null) ) { |
230 | 0 | return (lower.compareTo(upper) <= 0); |
231 | |
} |
232 | 0 | return true; |
233 | |
} |
234 | 0 | return null; |
235 | |
} |
236 | |
|
237 | |
public String getOjbConcreteClass() { |
238 | 0 | return ojbConcreteClass; |
239 | |
} |
240 | |
|
241 | |
public void setOjbConcreteClass(String ojbConcreteClass) { |
242 | 0 | this.ojbConcreteClass = ojbConcreteClass; |
243 | 0 | } |
244 | |
|
245 | |
public DocumentRouteHeaderValue getRouteHeader() { |
246 | 0 | return routeHeader; |
247 | |
} |
248 | |
|
249 | |
public void setRouteHeader(DocumentRouteHeaderValue routeHeader) { |
250 | 0 | this.routeHeader = routeHeader; |
251 | 0 | } |
252 | |
|
253 | |
public String getDocumentId() { |
254 | 0 | return documentId; |
255 | |
} |
256 | |
|
257 | |
public void setDocumentId(String documentId) { |
258 | 0 | this.documentId = documentId; |
259 | 0 | } |
260 | |
|
261 | |
public String getSearchableAttributeKey() { |
262 | 0 | return searchableAttributeKey; |
263 | |
} |
264 | |
|
265 | |
public void setSearchableAttributeKey(String searchableAttributeKey) { |
266 | 0 | this.searchableAttributeKey = searchableAttributeKey; |
267 | 0 | } |
268 | |
|
269 | |
public Long getSearchableAttributeValue() { |
270 | 0 | return searchableAttributeValue; |
271 | |
} |
272 | |
|
273 | |
public void setSearchableAttributeValue(Long searchableAttributeValue) { |
274 | 0 | this.searchableAttributeValue = searchableAttributeValue; |
275 | 0 | } |
276 | |
|
277 | |
public String getSearchableAttributeValueId() { |
278 | 0 | return searchableAttributeValueId; |
279 | |
} |
280 | |
|
281 | |
public void setSearchableAttributeValueId(String searchableAttributeValueId) { |
282 | 0 | this.searchableAttributeValueId = searchableAttributeValueId; |
283 | 0 | } |
284 | |
|
285 | |
|
286 | |
public void beforeInsert(){ |
287 | 0 | OrmUtils.populateAutoIncValue(this, KEWServiceLocator.getEntityManagerFactory().createEntityManager()); |
288 | 0 | } |
289 | |
} |
290 | |
|