1 /**
2 * Copyright 2005-2014 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 public class DocumentSearchTestBase extends KEWTestCase {
34
35 /**
36 * This method is used by tests that use xml attributes
37 *
38 * @param name - name of the attribute to retrieve
39 * @return
40 */
41 protected StandardGenericXMLSearchableAttribute getAttribute(String name) {
42 String attName = name;
43 if (attName == null) {
44 attName = "XMLSearchableAttribute";
45 }
46 StandardGenericXMLSearchableAttribute attribute = new StandardGenericXMLSearchableAttribute();
47 return attribute;
48 }
49
50 protected void addSearchableAttribute(DocumentSearchCriteria.Builder criteria, String name, String value) {
51 criteria.addDocumentAttributeValue(name, value);
52 }
53
54 protected void addSearchableAttribute(DocumentSearchCriteria.Builder criteria, String name, String[] values) {
55 for (String value : values) {
56 addSearchableAttribute(criteria, name, value);
57 }
58 }
59
60 protected String createSearchableAttributeRange(String lowerBound, String upperBound, boolean upperBoundInclusive) {
61 StringBuilder value = new StringBuilder();
62 if (StringUtils.isNotBlank(lowerBound) && StringUtils.isNotBlank(upperBound)) {
63 value.append(lowerBound);
64 if (upperBoundInclusive) {
65 value.append("..");
66 } else {
67 value.append("...");
68 }
69 value.append(upperBound);
70 } else if (StringUtils.isBlank(upperBound)) {
71 value.append(">= ").append(lowerBound);
72 } else {
73 value.append("<");
74 if (upperBoundInclusive) {
75 value.append("=");
76 }
77 value.append(" ").append(upperBound);
78 }
79 return value.toString();
80 }
81
82
83 /**
84 * Creates an ExtensionDefinition for the specified attribute with XML config pulled from the db
85 */
86 protected static ExtensionDefinition createExtensionDefinition(String attrName) {
87 ExtensionDefinition.Builder edb = ExtensionDefinition.Builder.create(attrName, KewApiConstants.SEARCHABLE_XML_ATTRIBUTE_TYPE, StandardGenericXMLSearchableAttribute.class.getName());
88 edb.getConfiguration().put(KewApiConstants.ATTRIBUTE_XML_CONFIG_DATA, KEWServiceLocator.getRuleAttributeService().findByName(attrName).getXmlConfigData());
89 return edb.build();
90 }
91
92 // protected SearchAttributeCriteriaComponent createSearchAttributeCriteriaComponent(String key,String value,DocumentType docType) {
93 // return createSearchAttributeCriteriaComponent(key, value, null, docType);
94 // }
95 //
96 // protected SearchAttributeCriteriaComponent createSearchAttributeCriteriaComponent(String key,String[] values,DocumentType docType) {
97 // return createSearchAttributeCriteriaComponent(key, values, null, docType);
98 // }
99 //
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 }