001/**
002 * Copyright 2005-2015 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kew.docsearch;
017
018import org.kuali.rice.kew.api.KewApiConstants;
019import org.kuali.rice.kew.api.document.attribute.DocumentAttributeFactory;
020import org.kuali.rice.kew.api.document.attribute.DocumentAttributeInteger;
021
022import javax.persistence.AttributeOverride;
023import javax.persistence.AttributeOverrides;
024import javax.persistence.Column;
025import javax.persistence.Entity;
026import javax.persistence.Inheritance;
027import javax.persistence.InheritanceType;
028import javax.persistence.NamedQueries;
029import javax.persistence.NamedQuery;
030import javax.persistence.Table;
031import java.io.Serializable;
032import java.math.BigInteger;
033import java.sql.ResultSet;
034import java.sql.SQLException;
035import java.text.DecimalFormat;
036import java.text.NumberFormat;
037import java.util.regex.Pattern;
038
039/**
040 *
041 * @author Kuali Rice Team (rice.collab@kuali.org)
042 */
043@Entity
044@Inheritance(strategy= InheritanceType.TABLE_PER_CLASS)
045@Table(name="KREW_DOC_HDR_EXT_LONG_T")
046@NamedQueries({
047        @NamedQuery(name="SearchableAttributeLongValue.FindByDocumentId", query="select s from "
048            + "SearchableAttributeLongValue as s where s.documentId = :documentId"),
049        @NamedQuery(name="SearchableAttributeLongValue.FindByKey", query="select s from "
050            + "SearchableAttributeLongValue as s where s.documentId = :documentId and "
051            + "s.searchableAttributeKey = :searchableAttributeKey")
052})
053@AttributeOverrides({
054        @AttributeOverride(name="searchableAttributeValueId", column=@Column(name="DOC_HDR_EXT_LONG_ID"))
055})
056public class SearchableAttributeLongValue extends SearchableAttributeNumericBase implements SearchableAttributeValue, Serializable {
057
058    private static final long serialVersionUID = 5786144436732198346L;
059
060    private static final String ATTRIBUTE_DATABASE_TABLE_NAME = "KREW_DOC_HDR_EXT_LONG_T";
061    private static final boolean DEFAULT_WILDCARD_ALLOWANCE_POLICY = false;
062    private static final boolean ALLOWS_RANGE_SEARCH = true;
063    private static final boolean ALLOWS_CASE_INSENSITIVE_SEARCH = false;
064    private static final String ATTRIBUTE_XML_REPRESENTATION = KewApiConstants.SearchableAttributeConstants.DATA_TYPE_LONG;
065    private static final String DEFAULT_FORMAT_PATTERN = "#";
066
067    private static final String DEFAULT_VALIDATION_REGEX_EXPRESSION = "^-?[0-9]+$";
068    private static final Pattern defaultValidationPattern = Pattern.compile(DEFAULT_VALIDATION_REGEX_EXPRESSION);
069
070    @Column(name="VAL")
071        private Long searchableAttributeValue;
072
073    /**
074     * Default constructor.
075     */
076    public SearchableAttributeLongValue() {
077        super();
078        this.ojbConcreteClass = this.getClass().getName();
079    }
080
081    @Override
082    public void setupAttributeValue(String value) {
083        this.setSearchableAttributeValue(convertStringToLong(value));
084    }
085
086    private Long convertStringToLong(String value) {
087        if (org.apache.commons.lang.StringUtils.isEmpty(value)) {
088            return null;
089        } else {
090            return Long.valueOf(value.trim());
091        }
092    }
093
094    @Override
095        public void setupAttributeValue(ResultSet resultSet, String columnName) throws SQLException {
096                this.setSearchableAttributeValue(resultSet.getLong(columnName));
097        }
098
099    @Override
100    public String getSearchableAttributeDisplayValue() {
101        NumberFormat format = DecimalFormat.getInstance();
102        ((DecimalFormat)format).applyPattern(DEFAULT_FORMAT_PATTERN);
103        return format.format(getSearchableAttributeValue().longValue());
104    }
105
106    @Override
107        public String getAttributeDataType() {
108                return ATTRIBUTE_XML_REPRESENTATION;
109        }
110
111    @Override
112        public String getAttributeTableName() {
113                return ATTRIBUTE_DATABASE_TABLE_NAME;
114        }
115
116    @Override
117        public boolean allowsWildcards() {
118                return DEFAULT_WILDCARD_ALLOWANCE_POLICY;
119        }
120
121    @Override
122        public boolean allowsCaseInsensitivity() {
123                return ALLOWS_CASE_INSENSITIVE_SEARCH;
124        }
125
126    @Override
127        public boolean allowsRangeSearches() {
128                return ALLOWS_RANGE_SEARCH;
129        }
130
131    @Override
132    public Boolean isRangeValid(String lowerValue, String upperValue) {
133        if (allowsRangeSearches()) {
134            Long lower = convertStringToLong(lowerValue);
135            Long upper = convertStringToLong(upperValue);
136            if ( (lower != null) && (upper != null) ) {
137                return (lower.compareTo(upper) <= 0);
138            }
139            return true;
140        }
141        return null;
142    }
143
144    @Override
145    public Long getSearchableAttributeValue() {
146        return searchableAttributeValue;
147    }
148
149    public void setSearchableAttributeValue(Long searchableAttributeValue) {
150        this.searchableAttributeValue = searchableAttributeValue;
151    }
152
153    @Override
154    public DocumentAttributeInteger toDocumentAttribute() {
155        BigInteger integer = null;
156        if (getSearchableAttributeValue() != null) {
157            integer = BigInteger.valueOf(getSearchableAttributeValue().longValue());
158        }
159        return DocumentAttributeFactory.createIntegerAttribute(getSearchableAttributeKey(), integer);
160    }
161
162    @Override
163    protected Pattern getDefaultValidationPattern() {
164        return defaultValidationPattern;
165    }
166}
167