View Javadoc
1   /**
2    * Copyright 2005-2014 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.joda.time.DateTime;
20  import org.kuali.rice.core.api.CoreApiServiceLocator;
21  import org.kuali.rice.core.api.util.RiceConstants;
22  import org.kuali.rice.core.framework.persistence.jdbc.sql.SqlBuilder;
23  import org.kuali.rice.kew.api.KewApiConstants;
24  import org.kuali.rice.kew.api.document.attribute.DocumentAttributeDateTime;
25  import org.kuali.rice.kew.api.document.attribute.DocumentAttributeFactory;
26  
27  import javax.persistence.AttributeOverride;
28  import javax.persistence.AttributeOverrides;
29  import javax.persistence.Column;
30  import javax.persistence.Entity;
31  import javax.persistence.Inheritance;
32  import javax.persistence.InheritanceType;
33  import javax.persistence.NamedQueries;
34  import javax.persistence.NamedQuery;
35  import javax.persistence.Table;
36  import java.io.Serializable;
37  import java.sql.Date;
38  import java.sql.ResultSet;
39  import java.sql.SQLException;
40  import java.sql.Timestamp;
41  import java.text.DateFormat;
42  import java.text.ParseException;
43  import java.text.SimpleDateFormat;
44  import java.util.Calendar;
45  
46  /**
47   *
48   * @author Kuali Rice Team (rice.collab@kuali.org)
49   */
50  @Entity
51  @Inheritance(strategy= InheritanceType.TABLE_PER_CLASS)
52  @Table(name="KREW_DOC_HDR_EXT_DT_T")
53  @NamedQueries({
54  	@NamedQuery(name="SearchableAttributeDateTimeValue.FindByDocumentId", query="select s from "
55          + "SearchableAttributeDateTimeValue as s where s.documentId = :documentId"),
56  @NamedQuery(name="SearchableAttributeDateTimeValue.FindByKey", query="select s from "
57          + "SearchableAttributeDateTimeValue as s where s.documentId = :documentId and "
58          + "s.searchableAttributeKey = :searchableAttributeKey")
59  })
60  @AttributeOverrides({
61          @AttributeOverride(name="searchableAttributeValueId", column=@Column(name="DOC_HDR_EXT_DT_ID"))
62  })
63  public class SearchableAttributeDateTimeValue extends SearchableAttributeBase implements SearchableAttributeValue, Serializable {
64      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SearchableAttributeDateTimeValue.class);
65  
66      private static final long serialVersionUID = 3045621112943214772L;
67  
68      private static final String ATTRIBUTE_DATABASE_TABLE_NAME = "KREW_DOC_HDR_EXT_DT_T";
69      private static final boolean DEFAULT_WILDCARD_ALLOWANCE_POLICY = false;
70      private static final boolean ALLOWS_RANGE_SEARCH = true;
71      private static final boolean ALLOWS_CASE_INSENSITIVE_SEARCH = false;
72      private static final String ATTRIBUTE_XML_REPRESENTATION = KewApiConstants.SearchableAttributeConstants.DATA_TYPE_DATE;
73  
74  	@Column(name="VAL")
75  	private Timestamp searchableAttributeValue;
76  
77      /**
78       * Default constructor.
79       */
80      public SearchableAttributeDateTimeValue() {
81          super();
82          this.ojbConcreteClass = this.getClass().getName();
83      }
84  
85      public void setupAttributeValue(String value) {
86          this.setSearchableAttributeValue(convertStringToTimestamp(value));
87      }
88  
89      private Timestamp convertStringToTimestamp(String value) {
90          if (org.apache.commons.lang.StringUtils.isEmpty(value)) {
91              return null;
92          } else {
93              Timestamp t;
94              try {
95              	t = CoreApiServiceLocator.getDateTimeService().convertToSqlTimestamp(value);
96              } catch (ParseException e) {
97              	t = null;
98              }
99              if (t == null) {
100                 String errorMsg = "Error converting timestamp value '" + value + "' to valid timestamp object.";
101                 LOG.error("setupAttributeValue() " + errorMsg);
102                 throw new RuntimeException(errorMsg);
103             }
104             return t;
105         }
106     }
107 
108     @Override
109 	public void setupAttributeValue(ResultSet resultSet, String columnName) throws SQLException {
110 		Calendar c = Calendar.getInstance();
111 		c.clear(Calendar.HOUR);
112 		c.clear(Calendar.MINUTE);
113 		c.clear(Calendar.SECOND);
114 		c.clear(Calendar.MILLISECOND);
115 		this.setSearchableAttributeValue(resultSet.getTimestamp(columnName, c));
116 	}
117 
118     @Override
119     public String getSearchableAttributeDisplayValue() {
120         return formatAttributeValue(null);
121     }
122 
123     private String formatAttributeValue(String formatPattern) {
124         DateFormat df = getDateFormatToUse(formatPattern);
125         return df.format(new Date(getSearchableAttributeValue().getTime()));
126     }
127 
128     private DateFormat getDateFormatToUse(String parameterFormatPattern) {
129         if (StringUtils.isNotBlank(parameterFormatPattern)) {
130             return new SimpleDateFormat(parameterFormatPattern);
131         }
132         return RiceConstants.getDefaultDateFormat();
133     }
134 
135     @Override
136 	public String getAttributeDataType() {
137 		return ATTRIBUTE_XML_REPRESENTATION;
138 	}
139 
140     @Override
141 	public String getAttributeTableName() {
142 		return ATTRIBUTE_DATABASE_TABLE_NAME;
143 	}
144 
145     @Override
146 	public boolean allowsWildcards() {
147 		return DEFAULT_WILDCARD_ALLOWANCE_POLICY;
148 	}
149 
150     @Override
151 	public boolean allowsCaseInsensitivity() {
152 		return ALLOWS_CASE_INSENSITIVE_SEARCH;
153 	}
154 
155     @Override
156 	public boolean allowsRangeSearches() {
157 		return ALLOWS_RANGE_SEARCH;
158 	}
159 
160     @Override
161     public boolean isPassesDefaultValidation(String valueEntered) {
162     	return new SqlBuilder().isValidDate(valueEntered);
163         //return (DocSearchUtils.getEntryFormattedDate(valueEntered) != null);
164     }
165 
166     @Override
167     public Boolean isRangeValid(String lowerValue, String upperValue) {
168         if (allowsRangeSearches()) {
169             Timestamp lowerTime = convertStringToTimestamp(lowerValue);
170             Timestamp upperTime = convertStringToTimestamp(upperValue);
171             if ( (lowerTime != null) && (upperTime != null) ) {
172                 return (lowerTime.compareTo(upperTime) <= 0);
173             }
174             return true;
175         }
176         return null;
177     }
178 
179     @Override
180     public Timestamp getSearchableAttributeValue() {
181         return searchableAttributeValue;
182     }
183 
184     public void setSearchableAttributeValue(Timestamp searchableAttributeValue) {
185         this.searchableAttributeValue = searchableAttributeValue;
186     }
187 
188     @Override
189     public DocumentAttributeDateTime toDocumentAttribute() {
190         DateTime dateTime = null;
191         if (getSearchableAttributeValue() != null) {
192             dateTime = new DateTime(getSearchableAttributeValue().getTime());
193         }
194         return DocumentAttributeFactory.createDateTimeAttribute(getSearchableAttributeKey(), dateTime);
195     }
196 
197 }
198