com.glaf.jbpm.action.SqlMapTaskInstanceAction.java Source code

Java tutorial

Introduction

Here is the source code for com.glaf.jbpm.action.SqlMapTaskInstanceAction.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF 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 com.glaf.jbpm.action;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.StringTokenizer;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.JbpmException;
import org.jbpm.context.exe.ContextInstance;
import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.def.Node;
import org.jbpm.graph.exe.ExecutionContext;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.graph.exe.Token;
import org.jbpm.graph.node.TaskNode;
import org.jbpm.taskmgmt.def.Task;
import org.jbpm.taskmgmt.exe.TaskInstance;
import org.jbpm.taskmgmt.exe.TaskMgmtInstance;

import com.glaf.core.identity.User;
import com.glaf.core.util.LogUtils;
import com.glaf.core.util.StringTools;
import com.glaf.jbpm.db.mybatis2.SqlMapContainer;
import com.glaf.jbpm.el.DefaultExpressionEvaluator;
import com.glaf.jbpm.util.Constant;

/**
 * ???task-nodecreate-tasks?create-tasks="false" 
 * <task-node name="" create-tasks="false"><br>
 * <event type="node-enter"><br>
 * <action ref-name="taskinstance_role07x"/><br>
 * </event><br>
 * <task name="task07x" description="" ></task><br>
 * <transition name="tr410" to=""></transition><br>
 * </task-node><br>
 */
public class SqlMapTaskInstanceAction implements ActionHandler {
    private static final Log logger = LogFactory.getLog(SqlMapTaskInstanceAction.class);

    private static final long serialVersionUID = 1L;

    /**
     * ??
     */
    protected String description;

    /**
     * ??true?
     */
    protected String expression;

    /**
     * ??
     */
    protected String deptId;

    /**
     * ? <br>
     * <deptIds><br>
     * <element>310</element><br>
     * <element>303</element><br>
     * </deptIds><br>
     */
    protected List<Object> deptIds;

    /**
     * ??
     */
    protected String roleId;

    /**
     * ? <br>
     * <roleIds><br>
     * <element>R001</element><br>
     * <element>R002</element><br>
     * </roleIds><br>
     */
    protected List<Object> roleIds;

    /**
     * SqlMap??
     */
    protected String queryId;

    /**
     * ??
     */
    protected String objectId;

    /**
     * ?
     */
    protected String objectValue;

    /**
     * ????????contextInstance.getVariable()?
     * contextInstance.getVariable("SendDocAuditor");
     */
    protected String dynamicActors;

    /**
     * ??
     */
    protected String transitionName;

    /**
     * ??
     */
    protected String taskName;

    /**
     * ???true
     */
    protected boolean isPooled;

    /**
     * ????
     */
    protected boolean leaveNodeIfActorNotAvailable;

    /**
     * ???
     */
    protected String sendMail;

    /**
     * 
     */
    protected String subject;

    /**
     * 
     */
    protected String content;

    /**
     * 
     */
    protected String taskContent;

    /**
     * ??
     */
    protected String templateId;

    public SqlMapTaskInstanceAction() {

    }

    public void execute(ExecutionContext ctx) {
        logger.debug("-------------------------------------------------------");
        logger.debug("---------------MyBatis3TaskInstanceAction----------------");
        logger.debug("-------------------------------------------------------");

        boolean executable = true;

        Map<String, Object> params = new java.util.HashMap<String, Object>();

        ContextInstance contextInstance = ctx.getContextInstance();
        Map<String, Object> variables = contextInstance.getVariables();
        if (variables != null && variables.size() > 0) {
            Set<Entry<String, Object>> entrySet = variables.entrySet();
            for (Entry<String, Object> entry : entrySet) {
                String name = entry.getKey();
                Object value = entry.getValue();
                if (name != null && value != null && params.get(name) == null) {
                    params.put(name, value);
                }
            }
        }

        ProcessInstance processInstance = ctx.getProcessInstance();
        params.put("processInstanceId", processInstance.getId());

        if (StringUtils.isNotEmpty(expression)) {
            if (expression.startsWith("#{") && expression.endsWith("}")) {
                Object value = DefaultExpressionEvaluator.evaluate(expression, params);
                if (value != null) {
                    if (value instanceof Boolean) {
                        Boolean b = (Boolean) value;
                        executable = b.booleanValue();
                    }
                }
            }
        }

        if (!executable) {
            logger.debug("???false??");
            return;
        }

        List<String> actorIds = new java.util.ArrayList<String>();
        if (StringUtils.isNotEmpty(dynamicActors)) {
            String actorId = (String) contextInstance.getVariable(dynamicActors);
            if (StringUtils.isNotEmpty(actorId)) {
                StringTokenizer st2 = new StringTokenizer(actorId, ",");
                while (st2.hasMoreTokens()) {
                    String elem = st2.nextToken();
                    if (StringUtils.isNotEmpty(elem)) {
                        actorIds.add(elem);
                    }
                }
            }
        }

        /**
         * ????
         */
        if (StringUtils.isNotEmpty(queryId)) {
            if (LogUtils.isDebug()) {
                logger.debug("queryId:" + queryId);
            }
            params.put("roleId", roleId);
            params.put("objectId", objectId);
            params.put("objectValue", objectValue);

            if (StringUtils.isNotEmpty(deptId)) {
                String tmp = deptId;
                Object value = deptId;
                if (tmp.startsWith("#{") && tmp.endsWith("}")) {
                    value = DefaultExpressionEvaluator.evaluate(tmp, params);
                } else if (tmp.startsWith("#P{") && tmp.endsWith("}")) {
                    tmp = StringTools.replaceIgnoreCase(tmp, "#P{", "");
                    tmp = StringTools.replaceIgnoreCase(tmp, "}", "");
                    value = contextInstance.getVariable(tmp);
                }
                params.put("deptId", value);
            }

            if (StringUtils.isNotEmpty(roleId)) {
                String tmp = roleId;
                Object value = roleId;
                if (tmp.startsWith("#{") && tmp.endsWith("}")) {
                    value = DefaultExpressionEvaluator.evaluate(tmp, params);
                } else if (tmp.startsWith("#P{") && tmp.endsWith("}")) {
                    tmp = StringTools.replaceIgnoreCase(tmp, "#P{", "");
                    tmp = StringTools.replaceIgnoreCase(tmp, "}", "");
                    value = contextInstance.getVariable(tmp);
                }
                params.put("roleId", value);
            }

            if (StringUtils.isNotEmpty(objectId)) {
                String tmp = objectId;
                Object value = objectId;
                if (tmp.startsWith("#{") && tmp.endsWith("}")) {
                    value = DefaultExpressionEvaluator.evaluate(tmp, params);
                } else if (tmp.startsWith("#P{") && tmp.endsWith("}")) {
                    tmp = StringTools.replaceIgnoreCase(tmp, "#P{", "");
                    tmp = StringTools.replaceIgnoreCase(tmp, "}", "");
                    value = contextInstance.getVariable(tmp);
                }
                params.put("objectId", value);
            }

            if (StringUtils.isNotEmpty(objectValue)) {
                String tmp = objectValue;
                Object value = objectValue;
                if (tmp.startsWith("#{") && tmp.endsWith("}")) {
                    value = DefaultExpressionEvaluator.evaluate(tmp, params);
                } else if (tmp.startsWith("#P{") && tmp.endsWith("}")) {
                    tmp = StringTools.replaceIgnoreCase(tmp, "#P{", "");
                    tmp = StringTools.replaceIgnoreCase(tmp, "}", "");
                    value = contextInstance.getVariable(tmp);
                }
                params.put("objectValue", value);
            }

            if (deptIds != null && deptIds.size() > 0) {
                params.put("deptIds", deptIds);
            }

            if (roleIds != null && roleIds.size() > 0) {
                params.put("roleIds", roleIds);
            }

            if (LogUtils.isDebug()) {
                logger.debug("params:" + params);
            }

            SqlMapContainer container = SqlMapContainer.getContainer();

            Collection<?> actors = container.getList(ctx.getJbpmContext().getConnection(), queryId, params);

            if (actors != null && actors.size() > 0) {
                Iterator<?> iterator = actors.iterator();
                while (iterator.hasNext()) {
                    Object object = iterator.next();
                    String actorId = null;
                    if (object instanceof String) {
                        actorId = (String) object;
                    } else if (object instanceof User) {
                        User user = (User) object;
                        actorId = user.getActorId();
                    }
                    if (actorId != null) {
                        actorIds.add(actorId);
                    }
                }
            }
        }

        if (LogUtils.isDebug()) {
            logger.debug("actorIds:" + actorIds);
        }

        String startActorId = (String) contextInstance.getVariable(Constant.PROCESS_STARTERID);
        actorIds.remove(startActorId);

        if (actorIds.size() == 0) {

            if (leaveNodeIfActorNotAvailable) {
                contextInstance.setVariable(Constant.IS_AGREE, "true");
                if (StringUtils.isNotEmpty(transitionName)) {
                    ctx.leaveNode(transitionName);
                } else {
                    ctx.leaveNode();
                }
                return;
            }

        }

        if (actorIds.size() > 0) {

            Task task = null;

            if (StringUtils.isNotEmpty(taskName)) {
                Node node = ctx.getNode();
                if (node instanceof TaskNode) {
                    TaskNode taskNode = (TaskNode) node;
                    task = taskNode.getTask(taskName);
                }
            }

            if (task == null) {
                task = ctx.getTask();
            }

            if (task == null) {
                throw new JbpmException(" task is null");
            }

            Token token = ctx.getToken();
            TaskMgmtInstance tmi = ctx.getTaskMgmtInstance();

            if (isPooled) {
                TaskInstance taskInstance = tmi.createTaskInstance(task, token);
                taskInstance.setCreate(new Date());
                if (task != null) {
                    taskInstance.setSignalling(task.isSignalling());
                }
                if (actorIds.size() == 1) {
                    String actorId = actorIds.iterator().next();
                    taskInstance.setActorId(actorId);
                } else {
                    int i = 0;
                    String[] pooledIds = new String[actorIds.size()];
                    Iterator<String> iterator2 = actorIds.iterator();
                    while (iterator2.hasNext()) {
                        pooledIds[i++] = iterator2.next();
                    }
                    taskInstance.setPooledActors(pooledIds);
                }
            } else {
                Set<String> userIds = new HashSet<String>();
                Iterator<String> iterator = actorIds.iterator();
                while (iterator.hasNext()) {
                    String actorId = iterator.next();
                    if (!userIds.contains(actorId)) {
                        TaskInstance taskInstance = tmi.createTaskInstance(task, token);
                        taskInstance.setActorId(actorId);
                        taskInstance.setCreate(new Date());
                        if (task != null) {
                            taskInstance.setSignalling(task.isSignalling());
                        }
                        userIds.add(actorId);
                    }
                }
            }
            if (StringUtils.isNotEmpty(sendMail) && StringUtils.equals(sendMail, "true")) {
                MailBean mailBean = new MailBean();
                mailBean.setContent(content);
                mailBean.setSubject(subject);
                mailBean.setTaskContent(taskContent);
                mailBean.setTaskName(taskName);
                mailBean.setTemplateId(templateId);
                mailBean.execute(ctx, actorIds);
            }
        }
    }

    public void setContent(String content) {
        this.content = content;
    }

    public void setDeptId(String deptId) {
        this.deptId = deptId;
    }

    public void setDeptIds(List<Object> deptIds) {
        this.deptIds = deptIds;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setDynamicActors(String dynamicActors) {
        this.dynamicActors = dynamicActors;
    }

    public void setExpression(String expression) {
        this.expression = expression;
    }

    public void setLeaveNodeIfActorNotAvailable(boolean leaveNodeIfActorNotAvailable) {
        this.leaveNodeIfActorNotAvailable = leaveNodeIfActorNotAvailable;
    }

    public void setObjectId(String objectId) {
        this.objectId = objectId;
    }

    public void setObjectValue(String objectValue) {
        this.objectValue = objectValue;
    }

    public void setPooled(boolean isPooled) {
        this.isPooled = isPooled;
    }

    public void setQueryId(String queryId) {
        this.queryId = queryId;
    }

    public void setRoleId(String roleId) {
        this.roleId = roleId;
    }

    public void setRoleIds(List<Object> roleIds) {
        this.roleIds = roleIds;
    }

    public void setSendMail(String sendMail) {
        this.sendMail = sendMail;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public void setTaskContent(String taskContent) {
        this.taskContent = taskContent;
    }

    public void setTaskName(String taskName) {
        this.taskName = taskName;
    }

    public void setTemplateId(String templateId) {
        this.templateId = templateId;
    }

    public void setTransitionName(String transitionName) {
        this.transitionName = transitionName;
    }

}