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 | 0 | @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 | 0 | 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 | 0 | super(); |
96 | 0 | this.ojbConcreteClass = this.getClass().getName(); |
97 | 0 | } |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
public void setupAttributeValue(String value) { |
103 | 0 | this.setSearchableAttributeValue(convertStringToTimestamp(value)); |
104 | 0 | } |
105 | |
|
106 | |
private Timestamp convertStringToTimestamp(String value) { |
107 | 0 | if (Utilities.isEmpty(value)) { |
108 | 0 | return null; |
109 | |
} else { |
110 | |
Timestamp t; |
111 | |
try { |
112 | 0 | t = KNSServiceLocator.getDateTimeService().convertToSqlTimestamp(value); |
113 | 0 | } catch (ParseException e) { |
114 | 0 | t = null; |
115 | 0 | } |
116 | 0 | if (t == null) { |
117 | 0 | String errorMsg = "Error converting timestamp value '" + value + "' to valid timestamp object."; |
118 | 0 | LOG.error("setupAttributeValue() " + errorMsg); |
119 | 0 | throw new RuntimeException(errorMsg); |
120 | |
} |
121 | 0 | return t; |
122 | |
} |
123 | |
} |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
public void setupAttributeValue(ResultSet resultSet, String columnName) throws SQLException { |
129 | 0 | Calendar c = Calendar.getInstance(); |
130 | 0 | c.clear(Calendar.HOUR); |
131 | 0 | c.clear(Calendar.MINUTE); |
132 | 0 | c.clear(Calendar.SECOND); |
133 | 0 | c.clear(Calendar.MILLISECOND); |
134 | 0 | this.setSearchableAttributeValue(resultSet.getTimestamp(columnName, c)); |
135 | 0 | } |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
public String getSearchableAttributeDisplayValue() { |
141 | 0 | return formatAttributeValue(null); |
142 | |
} |
143 | |
|
144 | |
private String formatAttributeValue(String formatPattern) { |
145 | 0 | DateFormat df = getDateFormatToUse(formatPattern); |
146 | 0 | return df.format(new Date(getSearchableAttributeValue().getTime())); |
147 | |
} |
148 | |
|
149 | |
private DateFormat getDateFormatToUse(String parameterFormatPattern) { |
150 | 0 | if (StringUtils.isNotBlank(parameterFormatPattern)) { |
151 | 0 | return new SimpleDateFormat(parameterFormatPattern); |
152 | |
} |
153 | 0 | return RiceConstants.getDefaultDateFormat(); |
154 | |
} |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
public String getAttributeDataType() { |
160 | 0 | return ATTRIBUTE_XML_REPRESENTATION; |
161 | |
} |
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
public String getAttributeTableName() { |
167 | 0 | return ATTRIBUTE_DATABASE_TABLE_NAME; |
168 | |
} |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
public boolean allowsWildcards() { |
174 | 0 | return DEFAULT_WILDCARD_ALLOWANCE_POLICY; |
175 | |
} |
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
public boolean allowsCaseInsensitivity() { |
181 | 0 | return ALLOWS_CASE_INSENSITIVE_SEARCH; |
182 | |
} |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
public boolean allowsRangeSearches() { |
188 | 0 | return ALLOWS_RANGE_SEARCH; |
189 | |
} |
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
public boolean isPassesDefaultValidation(String valueEntered) { |
195 | 0 | return new SqlBuilder().isValidDate(valueEntered); |
196 | |
|
197 | |
} |
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
public Boolean isRangeValid(String lowerValue, String upperValue) { |
203 | 0 | if (allowsRangeSearches()) { |
204 | 0 | Timestamp lowerTime = convertStringToTimestamp(lowerValue); |
205 | 0 | Timestamp upperTime = convertStringToTimestamp(upperValue); |
206 | 0 | if ( (lowerTime != null) && (upperTime != null) ) { |
207 | 0 | return (lowerTime.compareTo(upperTime) <= 0); |
208 | |
} |
209 | 0 | return true; |
210 | |
} |
211 | 0 | return null; |
212 | |
} |
213 | |
|
214 | |
public String getOjbConcreteClass() { |
215 | 0 | return ojbConcreteClass; |
216 | |
} |
217 | |
|
218 | |
public void setOjbConcreteClass(String ojbConcreteClass) { |
219 | 0 | this.ojbConcreteClass = ojbConcreteClass; |
220 | 0 | } |
221 | |
|
222 | |
public DocumentRouteHeaderValue getRouteHeader() { |
223 | 0 | return routeHeader; |
224 | |
} |
225 | |
|
226 | |
public void setRouteHeader(DocumentRouteHeaderValue routeHeader) { |
227 | 0 | this.routeHeader = routeHeader; |
228 | 0 | } |
229 | |
|
230 | |
public Long getRouteHeaderId() { |
231 | 0 | return routeHeaderId; |
232 | |
} |
233 | |
|
234 | |
public void setRouteHeaderId(Long routeHeaderId) { |
235 | 0 | this.routeHeaderId = routeHeaderId; |
236 | 0 | } |
237 | |
|
238 | |
public String getSearchableAttributeKey() { |
239 | 0 | return searchableAttributeKey; |
240 | |
} |
241 | |
|
242 | |
public void setSearchableAttributeKey(String searchableAttributeKey) { |
243 | 0 | this.searchableAttributeKey = searchableAttributeKey; |
244 | 0 | } |
245 | |
|
246 | |
public Timestamp getSearchableAttributeValue() { |
247 | 0 | return searchableAttributeValue; |
248 | |
} |
249 | |
|
250 | |
public void setSearchableAttributeValue(Timestamp searchableAttributeValue) { |
251 | 0 | this.searchableAttributeValue = searchableAttributeValue; |
252 | 0 | } |
253 | |
|
254 | |
public Long getSearchableAttributeValueId() { |
255 | 0 | return searchableAttributeValueId; |
256 | |
} |
257 | |
|
258 | |
public void setSearchableAttributeValueId(Long searchableAttributeValueId) { |
259 | 0 | this.searchableAttributeValueId = searchableAttributeValueId; |
260 | 0 | } |
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
public Object copy(boolean preserveKeys) { |
266 | 0 | return null; |
267 | |
} |
268 | |
|
269 | |
@PrePersist |
270 | |
public void beforeInsert(){ |
271 | 0 | OrmUtils.populateAutoIncValue(this, KEWServiceLocator.getEntityManagerFactory().createEntityManager()); |
272 | 0 | } |
273 | |
} |
274 | |
|