View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.docsearch;
17  
18  import org.kuali.rice.kew.api.KewApiConstants;
19  import org.kuali.rice.kew.api.document.attribute.DocumentAttributeDecimal;
20  import org.kuali.rice.kew.api.document.attribute.DocumentAttributeFactory;
21  
22  import javax.persistence.AttributeOverride;
23  import javax.persistence.AttributeOverrides;
24  import javax.persistence.Column;
25  import javax.persistence.Entity;
26  import javax.persistence.Inheritance;
27  import javax.persistence.InheritanceType;
28  import javax.persistence.NamedQueries;
29  import javax.persistence.NamedQuery;
30  import javax.persistence.Table;
31  import java.io.Serializable;
32  import java.math.BigDecimal;
33  import java.sql.ResultSet;
34  import java.sql.SQLException;
35  import java.text.DecimalFormat;
36  import java.text.NumberFormat;
37  import java.util.regex.Pattern;
38  
39  /**
40   *
41   * @author Kuali Rice Team (rice.collab@kuali.org)
42   */
43  @Entity
44  @Inheritance(strategy= InheritanceType.TABLE_PER_CLASS)
45  @Table(name="KREW_DOC_HDR_EXT_FLT_T")
46  @NamedQueries({
47  	@NamedQuery(name="SearchableAttributeFloatValue.FindByDocumentId", query="select s from "
48              + "SearchableAttributeFloatValue as s where s.documentId = :documentId"),
49  	@NamedQuery(name="SearchableAttributeFloatValue.FindByKey", query="select s from "
50              + "SearchableAttributeFloatValue as s where s.documentId = :documentId and "
51              + "s.searchableAttributeKey = :searchableAttributeKey")
52  })
53  @AttributeOverrides({
54          @AttributeOverride(name="searchableAttributeValueId", column=@Column(name="DOC_HDR_EXT_FLT_ID"))
55  })
56  public class SearchableAttributeFloatValue extends SearchableAttributeNumericBase implements SearchableAttributeValue, Serializable {
57  
58      private static final long serialVersionUID = -6682101853805320760L;
59  
60      private static final String ATTRIBUTE_DATABASE_TABLE_NAME = "KREW_DOC_HDR_EXT_FLT_T";
61      private static final boolean DEFAULT_WILDCARD_ALLOWANCE_POLICY = false;
62      private static final boolean ALLOWS_RANGE_SEARCH = true;
63      private static final boolean ALLOWS_CASE_INSENSITIVE_SEARCH = false;
64      private static final String ATTRIBUTE_XML_REPRESENTATION = KewApiConstants.SearchableAttributeConstants.DATA_TYPE_FLOAT;
65      private static final String DEFAULT_FORMAT_PATTERN = "";
66  
67      private static final String DEFAULT_VALIDATION_REGEX_EXPRESSION = "[-+]?[0-9]*\\.?[0-9]+";
68      private static final Pattern defaultValidationPattern = Pattern.compile(DEFAULT_VALIDATION_REGEX_EXPRESSION);
69  
70      @Column(name="VAL")
71  	private BigDecimal searchableAttributeValue;
72  
73      /**
74       * Default constructor.
75       */
76      public SearchableAttributeFloatValue() {
77          super();
78          this.ojbConcreteClass = this.getClass().getName();
79      }
80  
81      @Override
82      public void setupAttributeValue(String value) {
83          this.setSearchableAttributeValue(convertStringToBigDecimal(value));
84      }
85  
86      private BigDecimal convertStringToBigDecimal(String value) {
87          if (org.apache.commons.lang.StringUtils.isEmpty(value)) {
88              return null;
89          } else {
90              return new BigDecimal(value);
91          }
92      }
93  
94      @Override
95  	public void setupAttributeValue(ResultSet resultSet, String columnName) throws SQLException {
96  		this.setSearchableAttributeValue(resultSet.getBigDecimal(columnName));
97  	}
98  
99      @Override
100     public String getSearchableAttributeDisplayValue() {
101 	    NumberFormat format = DecimalFormat.getInstance();
102 	    ((DecimalFormat)format).toPattern();
103 	    ((DecimalFormat)format).applyPattern(DEFAULT_FORMAT_PATTERN);
104 	    return format.format(getSearchableAttributeValue().doubleValue());
105 	}
106 
107     @Override
108 	public String getAttributeDataType() {
109 		return ATTRIBUTE_XML_REPRESENTATION;
110 	}
111 
112     @Override
113 	public String getAttributeTableName() {
114 		return ATTRIBUTE_DATABASE_TABLE_NAME;
115 	}
116 
117     @Override
118 	public boolean allowsWildcards() {
119 		return DEFAULT_WILDCARD_ALLOWANCE_POLICY;
120 	}
121 
122     @Override
123 	public boolean allowsCaseInsensitivity() {
124 		return ALLOWS_CASE_INSENSITIVE_SEARCH;
125 	}
126 
127     @Override
128 	public boolean allowsRangeSearches() {
129 		return ALLOWS_RANGE_SEARCH;
130 	}
131 
132     @Override
133     public Boolean isRangeValid(String lowerValue, String upperValue) {
134         if (allowsRangeSearches()) {
135             BigDecimal lower = null;
136             BigDecimal upper = null;
137             try{
138             	lower = convertStringToBigDecimal(lowerValue);
139             	upper = convertStringToBigDecimal(upperValue);
140             }catch(NumberFormatException ex){
141             	return false;
142             }
143             if ( (lower != null) && (upper != null) ) {
144                 return (lower.compareTo(upper) <= 0);
145             }
146             return true;
147         }
148         return null;
149     }
150 
151     @Override
152     public BigDecimal getSearchableAttributeValue() {
153         return searchableAttributeValue;
154     }
155 
156     public void setSearchableAttributeValue(BigDecimal searchableAttributeValue) {
157         this.searchableAttributeValue = searchableAttributeValue;
158     }
159 
160     /**
161      * @deprecated USE method setSearchableAttributeValue(BigDecimal) instead
162      */
163     @Deprecated
164     public void setSearchableAttributeValue(Float floatValueToTranslate) {
165         this.searchableAttributeValue = null;
166         if (floatValueToTranslate != null) {
167             this.searchableAttributeValue = new BigDecimal(floatValueToTranslate.toString());
168         }
169     }
170 
171     @Override
172     public DocumentAttributeDecimal toDocumentAttribute() {
173         return DocumentAttributeFactory.createDecimalAttribute(getSearchableAttributeKey(), getSearchableAttributeValue());
174     }
175 
176     @Override
177     protected Pattern getDefaultValidationPattern() {
178         return defaultValidationPattern;
179     }
180 }
181