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 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
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;
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
93
94 public SearchableAttributeDateTimeValue() {
95 super();
96 this.ojbConcreteClass = this.getClass().getName();
97 }
98
99
100
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
126
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
138
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
157
158
159 public String getAttributeDataType() {
160 return ATTRIBUTE_XML_REPRESENTATION;
161 }
162
163
164
165
166 public String getAttributeTableName() {
167 return ATTRIBUTE_DATABASE_TABLE_NAME;
168 }
169
170
171
172
173 public boolean allowsWildcards() {
174 return DEFAULT_WILDCARD_ALLOWANCE_POLICY;
175 }
176
177
178
179
180 public boolean allowsCaseInsensitivity() {
181 return ALLOWS_CASE_INSENSITIVE_SEARCH;
182 }
183
184
185
186
187 public boolean allowsRangeSearches() {
188 return ALLOWS_RANGE_SEARCH;
189 }
190
191
192
193
194 public boolean isPassesDefaultValidation(String valueEntered) {
195 return new SqlBuilder().isValidDate(valueEntered);
196
197 }
198
199
200
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
263
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