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.ResultSet;
020: import java.sql.SQLException;
021: import java.text.DecimalFormat;
022: import java.text.NumberFormat;
023: import java.util.HashMap;
024: import java.util.Map;
025: import java.util.regex.Matcher;
026: import java.util.regex.Pattern;
027:
028: import org.apache.commons.lang.StringUtils;
029:
030: import edu.iu.uis.eden.WorkflowPersistable;
031: import edu.iu.uis.eden.routeheader.DocumentRouteHeaderValue;
032: import edu.iu.uis.eden.util.Utilities;
033:
034: /**
035: *
036: * @author delyea
037: */
038: public class SearchableAttributeLongValue implements
039: WorkflowPersistable, SearchableAttributeValue {
040:
041: private static final long serialVersionUID = 5786144436732198346L;
042:
043: private static final String ATTRIBUTE_DATABASE_TABLE_NAME = "EN_DOC_HDR_EXT_LONG_T";
044: private static final boolean DEFAULT_WILDCARD_ALLOWANCE_POLICY = false;
045: private static final boolean ALLOWS_RANGE_SEARCH = true;
046: private static final boolean ALLOWS_CASE_INSENSITIVE_SEARCH = false;
047: private static final String DEFAULT_VALIDATION_REGEX_EXPRESSION = "^[0-9]+$";
048: private static final String ATTRIBUTE_XML_REPRESENTATION = SearchableAttribute.DATA_TYPE_LONG;
049: private static final String DEFAULT_FORMAT_PATTERN = "#";
050:
051: private Long searchableAttributeValueId;
052: private String searchableAttributeKey;
053: private Long searchableAttributeValue;
054: protected String ojbConcreteClass; // attribute needed for OJB polymorphism - do not alter!
055:
056: private Long routeHeaderId;
057: private DocumentRouteHeaderValue routeHeader;
058:
059: /**
060: * Default constructor.
061: */
062: public SearchableAttributeLongValue() {
063: super ();
064: this .ojbConcreteClass = this .getClass().getName();
065: }
066:
067: /* (non-Javadoc)
068: * @see edu.iu.uis.eden.docsearch.SearchableAttributeValue#setupAttributeValue(java.lang.String)
069: */
070: public void setupAttributeValue(String value) {
071: this .setSearchableAttributeValue(convertStringToLong(value));
072: }
073:
074: private Long convertStringToLong(String value) {
075: if (Utilities.isEmpty(value)) {
076: return null;
077: } else {
078: return Long.valueOf(value);
079: }
080: }
081:
082: /* (non-Javadoc)
083: * @see edu.iu.uis.eden.docsearch.SearchableAttributeValue#setupAttributeValue(java.sql.ResultSet, java.lang.String)
084: */
085: public void setupAttributeValue(ResultSet resultSet,
086: String columnName) throws SQLException {
087: this .setSearchableAttributeValue(resultSet.getLong(columnName));
088: }
089:
090: /* (non-Javadoc)
091: * @see edu.iu.uis.eden.docsearch.SearchableAttributeValue#getSearchableAttributeDisplayValue()
092: */
093: public String getSearchableAttributeDisplayValue() {
094: return getSearchableAttributeDisplayValue(new HashMap<String, String>());
095: }
096:
097: /* (non-Javadoc)
098: * @see edu.iu.uis.eden.docsearch.SearchableAttributeValue#getSearchableAttributeDisplayValue(java.util.Map)
099: */
100: public String getSearchableAttributeDisplayValue(
101: Map<String, String> displayParameters) {
102: NumberFormat format = DecimalFormat.getInstance();
103: ((DecimalFormat) format)
104: .applyPattern(getFormatPatternToUse(displayParameters
105: .get(DISPLAY_FORMAT_PATTERN_MAP_KEY)));
106: return format.format(getSearchableAttributeValue().longValue());
107: }
108:
109: private String getFormatPatternToUse(String parameterFormatPattern) {
110: if (StringUtils.isNotBlank(parameterFormatPattern)) {
111: return parameterFormatPattern;
112: }
113: return DEFAULT_FORMAT_PATTERN;
114: }
115:
116: /* (non-Javadoc)
117: * @see edu.iu.uis.eden.docsearch.SearchableAttributeValue#getAttributeDataType()
118: */
119: public String getAttributeDataType() {
120: return ATTRIBUTE_XML_REPRESENTATION;
121: }
122:
123: /* (non-Javadoc)
124: * @see edu.iu.uis.eden.docsearch.SearchableAttributeValue#getAttributeTableName()
125: */
126: public String getAttributeTableName() {
127: return ATTRIBUTE_DATABASE_TABLE_NAME;
128: }
129:
130: /* (non-Javadoc)
131: * @see edu.iu.uis.eden.docsearch.SearchableAttributeValue#allowsWildcardsByDefault()
132: */
133: public boolean allowsWildcards() {
134: return DEFAULT_WILDCARD_ALLOWANCE_POLICY;
135: }
136:
137: /* (non-Javadoc)
138: * @see edu.iu.uis.eden.docsearch.SearchableAttributeValue#allowsCaseInsensitivity()
139: */
140: public boolean allowsCaseInsensitivity() {
141: return ALLOWS_CASE_INSENSITIVE_SEARCH;
142: }
143:
144: /* (non-Javadoc)
145: * @see edu.iu.uis.eden.docsearch.SearchableAttributeValue#allowsRangeSearches()
146: */
147: public boolean allowsRangeSearches() {
148: return ALLOWS_RANGE_SEARCH;
149: }
150:
151: /* (non-Javadoc)
152: * @see edu.iu.uis.eden.docsearch.SearchableAttributeValue#isPassesDefaultValidation()
153: */
154: public boolean isPassesDefaultValidation(String valueEntered) {
155: Pattern pattern = Pattern
156: .compile(DEFAULT_VALIDATION_REGEX_EXPRESSION);
157: Matcher matcher = pattern.matcher(valueEntered);
158: return (matcher.matches());
159: }
160:
161: /* (non-Javadoc)
162: * @see edu.iu.uis.eden.docsearch.SearchableAttributeValue#isRangeValid(java.lang.String, java.lang.String)
163: */
164: public Boolean isRangeValid(String lowerValue, String upperValue) {
165: if (allowsRangeSearches()) {
166: Long lower = convertStringToLong(lowerValue);
167: Long upper = convertStringToLong(upperValue);
168: if ((lower != null) && (upper != null)) {
169: return (lower.compareTo(upper) <= 0);
170: }
171: return true;
172: }
173: return null;
174: }
175:
176: public String getOjbConcreteClass() {
177: return ojbConcreteClass;
178: }
179:
180: public void setOjbConcreteClass(String ojbConcreteClass) {
181: this .ojbConcreteClass = ojbConcreteClass;
182: }
183:
184: public DocumentRouteHeaderValue getRouteHeader() {
185: return routeHeader;
186: }
187:
188: public void setRouteHeader(DocumentRouteHeaderValue routeHeader) {
189: this .routeHeader = routeHeader;
190: }
191:
192: public Long getRouteHeaderId() {
193: return routeHeaderId;
194: }
195:
196: public void setRouteHeaderId(Long routeHeaderId) {
197: this .routeHeaderId = routeHeaderId;
198: }
199:
200: public String getSearchableAttributeKey() {
201: return searchableAttributeKey;
202: }
203:
204: public void setSearchableAttributeKey(String searchableAttributeKey) {
205: this .searchableAttributeKey = searchableAttributeKey;
206: }
207:
208: public Long getSearchableAttributeValue() {
209: return searchableAttributeValue;
210: }
211:
212: public void setSearchableAttributeValue(
213: Long searchableAttributeValue) {
214: this .searchableAttributeValue = searchableAttributeValue;
215: }
216:
217: public Long getSearchableAttributeValueId() {
218: return searchableAttributeValueId;
219: }
220:
221: public void setSearchableAttributeValueId(
222: Long searchableAttributeValueId) {
223: this .searchableAttributeValueId = searchableAttributeValueId;
224: }
225:
226: /* (non-Javadoc)
227: * @see edu.iu.uis.eden.WorkflowPersistable#copy(boolean)
228: */
229: public Object copy(boolean preserveKeys) {
230: return null;
231: }
232: }
|