001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.docsearch;
018:
019: import java.sql.Date;
020: import java.sql.ResultSet;
021: import java.sql.SQLException;
022: import java.sql.Timestamp;
023: import java.text.DateFormat;
024: import java.text.DecimalFormat;
025: import java.text.NumberFormat;
026: import java.text.SimpleDateFormat;
027: import java.util.Calendar;
028: import java.util.Map;
029:
030: import org.apache.commons.lang.StringUtils;
031:
032: import edu.iu.uis.eden.EdenConstants;
033: import edu.iu.uis.eden.WorkflowPersistable;
034: import edu.iu.uis.eden.routeheader.DocumentRouteHeaderValue;
035: import edu.iu.uis.eden.util.Utilities;
036:
037: /**
038: *
039: * @author delyea
040: */
041: public class SearchableAttributeDateTimeValue implements
042: WorkflowPersistable, SearchableAttributeValue {
043: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
044: .getLogger(SearchableAttributeDateTimeValue.class);
045:
046: private static final long serialVersionUID = 3045621112943214772L;
047:
048: private static final String ATTRIBUTE_DATABASE_TABLE_NAME = "EN_DOC_HDR_EXT_DT_T";
049: private static final boolean DEFAULT_WILDCARD_ALLOWANCE_POLICY = false;
050: private static final boolean ALLOWS_RANGE_SEARCH = true;
051: private static final boolean ALLOWS_CASE_INSENSITIVE_SEARCH = false;
052: private static final String ATTRIBUTE_XML_REPRESENTATION = SearchableAttribute.DATA_TYPE_DATE;
053:
054: private Long searchableAttributeValueId;
055: private String searchableAttributeKey;
056: private Timestamp searchableAttributeValue;
057: protected String ojbConcreteClass; // attribute needed for OJB polymorphism - do not alter!
058:
059: private Long routeHeaderId;
060: private DocumentRouteHeaderValue routeHeader;
061:
062: /**
063: * Default constructor.
064: */
065: public SearchableAttributeDateTimeValue() {
066: super ();
067: this .ojbConcreteClass = this .getClass().getName();
068: }
069:
070: /* (non-Javadoc)
071: * @see edu.iu.uis.eden.docsearch.SearchableAttributeValue#setupAttributeValue(java.lang.String)
072: */
073: public void setupAttributeValue(String value) {
074: this
075: .setSearchableAttributeValue(convertStringToTimestamp(value));
076: }
077:
078: private Timestamp convertStringToTimestamp(String value) {
079: if (Utilities.isEmpty(value)) {
080: return null;
081: } else {
082: Timestamp t = DocSearchUtils
083: .convertStringDateToTimestamp(value);
084: if (t == null) {
085: String errorMsg = "Error converting timestamp value '"
086: + value + "' to valid timestamp object.";
087: LOG.error("setupAttributeValue() " + errorMsg);
088: throw new RuntimeException(errorMsg);
089: }
090: return t;
091: }
092: }
093:
094: /* (non-Javadoc)
095: * @see edu.iu.uis.eden.docsearch.SearchableAttributeValue#setupAttributeValue(java.sql.ResultSet, java.lang.String)
096: */
097: public void setupAttributeValue(ResultSet resultSet,
098: String columnName) throws SQLException {
099: Calendar c = Calendar.getInstance();
100: c.clear(Calendar.HOUR);
101: c.clear(Calendar.MINUTE);
102: c.clear(Calendar.SECOND);
103: c.clear(Calendar.MILLISECOND);
104: this .setSearchableAttributeValue(resultSet.getTimestamp(
105: columnName, c));
106: }
107:
108: /* (non-Javadoc)
109: * @see edu.iu.uis.eden.docsearch.SearchableAttributeValue#getSearchableAttributeDisplayValue()
110: */
111: public String getSearchableAttributeDisplayValue() {
112: return formatAttributeValue(null);
113: }
114:
115: /* (non-Javadoc)
116: * @see edu.iu.uis.eden.docsearch.SearchableAttributeValue#getSearchableAttributeDisplayValue(java.util.Map)
117: */
118: public String getSearchableAttributeDisplayValue(
119: Map<String, String> displayParameters) {
120: return formatAttributeValue(displayParameters
121: .get(DISPLAY_FORMAT_PATTERN_MAP_KEY));
122: }
123:
124: private String formatAttributeValue(String formatPattern) {
125: DateFormat df = getDateFormatToUse(formatPattern);
126: return df.format(new Date(getSearchableAttributeValue()
127: .getTime()));
128: }
129:
130: private DateFormat getDateFormatToUse(String parameterFormatPattern) {
131: if (StringUtils.isNotBlank(parameterFormatPattern)) {
132: return new SimpleDateFormat(parameterFormatPattern);
133: }
134: return EdenConstants.getDefaultDateFormat();
135: }
136:
137: /* (non-Javadoc)
138: * @see edu.iu.uis.eden.docsearch.SearchableAttributeValue#getAttributeDataType()
139: */
140: public String getAttributeDataType() {
141: return ATTRIBUTE_XML_REPRESENTATION;
142: }
143:
144: /* (non-Javadoc)
145: * @see edu.iu.uis.eden.docsearch.SearchableAttributeValue#getAttributeTableName()
146: */
147: public String getAttributeTableName() {
148: return ATTRIBUTE_DATABASE_TABLE_NAME;
149: }
150:
151: /* (non-Javadoc)
152: * @see edu.iu.uis.eden.docsearch.SearchableAttributeValue#allowsWildcardsByDefault()
153: */
154: public boolean allowsWildcards() {
155: return DEFAULT_WILDCARD_ALLOWANCE_POLICY;
156: }
157:
158: /* (non-Javadoc)
159: * @see edu.iu.uis.eden.docsearch.SearchableAttributeValue#allowsCaseInsensitivity()
160: */
161: public boolean allowsCaseInsensitivity() {
162: return ALLOWS_CASE_INSENSITIVE_SEARCH;
163: }
164:
165: /* (non-Javadoc)
166: * @see edu.iu.uis.eden.docsearch.SearchableAttributeValue#allowsRangeSearches()
167: */
168: public boolean allowsRangeSearches() {
169: return ALLOWS_RANGE_SEARCH;
170: }
171:
172: /* (non-Javadoc)
173: * @see edu.iu.uis.eden.docsearch.SearchableAttributeValue#isPassesDefaultValidation()
174: */
175: public boolean isPassesDefaultValidation(String valueEntered) {
176: return (DocSearchUtils.getEntryFormattedDate(valueEntered) != null);
177: }
178:
179: /* (non-Javadoc)
180: * @see edu.iu.uis.eden.docsearch.SearchableAttributeValue#isRangeValid(java.lang.String, java.lang.String)
181: */
182: public Boolean isRangeValid(String lowerValue, String upperValue) {
183: if (allowsRangeSearches()) {
184: Timestamp lowerTime = convertStringToTimestamp(lowerValue);
185: Timestamp upperTime = convertStringToTimestamp(upperValue);
186: if ((lowerTime != null) && (upperTime != null)) {
187: return (lowerTime.compareTo(upperTime) <= 0);
188: }
189: return true;
190: }
191: return null;
192: }
193:
194: public String getOjbConcreteClass() {
195: return ojbConcreteClass;
196: }
197:
198: public void setOjbConcreteClass(String ojbConcreteClass) {
199: this .ojbConcreteClass = ojbConcreteClass;
200: }
201:
202: public DocumentRouteHeaderValue getRouteHeader() {
203: return routeHeader;
204: }
205:
206: public void setRouteHeader(DocumentRouteHeaderValue routeHeader) {
207: this .routeHeader = routeHeader;
208: }
209:
210: public Long getRouteHeaderId() {
211: return routeHeaderId;
212: }
213:
214: public void setRouteHeaderId(Long routeHeaderId) {
215: this .routeHeaderId = routeHeaderId;
216: }
217:
218: public String getSearchableAttributeKey() {
219: return searchableAttributeKey;
220: }
221:
222: public void setSearchableAttributeKey(String searchableAttributeKey) {
223: this .searchableAttributeKey = searchableAttributeKey;
224: }
225:
226: public Timestamp getSearchableAttributeValue() {
227: return searchableAttributeValue;
228: }
229:
230: public void setSearchableAttributeValue(
231: Timestamp searchableAttributeValue) {
232: this .searchableAttributeValue = searchableAttributeValue;
233: }
234:
235: public Long getSearchableAttributeValueId() {
236: return searchableAttributeValueId;
237: }
238:
239: public void setSearchableAttributeValueId(
240: Long searchableAttributeValueId) {
241: this .searchableAttributeValueId = searchableAttributeValueId;
242: }
243:
244: /* (non-Javadoc)
245: * @see edu.iu.uis.eden.WorkflowPersistable#copy(boolean)
246: */
247: public Object copy(boolean preserveKeys) {
248: return null;
249: }
250: }
|