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 java.sql.Date;
20  import java.sql.ResultSet;
21  import java.sql.SQLException;
22  import java.sql.Timestamp;
23  import java.text.DateFormat;
24  import java.text.ParseException;
25  import java.text.SimpleDateFormat;
26  import java.util.Calendar;
27  
28  import javax.persistence.CascadeType;
29  import javax.persistence.Column;
30  import javax.persistence.Entity;
31  import javax.persistence.FetchType;
32  import javax.persistence.Id;
33  import javax.persistence.JoinColumn;
34  import javax.persistence.ManyToOne;
35  import javax.persistence.NamedQueries;
36  import javax.persistence.NamedQuery;
37  import javax.persistence.PrePersist;
38  import javax.persistence.Table;
39  import javax.persistence.Transient;
40  
41  import org.apache.commons.lang.StringUtils;
42  import org.kuali.rice.core.jdbc.SqlBuilder;
43  import org.kuali.rice.core.jpa.annotations.Sequence;
44  import org.kuali.rice.core.util.OrmUtils;
45  import org.kuali.rice.core.util.RiceConstants;
46  import org.kuali.rice.kew.bo.WorkflowPersistable;
47  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
48  import org.kuali.rice.kew.service.KEWServiceLocator;
49  import org.kuali.rice.kew.util.Utilities;
50  import org.kuali.rice.kns.service.KNSServiceLocator;
51  
52  
53  /**
54   *
55   * @author Kuali Rice Team (rice.collab@kuali.org)
56   */
57  @Entity
58  @Table(name="KREW_DOC_HDR_EXT_DT_T")
59  @Sequence(name="KREW_SRCH_ATTR_S",property="searchableAttributeValueId")
60  @NamedQueries({
61  	@NamedQuery(name="SearchableAttributeDateTimeValue.FindByRouteHeaderId", query="select s from SearchableAttributeDateTimeValue as s where s.routeHeaderId = :routeHeaderId"),
62  	@NamedQuery(name="SearchableAttributeDateTimeValue.FindByKey", query="select s from SearchableAttributeDateTimeValue as s where s.routeHeaderId = :routeHeaderId and s.searchableAttributeKey = :searchableAttributeKey")
63  })
64  public class SearchableAttributeDateTimeValue implements WorkflowPersistable, SearchableAttributeValue {
65      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SearchableAttributeDateTimeValue.class);
66  
67      private static final long serialVersionUID = 3045621112943214772L;
68  
69      private static final String ATTRIBUTE_DATABASE_TABLE_NAME = "KREW_DOC_HDR_EXT_DT_T";
70      private static final boolean DEFAULT_WILDCARD_ALLOWANCE_POLICY = false;
71      private static final boolean ALLOWS_RANGE_SEARCH = true;
72      private static final boolean ALLOWS_CASE_INSENSITIVE_SEARCH = false;
73      private static final String ATTRIBUTE_XML_REPRESENTATION = SearchableAttribute.DATA_TYPE_DATE;
74  
75      @Id
76  	@Column(name="DOC_HDR_EXT_DT_ID")
77  	private Long searchableAttributeValueId;
78      @Column(name="KEY_CD")
79  	private String searchableAttributeKey;
80  	@Column(name="VAL")
81  	private Timestamp searchableAttributeValue;
82      @Transient
83      protected String ojbConcreteClass; // attribute needed for OJB polymorphism - do not alter!
84  
85      @Column(name="DOC_HDR_ID")
86  	private Long routeHeaderId;
87      @ManyToOne(fetch=FetchType.EAGER, cascade={CascadeType.PERSIST})
88  	@JoinColumn(name="DOC_HDR_ID", insertable=false, updatable=false)
89  	private DocumentRouteHeaderValue routeHeader;
90  
91      /**
92       * Default constructor.
93       */
94      public SearchableAttributeDateTimeValue() {
95          super();
96          this.ojbConcreteClass = this.getClass().getName();
97      }
98  
99      /* (non-Javadoc)
100      * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#setupAttributeValue(java.lang.String)
101      */
102     public void setupAttributeValue(String value) {
103         this.setSearchableAttributeValue(convertStringToTimestamp(value));
104     }
105 
106     private Timestamp convertStringToTimestamp(String value) {
107         if (Utilities.isEmpty(value)) {
108             return null;
109         } else {
110             Timestamp t;
111             try {
112             	t = KNSServiceLocator.getDateTimeService().convertToSqlTimestamp(value);
113             } catch (ParseException e) {
114             	t = null;
115             }
116             if (t == null) {
117                 String errorMsg = "Error converting timestamp value '" + value + "' to valid timestamp object.";
118                 LOG.error("setupAttributeValue() " + errorMsg);
119                 throw new RuntimeException(errorMsg);
120             }
121             return t;
122         }
123     }
124 
125 	/* (non-Javadoc)
126 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#setupAttributeValue(java.sql.ResultSet, java.lang.String)
127 	 */
128 	public void setupAttributeValue(ResultSet resultSet, String columnName) throws SQLException {
129 		Calendar c = Calendar.getInstance();
130 		c.clear(Calendar.HOUR);
131 		c.clear(Calendar.MINUTE);
132 		c.clear(Calendar.SECOND);
133 		c.clear(Calendar.MILLISECOND);
134 		this.setSearchableAttributeValue(resultSet.getTimestamp(columnName, c));
135 	}
136 
137 	/* (non-Javadoc)
138 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#getSearchableAttributeDisplayValue()
139 	 */
140     public String getSearchableAttributeDisplayValue() {
141         return formatAttributeValue(null);
142     }
143 
144     private String formatAttributeValue(String formatPattern) {
145         DateFormat df = getDateFormatToUse(formatPattern);
146         return df.format(new Date(getSearchableAttributeValue().getTime()));
147     }
148 
149     private DateFormat getDateFormatToUse(String parameterFormatPattern) {
150         if (StringUtils.isNotBlank(parameterFormatPattern)) {
151             return new SimpleDateFormat(parameterFormatPattern);
152         }
153         return RiceConstants.getDefaultDateFormat();
154     }
155 
156 	/* (non-Javadoc)
157 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#getAttributeDataType()
158 	 */
159 	public String getAttributeDataType() {
160 		return ATTRIBUTE_XML_REPRESENTATION;
161 	}
162 
163 	/* (non-Javadoc)
164 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#getAttributeTableName()
165 	 */
166 	public String getAttributeTableName() {
167 		return ATTRIBUTE_DATABASE_TABLE_NAME;
168 	}
169 
170     /* (non-Javadoc)
171 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#allowsWildcardsByDefault()
172 	 */
173 	public boolean allowsWildcards() {
174 		return DEFAULT_WILDCARD_ALLOWANCE_POLICY;
175 	}
176 
177     /* (non-Javadoc)
178 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#allowsCaseInsensitivity()
179 	 */
180 	public boolean allowsCaseInsensitivity() {
181 		return ALLOWS_CASE_INSENSITIVE_SEARCH;
182 	}
183 
184 	/* (non-Javadoc)
185 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#allowsRangeSearches()
186 	 */
187 	public boolean allowsRangeSearches() {
188 		return ALLOWS_RANGE_SEARCH;
189 	}
190 
191 	/* (non-Javadoc)
192 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#isPassesDefaultValidation()
193 	 */
194     public boolean isPassesDefaultValidation(String valueEntered) {
195     	return new SqlBuilder().isValidDate(valueEntered);
196         //return (DocSearchUtils.getEntryFormattedDate(valueEntered) != null);
197     }
198 
199     /* (non-Javadoc)
200      * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#isRangeValid(java.lang.String, java.lang.String)
201      */
202     public Boolean isRangeValid(String lowerValue, String upperValue) {
203         if (allowsRangeSearches()) {
204             Timestamp lowerTime = convertStringToTimestamp(lowerValue);
205             Timestamp upperTime = convertStringToTimestamp(upperValue);
206             if ( (lowerTime != null) && (upperTime != null) ) {
207                 return (lowerTime.compareTo(upperTime) <= 0);
208             }
209             return true;
210         }
211         return null;
212     }
213 
214 	public String getOjbConcreteClass() {
215         return ojbConcreteClass;
216     }
217 
218     public void setOjbConcreteClass(String ojbConcreteClass) {
219         this.ojbConcreteClass = ojbConcreteClass;
220     }
221 
222     public DocumentRouteHeaderValue getRouteHeader() {
223         return routeHeader;
224     }
225 
226     public void setRouteHeader(DocumentRouteHeaderValue routeHeader) {
227         this.routeHeader = routeHeader;
228     }
229 
230     public Long getRouteHeaderId() {
231         return routeHeaderId;
232     }
233 
234     public void setRouteHeaderId(Long routeHeaderId) {
235         this.routeHeaderId = routeHeaderId;
236     }
237 
238     public String getSearchableAttributeKey() {
239         return searchableAttributeKey;
240     }
241 
242     public void setSearchableAttributeKey(String searchableAttributeKey) {
243         this.searchableAttributeKey = searchableAttributeKey;
244     }
245 
246     public Timestamp getSearchableAttributeValue() {
247         return searchableAttributeValue;
248     }
249 
250     public void setSearchableAttributeValue(Timestamp searchableAttributeValue) {
251         this.searchableAttributeValue = searchableAttributeValue;
252     }
253 
254     public Long getSearchableAttributeValueId() {
255         return searchableAttributeValueId;
256     }
257 
258     public void setSearchableAttributeValueId(Long searchableAttributeValueId) {
259         this.searchableAttributeValueId = searchableAttributeValueId;
260     }
261 
262     /* (non-Javadoc)
263      * @see org.kuali.rice.kew.WorkflowPersistable#copy(boolean)
264      */
265     public Object copy(boolean preserveKeys) {
266         return null;
267     }
268 
269 	@PrePersist
270 	public void beforeInsert(){
271 		OrmUtils.populateAutoIncValue(this, KEWServiceLocator.getEntityManagerFactory().createEntityManager());
272 	}
273 }
274