/**
 * Copyright 2010 OpenEngSB Division, Vienna University of Technology
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openengsb.core.workflow.internal;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;

import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.process.ProcessInstance;
import org.openengsb.core.common.workflow.WorkflowException;
import org.openengsb.core.common.workflow.model.ProcessBag;
import com.google.common.base.Preconditions;

public class WorkflowStarter implements Callable<Long> {

    private StatefulKnowledgeSession session;
    private String processId;
    private Map<String, Object> params;

    public WorkflowStarter(StatefulKnowledgeSession session, String processId, Map<String, Object> params) {
        Preconditions.checkNotNull(params, "params-map must not be null");
        this.session = session;
        this.processId = processId;
        this.params = params;
    }

    public WorkflowStarter(StatefulKnowledgeSession session, String processId) {
        this(session, processId, new HashMap<String, Object>());
    }

    public Long call() throws WorkflowException {
        ProcessBag processBag = getProcessBag();
        ProcessInstance processInstance = session.startProcess(processId, params);
        session.insert(processInstance);
        processBag.setProcessId(String.valueOf(processInstance.getId()));
        processInstance.signalEvent("FlowStartedEvent", null);
        return processInstance.getId();
    }

    private ProcessBag getProcessBag() {
        ProcessBag processBag;
        if (!params.containsKey("processBag")) {
            processBag = new ProcessBag();
            params.put("processBag", processBag);
        } else {
            processBag = (ProcessBag) params.get("processBag");
        }
        return processBag;
    }
}

@@ -986,16 +986,17 @@
 essBag;%0A
+%0A
 import c

fix checkstyle error