001    /**
002     * Copyright 2005-2013 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     */
016    package org.kuali.rice.kew.docsearch;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.junit.Ignore;
020    import org.kuali.rice.kew.api.KewApiConstants;
021    import org.kuali.rice.kew.api.document.search.DocumentSearchCriteria;
022    import org.kuali.rice.kew.api.extension.ExtensionDefinition;
023    import org.kuali.rice.kew.docsearch.xml.StandardGenericXMLSearchableAttribute;
024    import org.kuali.rice.kew.service.KEWServiceLocator;
025    import org.kuali.rice.kew.test.KEWTestCase;
026    
027    /**
028     * This is a base class used for document search unit test classes to consolidate some helper methods
029     *
030     * @author Kuali Rice Team (rice.collab@kuali.org)
031     *
032     */
033    @Ignore("This class is a helper class only with no specific tests within")
034    public class DocumentSearchTestBase extends KEWTestCase {
035    
036        /**
037         * This method is used by tests that use xml attributes
038         *
039         * @param name - name of the attribute to retrieve
040         * @return
041         */
042        protected StandardGenericXMLSearchableAttribute getAttribute(String name) {
043            String attName = name;
044            if (attName == null) {
045                attName = "XMLSearchableAttribute";
046            }
047            StandardGenericXMLSearchableAttribute attribute = new StandardGenericXMLSearchableAttribute();
048            return attribute;
049        }
050    
051        protected void addSearchableAttribute(DocumentSearchCriteria.Builder criteria, String name, String value) {
052            criteria.addDocumentAttributeValue(name, value);
053        }
054    
055        protected void addSearchableAttribute(DocumentSearchCriteria.Builder criteria, String name, String[] values) {
056            for (String value : values) {
057                addSearchableAttribute(criteria, name, value);
058            }
059        }
060    
061        protected String createSearchableAttributeRange(String lowerBound, String upperBound, boolean upperBoundInclusive) {
062            StringBuilder value = new StringBuilder();
063            if (StringUtils.isNotBlank(lowerBound) && StringUtils.isNotBlank(upperBound)) {
064                value.append(lowerBound);
065                if (upperBoundInclusive) {
066                    value.append("..");
067                } else {
068                    value.append("...");
069                }
070                value.append(upperBound);
071            } else if (StringUtils.isBlank(upperBound)) {
072                value.append(">= ").append(lowerBound);
073            } else {
074                value.append("<");
075                if (upperBoundInclusive) {
076                    value.append("=");
077                }
078                value.append(" ").append(upperBound);
079            }
080            return value.toString();
081        }
082    
083    
084        /**
085         * Creates an ExtensionDefinition for the specified attribute with XML config pulled from the db
086         */
087        protected static ExtensionDefinition createExtensionDefinition(String attrName) {
088            ExtensionDefinition.Builder edb = ExtensionDefinition.Builder.create(attrName, KewApiConstants.SEARCHABLE_XML_ATTRIBUTE_TYPE, StandardGenericXMLSearchableAttribute.class.getName());
089            edb.getConfiguration().put(KewApiConstants.ATTRIBUTE_XML_CONFIG_DATA, KEWServiceLocator.getRuleAttributeService().findByName(attrName).getXmlConfigData());
090            return edb.build();
091        }
092    
093    //    protected SearchAttributeCriteriaComponent createSearchAttributeCriteriaComponent(String key,String value,DocumentType docType) {
094    //        return createSearchAttributeCriteriaComponent(key, value, null, docType);
095    //    }
096    //
097    //    protected SearchAttributeCriteriaComponent createSearchAttributeCriteriaComponent(String key,String[] values,DocumentType docType) {
098    //        return createSearchAttributeCriteriaComponent(key, values, null, docType);
099    //    }
100    //
101    //    protected SearchAttributeCriteriaComponent createSearchAttributeCriteriaComponent(String key,String value,Boolean isLowerBoundValue,DocumentType docType) {
102    //        String formKey = (isLowerBoundValue == null) ? key : ((isLowerBoundValue != null && isLowerBoundValue.booleanValue()) ? KewApiConstants.SearchableAttributeConstants.RANGE_LOWER_BOUND_PROPERTY_PREFIX + key : KewApiConstants.SearchableAttributeConstants.RANGE_UPPER_BOUND_PROPERTY_PREFIX + key);
103    //        String savedKey = key;
104    //        SearchAttributeCriteriaComponent sacc = new SearchAttributeCriteriaComponent(formKey,value,savedKey);
105    //        applyAttributeFieldToComponent(sacc, docType, formKey);
106    //        return sacc;
107    //    }
108    //
109    //    protected SearchAttributeCriteriaComponent createSearchAttributeCriteriaComponent(String key,String[] values,Boolean isLowerBoundValue,DocumentType docType) {
110    //        String formKey = (isLowerBoundValue == null) ? key : ((isLowerBoundValue != null && isLowerBoundValue.booleanValue()) ? KewApiConstants.SearchableAttributeConstants.RANGE_LOWER_BOUND_PROPERTY_PREFIX + key : KewApiConstants.SearchableAttributeConstants.RANGE_UPPER_BOUND_PROPERTY_PREFIX + key);
111    //        String savedKey = key;
112    //        SearchAttributeCriteriaComponent sacc = new SearchAttributeCriteriaComponent(formKey,null,savedKey);
113    //        sacc.setValues(Arrays.asList(values));
114    //        applyAttributeFieldToComponent(sacc, docType, formKey);
115    //        return sacc;
116    //    }
117    //
118    //    private void applyAttributeFieldToComponent(SearchAttributeCriteriaComponent sacc, DocumentType docType, String formKey) {
119    //        RemotableAttributeField field = getFieldByFormKey(docType, formKey);
120    //        if (field != null) {
121    //            sacc.setSearchableAttributeValue(DocSearchUtils.getSearchableAttributeValueByDataTypeString(field.getDataType()));
122    //            boolean isRange = field.getAttributeLookupSettings() != null && field.getAttributeLookupSettings().isRanged();
123    //            sacc.setRangeSearch(isRange);
124    //            if (field.getAttributeLookupSettings().isCaseSensitive() != null) {
125    //                sacc.setCaseSensitive(field.getAttributeLookupSettings().isCaseSensitive());
126    //            }
127    //            if (isRange) {
128    //                if (field.getAttributeLookupSettings().getLowerBoundName().equals(formKey)) {
129    //                    sacc.setSearchInclusive(field.getAttributeLookupSettings().isLowerBoundInclusive());
130    //                } else if (field.getAttributeLookupSettings().getUpperBoundName().equals(formKey)) {
131    //                    sacc.setSearchInclusive(field.getAttributeLookupSettings().isUpperBoundInclusive());
132    //                } else {
133    //                    throw new IllegalStateException("Encountered an invalid ranged attribute field definition.");
134    //                }
135    //            }
136    //            boolean canHoldMultipleValues = field.getControl() instanceof Select &&
137    //                    ((Select) field.getControl()).isMultiple();
138    //            sacc.setCanHoldMultipleValues(canHoldMultipleValues);
139    //        }
140    //    }
141    //
142    //    private RemotableAttributeField getFieldByFormKey(DocumentType docType, String formKey) {
143    //        if (docType == null) {
144    //            return null;
145    //        }
146    //        for (DocumentType.ExtensionHolder<SearchableAttribute> holder : docType.loadSearchableAttributes()) {
147    //            for (RemotableAttributeField field : holder.getExtension().getSearchFields(holder.getExtensionDefinition(),
148    //                    docType.getName())) {
149    //                if (field.getName().equals(formKey)) {
150    //                    return field;
151    //                } else if (field.getAttributeLookupSettings() != null) {
152    //                    if (field.getName().equals(field.getAttributeLookupSettings().getLowerBoundName()) ||
153    //                            field.getName().equals(field.getAttributeLookupSettings().getUpperBoundName())) {
154    //                        return field;
155    //                    }
156    //                }
157    //            }
158    //        }
159    //        return null;
160    //    }
161    
162    }