Coverage Report - org.kuali.rice.kew.impl.document.attribute.DocumentAttributeIndexingQueueImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentAttributeIndexingQueueImpl
0%
0/52
0%
0/20
5.333
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.kew.impl.document.attribute;
 18  
 
 19  
 import org.apache.commons.lang.StringUtils;
 20  
 import org.apache.log4j.Logger;
 21  
 import org.apache.log4j.MDC;
 22  
 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
 23  
 import org.kuali.rice.kew.api.KewApiServiceLocator;
 24  
 import org.kuali.rice.kew.api.WorkflowRuntimeException;
 25  
 import org.kuali.rice.kew.api.document.Document;
 26  
 import org.kuali.rice.kew.api.document.DocumentContent;
 27  
 import org.kuali.rice.kew.api.document.WorkflowDocumentService;
 28  
 import org.kuali.rice.kew.api.document.attribute.DocumentAttribute;
 29  
 import org.kuali.rice.kew.api.document.attribute.DocumentAttributeDateTime;
 30  
 import org.kuali.rice.kew.api.document.attribute.DocumentAttributeDecimal;
 31  
 import org.kuali.rice.kew.api.document.attribute.DocumentAttributeIndexingQueue;
 32  
 import org.kuali.rice.kew.api.document.attribute.DocumentAttributeInteger;
 33  
 import org.kuali.rice.kew.api.document.attribute.DocumentAttributeString;
 34  
 import org.kuali.rice.kew.docsearch.SearchableAttributeDateTimeValue;
 35  
 import org.kuali.rice.kew.docsearch.SearchableAttributeFloatValue;
 36  
 import org.kuali.rice.kew.docsearch.SearchableAttributeLongValue;
 37  
 import org.kuali.rice.kew.docsearch.SearchableAttributeStringValue;
 38  
 import org.kuali.rice.kew.docsearch.SearchableAttributeValue;
 39  
 import org.kuali.rice.kew.doctype.bo.DocumentType;
 40  
 import org.kuali.rice.kew.framework.docsearch.DocumentSearchContext;
 41  
 import org.kuali.rice.kew.framework.docsearch.SearchableAttribute;
 42  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 43  
 
 44  
 import java.sql.Timestamp;
 45  
 import java.util.ArrayList;
 46  
 import java.util.List;
 47  
 
 48  
 /**
 49  
  * Reference implementation of the DocumentAttributeIndexingQueue.
 50  
  *
 51  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 52  
  */
 53  0
 public class DocumentAttributeIndexingQueueImpl implements DocumentAttributeIndexingQueue {
 54  
 
 55  0
         private static Logger LOG = Logger.getLogger(DocumentAttributeIndexingQueueImpl.class);
 56  
 
 57  
     @Override
 58  
     public void indexDocument(String documentId) {
 59  0
         if (StringUtils.isBlank(documentId)) {
 60  0
             throw new RiceIllegalArgumentException("documentId was null or blank");
 61  
         }
 62  0
         MDC.put("docId", documentId);
 63  
         try {
 64  0
             long t1 = System.currentTimeMillis();
 65  0
             LOG.info("Indexing document attributes for document " + documentId);
 66  0
             Document document = getWorkflowDocumentService().getDocument(documentId);
 67  0
             if (document == null) {
 68  0
                 throw new RiceIllegalArgumentException("Failed to locate document with the given id: " + documentId);
 69  
             }
 70  0
             DocumentContent documentContent =
 71  
                     KewApiServiceLocator.getWorkflowDocumentService().getDocumentContent(documentId);
 72  0
             List<SearchableAttributeValue> attributes = buildSearchableAttributeValues(document, documentContent);
 73  0
             KEWServiceLocator.getRouteHeaderService().updateRouteHeaderSearchValues(documentId, attributes);
 74  0
             long t2 = System.currentTimeMillis();
 75  0
             LOG.info("...finished indexing document " + documentId + " for document search, total time = " + (t2 - t1) +
 76  
                     " ms.");
 77  
         } finally {
 78  0
             MDC.remove("docId");
 79  0
         }
 80  0
     }
 81  
 
 82  
     /**
 83  
      * Determines the {@link DocumentAttribute}s for the given document and returns a List of SearchableAttributeValue
 84  
      * which will be saved.
 85  
      */
 86  
         private List<SearchableAttributeValue> buildSearchableAttributeValues(Document document, DocumentContent documentContent) {
 87  0
                 List<SearchableAttributeValue> searchableAttributeValues = new ArrayList<SearchableAttributeValue>();
 88  0
         DocumentType documentTypeBo = KEWServiceLocator.getDocumentTypeService().findByName(document.getDocumentTypeName());
 89  0
                 for (SearchableAttribute searchableAttribute : documentTypeBo.loadSearchableAttributes()) {
 90  0
             DocumentSearchContext context = DocumentSearchContext.createFullContext(document, documentContent, document.getDocumentTypeName());
 91  0
             List<DocumentAttribute<?>> documentAttributes = searchableAttribute.getDocumentAttributes(context);
 92  0
                         if (documentAttributes != null) {
 93  0
                 for (DocumentAttribute<?> documentAttribute : documentAttributes) {
 94  0
                     if (documentAttribute == null) {
 95  0
                         LOG.warn("Encountered a 'null' DocumentAttribute from searchable attribute: " + searchableAttribute);
 96  0
                         continue;
 97  
                     }
 98  0
                     SearchableAttributeValue searchableAttributeValue = null;
 99  0
                     if (documentAttribute instanceof DocumentAttributeString) {
 100  0
                         searchableAttributeValue = new SearchableAttributeStringValue();
 101  0
                         ((SearchableAttributeStringValue)searchableAttributeValue).setSearchableAttributeValue(((DocumentAttributeString)documentAttribute).getValue());
 102  0
                     } else if (documentAttribute instanceof DocumentAttributeDateTime) {
 103  0
                         searchableAttributeValue = new SearchableAttributeDateTimeValue();
 104  0
                         Timestamp timestamp = new Timestamp(((DocumentAttributeDateTime)documentAttribute).getValue().getMillis());
 105  0
                         ((SearchableAttributeDateTimeValue)searchableAttributeValue).setSearchableAttributeValue(timestamp);
 106  0
                     } else if (documentAttribute instanceof DocumentAttributeInteger) {
 107  0
                         searchableAttributeValue = new SearchableAttributeLongValue();
 108  0
                         Long longValue = new Long(((DocumentAttributeInteger)documentAttribute).getValue().longValue());
 109  0
                         ((SearchableAttributeLongValue)searchableAttributeValue).setSearchableAttributeValue(longValue);
 110  0
                     } else if (documentAttribute instanceof DocumentAttributeDecimal) {
 111  0
                         searchableAttributeValue = new SearchableAttributeFloatValue();
 112  0
                         ((SearchableAttributeFloatValue)searchableAttributeValue).setSearchableAttributeValue(((DocumentAttributeDecimal)documentAttribute).getValue());
 113  
                     } else {
 114  0
                         throw new WorkflowRuntimeException("Encountered an invalid instance of DocumentAttribute, was: " + documentAttribute.getClass());
 115  
                     }
 116  0
                     searchableAttributeValue.setSearchableAttributeKey(documentAttribute.getName());
 117  0
                     searchableAttributeValue.setDocumentId(document.getDocumentId());
 118  0
                     searchableAttributeValue.setRouteHeader(null); // let the documentId we set represent this reference
 119  0
                     searchableAttributeValues.add(searchableAttributeValue);
 120  0
                 }
 121  
                         }
 122  0
                 }
 123  0
                 return searchableAttributeValues;
 124  
         }
 125  
 
 126  
     protected WorkflowDocumentService getWorkflowDocumentService() {
 127  0
         return KewApiServiceLocator.getWorkflowDocumentService();
 128  
     }
 129  
 }