Coverage Report - org.kuali.rice.kew.docsearch.SearchableAttributeFloatValue
 
Classes in this File Line Coverage Branch Coverage Complexity
SearchableAttributeFloatValue
0%
0/84
0%
0/34
1.815
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.kew.docsearch;
 18  
 
 19  
 import java.math.BigDecimal;
 20  
 import java.sql.ResultSet;
 21  
 import java.sql.SQLException;
 22  
 import java.text.DecimalFormat;
 23  
 import java.text.NumberFormat;
 24  
 import java.util.Arrays;
 25  
 import java.util.List;
 26  
 import java.util.regex.Matcher;
 27  
 import java.util.regex.Pattern;
 28  
 
 29  
 import javax.persistence.CascadeType;
 30  
 import javax.persistence.Column;
 31  
 import javax.persistence.Entity;
 32  
 import javax.persistence.FetchType;
 33  
 import javax.persistence.Id;
 34  
 import javax.persistence.JoinColumn;
 35  
 import javax.persistence.ManyToOne;
 36  
 import javax.persistence.NamedQueries;
 37  
 import javax.persistence.NamedQuery;
 38  
 import javax.persistence.PrePersist;
 39  
 import javax.persistence.Table;
 40  
 import javax.persistence.Transient;
 41  
 
 42  
 import org.apache.commons.lang.StringUtils;
 43  
 import org.kuali.rice.core.jdbc.SqlBuilder;
 44  
 import org.kuali.rice.core.jpa.annotations.Sequence;
 45  
 import org.kuali.rice.core.util.OrmUtils;
 46  
 import org.kuali.rice.kew.bo.WorkflowPersistable;
 47  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 48  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 49  
 import org.kuali.rice.kew.util.Utilities;
 50  
 import org.kuali.rice.kns.util.KNSConstants;
 51  
 
 52  
 /**
 53  
  *
 54  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 55  
  */
 56  0
 @Entity
 57  
 @Table(name="KREW_DOC_HDR_EXT_FLT_T")
 58  
 @Sequence(name="KREW_SRCH_ATTR_S",property="searchableAttributeValueId")
 59  
 @NamedQueries({
 60  
         @NamedQuery(name="SearchableAttributeFloatValue.FindByRouteHeaderId", query="select s from SearchableAttributeFloatValue as s where s.routeHeaderId = :routeHeaderId"),
 61  
         @NamedQuery(name="SearchableAttributeFloatValue.FindByKey", query="select s from SearchableAttributeFloatValue as s where s.routeHeaderId = :routeHeaderId and s.searchableAttributeKey = :searchableAttributeKey")
 62  
 })
 63  
 public class SearchableAttributeFloatValue implements WorkflowPersistable, SearchableAttributeValue {
 64  
 
 65  
     private static final long serialVersionUID = -6682101853805320760L;
 66  
 
 67  
     private static final String ATTRIBUTE_DATABASE_TABLE_NAME = "KREW_DOC_HDR_EXT_FLT_T";
 68  
     private static final boolean DEFAULT_WILDCARD_ALLOWANCE_POLICY = false;
 69  
     private static final boolean ALLOWS_RANGE_SEARCH = true;
 70  
     private static final boolean ALLOWS_CASE_INSENSITIVE_SEARCH = false;
 71  
     private static final String DEFAULT_VALIDATION_REGEX_EXPRESSION = "[-+]?[0-9]*\\.?[0-9]+";
 72  
     private static final String ATTRIBUTE_XML_REPRESENTATION = SearchableAttribute.DATA_TYPE_FLOAT;
 73  
     private static final String DEFAULT_FORMAT_PATTERN = "";
 74  
 
 75  
     @Id
 76  
         @Column(name="DOC_HDR_EXT_FLT_ID")
 77  
         private Long searchableAttributeValueId;
 78  
     @Column(name="KEY_CD")
 79  
         private String searchableAttributeKey;
 80  
     @Column(name="VAL")
 81  
         private BigDecimal searchableAttributeValue;
 82  
     @Transient
 83  
     protected String ojbConcreteClass; // attribute needed for OJB polymorphism - do not alter!
 84  
 
 85  
     @Column(name="DOC_HDR_ID")
 86  
         private Long routeHeaderId;
 87  
     @ManyToOne(fetch=FetchType.EAGER, cascade={CascadeType.PERSIST})
 88  
         @JoinColumn(name="DOC_HDR_ID", insertable=false, updatable=false)
 89  
         private DocumentRouteHeaderValue routeHeader;
 90  
 
 91  
     /**
 92  
      * Default constructor.
 93  
      */
 94  
     public SearchableAttributeFloatValue() {
 95  0
         super();
 96  0
         this.ojbConcreteClass = this.getClass().getName();
 97  0
     }
 98  
 
 99  
     /* (non-Javadoc)
 100  
      * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#setupAttributeValue(java.lang.String)
 101  
      */
 102  
     public void setupAttributeValue(String value) {
 103  0
         this.setSearchableAttributeValue(convertStringToBigDecimal(value));
 104  0
     }
 105  
 
 106  
     private BigDecimal convertStringToBigDecimal(String value) {
 107  0
         if (Utilities.isEmpty(value)) {
 108  0
             return null;
 109  
         } else {
 110  0
             return new BigDecimal(value);
 111  
         }
 112  
     }
 113  
 
 114  
         /* (non-Javadoc)
 115  
          * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#setupAttributeValue(java.sql.ResultSet, java.lang.String)
 116  
          */
 117  
         public void setupAttributeValue(ResultSet resultSet, String columnName) throws SQLException {
 118  0
                 this.setSearchableAttributeValue(resultSet.getBigDecimal(columnName));
 119  0
         }
 120  
 
 121  
     /* (non-Javadoc)
 122  
      * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#getSearchableAttributeDisplayValue(java.util.Map)
 123  
      */
 124  
     public String getSearchableAttributeDisplayValue() {
 125  0
             NumberFormat format = DecimalFormat.getInstance();
 126  0
             ((DecimalFormat)format).toPattern();
 127  0
             ((DecimalFormat)format).applyPattern(DEFAULT_FORMAT_PATTERN);
 128  0
             return format.format(getSearchableAttributeValue().doubleValue());
 129  
         }
 130  
 
 131  
     /* (non-Javadoc)
 132  
          * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#getAttributeDataType()
 133  
          */
 134  
         public String getAttributeDataType() {
 135  0
                 return ATTRIBUTE_XML_REPRESENTATION;
 136  
         }
 137  
 
 138  
         /* (non-Javadoc)
 139  
          * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#getAttributeTableName()
 140  
          */
 141  
         public String getAttributeTableName() {
 142  0
                 return ATTRIBUTE_DATABASE_TABLE_NAME;
 143  
         }
 144  
 
 145  
     /* (non-Javadoc)
 146  
          * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#allowsWildcardsByDefault()
 147  
          */
 148  
         public boolean allowsWildcards() {
 149  0
                 return DEFAULT_WILDCARD_ALLOWANCE_POLICY;
 150  
         }
 151  
 
 152  
     /* (non-Javadoc)
 153  
          * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#allowsCaseInsensitivity()
 154  
          */
 155  
         public boolean allowsCaseInsensitivity() {
 156  0
                 return ALLOWS_CASE_INSENSITIVE_SEARCH;
 157  
         }
 158  
 
 159  
     /* (non-Javadoc)
 160  
          * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#allowsRangeSearches()
 161  
          */
 162  
         public boolean allowsRangeSearches() {
 163  0
                 return ALLOWS_RANGE_SEARCH;
 164  
         }
 165  
 
 166  
         /* (non-Javadoc)
 167  
          * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#isPassesDefaultValidation()
 168  
          */
 169  
     public boolean isPassesDefaultValidation(String valueEntered) {
 170  
 
 171  0
             boolean bRet = true;
 172  0
             boolean bSplit = false;
 173  
 
 174  0
                 if (StringUtils.contains(valueEntered, KNSConstants.BETWEEN_OPERATOR)) {
 175  0
                         List<String> l = Arrays.asList(valueEntered.split("\\.\\."));
 176  0
                         for(String value : l){
 177  0
                                 bSplit = true;
 178  0
                                 if(!isPassesDefaultValidation(value)){
 179  0
                                         bRet = false;
 180  
                                 }
 181  
                         }
 182  
                 }
 183  0
                 if (StringUtils.contains(valueEntered, KNSConstants.OR_LOGICAL_OPERATOR)) {
 184  
                         //splitValueList.addAll(Arrays.asList(StringUtils.split(valueEntered, KNSConstants.OR_LOGICAL_OPERATOR)));
 185  0
                         List<String> l = Arrays.asList(StringUtils.split(valueEntered, KNSConstants.OR_LOGICAL_OPERATOR));
 186  0
                         for(String value : l){
 187  0
                                 bSplit = true;
 188  0
                                 if(!isPassesDefaultValidation(value)){
 189  0
                                         bRet = false;
 190  
                                 }
 191  
                         }
 192  
                 }
 193  0
                 if (StringUtils.contains(valueEntered, KNSConstants.AND_LOGICAL_OPERATOR)) {
 194  
                         //splitValueList.addAll(Arrays.asList(StringUtils.split(valueEntered, KNSConstants.AND_LOGICAL_OPERATOR)));
 195  0
                         List<String> l = Arrays.asList(StringUtils.split(valueEntered, KNSConstants.AND_LOGICAL_OPERATOR));
 196  0
                         for(String value : l){
 197  0
                                 bSplit = true;
 198  0
                                 if(!isPassesDefaultValidation(value)){
 199  0
                                         bRet = false;
 200  
                                 }
 201  
                         }
 202  
                 }
 203  
 
 204  0
                 if(bSplit){
 205  0
                         return bRet;
 206  
                 }
 207  
 
 208  0
                 Pattern pattern = Pattern.compile(DEFAULT_VALIDATION_REGEX_EXPRESSION);
 209  0
                 Matcher matcher = pattern.matcher(SqlBuilder.cleanNumericOfValidOperators(valueEntered).trim());
 210  0
                 if(!matcher.matches()){
 211  0
                         bRet = false;
 212  
                 }
 213  
 
 214  0
                 return bRet;
 215  
 
 216  
     }
 217  
 
 218  
     /* (non-Javadoc)
 219  
      * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#isRangeValid(java.lang.String, java.lang.String)
 220  
      */
 221  
     public Boolean isRangeValid(String lowerValue, String upperValue) {
 222  0
         if (allowsRangeSearches()) {
 223  0
             BigDecimal lower = null;
 224  0
             BigDecimal upper = null;
 225  
             try{
 226  0
                     lower = convertStringToBigDecimal(lowerValue);
 227  0
                     upper = convertStringToBigDecimal(upperValue);
 228  0
             }catch(NumberFormatException ex){
 229  0
                     return false;
 230  0
             }
 231  0
             if ( (lower != null) && (upper != null) ) {
 232  0
                 return (lower.compareTo(upper) <= 0);
 233  
             }
 234  0
             return true;
 235  
         }
 236  0
         return null;
 237  
     }
 238  
 
 239  
         public String getOjbConcreteClass() {
 240  0
         return ojbConcreteClass;
 241  
     }
 242  
 
 243  
     public void setOjbConcreteClass(String ojbConcreteClass) {
 244  0
         this.ojbConcreteClass = ojbConcreteClass;
 245  0
     }
 246  
 
 247  
     public DocumentRouteHeaderValue getRouteHeader() {
 248  0
         return routeHeader;
 249  
     }
 250  
 
 251  
     public void setRouteHeader(DocumentRouteHeaderValue routeHeader) {
 252  0
         this.routeHeader = routeHeader;
 253  0
     }
 254  
 
 255  
     public Long getRouteHeaderId() {
 256  0
         return routeHeaderId;
 257  
     }
 258  
 
 259  
     public void setRouteHeaderId(Long routeHeaderId) {
 260  0
         this.routeHeaderId = routeHeaderId;
 261  0
     }
 262  
 
 263  
     public String getSearchableAttributeKey() {
 264  0
         return searchableAttributeKey;
 265  
     }
 266  
 
 267  
     public void setSearchableAttributeKey(String searchableAttributeKey) {
 268  0
         this.searchableAttributeKey = searchableAttributeKey;
 269  0
     }
 270  
 
 271  
     public BigDecimal getSearchableAttributeValue() {
 272  0
         return searchableAttributeValue;
 273  
     }
 274  
 
 275  
     public void setSearchableAttributeValue(BigDecimal searchableAttributeValue) {
 276  0
         this.searchableAttributeValue = searchableAttributeValue;
 277  0
     }
 278  
 
 279  
     /**
 280  
      * @deprecated USE method setSearchableAttributeValue(BigDecimal) instead
 281  
      */
 282  
     public void setSearchableAttributeValue(Float floatValueToTranslate) {
 283  0
         this.searchableAttributeValue = null;
 284  0
         if (floatValueToTranslate != null) {
 285  0
             this.searchableAttributeValue = new BigDecimal(floatValueToTranslate.toString());
 286  
         }
 287  0
     }
 288  
 
 289  
     public Long getSearchableAttributeValueId() {
 290  0
         return searchableAttributeValueId;
 291  
     }
 292  
 
 293  
     public void setSearchableAttributeValueId(Long searchableAttributeValueId) {
 294  0
         this.searchableAttributeValueId = searchableAttributeValueId;
 295  0
     }
 296  
 
 297  
     /* (non-Javadoc)
 298  
      * @see org.kuali.rice.kew.WorkflowPersistable#copy(boolean)
 299  
      */
 300  
     @Deprecated
 301  
     public Object copy(boolean preserveKeys) {
 302  0
         return null;
 303  
     }
 304  
 
 305  
         @PrePersist
 306  
         public void beforeInsert(){
 307  0
                 OrmUtils.populateAutoIncValue(this, KEWServiceLocator.getEntityManagerFactory().createEntityManager());
 308  0
         }
 309  
 
 310  
 }
 311