View Javadoc

1   /**
2    * Copyright 2005-2012 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.ObjectUtils;
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.framework.persistence.jpa.OrmUtils;
23  import org.kuali.rice.kew.api.document.attribute.DocumentAttribute;
24  import org.kuali.rice.kew.api.document.attribute.DocumentAttributeFactory;
25  import org.kuali.rice.kew.api.document.attribute.DocumentAttributeString;
26  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
27  import org.kuali.rice.kew.service.KEWServiceLocator;
28  import org.kuali.rice.kew.api.KewApiConstants;
29  
30  import javax.persistence.*;
31  import java.io.Serializable;
32  import java.sql.ResultSet;
33  import java.sql.SQLException;
34  
35  
36  /**
37   *
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   */
40  @Entity
41  @Table(name="KREW_DOC_HDR_EXT_T")
42  //@Sequence(name="KREW_SRCH_ATTR_S",property="searchableAttributeValueId")
43  @NamedQueries({
44  	@NamedQuery(name="SearchableAttributeStringValue.FindByDocumentId", query="select s from SearchableAttributeStringValue as s where s.documentId = :documentId"),
45  	@NamedQuery(name="SearchableAttributeStringValue.FindByKey", query="select s from SearchableAttributeStringValue as s where s.documentId = :documentId and s.searchableAttributeKey = :searchableAttributeKey")
46  })
47  public class SearchableAttributeStringValue implements CaseAwareSearchableAttributeValue, Serializable {
48  
49      private static final long serialVersionUID = 8696089933682052078L;
50  
51      private static final String ATTRIBUTE_DATABASE_TABLE_NAME = "KREW_DOC_HDR_EXT_T";
52      private static final boolean DEFAULT_WILDCARD_ALLOWANCE_POLICY = true;
53      private static final boolean ALLOWS_RANGE_SEARCH = true;
54      private static final boolean ALLOWS_CASE_INSENSITIVE_SEARCH = true;
55      private static final String ATTRIBUTE_XML_REPRESENTATION = KewApiConstants.SearchableAttributeConstants.DATA_TYPE_STRING;
56      private static final int STRING_MAX_LENGTH = 2000; // should match table creation
57  
58      @Id
59      @GeneratedValue(generator="KREW_SRCH_ATTR_S")
60  	@GenericGenerator(name="KREW_SRCH_ATTR_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
61  			@Parameter(name="sequence_name",value="KREW_SRCH_ATTR_S"),
62  			@Parameter(name="value_column",value="id")
63  	})
64  	@Column(name="DOC_HDR_EXT_ID")
65  	private String searchableAttributeValueId;
66      @Column(name="KEY_CD")
67  	private String searchableAttributeKey;
68      @Column(name="VAL")
69  	private String searchableAttributeValue;
70      @Transient
71      protected String ojbConcreteClass; // attribute needed for OJB polymorphism - do not alter!
72  
73      @Column(name="DOC_HDR_ID")
74  	private String documentId;
75      @ManyToOne(fetch=FetchType.EAGER, cascade={CascadeType.PERSIST})
76  	@JoinColumn(name="DOC_HDR_ID", insertable=false, updatable=false)
77  	private DocumentRouteHeaderValue routeHeader;
78  
79      /**
80       * Default constructor.
81       */
82      public SearchableAttributeStringValue() {
83      	super();
84          this.ojbConcreteClass = this.getClass().getName();
85      }
86  
87      /* (non-Javadoc)
88       * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#setupAttributeValue(java.lang.String)
89       */
90      public void setupAttributeValue(String value) {
91      	this.setSearchableAttributeValue(value);
92      }
93  
94  	/* (non-Javadoc)
95  	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#setupAttributeValue(java.sql.ResultSet, java.lang.String)
96  	 */
97  	public void setupAttributeValue(ResultSet resultSet, String columnName) throws SQLException {
98  		this.setSearchableAttributeValue(resultSet.getString(columnName));
99  	}
100 
101 	/* (non-Javadoc)
102 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#getSearchableAttributeDisplayValue()
103 	 */
104     public String getSearchableAttributeDisplayValue() {
105         return getSearchableAttributeValue();
106     }
107 
108 	/* (non-Javadoc)
109 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#getAttributeDataType()
110 	 */
111 	public String getAttributeDataType() {
112 		return ATTRIBUTE_XML_REPRESENTATION;
113 	}
114 
115 	/* (non-Javadoc)
116 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#getAttributeTableName()
117 	 */
118 	public String getAttributeTableName() {
119 		return ATTRIBUTE_DATABASE_TABLE_NAME;
120 	}
121 
122     /* (non-Javadoc)
123 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#allowsWildcardsByDefault()
124 	 */
125 	public boolean allowsWildcards() {
126 		return DEFAULT_WILDCARD_ALLOWANCE_POLICY;
127 	}
128 
129     /* (non-Javadoc)
130 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#allowsCaseInsensitivity()
131 	 */
132 	public boolean allowsCaseInsensitivity() {
133 		return ALLOWS_CASE_INSENSITIVE_SEARCH;
134 	}
135 
136     /* (non-Javadoc)
137 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#allowsRangeSearches()
138 	 */
139 	public boolean allowsRangeSearches() {
140 		return ALLOWS_RANGE_SEARCH;
141 	}
142 
143 	/**
144 	 * @return true if the {@code valueEntered} parameter is not null and is equal to or
145 	 * less than the specified max length defined by {@link #STRING_MAX_LENGTH}
146 	 *
147 	 * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#isPassesDefaultValidation()
148 	 */
149 	public boolean isPassesDefaultValidation(String valueEntered) {
150 	    if (valueEntered != null && (valueEntered.length() > STRING_MAX_LENGTH)) {
151 	        return false;
152 	    }
153 		return true;
154 	}
155 
156     /* (non-Javadoc)
157      * @see org.kuali.rice.kew.docsearch.SearchableAttributeValue#isRangeValid(java.lang.String, java.lang.String)
158      */
159     public Boolean isRangeValid(String lowerValue, String upperValue) {
160         return isRangeValid(lowerValue, upperValue, true);
161     }
162 
163     /* (non-Javadoc)
164      * @see org.kuali.rice.kew.docsearch.CaseAwareSearchableAttributeValue#isRangeValid(java.lang.String, java.lang.String, boolean)
165      */
166     public Boolean isRangeValid(String lowerValue, String upperValue, boolean caseSensitive) {
167         if (allowsRangeSearches()) {
168             return StringUtils.isBlank(lowerValue) ||
169                    StringUtils.isBlank(upperValue) ||
170                    (caseSensitive ?
171                      ObjectUtils.compare(lowerValue, upperValue) <= 0 :
172                      String.CASE_INSENSITIVE_ORDER.compare(lowerValue, upperValue) <= 0);
173         }
174         return null;
175     }
176 
177 	public String getOjbConcreteClass() {
178 		return ojbConcreteClass;
179 	}
180 
181 	public void setOjbConcreteClass(String ojbConcreteClass) {
182 		this.ojbConcreteClass = ojbConcreteClass;
183 	}
184 
185 	public DocumentRouteHeaderValue getRouteHeader() {
186 		return routeHeader;
187 	}
188 
189 	public void setRouteHeader(DocumentRouteHeaderValue routeHeader) {
190 		this.routeHeader = routeHeader;
191 	}
192 
193 	public String getDocumentId() {
194 		return documentId;
195 	}
196 
197 	public void setDocumentId(String documentId) {
198 		this.documentId = documentId;
199 	}
200 
201 	public String getSearchableAttributeKey() {
202 		return searchableAttributeKey;
203 	}
204 
205 	public void setSearchableAttributeKey(String searchableAttributeKey) {
206 		this.searchableAttributeKey = searchableAttributeKey;
207 	}
208 
209 	public String getSearchableAttributeValue() {
210 		return searchableAttributeValue;
211 	}
212 
213 	public void setSearchableAttributeValue(String searchableAttributeValue) {
214 		this.searchableAttributeValue = searchableAttributeValue;
215 	}
216 
217 	public String getSearchableAttributeValueId() {
218 		return searchableAttributeValueId;
219 	}
220 
221 	public void setSearchableAttributeValueId(String searchableAttributeValueId) {
222 		this.searchableAttributeValueId = searchableAttributeValueId;
223 	}
224 
225 	//@PrePersist
226 	public void beforeInsert(){
227 		OrmUtils.populateAutoIncValue(this, KEWServiceLocator.getEntityManagerFactory().createEntityManager());
228 	}
229 
230     @Override
231     public DocumentAttributeString toDocumentAttribute() {
232         return DocumentAttributeFactory.createStringAttribute(getSearchableAttributeKey(), getSearchableAttributeValue());
233     }
234 }
235