View Javadoc
1   /**
2    * Copyright 2005-2014 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.DocumentAttributeFactory;
20  import org.kuali.rice.kew.api.document.attribute.DocumentAttributeInteger;
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.BigInteger;
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_LONG_T")
46  @NamedQueries({
47  	@NamedQuery(name="SearchableAttributeLongValue.FindByDocumentId", query="select s from "
48              + "SearchableAttributeLongValue as s where s.documentId = :documentId"),
49  	@NamedQuery(name="SearchableAttributeLongValue.FindByKey", query="select s from "
50              + "SearchableAttributeLongValue as s where s.documentId = :documentId and "
51              + "s.searchableAttributeKey = :searchableAttributeKey")
52  })
53  @AttributeOverrides({
54          @AttributeOverride(name="searchableAttributeValueId", column=@Column(name="DOC_HDR_EXT_LONG_ID"))
55  })
56  public class SearchableAttributeLongValue extends SearchableAttributeNumericBase implements SearchableAttributeValue, Serializable {
57  
58      private static final long serialVersionUID = 5786144436732198346L;
59  
60      private static final String ATTRIBUTE_DATABASE_TABLE_NAME = "KREW_DOC_HDR_EXT_LONG_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_LONG;
65      private static final String DEFAULT_FORMAT_PATTERN = "#";
66  
67      private static final String DEFAULT_VALIDATION_REGEX_EXPRESSION = "^-?[0-9]+$";
68      private static final Pattern defaultValidationPattern = Pattern.compile(DEFAULT_VALIDATION_REGEX_EXPRESSION);
69  
70      @Column(name="VAL")
71  	private Long searchableAttributeValue;
72  
73      /**
74       * Default constructor.
75       */
76      public SearchableAttributeLongValue() {
77          super();
78          this.ojbConcreteClass = this.getClass().getName();
79      }
80  
81      @Override
82      public void setupAttributeValue(String value) {
83          this.setSearchableAttributeValue(convertStringToLong(value));
84      }
85  
86      private Long convertStringToLong(String value) {
87          if (org.apache.commons.lang.StringUtils.isEmpty(value)) {
88              return null;
89          } else {
90              return Long.valueOf(value.trim());
91          }
92      }
93  
94      @Override
95  	public void setupAttributeValue(ResultSet resultSet, String columnName) throws SQLException {
96  		this.setSearchableAttributeValue(resultSet.getLong(columnName));
97  	}
98  
99      @Override
100     public String getSearchableAttributeDisplayValue() {
101         NumberFormat format = DecimalFormat.getInstance();
102         ((DecimalFormat)format).applyPattern(DEFAULT_FORMAT_PATTERN);
103         return format.format(getSearchableAttributeValue().longValue());
104     }
105 
106     @Override
107 	public String getAttributeDataType() {
108 		return ATTRIBUTE_XML_REPRESENTATION;
109 	}
110 
111     @Override
112 	public String getAttributeTableName() {
113 		return ATTRIBUTE_DATABASE_TABLE_NAME;
114 	}
115 
116     @Override
117 	public boolean allowsWildcards() {
118 		return DEFAULT_WILDCARD_ALLOWANCE_POLICY;
119 	}
120 
121     @Override
122 	public boolean allowsCaseInsensitivity() {
123 		return ALLOWS_CASE_INSENSITIVE_SEARCH;
124 	}
125 
126     @Override
127 	public boolean allowsRangeSearches() {
128 		return ALLOWS_RANGE_SEARCH;
129 	}
130 
131     @Override
132     public Boolean isRangeValid(String lowerValue, String upperValue) {
133         if (allowsRangeSearches()) {
134             Long lower = convertStringToLong(lowerValue);
135             Long upper = convertStringToLong(upperValue);
136             if ( (lower != null) && (upper != null) ) {
137                 return (lower.compareTo(upper) <= 0);
138             }
139             return true;
140         }
141         return null;
142     }
143 
144     @Override
145     public Long getSearchableAttributeValue() {
146         return searchableAttributeValue;
147     }
148 
149     public void setSearchableAttributeValue(Long searchableAttributeValue) {
150         this.searchableAttributeValue = searchableAttributeValue;
151     }
152 
153     @Override
154     public DocumentAttributeInteger toDocumentAttribute() {
155         BigInteger integer = null;
156         if (getSearchableAttributeValue() != null) {
157             integer = BigInteger.valueOf(getSearchableAttributeValue().longValue());
158         }
159         return DocumentAttributeFactory.createIntegerAttribute(getSearchableAttributeKey(), integer);
160     }
161 
162     @Override
163     protected Pattern getDefaultValidationPattern() {
164         return defaultValidationPattern;
165     }
166 }
167