| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.kuali.rice.kew.docsearch; |
| 18 | |
|
| 19 | |
import org.apache.commons.lang.StringUtils; |
| 20 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
| 21 | |
import org.kuali.rice.core.api.reflect.ObjectDefinition; |
| 22 | |
import org.kuali.rice.core.api.util.ClassLoaderUtils; |
| 23 | |
import org.kuali.rice.core.api.util.RiceConstants; |
| 24 | |
import org.kuali.rice.core.framework.resourceloader.ObjectDefinitionResolver; |
| 25 | |
import org.kuali.rice.kew.api.WorkflowRuntimeException; |
| 26 | |
import org.kuali.rice.kew.docsearch.web.SearchAttributeFormContainer; |
| 27 | |
import org.kuali.rice.kew.doctype.bo.DocumentType; |
| 28 | |
import org.kuali.rice.kew.doctype.service.DocumentTypeService; |
| 29 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
| 30 | |
import org.kuali.rice.kew.user.UserUtils; |
| 31 | |
import org.kuali.rice.kns.web.ui.Field; |
| 32 | |
import org.kuali.rice.kns.web.ui.Row; |
| 33 | |
import org.kuali.rice.krad.UserSession; |
| 34 | |
import org.kuali.rice.krad.util.GlobalVariables; |
| 35 | |
|
| 36 | |
import java.sql.Date; |
| 37 | |
import java.sql.Timestamp; |
| 38 | |
import java.util.ArrayList; |
| 39 | |
import java.util.Arrays; |
| 40 | |
import java.util.HashMap; |
| 41 | |
import java.util.Iterator; |
| 42 | |
import java.util.List; |
| 43 | |
import java.util.Map; |
| 44 | |
import java.util.StringTokenizer; |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
public final class DocSearchUtils { |
| 53 | |
|
| 54 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DocSearchUtils.class); |
| 55 | |
|
| 56 | 0 | public static final List<Class<? extends SearchableAttributeValue>> SEARCHABLE_ATTRIBUTE_BASE_CLASS_LIST = |
| 57 | |
new ArrayList<Class<? extends SearchableAttributeValue>>(); |
| 58 | |
static { |
| 59 | 0 | SEARCHABLE_ATTRIBUTE_BASE_CLASS_LIST.add(SearchableAttributeStringValue.class); |
| 60 | 0 | SEARCHABLE_ATTRIBUTE_BASE_CLASS_LIST.add(SearchableAttributeFloatValue.class); |
| 61 | 0 | SEARCHABLE_ATTRIBUTE_BASE_CLASS_LIST.add(SearchableAttributeLongValue.class); |
| 62 | 0 | SEARCHABLE_ATTRIBUTE_BASE_CLASS_LIST.add(SearchableAttributeDateTimeValue.class); |
| 63 | 0 | } |
| 64 | |
|
| 65 | 0 | private DocSearchUtils() { |
| 66 | 0 | throw new UnsupportedOperationException("do not call"); |
| 67 | |
} |
| 68 | |
|
| 69 | |
public static List<SearchableAttributeValue> getSearchableAttributeValueObjectTypes() { |
| 70 | 0 | List<SearchableAttributeValue> searchableAttributeValueClasses = new ArrayList<SearchableAttributeValue>(); |
| 71 | 0 | for (Class<? extends SearchableAttributeValue> searchAttributeValueClass : SEARCHABLE_ATTRIBUTE_BASE_CLASS_LIST) { |
| 72 | 0 | ObjectDefinition objDef = new ObjectDefinition(searchAttributeValueClass); |
| 73 | 0 | SearchableAttributeValue attributeValue = (SearchableAttributeValue) ObjectDefinitionResolver.createObject(objDef, ClassLoaderUtils.getDefaultClassLoader(), false); |
| 74 | 0 | searchableAttributeValueClasses.add(attributeValue); |
| 75 | 0 | } |
| 76 | 0 | return searchableAttributeValueClasses; |
| 77 | |
} |
| 78 | |
|
| 79 | |
public static SearchableAttributeValue getSearchableAttributeValueByDataTypeString(String dataType) { |
| 80 | 0 | SearchableAttributeValue returnableValue = null; |
| 81 | 0 | if (StringUtils.isBlank(dataType)) { |
| 82 | 0 | return returnableValue; |
| 83 | |
} |
| 84 | 0 | for (SearchableAttributeValue attValue : getSearchableAttributeValueObjectTypes()) |
| 85 | |
{ |
| 86 | 0 | if (dataType.equalsIgnoreCase(attValue.getAttributeDataType())) |
| 87 | |
{ |
| 88 | 0 | if (returnableValue != null) |
| 89 | |
{ |
| 90 | 0 | String errorMsg = "Found two SearchableAttributeValue objects with same data type string ('" + dataType + "' while ignoring case): " + returnableValue.getClass().getName() + " and " + attValue.getClass().getName(); |
| 91 | 0 | LOG.error("getSearchableAttributeValueByDataTypeString() " + errorMsg); |
| 92 | 0 | throw new RuntimeException(errorMsg); |
| 93 | |
} |
| 94 | 0 | LOG.debug("getSearchableAttributeValueByDataTypeString() SearchableAttributeValue class name is " + attValue.getClass().getName() + "... ojbConcreteClassName is " + attValue.getOjbConcreteClass()); |
| 95 | 0 | ObjectDefinition objDef = new ObjectDefinition(attValue.getClass()); |
| 96 | 0 | returnableValue = (SearchableAttributeValue) ObjectDefinitionResolver.createObject(objDef, ClassLoaderUtils.getDefaultClassLoader(), false); |
| 97 | 0 | } |
| 98 | |
} |
| 99 | 0 | return returnableValue; |
| 100 | |
} |
| 101 | |
|
| 102 | |
|
| 103 | |
public static String getDisplayValueWithDateOnly(Timestamp value) { |
| 104 | 0 | return RiceConstants.getDefaultDateFormat().format(new Date(value.getTime())); |
| 105 | |
} |
| 106 | |
|
| 107 | |
public static String getDisplayValueWithDateTime(Timestamp value) { |
| 108 | 0 | return RiceConstants.getDefaultDateAndTimeFormat().format(new Date(value.getTime())); |
| 109 | |
} |
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
private static final String CURRENT_USER_PREFIX = "CURRENT_USER."; |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
public static List<SearchAttributeCriteriaComponent> buildSearchableAttributesFromString(String searchableAttributeString, String documentTypeName) { |
| 124 | 0 | List<SearchAttributeCriteriaComponent> searchableAttributes = new ArrayList<SearchAttributeCriteriaComponent>(); |
| 125 | 0 | Map<String, SearchAttributeCriteriaComponent> criteriaComponentsByKey = new HashMap<String, SearchAttributeCriteriaComponent>(); |
| 126 | |
|
| 127 | 0 | DocumentType docType = getDocumentType(documentTypeName); |
| 128 | |
|
| 129 | 0 | if (docType != null) { |
| 130 | |
|
| 131 | 0 | for (SearchableAttributeOld searchableAttribute : docType.getSearchableAttributesOld()) { |
| 132 | |
|
| 133 | 0 | for (Row row : searchableAttribute.getSearchingRows( |
| 134 | |
DocSearchUtils.getDocumentSearchContext("", docType.getName(), ""))) { |
| 135 | 0 | for (Field field : row.getFields()) { |
| 136 | 0 | if (field instanceof Field) { |
| 137 | 0 | SearchableAttributeValue searchableAttributeValue = DocSearchUtils.getSearchableAttributeValueByDataTypeString(field.getFieldDataType()); |
| 138 | 0 | SearchAttributeCriteriaComponent sacc = new SearchAttributeCriteriaComponent(field.getPropertyName(), null, field.getPropertyName(), searchableAttributeValue); |
| 139 | 0 | sacc.setRangeSearch(field.isMemberOfRange()); |
| 140 | 0 | sacc.setSearchInclusive(field.isInclusive()); |
| 141 | 0 | sacc.setSearchable(field.isIndexedForSearch()); |
| 142 | 0 | sacc.setLookupableFieldType(field.getFieldType()); |
| 143 | 0 | sacc.setCanHoldMultipleValues(Field.MULTI_VALUE_FIELD_TYPES.contains(field.getFieldType())); |
| 144 | 0 | criteriaComponentsByKey.put(field.getPropertyName(), sacc); |
| 145 | 0 | } else { |
| 146 | 0 | throw new RiceRuntimeException("Fields must be of type org.kuali.rice.kew.docsearch.Field"); |
| 147 | |
} |
| 148 | |
} |
| 149 | |
} |
| 150 | |
} |
| 151 | |
} |
| 152 | |
|
| 153 | 0 | Map<String, List<String>> checkForMultiValueSearchableAttributes = new HashMap<String, List<String>>(); |
| 154 | 0 | if ((searchableAttributeString != null) && (searchableAttributeString.trim().length() > 0)) { |
| 155 | 0 | StringTokenizer tokenizer = new StringTokenizer(searchableAttributeString, ","); |
| 156 | 0 | while (tokenizer.hasMoreTokens()) { |
| 157 | 0 | String searchableAttribute = tokenizer.nextToken(); |
| 158 | 0 | int index = searchableAttribute.indexOf(":"); |
| 159 | 0 | if (index != -1) { |
| 160 | 0 | String key = searchableAttribute.substring(0, index); |
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | 0 | String value = searchableAttribute.substring(index + 1); |
| 168 | 0 | if (value.startsWith(CURRENT_USER_PREFIX)) { |
| 169 | 0 | String idType = value.substring(CURRENT_USER_PREFIX.length()); |
| 170 | 0 | UserSession session = GlobalVariables.getUserSession(); |
| 171 | 0 | String idValue = UserUtils.getIdValue(idType, session.getPerson()); |
| 172 | 0 | if (!StringUtils.isBlank(idValue)) { |
| 173 | 0 | value = idValue; |
| 174 | |
} |
| 175 | |
} |
| 176 | 0 | SearchAttributeCriteriaComponent critComponent = criteriaComponentsByKey.get(key); |
| 177 | 0 | if (critComponent == null) { |
| 178 | |
|
| 179 | |
|
| 180 | 0 | continue; |
| 181 | |
} |
| 182 | 0 | if (critComponent.getSearchableAttributeValue() == null) { |
| 183 | 0 | String errorMsg = "Cannot find SearchableAttributeValue for given key '" + key + "'"; |
| 184 | 0 | LOG.error("buildSearchableAttributesFromString() " + errorMsg); |
| 185 | 0 | throw new RuntimeException(errorMsg); |
| 186 | |
} |
| 187 | 0 | if (critComponent.isCanHoldMultipleValues()) { |
| 188 | |
|
| 189 | 0 | if (checkForMultiValueSearchableAttributes.containsKey(key)) { |
| 190 | 0 | List<String> keyList = checkForMultiValueSearchableAttributes.get(key); |
| 191 | 0 | keyList.add(value); |
| 192 | 0 | checkForMultiValueSearchableAttributes.put(key, keyList); |
| 193 | 0 | } else { |
| 194 | 0 | List<String> tempList = new ArrayList<String>(); |
| 195 | 0 | tempList.add(value); |
| 196 | |
|
| 197 | 0 | checkForMultiValueSearchableAttributes.put(key, tempList); |
| 198 | 0 | searchableAttributes.add(critComponent); |
| 199 | 0 | } |
| 200 | |
} else { |
| 201 | |
|
| 202 | 0 | if (checkForMultiValueSearchableAttributes.containsKey(key)) { |
| 203 | |
|
| 204 | 0 | String error = "Attempting to add multiple values to a search attribute (key: '" + key + "') that does not suppor them"; |
| 205 | 0 | LOG.error("buildSearchableAttributesFromString() " + error); |
| 206 | |
|
| 207 | |
|
| 208 | |
} |
| 209 | 0 | critComponent.setValue(value); |
| 210 | 0 | searchableAttributes.add(critComponent); |
| 211 | |
} |
| 212 | |
|
| 213 | |
} |
| 214 | 0 | } |
| 215 | 0 | for (SearchAttributeCriteriaComponent criteriaComponent : searchableAttributes) |
| 216 | |
{ |
| 217 | 0 | if (criteriaComponent.isCanHoldMultipleValues()) |
| 218 | |
{ |
| 219 | 0 | List<String> values = checkForMultiValueSearchableAttributes.get(criteriaComponent.getFormKey()); |
| 220 | 0 | criteriaComponent.setValue(null); |
| 221 | 0 | criteriaComponent.setValues(values); |
| 222 | 0 | } |
| 223 | |
} |
| 224 | |
} |
| 225 | |
|
| 226 | 0 | return searchableAttributes; |
| 227 | |
} |
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
public static void addSearchableAttributesToCriteria(DocSearchCriteriaDTO criteria, List propertyFields, String searchAttributesString) { |
| 246 | 0 | addSearchableAttributesToCriteria(criteria, propertyFields, searchAttributesString, false); |
| 247 | 0 | } |
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
public static void addSearchableAttributesToCriteria(DocSearchCriteriaDTO criteria, List propertyFields, boolean setAttributesStrictly) { |
| 268 | 0 | addSearchableAttributesToCriteria(criteria, propertyFields, null, setAttributesStrictly); |
| 269 | 0 | } |
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
public static void addSearchableAttributesToCriteria(DocSearchCriteriaDTO criteria, List propertyFields, String searchAttributesString, boolean setAttributesStrictly) { |
| 289 | 0 | if (criteria != null) { |
| 290 | 0 | DocumentType docType = getDocumentType(criteria.getDocTypeFullName()); |
| 291 | 0 | if (docType == null) { |
| 292 | 0 | return; |
| 293 | |
} |
| 294 | 0 | criteria.getSearchableAttributes().clear(); |
| 295 | 0 | Map<String, SearchAttributeCriteriaComponent> urlParameterSearchAttributesByFormKey = new HashMap<String, SearchAttributeCriteriaComponent>(); |
| 296 | 0 | if (!StringUtils.isBlank(searchAttributesString)) { |
| 297 | 0 | List<SearchAttributeCriteriaComponent> components = buildSearchableAttributesFromString(searchAttributesString, docType.getName()); |
| 298 | 0 | for (SearchAttributeCriteriaComponent component : components) { |
| 299 | 0 | urlParameterSearchAttributesByFormKey.put(component.getFormKey(), component); |
| 300 | 0 | criteria.addSearchableAttribute(component); |
| 301 | |
} |
| 302 | |
|
| 303 | |
} |
| 304 | 0 | if (!propertyFields.isEmpty()) { |
| 305 | 0 | Map<String, SearchAttributeCriteriaComponent> criteriaComponentsByFormKey = new HashMap<String, SearchAttributeCriteriaComponent>(); |
| 306 | 0 | for (SearchableAttributeOld searchableAttribute : docType.getSearchableAttributesOld()) { |
| 307 | |
|
| 308 | 0 | for (Row row : searchableAttribute.getSearchingRows( |
| 309 | |
DocSearchUtils.getDocumentSearchContext("", docType.getName(), ""))) { |
| 310 | 0 | for (Field field : row.getFields()) { |
| 311 | 0 | if (field instanceof Field) { |
| 312 | 0 | SearchableAttributeValue searchableAttributeValue = DocSearchUtils.getSearchableAttributeValueByDataTypeString(field.getFieldDataType()); |
| 313 | 0 | SearchAttributeCriteriaComponent sacc = new SearchAttributeCriteriaComponent(field.getPropertyName(), null, field.getPropertyName(), searchableAttributeValue); |
| 314 | 0 | sacc.setRangeSearch(field.isMemberOfRange()); |
| 315 | 0 | sacc.setSearchInclusive(field.isInclusive()); |
| 316 | 0 | sacc.setLookupableFieldType(field.getFieldType()); |
| 317 | 0 | sacc.setSearchable(field.isIndexedForSearch()); |
| 318 | 0 | sacc.setCanHoldMultipleValues(Field.MULTI_VALUE_FIELD_TYPES.contains(field.getFieldType())); |
| 319 | 0 | criteriaComponentsByFormKey.put(field.getPropertyName(), sacc); |
| 320 | 0 | } else { |
| 321 | 0 | throw new RiceRuntimeException("Fields must be of type org.kuali.rice.kew.docsearch.Field"); |
| 322 | |
} |
| 323 | |
} |
| 324 | |
} |
| 325 | |
} |
| 326 | 0 | for (Iterator iterator = propertyFields.iterator(); iterator.hasNext();) { |
| 327 | 0 | SearchAttributeFormContainer propertyField = (SearchAttributeFormContainer) iterator.next(); |
| 328 | 0 | SearchAttributeCriteriaComponent sacc = criteriaComponentsByFormKey.get(propertyField.getKey()); |
| 329 | 0 | if (sacc != null) { |
| 330 | 0 | if (sacc.getSearchableAttributeValue() == null) { |
| 331 | 0 | String errorMsg = "Searchable attribute with form field key " + sacc.getFormKey() + " does not have a valid SearchableAttributeValue"; |
| 332 | 0 | LOG.error("addSearchableAttributesToCriteria() " + errorMsg); |
| 333 | 0 | throw new RuntimeException(errorMsg); |
| 334 | |
} |
| 335 | |
|
| 336 | 0 | if (urlParameterSearchAttributesByFormKey.containsKey(sacc.getFormKey())) { |
| 337 | 0 | setupPropertyField(urlParameterSearchAttributesByFormKey.get(sacc.getFormKey()), propertyFields); |
| 338 | |
} else { |
| 339 | |
|
| 340 | |
|
| 341 | |
|
| 342 | |
|
| 343 | |
|
| 344 | 0 | if (Field.MULTI_VALUE_FIELD_TYPES.contains(sacc.getLookupableFieldType())) { |
| 345 | |
|
| 346 | 0 | sacc.setCanHoldMultipleValues(true); |
| 347 | 0 | if (propertyField.getValues() == null) { |
| 348 | 0 | sacc.setValues(new ArrayList<String>()); |
| 349 | |
} else { |
| 350 | 0 | sacc.setValues(Arrays.asList(propertyField.getValues())); |
| 351 | |
} |
| 352 | |
} else { |
| 353 | 0 | sacc.setValue(propertyField.getValue()); |
| 354 | |
} |
| 355 | 0 | criteria.addSearchableAttribute(sacc); |
| 356 | |
} |
| 357 | |
} else { |
| 358 | 0 | if (setAttributesStrictly) { |
| 359 | 0 | String message = "Cannot find matching search attribute with key '" + propertyField.getKey() + "' on document type '" + docType.getName() + "'"; |
| 360 | 0 | LOG.error(message); |
| 361 | 0 | throw new WorkflowRuntimeException(message); |
| 362 | |
} |
| 363 | |
} |
| 364 | 0 | } |
| 365 | |
} |
| 366 | |
} |
| 367 | 0 | } |
| 368 | |
|
| 369 | |
public static void setupPropertyField(SearchAttributeCriteriaComponent searchableAttribute, List propertyFields) { |
| 370 | 0 | SearchAttributeFormContainer propertyField = getPropertyField(searchableAttribute.getFormKey(), propertyFields); |
| 371 | 0 | if (propertyField != null) { |
| 372 | 0 | propertyField.setValue(searchableAttribute.getValue()); |
| 373 | 0 | if (searchableAttribute.getValues() != null) { |
| 374 | 0 | propertyField.setValues(searchableAttribute.getValues().toArray(new String[searchableAttribute.getValues().size()])); |
| 375 | |
} |
| 376 | |
} |
| 377 | 0 | } |
| 378 | |
|
| 379 | |
public static SearchAttributeFormContainer getPropertyField(String key, List propertyFields) { |
| 380 | 0 | if (StringUtils.isBlank(key)) { |
| 381 | 0 | return null; |
| 382 | |
} |
| 383 | 0 | for (Iterator iter = propertyFields.iterator(); iter.hasNext();) { |
| 384 | 0 | SearchAttributeFormContainer container = (SearchAttributeFormContainer) iter.next(); |
| 385 | 0 | if (key.equals(container.getKey())) { |
| 386 | 0 | return container; |
| 387 | |
} |
| 388 | 0 | } |
| 389 | 0 | return null; |
| 390 | |
} |
| 391 | |
|
| 392 | |
private static DocumentType getDocumentType(String docTypeName) { |
| 393 | 0 | if ((docTypeName != null && !"".equals(docTypeName))) { |
| 394 | 0 | return ((DocumentTypeService) KEWServiceLocator.getService(KEWServiceLocator.DOCUMENT_TYPE_SERVICE)).findByName(docTypeName); |
| 395 | |
} |
| 396 | 0 | return null; |
| 397 | |
} |
| 398 | |
|
| 399 | |
public static DocumentSearchContext getDocumentSearchContext(String documentId, String documentTypeName, String documentContent){ |
| 400 | 0 | DocumentSearchContext documentSearchContext = new DocumentSearchContext(); |
| 401 | 0 | documentSearchContext.setDocumentId(documentId); |
| 402 | 0 | documentSearchContext.setDocumentTypeName(documentTypeName); |
| 403 | 0 | documentSearchContext.setDocumentContent(documentContent); |
| 404 | 0 | return documentSearchContext; |
| 405 | |
} |
| 406 | |
} |