启动一个流程

应用场景

  • 通过流程TOOL启动流程

样例代码

启动本环境中的流程

    1. 替换样例代码中的
      • 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 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 = ":";
        	/**
        	 * 
        	 * @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 protocol = request.getProtocol();
        		protocol = protocol.substring(0, protocol.indexOf("/"));
        		String serverName = request.getServerName();
        		int port = request.getServerPort();
        		String contextPath = request.getContextPath();
        		
        		return protocol + "://" + serverName + ":" + String.valueOf(port) + contextPath + "/";
        	}
        	/**
        	 * 启动流程
        	 */
        	private void startProcess(){
        		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();
        

启动其它环境的流程

    1. 替换样例代码中的
      • 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;
        /**
         * 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 = ":";
        	/**
        	 * 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(){
        		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();
        
        
Create by Sean Fung on 2016-03-24 14:26:58.0
Last updated by Sean Fung on 2016-05-30 15:01:55.0