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.messaging;
018    
019    import org.apache.log4j.Logger;
020    import org.kuali.rice.kew.api.KewApiServiceLocator;
021    import org.kuali.rice.kew.api.WorkflowRuntimeException;
022    import org.kuali.rice.kew.api.document.attribute.DocumentAttributeIndexingQueue;
023    import org.kuali.rice.kew.engine.OrchestrationConfig;
024    import org.kuali.rice.kew.engine.OrchestrationConfig.EngineCapability;
025    import org.kuali.rice.kew.engine.WorkflowEngine;
026    import org.kuali.rice.kew.service.KEWServiceLocator;
027    import org.kuali.rice.ksb.messaging.service.KSBXMLService;
028    
029    
030    /**
031     * An XML services which is used to submit documents to the engine.
032     *
033     * @author Kuali Rice Team (rice.collab@kuali.org)
034     */
035    public class RouteDocumentMessageService implements KSBXMLService {
036    
037            private static final Logger LOG = Logger.getLogger(RouteDocumentMessageService.class);
038            
039            public void invoke(String xml) {
040                    try {
041                            RouteMessageXmlElement newElement = RouteMessageXmlElement.construct(xml);
042                            OrchestrationConfig config = new OrchestrationConfig(EngineCapability.STANDARD, newElement.runPostProcessor);
043                            WorkflowEngine engine = KEWServiceLocator.getWorkflowEngineFactory().newEngine(config);
044                            engine.process(newElement.documentId, null);
045                            if (newElement.shouldSearchAttributeIndex) {
046                    DocumentAttributeIndexingQueue queue = KewApiServiceLocator.getDocumentAttributeIndexingQueue(KEWServiceLocator.getRouteHeaderService().getRouteHeader(newElement.documentId).getDocumentType().getApplicationId());
047                    queue.indexDocument(newElement.documentId);
048                            }
049                    } catch (Exception e) {
050                            LOG.error(e);
051                            throw new WorkflowRuntimeException(e);
052                    }
053            }
054            
055            public static class RouteMessageXmlElement {
056                    public String documentId;
057                    public boolean runPostProcessor = true;
058                    public boolean shouldSearchAttributeIndex = false;
059                    public RouteMessageXmlElement(String documentId) {
060                            this.documentId = documentId;
061                    }
062                    public RouteMessageXmlElement(String documentId, boolean runPostProcessor) {
063                            this(documentId);
064                            this.runPostProcessor = runPostProcessor;
065                    }
066                    public RouteMessageXmlElement(String documentId, boolean runPostProcessor, boolean shouldIndex) {
067                            this(documentId, runPostProcessor);
068                            this.shouldSearchAttributeIndex = shouldIndex;
069                    }
070                    private static final String SPLIT = "::~~::";
071                    public static RouteMessageXmlElement construct(String content) {
072                            if (content.contains(SPLIT)) {
073                                    String[] values = content.split(SPLIT);
074                                    if (values.length == 3) {
075                                            return new RouteMessageXmlElement(values[0], Boolean.valueOf(values[1]), Boolean.valueOf(values[2]));
076                                    } else {
077                                            return new RouteMessageXmlElement(values[0],Boolean.valueOf(values[1]));
078                                    }
079                            } else {
080                                    return new RouteMessageXmlElement(content);
081                            }
082                    }
083                    public String translate() {
084                            return documentId + SPLIT + String.valueOf(runPostProcessor) + SPLIT + String.valueOf(shouldSearchAttributeIndex);
085                    }
086            }
087    
088    }