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