Package org.apache.agila.services.task

Source Code of org.apache.agila.services.task.AbstractTaskService

/*
* Copyright 2004 The Apache Software Foundation.
*
* 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.apache.agila.services.task;

import org.apache.agila.engine.Instance;
import org.apache.agila.engine.InstanceID;
import org.apache.agila.engine.Token;
import org.apache.agila.engine.TokenID;
import org.apache.agila.impl.NodeContextImpl;
import org.apache.agila.model.BusinessProcess;
import org.apache.agila.model.BusinessProcessID;
import org.apache.agila.model.Node;
import org.apache.agila.services.BusinessProcessService;
import org.apache.agila.services.InstanceService;
import org.apache.agila.services.notification.NotificationService;
import org.apache.agila.services.TimerService;
import org.apache.agila.services.TokenService;
import org.apache.agila.services.user.UserID;
import org.apache.agila.services.user.GroupID;

import java.util.Date;
import java.util.List;

/**
*
* @author <a href="mailto:geir@gluecode.com">Geir Magnusson Jr.</a>
* @version $Id: AbstractTaskService.java 287 2005-07-26 10:08:26Z jstrachan $
*/
public abstract class AbstractTaskService implements TaskService {

    private TokenService tokenService;
    private InstanceService instanceService;
    private BusinessProcessService businessProcessService;

    private TimerService timerService;
    private NotificationService notificationService;

    public void setTokenService(TokenService ts) {
        this.tokenService = ts;
    }

    public void setInstanceService(InstanceService is) {
        this.instanceService = is;
    }

    public void setBusinessProcessService(BusinessProcessService bps) {
        this.businessProcessService = bps;
    }

    public void setTimerService( TimerService timerService ) {
        this.timerService = timerService;
    }

    public void setNotificationService( NotificationService notificationService ) {
        this.notificationService = notificationService;
    }

    /**
     * assigns a new task to a given user
     *
     * @param tokenID
     * @param message
     * @param userID
     * @param due
     * @return
     */
    public TaskID assignTask(TokenID tokenID, String message, UserID userID, Date due) {

        InstanceID instanceID = tokenService.getTokenByID(tokenID).getInstanceID();

        TaskImpl ti = new TaskImpl(userID, tokenID, instanceID, message, due, Task.TASK_INCOMPLETE);

        internalSave(ti);

        return ti.getTaskID();
    }

    public TaskID assignTaskToTeam(TokenID tokenID, String message, GroupID groupID, Date due) {

        InstanceID instanceID = tokenService.getTokenByID(tokenID).getInstanceID();

        TaskImpl ti = new TaskImpl(groupID, tokenID, instanceID, message, due, Task.TASK_INCOMPLETE);

        internalSave(ti);

        return ti.getTaskID();
    }

    /**
     * returns a list of tasks for a user
     *
     * @param userID
     * @return
     */
    public List getTasksForUser(UserID userID, int status) {

        return internalGetTasksByUserID(userID, status);
    }

    /**
     * sets the status of a task
     *
     * @param taskID
     * @param status
     */
    public void setTaskStatus(TaskID taskID, int status) {
        Task t = internalGetTask(taskID);

        if (t != null) {
            t.setTaskStatus(status);
            internalSave(t);
        }
    }

    /**
     * gets a task object by id
     *
     * @param taskID
     * @return
     */
    public Task getTaskByID(TaskID taskID) {
        return internalGetTask(taskID);
    }

    public Renderer getRendererForTask(TaskID taskID, Class rendererType) {

        Token token = getTokenForTaskID(taskID);
        Node node = getNodeForToken(token);

        NodeContextImpl ctx = new NodeContextImpl(node,
                instanceService.getInstanceByID(token.getInstanceID()),
                timerService, this, notificationService);

        ctx.setCurrentExecutionToken(token);

        if (node instanceof TaskActivity) {
            return ((TaskActivity) node).getRenderer(ctx, rendererType);
        }

        return null;
    }

    public ResponseHandler getResponseHandlerForTask(TaskID taskID, Class handlerType) {


        Token token = getTokenForTaskID(taskID);
        Node node = getNodeForToken(token);

        NodeContextImpl ctx = new NodeContextImpl(node,
                instanceService.getInstanceByID(token.getInstanceID()),
                timerService, this, notificationService);

        ctx.setCurrentExecutionToken(token);

        if (node instanceof TaskActivity) {
            return ((TaskActivity) node).getResponseHandler(ctx, handlerType);
        }

        return null;
    }


    private Node getNodeForToken(Token token) {

        InstanceID instanceID = token.getInstanceID();
        Instance instance = instanceService.getInstanceByID(instanceID);

        BusinessProcessID bpid = businessProcessService.getGraphIDByName(instance.getGraphName());

        BusinessProcess bp = businessProcessService.getGraphByID(bpid);

        return bp.getNode(token.getCurrentNodeID());
    }

    private Token getTokenForTaskID(TaskID taskID) {

        Task task = internalGetTask(taskID);

        if (task == null) {
            return null;
        }

        TokenID tokenID = task.getSourceTokenID();
        Token token = tokenService.getTokenByID(tokenID);

        return token;
    }

    /**
     * Add or update a task
     *
     * @param ti
     */
    protected abstract void internalSave(Task ti);

    protected abstract List internalGetTasksByUserID(UserID userID, int status);

    protected abstract Task internalGetTask(TaskID taskID);
}
TOP

Related Classes of org.apache.agila.services.task.AbstractTaskService

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.