Coverage Report - org.kuali.rice.kew.impl.document.attribute.DocumentAttributeIndexingQueueImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentAttributeIndexingQueueImpl
0%
0/55
0%
0/24
6
 
 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.impl.document.attribute;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.apache.log4j.Logger;
 20  
 import org.apache.log4j.MDC;
 21  
 import org.joda.time.DateTime;
 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.DocumentWithContent;
 28  
 import org.kuali.rice.kew.api.document.WorkflowDocumentService;
 29  
 import org.kuali.rice.kew.api.document.attribute.DocumentAttribute;
 30  
 import org.kuali.rice.kew.api.document.attribute.DocumentAttributeDateTime;
 31  
 import org.kuali.rice.kew.api.document.attribute.DocumentAttributeDecimal;
 32  
 import org.kuali.rice.kew.api.document.attribute.DocumentAttributeIndexingQueue;
 33  
 import org.kuali.rice.kew.api.document.attribute.DocumentAttributeInteger;
 34  
 import org.kuali.rice.kew.api.document.attribute.DocumentAttributeString;
 35  
 import org.kuali.rice.kew.docsearch.SearchableAttributeDateTimeValue;
 36  
 import org.kuali.rice.kew.docsearch.SearchableAttributeFloatValue;
 37  
 import org.kuali.rice.kew.docsearch.SearchableAttributeLongValue;
 38  
 import org.kuali.rice.kew.docsearch.SearchableAttributeStringValue;
 39  
 import org.kuali.rice.kew.docsearch.SearchableAttributeValue;
 40  
 import org.kuali.rice.kew.doctype.bo.DocumentType;
 41  
 import org.kuali.rice.kew.framework.document.attribute.SearchableAttribute;
 42  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 43  
 
 44  
 import java.math.BigInteger;
 45  
 import java.sql.Timestamp;
 46  
 import java.util.ArrayList;
 47  
 import java.util.List;
 48  
 
 49  
 /**
 50  
  * Reference implementation of the DocumentAttributeIndexingQueue.
 51  
  *
 52  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 53  
  */
 54  0
 public class DocumentAttributeIndexingQueueImpl implements DocumentAttributeIndexingQueue {
 55  
 
 56  0
         private static Logger LOG = Logger.getLogger(DocumentAttributeIndexingQueueImpl.class);
 57  
 
 58  
     @Override
 59  
     public void indexDocument(String documentId) {
 60  0
         if (StringUtils.isBlank(documentId)) {
 61  0
             throw new RiceIllegalArgumentException("documentId was null or blank");
 62  
         }
 63  0
         MDC.put("docId", documentId);
 64  
         try {
 65  0
             long t1 = System.currentTimeMillis();
 66  0
             LOG.info("Indexing document attributes for document " + documentId);
 67  0
             Document document = getWorkflowDocumentService().getDocument(documentId);
 68  0
             if (document == null) {
 69  0
                 throw new RiceIllegalArgumentException("Failed to locate document with the given id: " + documentId);
 70  
             }
 71  0
             DocumentContent documentContent =
 72  
                     KewApiServiceLocator.getWorkflowDocumentService().getDocumentContent(documentId);
 73  0
             List<SearchableAttributeValue> attributes = buildSearchableAttributeValues(document, documentContent);
 74  0
             KEWServiceLocator.getRouteHeaderService().updateRouteHeaderSearchValues(documentId, attributes);
 75  0
             long t2 = System.currentTimeMillis();
 76  0
             LOG.info("...finished indexing document " + documentId + " for document search, total time = " + (t2 - t1) +
 77  
                     " ms.");
 78  
         } finally {
 79  0
             MDC.remove("docId");
 80  0
         }
 81  0
     }
 82  
 
 83  
     /**
 84  
      * Determines the {@link DocumentAttribute}s for the given document and returns a List of SearchableAttributeValue
 85  
      * which will be saved.
 86  
      */
 87  
         private List<SearchableAttributeValue> buildSearchableAttributeValues(Document document, DocumentContent documentContent) {
 88  0
                 List<SearchableAttributeValue> searchableAttributeValues = new ArrayList<SearchableAttributeValue>();
 89  0
         DocumentType documentTypeBo = KEWServiceLocator.getDocumentTypeService().findByName(document.getDocumentTypeName());
 90  0
                 for (DocumentType.ExtensionHolder<SearchableAttribute> searchableAttributeHolder : documentTypeBo.loadSearchableAttributes()) {
 91  0
             DocumentWithContent documentWithContent = DocumentWithContent.create(document, documentContent);
 92  0
             SearchableAttribute searchableAttribute = searchableAttributeHolder.getExtension();
 93  0
             List<DocumentAttribute> documentAttributes = searchableAttribute.extractDocumentAttributes(
 94  
                     searchableAttributeHolder.getExtensionDefinition(), documentWithContent);
 95  0
                         if (documentAttributes != null) {
 96  0
                 for (DocumentAttribute documentAttribute : documentAttributes) {
 97  0
                     if (documentAttribute == null) {
 98  0
                         LOG.warn("Encountered a 'null' DocumentAttribute from searchable attribute: " + searchableAttribute);
 99  0
                         continue;
 100  
                     }
 101  0
                     SearchableAttributeValue searchableAttributeValue = null;
 102  0
                     if (documentAttribute instanceof DocumentAttributeString) {
 103  0
                         searchableAttributeValue = new SearchableAttributeStringValue();
 104  0
                         ((SearchableAttributeStringValue)searchableAttributeValue).setSearchableAttributeValue(((DocumentAttributeString)documentAttribute).getValue());
 105  0
                     } else if (documentAttribute instanceof DocumentAttributeDateTime) {
 106  0
                         searchableAttributeValue = new SearchableAttributeDateTimeValue();
 107  0
                         DateTime dateTimeValue = ((DocumentAttributeDateTime)documentAttribute).getValue();
 108  0
                         Timestamp timestamp = (dateTimeValue == null ? null : new Timestamp(dateTimeValue.getMillis()));
 109  0
                         ((SearchableAttributeDateTimeValue)searchableAttributeValue).setSearchableAttributeValue(timestamp);
 110  0
                     } else if (documentAttribute instanceof DocumentAttributeInteger) {
 111  0
                         searchableAttributeValue = new SearchableAttributeLongValue();
 112  0
                         BigInteger bigIntegerValue = ((DocumentAttributeInteger)documentAttribute).getValue();
 113  0
                         Long longValue = (bigIntegerValue == null ? null : bigIntegerValue.longValue());
 114  0
                         ((SearchableAttributeLongValue)searchableAttributeValue).setSearchableAttributeValue(longValue);
 115  0
                     } else if (documentAttribute instanceof DocumentAttributeDecimal) {
 116  0
                         searchableAttributeValue = new SearchableAttributeFloatValue();
 117  0
                         ((SearchableAttributeFloatValue)searchableAttributeValue).setSearchableAttributeValue(((DocumentAttributeDecimal)documentAttribute).getValue());
 118  
                     } else {
 119  0
                         throw new WorkflowRuntimeException("Encountered an invalid instance of DocumentAttribute, was: " + documentAttribute.getClass());
 120  
                     }
 121  0
                     searchableAttributeValue.setSearchableAttributeKey(documentAttribute.getName());
 122  0
                     searchableAttributeValue.setDocumentId(document.getDocumentId());
 123  0
                     searchableAttributeValue.setRouteHeader(null); // let the documentId we set represent this reference
 124  0
                     searchableAttributeValues.add(searchableAttributeValue);
 125  0
                 }
 126  
                         }
 127  0
                 }
 128  0
                 return searchableAttributeValues;
 129  
         }
 130  
 
 131  
     protected WorkflowDocumentService getWorkflowDocumentService() {
 132  0
         return KewApiServiceLocator.getWorkflowDocumentService();
 133  
     }
 134  
 }