View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.docsearch;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.hibernate.annotations.GenericGenerator;
20  import org.hibernate.annotations.Parameter;
21  import org.joda.time.DateTime;
22  import org.kuali.rice.core.api.CoreApiServiceLocator;
23  import org.kuali.rice.core.api.util.RiceConstants;
24  import org.kuali.rice.core.framework.persistence.jdbc.sql.SqlBuilder;
25  import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
26  import org.kuali.rice.kew.api.KewApiConstants;
27  import org.kuali.rice.kew.api.document.attribute.DocumentAttributeDateTime;
28  import org.kuali.rice.kew.api.document.attribute.DocumentAttributeFactory;
29  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
30  import org.kuali.rice.kew.service.KEWServiceLocator;
31  
32  import javax.persistence.CascadeType;
33  import javax.persistence.Column;
34  import javax.persistence.Entity;
35  import javax.persistence.FetchType;
36  import javax.persistence.GeneratedValue;
37  import javax.persistence.Id;
38  import javax.persistence.JoinColumn;
39  import javax.persistence.ManyToOne;
40  import javax.persistence.NamedQueries;
41  import javax.persistence.NamedQuery;
42  import javax.persistence.Table;
43  import javax.persistence.Transient;
44  import java.io.Serializable;
45  import java.sql.Date;
46  import java.sql.ResultSet;
47  import java.sql.SQLException;
48  import java.sql.Timestamp;
49  import java.text.DateFormat;
50  import java.text.ParseException;
51  import java.text.SimpleDateFormat;
52  import java.util.Calendar;
53  
54  
55  /**
56   *
57   * @author Kuali Rice Team (rice.collab@kuali.org)
58   */
59  @Entity
60  @Table(name="KREW_DOC_HDR_EXT_DT_T")
61  //@Sequence(name="KREW_SRCH_ATTR_S",property="searchableAttributeValueId")
62  @NamedQueries({
63  	@NamedQuery(name="SearchableAttributeDateTimeValue.FindByDocumentId", query="select s from SearchableAttributeDateTimeValue as s where s.documentId = :documentId"),
64  	@NamedQuery(name="SearchableAttributeDateTimeValue.FindByKey", query="select s from SearchableAttributeDateTimeValue as s where s.documentId = :documentId and s.searchableAttributeKey = :searchableAttributeKey")
65  })
66  public class SearchableAttributeDateTimeValue implements SearchableAttributeValue, Serializable {
67      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SearchableAttributeDateTimeValue.class);
68  
69      private static final long serialVersionUID = 3045621112943214772L;
70  
71      private static final String ATTRIBUTE_DATABASE_TABLE_NAME = "KREW_DOC_HDR_EXT_DT_T";
72      private static final boolean DEFAULT_WILDCARD_ALLOWANCE_POLICY = false;
73      private static final boolean ALLOWS_RANGE_SEARCH = true;
74      private static final boolean ALLOWS_CASE_INSENSITIVE_SEARCH = false;
75      private static final String ATTRIBUTE_XML_REPRESENTATION = KewApiConstants.SearchableAttributeConstants.DATA_TYPE_DATE;
76  
77      @Id
78      @GeneratedValue(generator="KREW_SRCH_ATTR_S")
79  	@GenericGenerator(name="KREW_SRCH_ATTR_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
80  			@Parameter(name="sequence_name",value="KREW_SRCH_ATTR_S"),
81  			@Parameter(name="value_column",value="id")
82  	})
83  	@Column(name="DOC_HDR_EXT_DT_ID")
84  	private String searchableAttributeValueId;
85      @Column(name="KEY_CD")
86  	private String searchableAttributeKey;
87  	@Column(name="VAL")
88  	private Timestamp searchableAttributeValue;
89      @Transient
90      protected String ojbConcreteClass; // attribute needed for OJB polymorphism - do not alter!
91  
92      @Column(name="DOC_HDR_ID")
93  	private String documentId;
94      @ManyToOne(fetch=FetchType.EAGER, cascade={CascadeType.PERSIST})
95  	@JoinColumn(name="DOC_HDR_ID", insertable=false, updatable=false)
96  	private DocumentRouteHeaderValue routeHeader;
97  
98      /**
99       * Default constructor.
100      */
101     public SearchableAttributeDateTimeValue() {
102         super();
103         this.ojbConcreteClass = this.getClass().getName();
104     }
105 
106     /* (non-Javadoc)
107      * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#setupAttributeValue(java.lang.String)
108      */
109     public void setupAttributeValue(String value) {
110         this.setSearchableAttributeValue(convertStringToTimestamp(value));
111     }
112 
113     private Timestamp convertStringToTimestamp(String value) {
114         if (org.apache.commons.lang.StringUtils.isEmpty(value)) {
115             return null;
116         } else {
117             Timestamp t;
118             try {
119             	t = CoreApiServiceLocator.getDateTimeService().convertToSqlTimestamp(value);
120             } catch (ParseException e) {
121             	t = null;
122             }
123             if (t == null) {
124                 String errorMsg = "Error converting timestamp value '" + value + "' to valid timestamp object.";
125                 LOG.error("setupAttributeValue() " + errorMsg);
126                 throw new RuntimeException(errorMsg);
127             }
128             return t;
129         }
130     }
131 
132 	/* (non-Javadoc)
133 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#setupAttributeValue(java.sql.ResultSet, java.lang.String)
134 	 */
135 	public void setupAttributeValue(ResultSet resultSet, String columnName) throws SQLException {
136 		Calendar c = Calendar.getInstance();
137 		c.clear(Calendar.HOUR);
138 		c.clear(Calendar.MINUTE);
139 		c.clear(Calendar.SECOND);
140 		c.clear(Calendar.MILLISECOND);
141 		this.setSearchableAttributeValue(resultSet.getTimestamp(columnName, c));
142 	}
143 
144 	/* (non-Javadoc)
145 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#getSearchableAttributeDisplayValue()
146 	 */
147     public String getSearchableAttributeDisplayValue() {
148         return formatAttributeValue(null);
149     }
150 
151     private String formatAttributeValue(String formatPattern) {
152         DateFormat df = getDateFormatToUse(formatPattern);
153         return df.format(new Date(getSearchableAttributeValue().getTime()));
154     }
155 
156     private DateFormat getDateFormatToUse(String parameterFormatPattern) {
157         if (StringUtils.isNotBlank(parameterFormatPattern)) {
158             return new SimpleDateFormat(parameterFormatPattern);
159         }
160         return RiceConstants.getDefaultDateFormat();
161     }
162 
163 	/* (non-Javadoc)
164 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#getAttributeDataType()
165 	 */
166 	public String getAttributeDataType() {
167 		return ATTRIBUTE_XML_REPRESENTATION;
168 	}
169 
170 	/* (non-Javadoc)
171 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#getAttributeTableName()
172 	 */
173 	public String getAttributeTableName() {
174 		return ATTRIBUTE_DATABASE_TABLE_NAME;
175 	}
176 
177     /* (non-Javadoc)
178 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#allowsWildcardsByDefault()
179 	 */
180 	public boolean allowsWildcards() {
181 		return DEFAULT_WILDCARD_ALLOWANCE_POLICY;
182 	}
183 
184     /* (non-Javadoc)
185 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#allowsCaseInsensitivity()
186 	 */
187 	public boolean allowsCaseInsensitivity() {
188 		return ALLOWS_CASE_INSENSITIVE_SEARCH;
189 	}
190 
191 	/* (non-Javadoc)
192 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#allowsRangeSearches()
193 	 */
194 	public boolean allowsRangeSearches() {
195 		return ALLOWS_RANGE_SEARCH;
196 	}
197 
198 	/* (non-Javadoc)
199 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#isPassesDefaultValidation()
200 	 */
201     public boolean isPassesDefaultValidation(String valueEntered) {
202     	return new SqlBuilder().isValidDate(valueEntered);
203         //return (DocSearchUtils.getEntryFormattedDate(valueEntered) != null);
204     }
205 
206     /* (non-Javadoc)
207      * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#isRangeValid(java.lang.String, java.lang.String)
208      */
209     public Boolean isRangeValid(String lowerValue, String upperValue) {
210         if (allowsRangeSearches()) {
211             Timestamp lowerTime = convertStringToTimestamp(lowerValue);
212             Timestamp upperTime = convertStringToTimestamp(upperValue);
213             if ( (lowerTime != null) && (upperTime != null) ) {
214                 return (lowerTime.compareTo(upperTime) <= 0);
215             }
216             return true;
217         }
218         return null;
219     }
220 
221 	public String getOjbConcreteClass() {
222         return ojbConcreteClass;
223     }
224 
225     public void setOjbConcreteClass(String ojbConcreteClass) {
226         this.ojbConcreteClass = ojbConcreteClass;
227     }
228 
229     public DocumentRouteHeaderValue getRouteHeader() {
230         return routeHeader;
231     }
232 
233     public void setRouteHeader(DocumentRouteHeaderValue routeHeader) {
234         this.routeHeader = routeHeader;
235     }
236 
237     public String getDocumentId() {
238         return documentId;
239     }
240 
241     public void setDocumentId(String documentId) {
242         this.documentId = documentId;
243     }
244 
245     public String getSearchableAttributeKey() {
246         return searchableAttributeKey;
247     }
248 
249     public void setSearchableAttributeKey(String searchableAttributeKey) {
250         this.searchableAttributeKey = searchableAttributeKey;
251     }
252 
253     public Timestamp getSearchableAttributeValue() {
254         return searchableAttributeValue;
255     }
256 
257     public void setSearchableAttributeValue(Timestamp searchableAttributeValue) {
258         this.searchableAttributeValue = searchableAttributeValue;
259     }
260 
261     public String getSearchableAttributeValueId() {
262         return searchableAttributeValueId;
263     }
264 
265     public void setSearchableAttributeValueId(String searchableAttributeValueId) {
266         this.searchableAttributeValueId = searchableAttributeValueId;
267     }
268 
269 	//@PrePersist
270 	public void beforeInsert(){
271 		OrmUtils.populateAutoIncValue(this, KEWServiceLocator.getEntityManagerFactory().createEntityManager());
272 	}
273 
274     @Override
275     public DocumentAttributeDateTime toDocumentAttribute() {
276         DateTime dateTime = null;
277         if (getSearchableAttributeValue() != null) {
278             dateTime = new DateTime(getSearchableAttributeValue().getTime());
279         }
280         return DocumentAttributeFactory.createDateTimeAttribute(getSearchableAttributeKey(), dateTime);
281     }
282 }
283