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