Package org.openengsb.core.workflow.drools.internal

Source Code of org.openengsb.core.workflow.drools.internal.TaskboxServiceImpl

/**
* Licensed to the Austrian Association for Software Tool Integration (AASTI)
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. The AASTI licenses this file to you 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.drools.internal;

import java.util.List;

import org.openengsb.core.api.persistence.PersistenceException;
import org.openengsb.core.api.persistence.PersistenceManager;
import org.openengsb.core.api.persistence.PersistenceService;
import org.openengsb.core.workflow.api.TaskboxException;
import org.openengsb.core.workflow.api.TaskboxService;
import org.openengsb.core.workflow.api.WorkflowException;
import org.openengsb.core.workflow.api.WorkflowService;
import org.openengsb.core.workflow.api.model.InternalWorkflowEvent;
import org.openengsb.core.workflow.api.model.ProcessBag;
import org.openengsb.core.workflow.api.model.Task;
import org.osgi.framework.BundleContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Objects;

public class TaskboxServiceImpl implements TaskboxService {
    private static final Logger LOGGER = LoggerFactory.getLogger(TaskboxServiceImpl.class);

    private WorkflowService workflowService;
    private PersistenceService persistence;
    private PersistenceManager persistenceManager;
    private BundleContext bundleContext;

    public void init() {
        persistence = persistenceManager.getPersistenceForBundle(bundleContext.getBundle());
    }

    public void setWorkflowService(WorkflowService workflowService) {
        this.workflowService = workflowService;
    }

    public void setPersistenceManager(PersistenceManager persistenceManager) {
        this.persistenceManager = persistenceManager;
    }

    public void setBundleContext(BundleContext bundleContext) {
        this.bundleContext = bundleContext;
    }

    @Override
    public List<Task> getOpenTasks() {
        return getTasksForExample(Task.createTaskWithAllValuesSetToNull());
    }

    @Override
    public List<Task> getTasksForExample(Task example) {
        return persistence.query(example);
    }

    @Override
    public Task getTaskForId(String id) throws TaskboxException {
        Task example = Task.createTaskWithAllValuesSetToNull();
        example.setTaskId(id);
        List<Task> list = getTasksForExample(example);
        if (list.size() != 1) {
            throw new TaskboxException((list.size() == 0 ? "No" : "More than one") + " task with ID " + id + " found!");
        }
        return list.get(0);
    }

    @Override
    public List<Task> getTasksForProcessId(String id) {
        Task example = Task.createTaskWithAllValuesSetToNull();
        example.setProcessId(id);
        return getTasksForExample(example);
    }

    @Override
    public synchronized void finishTask(Task task) throws WorkflowException {
        InternalWorkflowEvent finishedEvent = new InternalWorkflowEvent(task);
        Task t = Task.createTaskWithAllValuesSetToNull();
        t.setTaskId(task.getTaskId());
        List<Task> old = getTasksForExample(t);
        if (old.size() > 0) {
            try {
                updateInRunningWorkflow(old.get(0), task);
                persistence.delete(t);
            } catch (PersistenceException e) {
                throw new WorkflowException(e);
            }

            workflowService.processEvent(finishedEvent);
            LOGGER.info("finished task {}", task.getTaskId());
        } else {
            LOGGER.error("tried to finish task {}, BUT there is no such task.", task.getTaskId());
        }
    }

    @Override
    public void updateTask(Task task) throws WorkflowException {
        Task oldTask = getTaskForId(task.getTaskId());
        try {
            persistence.update(oldTask, task);
            updateInRunningWorkflow(oldTask, task);
            LOGGER.info("updated task {}", task.getTaskId());
        } catch (PersistenceException e) {
            LOGGER.error("tried to update task {}, but it didnt work!", task.getTaskId());
            throw new WorkflowException(e);
        }
    }

    private void updateInRunningWorkflow(Task oldTask, Task task) {
        if (oldTask.getProcessId() == null) {
            return;
        }
        long id = Long.parseLong(oldTask.getProcessId());
        ProcessBag bag = null;
        try {
            bag = workflowService.getProcessBagForInstance(id);
        } catch (IllegalArgumentException e) {
            return;
        }
        if (Objects.equal(oldTask, bag)) {
            bag.setProperties(task.getProperties());
        }
    }
}
TOP

Related Classes of org.openengsb.core.workflow.drools.internal.TaskboxServiceImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.