Coverage Report - org.kuali.rice.kew.docsearch.SearchableAttributeDateTimeValue
 
Classes in this File Line Coverage Branch Coverage Complexity
SearchableAttributeDateTimeValue
0%
0/64
0%
0/14
1.444
 
 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 org.apache.commons.lang.StringUtils;
 20  
 import org.hibernate.annotations.GenericGenerator;
 21  
 import org.hibernate.annotations.Parameter;
 22  
 import org.kuali.rice.core.api.services.CoreApiServiceLocator;
 23  
 import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
 24  
 import org.kuali.rice.core.framework.persistence.jdbc.sql.SqlBuilder;
 25  
 import org.kuali.rice.core.util.RiceConstants;
 26  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 27  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 28  
 import org.kuali.rice.kew.util.KEWConstants;
 29  
 
 30  
 import javax.persistence.*;
 31  
 import java.io.Serializable;
 32  
 import java.sql.Date;
 33  
 import java.sql.ResultSet;
 34  
 import java.sql.SQLException;
 35  
 import java.sql.Timestamp;
 36  
 import java.text.DateFormat;
 37  
 import java.text.ParseException;
 38  
 import java.text.SimpleDateFormat;
 39  
 import java.util.Calendar;
 40  
 
 41  
 
 42  
 /**
 43  
  *
 44  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 45  
  */
 46  0
 @Entity
 47  
 @Table(name="KREW_DOC_HDR_EXT_DT_T")
 48  
 //@Sequence(name="KREW_SRCH_ATTR_S",property="searchableAttributeValueId")
 49  
 @NamedQueries({
 50  
         @NamedQuery(name="SearchableAttributeDateTimeValue.FindByDocumentId", query="select s from SearchableAttributeDateTimeValue as s where s.documentId = :documentId"),
 51  
         @NamedQuery(name="SearchableAttributeDateTimeValue.FindByKey", query="select s from SearchableAttributeDateTimeValue as s where s.documentId = :documentId and s.searchableAttributeKey = :searchableAttributeKey")
 52  
 })
 53  
 public class SearchableAttributeDateTimeValue implements SearchableAttributeValue, Serializable {
 54  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SearchableAttributeDateTimeValue.class);
 55  
 
 56  
     private static final long serialVersionUID = 3045621112943214772L;
 57  
 
 58  
     private static final String ATTRIBUTE_DATABASE_TABLE_NAME = "KREW_DOC_HDR_EXT_DT_T";
 59  
     private static final boolean DEFAULT_WILDCARD_ALLOWANCE_POLICY = false;
 60  
     private static final boolean ALLOWS_RANGE_SEARCH = true;
 61  
     private static final boolean ALLOWS_CASE_INSENSITIVE_SEARCH = false;
 62  
     private static final String ATTRIBUTE_XML_REPRESENTATION = KEWConstants.SearchableAttributeConstants.DATA_TYPE_DATE;
 63  
 
 64  
     @Id
 65  
     @GeneratedValue(generator="KREW_SRCH_ATTR_S")
 66  
         @GenericGenerator(name="KREW_SRCH_ATTR_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
 67  
                         @Parameter(name="sequence_name",value="KREW_SRCH_ATTR_S"),
 68  
                         @Parameter(name="value_column",value="id")
 69  
         })
 70  
         @Column(name="DOC_HDR_EXT_DT_ID")
 71  
         private Long searchableAttributeValueId;
 72  
     @Column(name="KEY_CD")
 73  
         private String searchableAttributeKey;
 74  
         @Column(name="VAL")
 75  
         private Timestamp searchableAttributeValue;
 76  
     @Transient
 77  
     protected String ojbConcreteClass; // attribute needed for OJB polymorphism - do not alter!
 78  
 
 79  
     @Column(name="DOC_HDR_ID")
 80  
         private String documentId;
 81  
     @ManyToOne(fetch=FetchType.EAGER, cascade={CascadeType.PERSIST})
 82  
         @JoinColumn(name="DOC_HDR_ID", insertable=false, updatable=false)
 83  
         private DocumentRouteHeaderValue routeHeader;
 84  
 
 85  
     /**
 86  
      * Default constructor.
 87  
      */
 88  
     public SearchableAttributeDateTimeValue() {
 89  0
         super();
 90  0
         this.ojbConcreteClass = this.getClass().getName();
 91  0
     }
 92  
 
 93  
     /* (non-Javadoc)
 94  
      * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#setupAttributeValue(java.lang.String)
 95  
      */
 96  
     public void setupAttributeValue(String value) {
 97  0
         this.setSearchableAttributeValue(convertStringToTimestamp(value));
 98  0
     }
 99  
 
 100  
     private Timestamp convertStringToTimestamp(String value) {
 101  0
         if (org.apache.commons.lang.StringUtils.isEmpty(value)) {
 102  0
             return null;
 103  
         } else {
 104  
             Timestamp t;
 105  
             try {
 106  0
                     t = CoreApiServiceLocator.getDateTimeService().convertToSqlTimestamp(value);
 107  0
             } catch (ParseException e) {
 108  0
                     t = null;
 109  0
             }
 110  0
             if (t == null) {
 111  0
                 String errorMsg = "Error converting timestamp value '" + value + "' to valid timestamp object.";
 112  0
                 LOG.error("setupAttributeValue() " + errorMsg);
 113  0
                 throw new RuntimeException(errorMsg);
 114  
             }
 115  0
             return t;
 116  
         }
 117  
     }
 118  
 
 119  
         /* (non-Javadoc)
 120  
          * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#setupAttributeValue(java.sql.ResultSet, java.lang.String)
 121  
          */
 122  
         public void setupAttributeValue(ResultSet resultSet, String columnName) throws SQLException {
 123  0
                 Calendar c = Calendar.getInstance();
 124  0
                 c.clear(Calendar.HOUR);
 125  0
                 c.clear(Calendar.MINUTE);
 126  0
                 c.clear(Calendar.SECOND);
 127  0
                 c.clear(Calendar.MILLISECOND);
 128  0
                 this.setSearchableAttributeValue(resultSet.getTimestamp(columnName, c));
 129  0
         }
 130  
 
 131  
         /* (non-Javadoc)
 132  
          * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#getSearchableAttributeDisplayValue()
 133  
          */
 134  
     public String getSearchableAttributeDisplayValue() {
 135  0
         return formatAttributeValue(null);
 136  
     }
 137  
 
 138  
     private String formatAttributeValue(String formatPattern) {
 139  0
         DateFormat df = getDateFormatToUse(formatPattern);
 140  0
         return df.format(new Date(getSearchableAttributeValue().getTime()));
 141  
     }
 142  
 
 143  
     private DateFormat getDateFormatToUse(String parameterFormatPattern) {
 144  0
         if (StringUtils.isNotBlank(parameterFormatPattern)) {
 145  0
             return new SimpleDateFormat(parameterFormatPattern);
 146  
         }
 147  0
         return RiceConstants.getDefaultDateFormat();
 148  
     }
 149  
 
 150  
         /* (non-Javadoc)
 151  
          * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#getAttributeDataType()
 152  
          */
 153  
         public String getAttributeDataType() {
 154  0
                 return ATTRIBUTE_XML_REPRESENTATION;
 155  
         }
 156  
 
 157  
         /* (non-Javadoc)
 158  
          * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#getAttributeTableName()
 159  
          */
 160  
         public String getAttributeTableName() {
 161  0
                 return ATTRIBUTE_DATABASE_TABLE_NAME;
 162  
         }
 163  
 
 164  
     /* (non-Javadoc)
 165  
          * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#allowsWildcardsByDefault()
 166  
          */
 167  
         public boolean allowsWildcards() {
 168  0
                 return DEFAULT_WILDCARD_ALLOWANCE_POLICY;
 169  
         }
 170  
 
 171  
     /* (non-Javadoc)
 172  
          * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#allowsCaseInsensitivity()
 173  
          */
 174  
         public boolean allowsCaseInsensitivity() {
 175  0
                 return ALLOWS_CASE_INSENSITIVE_SEARCH;
 176  
         }
 177  
 
 178  
         /* (non-Javadoc)
 179  
          * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#allowsRangeSearches()
 180  
          */
 181  
         public boolean allowsRangeSearches() {
 182  0
                 return ALLOWS_RANGE_SEARCH;
 183  
         }
 184  
 
 185  
         /* (non-Javadoc)
 186  
          * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#isPassesDefaultValidation()
 187  
          */
 188  
     public boolean isPassesDefaultValidation(String valueEntered) {
 189  0
             return new SqlBuilder().isValidDate(valueEntered);
 190  
         //return (DocSearchUtils.getEntryFormattedDate(valueEntered) != null);
 191  
     }
 192  
 
 193  
     /* (non-Javadoc)
 194  
      * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#isRangeValid(java.lang.String, java.lang.String)
 195  
      */
 196  
     public Boolean isRangeValid(String lowerValue, String upperValue) {
 197  0
         if (allowsRangeSearches()) {
 198  0
             Timestamp lowerTime = convertStringToTimestamp(lowerValue);
 199  0
             Timestamp upperTime = convertStringToTimestamp(upperValue);
 200  0
             if ( (lowerTime != null) && (upperTime != null) ) {
 201  0
                 return (lowerTime.compareTo(upperTime) <= 0);
 202  
             }
 203  0
             return true;
 204  
         }
 205  0
         return null;
 206  
     }
 207  
 
 208  
         public String getOjbConcreteClass() {
 209  0
         return ojbConcreteClass;
 210  
     }
 211  
 
 212  
     public void setOjbConcreteClass(String ojbConcreteClass) {
 213  0
         this.ojbConcreteClass = ojbConcreteClass;
 214  0
     }
 215  
 
 216  
     public DocumentRouteHeaderValue getRouteHeader() {
 217  0
         return routeHeader;
 218  
     }
 219  
 
 220  
     public void setRouteHeader(DocumentRouteHeaderValue routeHeader) {
 221  0
         this.routeHeader = routeHeader;
 222  0
     }
 223  
 
 224  
     public String getDocumentId() {
 225  0
         return documentId;
 226  
     }
 227  
 
 228  
     public void setDocumentId(String documentId) {
 229  0
         this.documentId = documentId;
 230  0
     }
 231  
 
 232  
     public String getSearchableAttributeKey() {
 233  0
         return searchableAttributeKey;
 234  
     }
 235  
 
 236  
     public void setSearchableAttributeKey(String searchableAttributeKey) {
 237  0
         this.searchableAttributeKey = searchableAttributeKey;
 238  0
     }
 239  
 
 240  
     public Timestamp getSearchableAttributeValue() {
 241  0
         return searchableAttributeValue;
 242  
     }
 243  
 
 244  
     public void setSearchableAttributeValue(Timestamp searchableAttributeValue) {
 245  0
         this.searchableAttributeValue = searchableAttributeValue;
 246  0
     }
 247  
 
 248  
     public Long getSearchableAttributeValueId() {
 249  0
         return searchableAttributeValueId;
 250  
     }
 251  
 
 252  
     public void setSearchableAttributeValueId(Long searchableAttributeValueId) {
 253  0
         this.searchableAttributeValueId = searchableAttributeValueId;
 254  0
     }
 255  
 
 256  
         //@PrePersist
 257  
         public void beforeInsert(){
 258  0
                 OrmUtils.populateAutoIncValue(this, KEWServiceLocator.getEntityManagerFactory().createEntityManager());
 259  0
         }
 260  
 }
 261