Jbpm4監聽的實現

livedba發表於2012-07-26
Jbpm4監聽的實現 ,包括驗證下一個節點是否有審批人,如果沒有提示審批不了,還有更改業務資料的狀態,實現如下:[@more@]



<!--把對應的報價單審批狀態置為審批中--&gt

<!--看下一個部門經理有沒有人,沒有人提示--&gt







JbpmEventListener實現如下

/**
* Name: net.uni.ap.jbpm.handle.JbpmEventListener.java
* Version: 1.0
* Date: 2012-4-18
* Author: 孫偉
*/
package net.uni.ap.jbpm.handle;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.uni.ap.exp.BOException;
import net.uni.ap.jbpm.BaseJbpmVar;
import net.uni.ap.jbpm.JbpmConstants;
import net.uni.ap.jbpm.bo.IJbpmBO;
import net.uni.in1.bo.IUserBO;
import net.uni.in1.model.User;

import org.jbpm.api.JbpmException;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;

/**
* 處理內容:jbpm監聽實現類
* @version: 1.0
* @see:net.uni.ap.jbpm.handle.JbpmEventListener.java
* @date:2012-4-18
* @author:孫偉
*/
public class JbpmEventListener implements EventListener {

private static final long serialVersionUID = -6222489823315634987L;

protected IJbpmBO jbpmService;//注入的jbpmservice
protected String sql;//需要執行的業務sql

//下面這幾個屬性是為了判斷下一個節點是否有審批人用的
//第一個節點只要在開始節點的啟動事件中監聽就行了,後面的節點就在前一個節點監聽
protected String roleIds;//角色ID
protected String userIds;//使用者ID串
protected String roleTypes;//角色型別
protected String orgIds;//機構ID
protected String orgTypes;//機構型別
protected IUserBO userService;//使用者介面

protected String appRole;//角色ID對應的審批表主鍵
protected String appOrg;//機構ID對應的審批表主鍵
protected String corpId;//發起審批的人所在的公司

protected String entityId;//業務資料的Id或者Id串

@Override
public void notify(EventListenerExecution execution) throws JbpmException {
try{
//先獲取發起審批的時候傳遞的引數
BaseJbpmVar jbpm= (BaseJbpmVar)execution.getVariable("var");
Map jbpmArgs = jbpm.getJbpmArgs();//argsKey如果只有一個引數,預設不用穿
corpId = jbpm.getCorpId();//當前審批所在的公司
entityId = jbpm.getApplyEntityId();//審批的業務資料ID
List userList =null;
//如果appRole和appOrg都不為空,則根據審批角色和審批機構去查詢使用者
if(appRole!=null&&appOrg!=null){
userList = this.userService.getJbpmUserList(appRole, appOrg, corpId);
}else if(appRole!=null&&orgIds!=null){
//角色是固定的,但是機構是動態的,例如提交本部門經理審批
userList = this.userService.getJbpmUserListAppRoleOrgIds(appRole, orgIds, corpId);
}else{
//這個是為了相容以前的程式,最新的寫法都不需要這個
if(roleIds!=null&&orgIds!=null){
userList= this.userService.getJbpmUserList(roleIds, roleTypes, orgIds, orgTypes);
}
}

//檢視有沒有直接指定使用者ID的
if(userIds!=null){
String[] userId = userIds.split(",");
for(String id:userId){
User user = new User();
user.setId(Long.parseLong(id));
userList.add(user);
}
}
//如果下一個節點沒有使用者,則提示失敗
if(userList!=null&&userList.size()<1){
throw new JbpmException("您好,下一個節點沒有對應的審批人,請聯絡管理員");
}

//如果下一個節點沒有使用者,則提示失敗
if(userList!=null&&userList.size()>0){
//下一個節點的使用者存放入流程變數中,用來在頁面上提示使用者下一個節點誰來審批
StringBuffer userStr = new StringBuffer("下一個節點的審批人是:");
for(User user : userList){
userStr.append(user.getUserName()+"/"+user.getLoginName()+";");
}
execution.setVariable(JbpmConstants.NEXT_NODE_USER, userStr.toString());

}

//更新業務資料的審批狀態
//sql裡可能帶問號,需要把引數拼進去
if(sql!=null&&!sql.trim().equals("")){
//包含問號的說明sql只傳遞資料主鍵的引數
if(sql.indexOf("?")>-1){
sql = sql.replaceAll("[?]", jbpm.getApplyEntityId());
}else if(sql.indexOf("#")>-1){
//如果sql裡包含#,說明傳遞的引數有多個,存放在引數map中

//正規表示式替換--尾部為空格;
String regex = "#w+()";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(sql);
StringBuffer sbf = new StringBuffer();
while(matcher.find()){
//System.out.println(sql.substring(matcher.start()+1, matcher.end()-1));
//System.out.println(sql.substring(matcher.start()+1, matcher.end()-1).trim());
String argsValue = jbpmArgs.get(sql.substring(matcher.start()+1, matcher.end()-1).trim());
matcher.appendReplacement(sbf,argsValue);
sbf.append(" ");
}
matcher.appendTail(sbf);

sql = sbf.toString();
//正規表示式替換--尾部是字串結尾
regex = "#w+($)";
pattern = Pattern.compile(regex);
matcher = pattern.matcher(sql);
sbf = new StringBuffer();
while(matcher.find()){
//System.out.println(sql.substring(matcher.start()+1, matcher.end()));
//System.out.println(sql.substring(matcher.start()+1, matcher.end()).trim());
String argsValue = jbpmArgs.get(sql.substring(matcher.start()+1, matcher.end()).trim());
matcher.appendReplacement(sbf,argsValue);
sbf.append(" ");
}
matcher.appendTail(sbf);
sql=sbf.toString();

//正規表示式替換--尾部是括號的
regex = "#w+())";
pattern = Pattern.compile(regex);
matcher = pattern.matcher(sql);
sbf = new StringBuffer();
while(matcher.find()){
//System.out.println(sql.substring(matcher.start()+1, matcher.end()));
//System.out.println(sql.substring(matcher.start()+1, matcher.end()).trim());
String argsValue = jbpmArgs.get(sql.substring(matcher.start()+1, matcher.end()-1).trim());
matcher.appendReplacement(sbf,argsValue);
sbf.append(" ) ");
}
matcher.appendTail(sbf);
sql=sbf.toString();

//正規表示式替換--尾部是;的
regex = "#w+(;)";
pattern = Pattern.compile(regex);
matcher = pattern.matcher(sql);
sbf = new StringBuffer();
while(matcher.find()){
//System.out.println(sql.substring(matcher.start()+1, matcher.end()));
//System.out.println(sql.substring(matcher.start()+1, matcher.end()).trim());
String argsValue = jbpmArgs.get(sql.substring(matcher.start()+1, matcher.end()-1).trim());
matcher.appendReplacement(sbf,argsValue);
sbf.append(" ; ");
}
matcher.appendTail(sbf);
sql=sbf.toString();

//正規表示式替換--尾部是,的
regex = "#w+(,)";
pattern = Pattern.compile(regex);
matcher = pattern.matcher(sql);
sbf = new StringBuffer();
while(matcher.find()){
//System.out.println(sql.substring(matcher.start()+1, matcher.end()));
//System.out.println(sql.substring(matcher.start()+1, matcher.end()).trim());
String argsValue = jbpmArgs.get(sql.substring(matcher.start()+1, matcher.end()-1).trim());
matcher.appendReplacement(sbf,argsValue);
sbf.append(" , ");
}
matcher.appendTail(sbf);
sql=sbf.toString();
}

//處理多條sql的情況,如果有多條sql,用分號分隔
String[] sqlStr = sql.split(";");
for(String mySql:sqlStr){
if(mySql!=null&&!mySql.trim().equals("")){
jbpmService.update(mySql);
}
}
}



/*//判斷下一個節點是否有人審批,如果沒有人審批提示它下一個節點沒有人審批,請聯絡管理員
List userList =null;
if((appRole==null||appRole.equals(""))&&(appOrg==null||appOrg.equals(""))){
userList= this.userService.getJbpmUserList(roleIds, roleTypes, orgIds, orgTypes);
}else if(appRole!=null&&!appRole.trim().equals("")&&orgIds!=null&&!orgIds.trim().equals("")){
//角色使用審批角色ID,機構透過流程引數傳遞過來的
userList = this.userService.getJbpmUserListAppRoleOrgIds(appRole, orgIds, corpId);
}else{
userList = this.userService.getJbpmUserList(appRole, appOrg, corpId);
}

if(userIds!=null){
String[] userId = userIds.split(",");
for(String id:userId){
User user = new User();
user.setId(Long.parseLong(id));
userList.add(user);
}
}
if(userList.size()<1){
throw new JbpmException("您好,下一個節點沒有對應的審批人,請聯絡管理員");
}
*/


}catch(BOException e){
throw new JbpmException(e.getMessage());
}catch(Exception e){
throw new JbpmException(e.getMessage());
}
}

public String getSql() {
return sql;
}
public void setSql(String sql) {
this.sql = sql;
}
public IJbpmBO getJbpmService() {
return jbpmService;
}
public void setJbpmService(IJbpmBO jbpmService) {
this.jbpmService = jbpmService;
}
public String getRoleIds() {
return roleIds;
}
public void setRoleIds(String roleIds) {
this.roleIds = roleIds;
}
public String getRoleTypes() {
return roleTypes;
}
public void setRoleTypes(String roleTypes) {
this.roleTypes = roleTypes;
}
public String getOrgIds() {
return orgIds;
}
public void setOrgIds(String orgIds) {
this.orgIds = orgIds;
}
public String getOrgTypes() {
return orgTypes;
}
public void setOrgTypes(String orgTypes) {
this.orgTypes = orgTypes;
}
public IUserBO getUserService() {
return userService;
}
public void setUserService(IUserBO userService) {
this.userService = userService;
}
public String getUserIds() {
return userIds;
}
public void setUserIds(String userIds) {
this.userIds = userIds;
}
public String getAppRole() {
return appRole;
}
public void setAppRole(String appRole) {
this.appRole = appRole;
}
public String getAppOrg() {
return appOrg;
}
public void setAppOrg(String appOrg) {
this.appOrg = appOrg;
}
public String getCorpId() {
return corpId;
}
public void setCorpId(String corpId) {
this.corpId = corpId;
}
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25261409/viewspace-1059011/,如需轉載,請註明出處,否則將追究法律責任。

相關文章