启动本环境中的流程

直接启动

替换样例代码中的

    • appId - 应用ID
    • processDefId - 流程ID
    • loginAs - 启动流程的人员身份(可用Hash变量,如#currentUser.username#)
    • workflowVariable - 工作流变量,例如“var_status=approved&var_result=successful”
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletRequest;
import org.joget.apps.app.service.AppUtil;
import org.joget.commons.util.SecurityUtil;
import org.joget.commons.util.SetupManager;
import org.joget.commons.util.StringUtil;
import org.joget.directory.model.User;
import org.joget.workflow.util.WorkflowUtil;
/**
 * json api 启动流程(本环境)
 * @author eric
 *
 */
public class InnerProcessManagement {
    /**
     * 以下四个参数根据实际情况自定义
     * 1、appId:应用id。如:“appid”
     * 2、processDefId:流程id。如:“process1”
     * 3、loginAs:流程启动者id。如:“admin”
     * 4、workflowVariable:工作流变量赋值,变量名以“var_”为前缀,多个变量之间以“&”连接,如:var_status=approved&var_result=successful
     */
    private String appId = "";
    private String processDefId = "";
    private String loginAs = "";
    private String workflowVariable = "";
     
    private static final String VERSION = "latest";
    private static final String SEPARATOR = ":";
	//判断是否启动的条件,大多是哈希变量,可根据需要修改
	private String condition= "#variable.status#";

    /**
     * 
     * @author eric
     *
     */
    class Master {
        private String username;
        private String password;
        private String hash;
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        public String getHash() {
            return hash;
        }
        public void setHash(String hash) {
            this.hash = hash;
        }
 
    }
    /**
     * post 请求 
     * @param urlString 请求链接
     * @return result 请求结果 
     */
    private String doPost(String urlString){
        BufferedReader br = null;
        String result = "";
        URL url = null;
        try {
            url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("referer", "DOMAIN");//对设置白名单的生效,替换DOMAIN为实际IP或域名
            if (connection.getResponseCode() == 200) {
                br = new BufferedReader(new InputStreamReader(
                        connection.getInputStream(), "UTF-8"));
                String line;
                while ((line = br.readLine()) != null) {
                    result += line;
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
         
        return result;
    }
    /**
     * 
     * @param request
     * @return
     */
    private String getBaseUrl(HttpServletRequest request){
       	String scheme = request.getScheme();
		String serverName = request.getServerName();
		int port = request.getServerPort();
		String contextPath = request.getContextPath();

		return scheme + "://" + serverName + ":" + String.valueOf(port) + contextPath + "/";
    }
    /**
     * 启动流程
     */
    private void startProcess(){
		//将"#variable.status#"替换为需要的值
		if(!"#variable.status#".equals(condition)){
			return;
		}
		//将key=value&key=value 中的value部分进行编码
        if(workflowVariable!=null && !workflowVariable.equals("")){
                String [] arr  = workflowVariable.split("&");
                StringBuffer buffer = new StringBuffer();
                for (int i = 0; i <arr.length ; i++) {
                    String [] s = arr[i].split("=");
                    if(s.length == 2){
                        buffer.append(s[0]+"=");
                        buffer.append(URLEncoder.encode(s[1],"UTF-8")+"&");
                    }else{
                        buffer.append(arr[i]+"&");
                    }
                }
                workflowVariable = buffer.deleteCharAt(buffer.length()-1).toString();
		//                System.out.println(workflowVariable);
        }
        Master master = getMaster();
        HttpServletRequest request = WorkflowUtil.getHttpServletRequest();
        String baseUrl = getBaseUrl(request);
        String url = baseUrl + "web/json/workflow/process/start/"
                + appId + SEPARATOR + VERSION + SEPARATOR + processDefId
                + "?j_username=" + master.getUsername()
                + "&hash=" + master.getHash()
                + "&loginAs=" + loginAs
                + "&" + workflowVariable;
        doPost(url);    
    }
    /**
     * 
     * @return
     */
    private Master getMaster(){
        Master mmaster = new Master();
        SetupManager setupManager = (SetupManager) AppUtil.getApplicationContext().getBean("setupManager");
 
        String masterLoginUsername = setupManager.getSettingValue("masterLoginUsername");
        String masterLoginPassword = SecurityUtil.decrypt(setupManager.getSettingValue("masterLoginPassword")); //clear text password
 
        //get login hash
        User master = new User();
        master.setUsername(masterLoginUsername.trim());
        master.setPassword(StringUtil.md5Base16(masterLoginPassword));
 
        String loginHash = master.getLoginHash();
         
        mmaster.setUsername(masterLoginUsername.trim());
        mmaster.setPassword(StringUtil.md5Base16(masterLoginPassword));
        mmaster.setHash(loginHash);
         
        return mmaster;
    }
    /**
     * 主方法
     */
    public void run(){
        startProcess();
    }
}
new InnerProcessManagement().run();

判断满足条件后才启动

启动流程后获取流程ID并保存到当前表单上某个字段

启动其它环境的流程

直接启动

替换样例代码中的

    • ApprovalLevel - 工作流变量(被转化为整数后加1)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
/**
 * json api 启动流程(非本环境)
 * @author eric
 *
 */
public class OuterProcessManagement {
    /**
     * 以下七个参数根据实际情况自定义
     * 1、baseUrl:流程所在地址。如:“http://abc.com”
     * 2、appId:应用id。如:“appid”
     * 3、processDefId:流程id。如:“process1”
     * 4、workflowVariable:工作流变量。变量名以“var_”为前缀,多个变量之间以“&”连接,如:var_status=approved&var_result=successful
     * 5、masterUsername:主登录用户名。在“设置->基础设置->系统管理员设置中获取”。
     * 6、masterHash:主登录hash。在“设置->基础设置->系统管理员设置中获取”。
     * 5、loginAs:流程启动者id。如:“admin”
     */
    private String baseUrl = "";
     
    private String appId = "";
    private String processDefId = "";
 
    private String workflowVariable = "";
 
    private String masterUsername = "";
    private String masterHash = "";
    private String loginAs = "";
     
    private static final String VERSION = "latest";
    private static final String SEPARATOR = ":";


	//判断是否启动的条件,大多是哈希变量,可根据需要修改
	private String condition= "#variable.status#";

    /**
     * post 请求 
     * @param urlString 请求链接
     * @return result 请求结果 
     */
    private String doPost(String urlString){
        BufferedReader br = null;
        String result = "";
        URL url = null;
        try {
            url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("referer", "DOMAIN");//对设置白名单的生效,替换DOMAIN为实际IP或域名
            if (connection.getResponseCode() == 200) {
                br = new BufferedReader(new InputStreamReader(
                        connection.getInputStream(), "UTF-8"));
                String line;
                while ((line = br.readLine()) != null) {
                    result += line;
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
         
        return result;
    }
    /**
     * 启动流程
     */
    private void startProcess(){
		//将"#variable.status#"替换为需要的值
		if(!"#variable.status#".equals(condition)){
			return;
		}
        //将key=value&key=value 中的value部分进行编码
		if(workflowVariable!=null && !workflowVariable.equals("")){
                String [] arr  = workflowVariable.split("&");
                StringBuffer buffer = new StringBuffer();
                for (int i = 0; i <arr.length ; i++) {
                    String [] s = arr[i].split("=");
                    if(s.length == 2){
                        buffer.append(s[0]+"=");
                        buffer.append(URLEncoder.encode(s[1],"UTF-8")+"&");
                    }else{
                        buffer.append(arr[i]+"&");
                    }
                }
                workflowVariable = buffer.deleteCharAt(buffer.length()-1).toString();
//                System.out.println(workflowVariable);
        }
		
        String url = baseUrl + "#request.contextPath#" + "/web/json/workflow/process/start/"
                + appId + SEPARATOR + VERSION + SEPARATOR + processDefId
                + "?j_username=" + masterUsername
                + "&hash=" + masterHash
                + "&loginAs=" + loginAs
				+"&"+workflowVariable;
        doPost(url);    
    }
    /**
     * 主方法
     */
    public void run(){
        startProcess();
    }
}
new OuterProcessManagement().run();

判断满足条件后才启动

启动流程后获取流程ID并保存到当前表单上某个字段


在本地通过接口直接启动流程

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.joget.apps.app.model.AppDefinition;
import org.joget.apps.app.service.AppService;
import org.joget.apps.app.service.AppUtil;
import org.joget.workflow.model.WorkflowProcessResult;
import org.joget.workflow.model.service.WorkflowManager;
import org.joget.workflow.model.WorkflowAssignment;
import org.joget.workflow.model.WorkflowProcess;
    
public void execute(WorkflowAssignment assignment, AppDefinition appDef, HttpServletRequest request) {
	// 流程ID, 根据需要修改
    String processId = "process1";
	//工作流变量
   	String status = "status";

    AppService appService = (AppService) AppUtil.getApplicationContext().getBean("appService");
    WorkflowManager workflowManager = (WorkflowManager) AppUtil.getApplicationContext().getBean("workflowManager");
    //get current record id
    String recordId = appService.getOriginProcessId(assignment.getProcessId());
    //get process
    WorkflowProcess process = appService.getWorkflowProcessForApp(appDef.getAppId(), appDef.getVersion().toString(), processId);
		
	//工作流变脸MAP对象
    Map variables = new HashMap();
    variables.put("status",status);

    //start process
    WorkflowProcessResult result = workflowManager.processStart(process.getId(), null, variables, null, recordId, false);
    System.out.println(result.getProcess().getId());
}

execute(workflowAssignment, appDef, request);
  • No labels