Coverage Report - org.kuali.rice.kew.docsearch.SearchableAttributeLongValue
 
Classes in this File Line Coverage Branch Coverage Complexity
SearchableAttributeLongValue
0%
0/74
0%
0/32
1.731
 
 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.sql.ResultSet;
 20  
 import java.sql.SQLException;
 21  
 import java.text.DecimalFormat;
 22  
 import java.text.NumberFormat;
 23  
 import java.util.Arrays;
 24  
 import java.util.List;
 25  
 import java.util.regex.Matcher;
 26  
 import java.util.regex.Pattern;
 27  
 
 28  
 import javax.persistence.CascadeType;
 29  
 import javax.persistence.Column;
 30  
 import javax.persistence.Entity;
 31  
 import javax.persistence.FetchType;
 32  
 import javax.persistence.Id;
 33  
 import javax.persistence.JoinColumn;
 34  
 import javax.persistence.ManyToOne;
 35  
 import javax.persistence.NamedQueries;
 36  
 import javax.persistence.NamedQuery;
 37  
 import javax.persistence.PrePersist;
 38  
 import javax.persistence.Table;
 39  
 import javax.persistence.Transient;
 40  
 
 41  
 import org.apache.commons.lang.StringUtils;
 42  
 import org.kuali.rice.core.jdbc.SqlBuilder;
 43  
 import org.kuali.rice.core.jpa.annotations.Sequence;
 44  
 import org.kuali.rice.core.util.OrmUtils;
 45  
 import org.kuali.rice.kew.bo.WorkflowPersistable;
 46  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 47  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 48  
 import org.kuali.rice.kew.util.Utilities;
 49  
 import org.kuali.rice.kns.util.KNSConstants;
 50  
 
 51  
 /**
 52  
  *
 53  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 54  
  */
 55  0
 @Entity
 56  
 @Table(name="KREW_DOC_HDR_EXT_LONG_T")
 57  
 @Sequence(name="KREW_SRCH_ATTR_S",property="searchableAttributeValueId")
 58  
 @NamedQueries({
 59  
         @NamedQuery(name="SearchableAttributeLongValue.FindByRouteHeaderId", query="select s from SearchableAttributeLongValue as s where s.routeHeaderId = :routeHeaderId"),
 60  
         @NamedQuery(name="SearchableAttributeLongValue.FindByKey", query="select s from SearchableAttributeLongValue as s where s.routeHeaderId = :routeHeaderId and s.searchableAttributeKey = :searchableAttributeKey")
 61  
 })
 62  
 public class SearchableAttributeLongValue implements WorkflowPersistable, SearchableAttributeValue {
 63  
 
 64  
     private static final long serialVersionUID = 5786144436732198346L;
 65  
 
 66  
     private static final String ATTRIBUTE_DATABASE_TABLE_NAME = "KREW_DOC_HDR_EXT_LONG_T";
 67  
     private static final boolean DEFAULT_WILDCARD_ALLOWANCE_POLICY = false;
 68  
     private static final boolean ALLOWS_RANGE_SEARCH = true;
 69  
     private static final boolean ALLOWS_CASE_INSENSITIVE_SEARCH = false;
 70  
     private static final String DEFAULT_VALIDATION_REGEX_EXPRESSION = "^-?[0-9]+$";
 71  
     private static final String ATTRIBUTE_XML_REPRESENTATION = SearchableAttribute.DATA_TYPE_LONG;
 72  
     private static final String DEFAULT_FORMAT_PATTERN = "#";
 73  
 
 74  
     @Id
 75  
         @Column(name="DOC_HDR_EXT_LONG_ID")
 76  
         private Long searchableAttributeValueId;
 77  
     @Column(name="KEY_CD")
 78  
         private String searchableAttributeKey;
 79  
     @Column(name="VAL")
 80  
         private Long searchableAttributeValue;
 81  
     @Transient
 82  
     protected String ojbConcreteClass; // attribute needed for OJB polymorphism - do not alter!
 83  
 
 84  
     @Column(name="DOC_HDR_ID")
 85  
         private Long routeHeaderId;
 86  
     @ManyToOne(fetch=FetchType.EAGER, cascade={CascadeType.PERSIST})
 87  
         @JoinColumn(name="DOC_HDR_ID", insertable=false, updatable=false)
 88  
         private DocumentRouteHeaderValue routeHeader;
 89  
 
 90  
     /**
 91  
      * Default constructor.
 92  
      */
 93  
     public SearchableAttributeLongValue() {
 94  0
         super();
 95  0
         this.ojbConcreteClass = this.getClass().getName();
 96  0
     }
 97  
 
 98  
     /* (non-Javadoc)
 99  
      * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#setupAttributeValue(java.lang.String)
 100  
      */
 101  
     public void setupAttributeValue(String value) {
 102  0
         this.setSearchableAttributeValue(convertStringToLong(value));
 103  0
     }
 104  
 
 105  
     private Long convertStringToLong(String value) {
 106  0
         if (Utilities.isEmpty(value)) {
 107  0
             return null;
 108  
         } else {
 109  0
             return Long.valueOf(value);
 110  
         }
 111  
     }
 112  
 
 113  
         /* (non-Javadoc)
 114  
          * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#setupAttributeValue(java.sql.ResultSet, java.lang.String)
 115  
          */
 116  
         public void setupAttributeValue(ResultSet resultSet, String columnName) throws SQLException {
 117  0
                 this.setSearchableAttributeValue(resultSet.getLong(columnName));
 118  0
         }
 119  
 
 120  
     /* (non-Javadoc)
 121  
      * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#getSearchableAttributeDisplayValue(java.util.Map)
 122  
      */
 123  
     public String getSearchableAttributeDisplayValue() {
 124  0
         NumberFormat format = DecimalFormat.getInstance();
 125  0
         ((DecimalFormat)format).applyPattern(DEFAULT_FORMAT_PATTERN);
 126  0
         return format.format(getSearchableAttributeValue().longValue());
 127  
     }
 128  
 
 129  
         /* (non-Javadoc)
 130  
          * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#getAttributeDataType()
 131  
          */
 132  
         public String getAttributeDataType() {
 133  0
                 return ATTRIBUTE_XML_REPRESENTATION;
 134  
         }
 135  
 
 136  
         /* (non-Javadoc)
 137  
          * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#getAttributeTableName()
 138  
          */
 139  
         public String getAttributeTableName() {
 140  0
                 return ATTRIBUTE_DATABASE_TABLE_NAME;
 141  
         }
 142  
 
 143  
     /* (non-Javadoc)
 144  
          * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#allowsWildcardsByDefault()
 145  
          */
 146  
         public boolean allowsWildcards() {
 147  0
                 return DEFAULT_WILDCARD_ALLOWANCE_POLICY;
 148  
         }
 149  
 
 150  
     /* (non-Javadoc)
 151  
          * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#allowsCaseInsensitivity()
 152  
          */
 153  
         public boolean allowsCaseInsensitivity() {
 154  0
                 return ALLOWS_CASE_INSENSITIVE_SEARCH;
 155  
         }
 156  
 
 157  
     /* (non-Javadoc)
 158  
          * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#allowsRangeSearches()
 159  
          */
 160  
         public boolean allowsRangeSearches() {
 161  0
                 return ALLOWS_RANGE_SEARCH;
 162  
         }
 163  
 
 164  
         /* (non-Javadoc)
 165  
          * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#isPassesDefaultValidation()
 166  
          */
 167  
         public boolean isPassesDefaultValidation(String valueEntered) {
 168  
 
 169  0
             boolean bRet = true;
 170  0
             boolean bSplit = false;
 171  
 
 172  0
                 if (StringUtils.contains(valueEntered, KNSConstants.BETWEEN_OPERATOR)) {
 173  0
                         List<String> l = Arrays.asList(valueEntered.split("\\.\\."));
 174  0
                         for(String value : l){
 175  0
                                 bSplit = true;
 176  0
                                 if(!isPassesDefaultValidation(value)){
 177  0
                                         bRet = false;
 178  
                                 }
 179  
                         }
 180  
                 }
 181  0
                 if (StringUtils.contains(valueEntered, KNSConstants.OR_LOGICAL_OPERATOR)) {
 182  
                         //splitValueList.addAll(Arrays.asList(StringUtils.split(valueEntered, KNSConstants.OR_LOGICAL_OPERATOR)));
 183  0
                         List<String> l = Arrays.asList(StringUtils.split(valueEntered, KNSConstants.OR_LOGICAL_OPERATOR));
 184  0
                         for(String value : l){
 185  0
                                 bSplit = true;
 186  0
                                 if(!isPassesDefaultValidation(value)){
 187  0
                                         bRet = false;
 188  
                                 }
 189  
                         }
 190  
                 }
 191  0
                 if (StringUtils.contains(valueEntered, KNSConstants.AND_LOGICAL_OPERATOR)) {
 192  
                         //splitValueList.addAll(Arrays.asList(StringUtils.split(valueEntered, KNSConstants.AND_LOGICAL_OPERATOR)));
 193  0
                         List<String> l = Arrays.asList(StringUtils.split(valueEntered, KNSConstants.AND_LOGICAL_OPERATOR));
 194  0
                         for(String value : l){
 195  0
                                 bSplit = true;
 196  0
                                 if(!isPassesDefaultValidation(value)){
 197  0
                                         bRet = false;
 198  
                                 }
 199  
                         }
 200  
                 }
 201  
 
 202  0
                 if(bSplit){
 203  0
                         return bRet;
 204  
                 }
 205  
 
 206  0
                 Pattern pattern = Pattern.compile(DEFAULT_VALIDATION_REGEX_EXPRESSION);
 207  0
                 Matcher matcher = pattern.matcher(SqlBuilder.cleanNumericOfValidOperators(valueEntered).trim());
 208  0
                 if(!matcher.matches()){
 209  0
                         bRet = false;
 210  
                 }
 211  
 
 212  0
                 return bRet;
 213  
 
 214  
     }
 215  
 
 216  
 
 217  
     /* (non-Javadoc)
 218  
      * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#isRangeValid(java.lang.String, java.lang.String)
 219  
      */
 220  
     public Boolean isRangeValid(String lowerValue, String upperValue) {
 221  0
         if (allowsRangeSearches()) {
 222  0
             Long lower = convertStringToLong(lowerValue);
 223  0
             Long upper = convertStringToLong(upperValue);
 224  0
             if ( (lower != null) && (upper != null) ) {
 225  0
                 return (lower.compareTo(upper) <= 0);
 226  
             }
 227  0
             return true;
 228  
         }
 229  0
         return null;
 230  
     }
 231  
 
 232  
     public String getOjbConcreteClass() {
 233  0
         return ojbConcreteClass;
 234  
     }
 235  
 
 236  
     public void setOjbConcreteClass(String ojbConcreteClass) {
 237  0
         this.ojbConcreteClass = ojbConcreteClass;
 238  0
     }
 239  
 
 240  
     public DocumentRouteHeaderValue getRouteHeader() {
 241  0
         return routeHeader;
 242  
     }
 243  
 
 244  
     public void setRouteHeader(DocumentRouteHeaderValue routeHeader) {
 245  0
         this.routeHeader = routeHeader;
 246  0
     }
 247  
 
 248  
     public Long getRouteHeaderId() {
 249  0
         return routeHeaderId;
 250  
     }
 251  
 
 252  
     public void setRouteHeaderId(Long routeHeaderId) {
 253  0
         this.routeHeaderId = routeHeaderId;
 254  0
     }
 255  
 
 256  
     public String getSearchableAttributeKey() {
 257  0
         return searchableAttributeKey;
 258  
     }
 259  
 
 260  
     public void setSearchableAttributeKey(String searchableAttributeKey) {
 261  0
         this.searchableAttributeKey = searchableAttributeKey;
 262  0
     }
 263  
 
 264  
     public Long getSearchableAttributeValue() {
 265  0
         return searchableAttributeValue;
 266  
     }
 267  
 
 268  
     public void setSearchableAttributeValue(Long searchableAttributeValue) {
 269  0
         this.searchableAttributeValue = searchableAttributeValue;
 270  0
     }
 271  
 
 272  
     public Long getSearchableAttributeValueId() {
 273  0
         return searchableAttributeValueId;
 274  
     }
 275  
 
 276  
     public void setSearchableAttributeValueId(Long searchableAttributeValueId) {
 277  0
         this.searchableAttributeValueId = searchableAttributeValueId;
 278  0
     }
 279  
 
 280  
     /* (non-Javadoc)
 281  
      * @see org.kuali.rice.kew.WorkflowPersistable#copy(boolean)
 282  
      */
 283  
     public Object copy(boolean preserveKeys) {
 284  0
         return null;
 285  
     }
 286  
 
 287  
         @PrePersist
 288  
         public void beforeInsert(){
 289  0
                 OrmUtils.populateAutoIncValue(this, KEWServiceLocator.getEntityManagerFactory().createEntityManager());
 290  0
         }
 291  
 }
 292