001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kew.impl.document;
017
018import org.apache.commons.lang.StringUtils;
019import org.apache.log4j.Logger;
020import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
021import org.kuali.rice.kew.api.WorkflowRuntimeException;
022import org.kuali.rice.kew.api.document.DocumentProcessingOptions;
023import org.kuali.rice.kew.api.document.DocumentProcessingQueue;
024import org.kuali.rice.kew.api.document.attribute.DocumentAttributeIndexingQueue;
025import org.kuali.rice.kew.engine.OrchestrationConfig;
026import org.kuali.rice.kew.engine.WorkflowEngine;
027import org.kuali.rice.kew.engine.WorkflowEngineFactory;
028
029import javax.jws.WebParam;
030import java.util.Collections;
031
032/**
033 * Reference implementation of the {@code DocumentProcessingQueue}.
034 *
035 * @author Kuali Rice Team (rice.collab@kuali.org)
036 */
037public class DocumentProcessingQueueImpl implements DocumentProcessingQueue {
038
039    private static final Logger LOG = Logger.getLogger(DocumentProcessingQueueImpl.class);
040
041    private WorkflowEngineFactory workflowEngineFactory;
042    private DocumentAttributeIndexingQueue documentAttributeIndexingQueue;
043
044    @Override
045    public void process(@WebParam(name = "documentId") String documentId) {
046        processWithOptions(documentId, null);
047    }
048
049    @Override
050    public void processWithOptions(@WebParam(name = "documentId") String documentId,
051            @WebParam(name = "options") DocumentProcessingOptions options) {
052        if (StringUtils.isBlank(documentId)) {
053            throw new RiceIllegalArgumentException("documentId was a null or blank value");
054        }
055        if (options == null) {
056            options = DocumentProcessingOptions.createDefault();
057        }
058        OrchestrationConfig config = new OrchestrationConfig(OrchestrationConfig.EngineCapability.STANDARD,
059                Collections.<String>emptySet(), null, options.isSendNotifications(), options.isRunPostProcessor());
060        WorkflowEngine engine = getWorkflowEngineFactory().newEngine(config);
061        try {
062                        engine.process(documentId, null);
063                } catch (Exception e) {
064                        LOG.error("Failed to process document through the workflow engine", e);
065            if (e instanceof RuntimeException) {
066                throw (RuntimeException)e;
067            }
068                        throw new WorkflowRuntimeException(e);
069                }
070        if (options.isIndexSearchAttributes()) {
071            getDocumentAttributeIndexingQueue().indexDocument(documentId);
072        }
073    }
074
075    public WorkflowEngineFactory getWorkflowEngineFactory() {
076        return workflowEngineFactory;
077    }
078
079    public void setWorkflowEngineFactory(WorkflowEngineFactory workflowEngineFactory) {
080        this.workflowEngineFactory = workflowEngineFactory;
081    }
082
083    public DocumentAttributeIndexingQueue getDocumentAttributeIndexingQueue() {
084        return documentAttributeIndexingQueue;
085    }
086
087    public void setDocumentAttributeIndexingQueue(DocumentAttributeIndexingQueue documentAttributeIndexingQueue) {
088        this.documentAttributeIndexingQueue = documentAttributeIndexingQueue;
089    }
090
091}