View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    *
4    *
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.opensource.org/licenses/ecl2.php
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * @author Kuali Rice Team (rice.collab@kuali.org)
54   */
55  @Entity
56  @Table(name="KREW_DOC_HDR_EXT_LONG_T")
57  //@Sequence(name="KREW_SRCH_ATTR_S",property="searchableAttributeValueId")
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; // attribute needed for OJB polymorphism - do not alter!
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       * Default constructor.
97       */
98      public SearchableAttributeLongValue() {
99          super();
100         this.ojbConcreteClass = this.getClass().getName();
101     }
102 
103     /* (non-Javadoc)
104      * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#setupAttributeValue(java.lang.String)
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 	/* (non-Javadoc)
119 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#setupAttributeValue(java.sql.ResultSet, java.lang.String)
120 	 */
121 	public void setupAttributeValue(ResultSet resultSet, String columnName) throws SQLException {
122 		this.setSearchableAttributeValue(resultSet.getLong(columnName));
123 	}
124 
125     /* (non-Javadoc)
126      * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#getSearchableAttributeDisplayValue(java.util.Map)
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 	/* (non-Javadoc)
135 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#getAttributeDataType()
136 	 */
137 	public String getAttributeDataType() {
138 		return ATTRIBUTE_XML_REPRESENTATION;
139 	}
140 
141 	/* (non-Javadoc)
142 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#getAttributeTableName()
143 	 */
144 	public String getAttributeTableName() {
145 		return ATTRIBUTE_DATABASE_TABLE_NAME;
146 	}
147 
148     /* (non-Javadoc)
149 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#allowsWildcardsByDefault()
150 	 */
151 	public boolean allowsWildcards() {
152 		return DEFAULT_WILDCARD_ALLOWANCE_POLICY;
153 	}
154 
155     /* (non-Javadoc)
156 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#allowsCaseInsensitivity()
157 	 */
158 	public boolean allowsCaseInsensitivity() {
159 		return ALLOWS_CASE_INSENSITIVE_SEARCH;
160 	}
161 
162     /* (non-Javadoc)
163 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#allowsRangeSearches()
164 	 */
165 	public boolean allowsRangeSearches() {
166 		return ALLOWS_RANGE_SEARCH;
167 	}
168 
169 	/* (non-Javadoc)
170 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#isPassesDefaultValidation()
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 			//splitValueList.addAll(Arrays.asList(StringUtils.split(valueEntered, KRADConstants.OR_LOGICAL_OPERATOR)));
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 			//splitValueList.addAll(Arrays.asList(StringUtils.split(valueEntered, KRADConstants.AND_LOGICAL_OPERATOR)));
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     /* (non-Javadoc)
223      * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#isRangeValid(java.lang.String, java.lang.String)
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 	//@PrePersist
286 	public void beforeInsert(){
287 		OrmUtils.populateAutoIncValue(this, KEWServiceLocator.getEntityManagerFactory().createEntityManager());
288 	}
289 }
290