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