View Javadoc

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.junit.Ignore;
20  import org.kuali.rice.kew.api.KewApiConstants;
21  import org.kuali.rice.kew.api.document.search.DocumentSearchCriteria;
22  import org.kuali.rice.kew.api.extension.ExtensionDefinition;
23  import org.kuali.rice.kew.docsearch.xml.StandardGenericXMLSearchableAttribute;
24  import org.kuali.rice.kew.service.KEWServiceLocator;
25  import org.kuali.rice.kew.test.KEWTestCase;
26  
27  /**
28   * This is a base class used for document search unit test classes to consolidate some helper methods
29   *
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   *
32   */
33  @Ignore("This class is a helper class only with no specific tests within")
34  public class DocumentSearchTestBase extends KEWTestCase {
35  
36      /**
37       * This method is used by tests that use xml attributes
38       *
39       * @param name - name of the attribute to retrieve
40       * @return
41       */
42      protected StandardGenericXMLSearchableAttribute getAttribute(String name) {
43          String attName = name;
44          if (attName == null) {
45              attName = "XMLSearchableAttribute";
46          }
47          StandardGenericXMLSearchableAttribute attribute = new StandardGenericXMLSearchableAttribute();
48          return attribute;
49      }
50  
51      protected void addSearchableAttribute(DocumentSearchCriteria.Builder criteria, String name, String value) {
52          criteria.addDocumentAttributeValue(name, value);
53      }
54  
55      protected void addSearchableAttribute(DocumentSearchCriteria.Builder criteria, String name, String[] values) {
56          for (String value : values) {
57              addSearchableAttribute(criteria, name, value);
58          }
59      }
60  
61      protected String createSearchableAttributeRange(String lowerBound, String upperBound, boolean upperBoundInclusive) {
62          StringBuilder value = new StringBuilder();
63          if (StringUtils.isNotBlank(lowerBound) && StringUtils.isNotBlank(upperBound)) {
64              value.append(lowerBound);
65              if (upperBoundInclusive) {
66                  value.append("..");
67              } else {
68                  value.append("...");
69              }
70              value.append(upperBound);
71          } else if (StringUtils.isBlank(upperBound)) {
72              value.append(">= ").append(lowerBound);
73          } else {
74              value.append("<");
75              if (upperBoundInclusive) {
76                  value.append("=");
77              }
78              value.append(" ").append(upperBound);
79          }
80          return value.toString();
81      }
82  
83  
84      /**
85       * Creates an ExtensionDefinition for the specified attribute with XML config pulled from the db
86       */
87      protected static ExtensionDefinition createExtensionDefinition(String attrName) {
88          ExtensionDefinition.Builder edb = ExtensionDefinition.Builder.create(attrName, KewApiConstants.SEARCHABLE_XML_ATTRIBUTE_TYPE, StandardGenericXMLSearchableAttribute.class.getName());
89          edb.getConfiguration().put(KewApiConstants.ATTRIBUTE_XML_CONFIG_DATA, KEWServiceLocator.getRuleAttributeService().findByName(attrName).getXmlConfigData());
90          return edb.build();
91      }
92  
93  //    protected SearchAttributeCriteriaComponent createSearchAttributeCriteriaComponent(String key,String value,DocumentType docType) {
94  //        return createSearchAttributeCriteriaComponent(key, value, null, docType);
95  //    }
96  //
97  //    protected SearchAttributeCriteriaComponent createSearchAttributeCriteriaComponent(String key,String[] values,DocumentType docType) {
98  //        return createSearchAttributeCriteriaComponent(key, values, null, docType);
99  //    }
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 }