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