001 /*
002 * Copyright 2005-2007 The Kuali Foundation
003 *
004 *
005 * Licensed under the Educational Community License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.opensource.org/licenses/ecl2.php
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.kuali.rice.kew.docsearch.service.impl;
018
019 import java.util.ArrayList;
020 import java.util.Iterator;
021 import java.util.List;
022
023 import org.apache.log4j.Logger;
024 import org.kuali.rice.kew.docsearch.DocSearchUtils;
025 import org.kuali.rice.kew.docsearch.SearchableAttribute;
026 import org.kuali.rice.kew.docsearch.SearchableAttributeValue;
027 import org.kuali.rice.kew.docsearch.service.SearchableAttributeProcessingService;
028 import org.kuali.rice.kew.doctype.bo.DocumentType;
029 import org.kuali.rice.kew.exception.WorkflowRuntimeException;
030 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValueContent;
031 import org.kuali.rice.kew.service.KEWServiceLocator;
032
033
034 /**
035 * Implementation of {@link SearchableAttributeProcessingService}.
036 *
037 * @author Kuali Rice Team (rice.collab@kuali.org)
038 */
039 public class SearchableAttributeProcessor implements SearchableAttributeProcessingService {
040
041 private static Logger LOG = Logger.getLogger(SearchableAttributeProcessor.class);
042
043 public void indexDocument(Long documentId) {
044 indexDocument(documentId, true);
045 }
046
047 public void indexDocument(Long documentId, boolean useMostRecentDocType) {
048 long t1 = System.currentTimeMillis();
049 LOG.info("Indexing document " + documentId + " for document search...");
050 try {
051 DocumentType documentType = KEWServiceLocator.getDocumentTypeService().findByDocumentId(documentId);
052 DocumentRouteHeaderValueContent docContent = KEWServiceLocator.getRouteHeaderService().getContent(documentId);
053 List<SearchableAttributeValue> attributes = buildSearchableAttributeValues(documentType, documentId, docContent.getDocumentContent(), useMostRecentDocType);
054 KEWServiceLocator.getRouteHeaderService().updateRouteHeaderSearchValues(documentId, attributes);
055 } catch (Exception e) {
056 String errorMsg = "Encountered an error when attempting to index searchable attributes, requeuing.";
057 LOG.error(errorMsg, e);
058 throw new WorkflowRuntimeException(errorMsg,e);
059 }
060 long t2 = System.currentTimeMillis();
061 LOG.info("...finished indexing document " + documentId + " for document search, total time = " + (t2-t1) + " ms.");
062 }
063
064 private List<SearchableAttributeValue> buildSearchableAttributeValues(DocumentType docType, Long documentId, String docContent, boolean useMostRecentDocType) {
065 if (useMostRecentDocType) {
066 docType = KEWServiceLocator.getDocumentTypeService().findByName(docType.getName());
067 }
068 List<SearchableAttributeValue> searchableAttributeValues = new ArrayList<SearchableAttributeValue>();
069
070 for (Iterator iterator = docType.getSearchableAttributes().iterator(); iterator.hasNext();) {
071 SearchableAttribute searchableAttribute = (SearchableAttribute) iterator.next();
072 List searchStorageValues = searchableAttribute.getSearchStorageValues(
073 DocSearchUtils.getDocumentSearchContext(documentId.toString(), docType.getName(), docContent));
074 if (searchStorageValues != null) {
075 for (Iterator iterator2 = searchStorageValues.iterator(); iterator2.hasNext();) {
076 SearchableAttributeValue searchableAttributeValue = (SearchableAttributeValue) iterator2.next();
077 searchableAttributeValue.setRouteHeaderId(documentId);
078 searchableAttributeValues.add(searchableAttributeValue);
079 searchableAttributeValue.setRouteHeader(null); // let the routeHeaderId we set represent this reference
080 }
081 }
082 }
083
084 return searchableAttributeValues;
085 }
086
087 }