package com.tenwa.flow.util; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import jbo.sys.FLOW_CATALOG; import jbo.sys.FLOW_MODEL; import jbo.sys.FLOW_TASK; import com.amarsoft.are.jbo.BizObject; import com.amarsoft.are.jbo.BizObjectManager; import com.amarsoft.are.jbo.BizObjectQuery; import com.amarsoft.are.jbo.JBOFactory; import com.amarsoft.are.jbo.JBOTransaction; import com.amarsoft.are.util.json.JSONDecoder; import com.amarsoft.are.util.json.JSONEncoder; import com.amarsoft.are.util.json.JSONObject; public class FlowViewGraph { private String flowNo; public String getFlowNo() { return flowNo; } public void setFlowNo(String flowNo) { this.flowNo = flowNo; } /** * 获取ade工具开发的旧流程图 * @param tx * @return * @throws Exception */ public String getModel(JBOTransaction tx,String flowNo) throws Exception{ List fms = JBOFactory.getBizObjectManager(FLOW_MODEL.CLASS_NAME).createQuery("flowno=:flowno ") .setParameter("flowno", flowNo).getResultList(false); BizObjectQuery beginEndPointQuery = JBOFactory.getBizObjectManager(FLOW_MODEL.CLASS_NAME).createQuery( "select O.phaseno from O where flowno=:flowno and postscript is not null and postscript <> '' and "+ "(postscript like :phaseno1 or postScript like :phaseno2 or postScript like :phaseno3 or postscript like :phaseno4) ") .setParameter("flowno", flowNo); Map stateMap = new HashMap(); Map rectPhaseno = new HashMap(); Map pathMap = new HashMap(); Map propsMap = new HashMap(); Map modelMap = new HashMap(); if(fms.size()>0){ for(int i=0;i nodeName = new HashMap(); Map attrValue = new HashMap(); Map textValue = new HashMap(); Map keyValue = new HashMap(); Map positionValue = new HashMap(); Map props = new HashMap(); Map rect = new HashMap(); BizObject fm = fms.get(i); if(fm.getAttribute("phaseno").getString().equals("8000")){ rect.put("type", "reject"); }else if(fm.getAttribute("phaseno").getString().equals("0010")){ rect.put("type", "task"); }else{ rect.put("type", fm.getAttribute("nodetype").getString().toLowerCase()); } nodeName.put("text",fm.getAttribute("phasename").getString()); rect.put("text", nodeName); attrValue.put("x", fm.getAttribute("xcoordinate").getInt()); attrValue.put("y", fm.getAttribute("ycoordinate").getInt()); if(fm.getAttribute("nodetype").getString().equalsIgnoreCase("task")||fm.getAttribute("nodetype").getString().equalsIgnoreCase("start")){ attrValue.put("width", 100); attrValue.put("height",50); }else{ attrValue.put("width", 50); attrValue.put("height", 50); } rect.put("attr", attrValue); textValue.put("value",fm.getAttribute("phasename").getString()); keyValue.put("value", String.valueOf(i)); positionValue.put("value", fm.getAttribute("phaseno").getString()); props.put("text", textValue); props.put("key", keyValue); props.put("position", positionValue); rect.put("props", props); stateMap.put("rect"+String.valueOf(i+1), rect); rectPhaseno.put(fm.getAttribute("phaseno").getString(), "rect"+String.valueOf(i+1)); } } modelMap.put("states", stateMap); if(fms.size()>0){ int num = fms.size()+1; for(int i=0;i phases = beginEndPointQuery.setParameter("phaseno1", "%'"+phaseNo+"'%").setParameter("phaseno2", "%\""+phaseNo+"\"%") .setParameter("phaseno3", "%;"+phaseNo+"%").setParameter("phaseno4", "%"+phaseNo+";%").getResultList(false); if(phases.size()>0){ for(BizObject phase : phases){ String prePhaseno = phase.getAttribute("phaseno").getString(); Map path = new HashMap();// path.put("from", rectPhaseno.get(prePhaseno)); path.put("to", rectPhaseno.get(phaseNo)); path.put("dots", new ArrayList()); Map toPhasename = new HashMap(); toPhasename.put("text", "to"+fm.getAttribute("phasename").getString()); path.put("text", toPhasename); Map textPos = new HashMap();// textPos.put("x", 0); textPos.put("y", -10); path.put("textPos", textPos); Map text =new HashMap(); text.put("value", ""); Map propstext =new HashMap(); propstext.put("text", text); path.put("props",propstext); pathMap.put("path"+String.valueOf(num), path); num++; } } } } modelMap.put("paths", pathMap); Map type = new HashMap(); Map displayName = new HashMap(); Map code = new HashMap(); Map name = new HashMap(); Map version = new HashMap(); Map jpdlVersion = new HashMap(); Map desc = new HashMap(); Map position = new HashMap(); Map props = new HashMap(); type.put("value", "null"); displayName.put("value", "null"); code.put("value", "null"); name.put("value", "null"); version.put("value", "null"); jpdlVersion.put("value", "4.4"); desc.put("value", "null"); position.put("value", "null"); props.put("type",type); props.put("displayName",displayName); props.put("code",code); props.put("name",name); props.put("type",version); props.put("jpdlVersion",jpdlVersion); props.put("desc",desc); props.put("position",position); propsMap.put("props", props); modelMap.put("props", propsMap); String graphJsonStr = JSONEncoder.encode(modelMap); graphJsonStr = graphJsonStr.replaceAll("\"","\'"); return graphJsonStr; } /** * 获取jbpm格式的json串 * @param tx,flowNo * @return * @throws Exception */ public String getGraphJson(JBOTransaction tx,String flowNo) throws Exception{ BizObject fc = JBOFactory.getBizObjectManager(FLOW_CATALOG.CLASS_NAME).createQuery("flowno=:flowno ").setParameter("flowno",flowNo).getSingleResult(false); String graphJsonData=""; if(fc != null){ graphJsonData = fc.getAttribute("graphjsondata").getString(); if(graphJsonData ==null || graphJsonData.equals("")){ List fms = JBOFactory.getBizObjectManager(FLOW_MODEL.CLASS_NAME).createQuery("flowno=:flowno ") .setParameter("flowno", flowNo).getResultList(false); if(fms.size()>0){ graphJsonData = getModel(tx,flowNo); } } } return graphJsonData; } /** * 发布流程 * @param tx * @return * @throws Exception */ public String publishFlow(JBOTransaction tx) throws Exception{ String result = "fail"; BizObjectManager fcManager = JBOFactory.getBizObjectManager(FLOW_CATALOG.CLASS_NAME); tx.join(fcManager); BizObject fc = fcManager.createQuery("flowno=:flowno ").setParameter("flowno",flowNo).getSingleResult(false); BizObjectManager fmManager = JBOFactory.getBizObjectManager(FLOW_MODEL.CLASS_NAME); tx.join(fmManager); List fms = fmManager.createQuery("flowno=:flowno ").setParameter("flowno", flowNo).getResultList(false); if(fms.size()==0){ String graphJson = fc.getAttribute("graphjsondata").getString(); JSONObject jsonObj = JSONDecoder.decode(graphJson); JSONObject states = (JSONObject)jsonObj.getValue("states"); JSONObject paths = (JSONObject)jsonObj.getValue("paths"); Map> rect_phaseno_type = new HashMap>(); Map> rect_phaseno_specialtype = new HashMap>(); for(int i=0; i oldFlowModels = fmManager.createQuery("flowno=:flowno ").setParameter("flowno",flowNo).getResultList(false); Map phasenos = new HashMap(); for(BizObject oldFlowModel:oldFlowModels){ phasenos.put(oldFlowModel.getAttribute("phaseno").getString(), oldFlowModel.getAttribute("phaseno").getString()); } String graphJson = fc.getAttribute("graphjsondata").getString(); JSONObject jsonObj = JSONDecoder.decode(graphJson); JSONObject states = (JSONObject)jsonObj.getValue("states"); JSONObject paths = (JSONObject)jsonObj.getValue("paths"); Map> rect_phaseno_type = new HashMap>(); Map> rect_phaseno_specialtype = new HashMap>(); for(int i=0; i it = phasenos.values().iterator(); while (it.hasNext()) { String exphaseno = it.next(); fmManager.createQuery("delete from o where phaseno=:phaseno and flowno=:flowno ").setParameter("phaseno",exphaseno).setParameter("flowno", flowNo).executeUpdate(); } } if(!rect_phaseno_specialtype.isEmpty()){ for(int i=0;i> rect_phaseno_type,Map> rect_phaseno_specialtype, String phaseNo)throws Exception{ BizObjectManager fcManager = JBOFactory.getBizObjectManager(FLOW_CATALOG.CLASS_NAME); tx.join(fcManager); BizObjectManager fmManager = JBOFactory.getBizObjectManager(FLOW_MODEL.CLASS_NAME); tx.join(fmManager); JSONObject text = (JSONObject)rect.getValue("text"); String type = (String)rect.getValue("type"); String phaseName = (String)text.getValue("text"); String phaseType = ""; String initScript = ""; String nodeType =""; if(phaseNo==null) phaseNo=""; if(!type.equalsIgnoreCase("start")){//开始符号不涉及逻辑,忽略 if(type.equalsIgnoreCase("exclusive")||type.equalsIgnoreCase("join")||type.equalsIgnoreCase("fork")){//分支,并行作为特殊符号,不需加在flow_model Map rect_type = new HashMap(); rect_type.put("nodetype", type); rect_type.put("phaseno", phaseNo); rect_phaseno_specialtype.put(key,rect_type); }else{ Map rect_type = new HashMap(); rect_type.put("nodetype", type); rect_type.put("phaseno", phaseNo); rect_phaseno_type.put(key,rect_type); if(phaseNo.equals("0010")){ phaseType = "1010"; initScript = "{#UserID}"; nodeType = "TASK"; }else if(phaseNo.equals("1000")){ phaseType = "1040"; initScript = "{\"system\"}"; nodeType = "END"; }else if(phaseNo.equals("8000")){ phaseType = "1050"; initScript = "{\"system\"}"; nodeType = "END"; }else{ phaseType = "1020"; initScript = "toStringArray(\"#PhaseAction\",\",\",\" \",1)"; nodeType = "TASK"; } JSONObject attr = (JSONObject)rect.getValue("attr"); Integer xcoordinate =(Integer)attr.getValue(0); Integer ycoordinate = (Integer)attr.getValue(1); Integer width = (Integer)attr.getValue(2); Integer height = (Integer)attr.getValue(3); BizObjectQuery bq = fmManager.createQuery("UPDATE O SET phaseno=:phaseno, phasetype=:phasetype, phasename=:phasename, initscript=:initscript, xcoordinate=:xcoordinate, ycoordinate=:ycoordinate, width=:width, height=:height, nodetype=:nodetype WHERE phaseno=:phaseno and flowno=:flowno"); bq.setParameter("phaseno", phaseNo).setParameter("phasetype", phaseType).setParameter("phasename", phaseName).setParameter("initscript", initScript) .setParameter("xcoordinate", xcoordinate).setParameter("ycoordinate", ycoordinate).setParameter("width", width).setParameter("height", height).setParameter("nodetype", nodeType).setParameter("flowno",flowNo); bq.executeUpdate(); } } } /** * 新建flow_model * @param tx * @param rect * @param phaseNo * @throws Exception */ public void newFlowModel(JBOTransaction tx,JSONObject rect,String key, Map> rect_phaseno_type,Map> rect_phaseno_specialtype)throws Exception{ BizObjectManager fcManager = JBOFactory.getBizObjectManager(FLOW_CATALOG.CLASS_NAME); tx.join(fcManager); BizObjectManager fmManager = JBOFactory.getBizObjectManager(FLOW_MODEL.CLASS_NAME); tx.join(fmManager); BizObject newFm = fmManager.newObject(); String type = (String)rect.getValue("type"); JSONObject text = (JSONObject)rect.getValue("text"); String phaseName = (String)text.getValue("text"); String phaseType = ""; String initScript = ""; String nodeType =""; JSONObject props = (JSONObject)rect.getValue("props"); JSONObject position = (JSONObject)props.getValue("position"); String phaseNo = (String)position.getValue("value"); if(phaseNo==null) phaseNo=""; if(!type.equalsIgnoreCase("start")){//开始符号不涉及逻辑,忽略 if(type.equalsIgnoreCase("exclusive")||type.equalsIgnoreCase("join")||type.equalsIgnoreCase("fork")){//分支,并行作为特殊符号,不需加在flow_model Map rect_type = new HashMap(); rect_type.put("nodetype", type); rect_type.put("phaseno", phaseNo); rect_phaseno_specialtype.put(key,rect_type); }else{ Map rect_type = new HashMap(); rect_type.put("nodetype", type); rect_type.put("phaseno", phaseNo); rect_phaseno_type.put(key,rect_type); if(phaseNo.equals("0010")){ phaseType = "1010"; initScript = "{#UserID}"; nodeType = "TASK"; }else if(phaseNo.equals("1000")){ phaseType = "1040"; initScript = "{\"system\"}"; nodeType = "END"; }else if(phaseNo.equals("8000")){ phaseType = "1050"; initScript = "{\"system\"}"; nodeType = "END"; }else{ phaseType = "1020"; initScript = "toStringArray(\"#PhaseAction\",\",\",\" \",1)"; nodeType = "TASK"; } JSONObject attr = (JSONObject)rect.getValue("attr"); Integer xcoordinate =(Integer)attr.getValue(0); Integer ycoordinate = (Integer)attr.getValue(1); Integer width = (Integer)attr.getValue(2); Integer height = (Integer)attr.getValue(3); int standardtime1 = 0; int standardtime2 = 0; if(nodeType.equals("END")){ newFm.setAttributeValue("flowno", flowNo).setAttributeValue("phaseno", phaseNo).setAttributeValue("phasetype", phaseType).setAttributeValue("phasename", phaseName) .setAttributeValue("initscript", initScript).setAttributeValue("standardtime1", standardtime1).setAttributeValue("standardtime2", standardtime2) .setAttributeValue("xcoordinate", xcoordinate).setAttributeValue("ycoordinate", ycoordinate).setAttributeValue("width", width).setAttributeValue("height", height).setAttributeValue("nodetype", nodeType).setAttributeValue("version", "1"); }else{ newFm.setAttributeValue("flowno", flowNo).setAttributeValue("phaseno", phaseNo).setAttributeValue("phasetype", phaseType).setAttributeValue("phasename", phaseName) .setAttributeValue("initscript", initScript).setAttributeValue("standardtime1", standardtime1).setAttributeValue("standardtime2", standardtime2).setAttributeValue("postscript", "#PhaseOpinion1") .setAttributeValue("xcoordinate", xcoordinate).setAttributeValue("ycoordinate", ycoordinate).setAttributeValue("width", width).setAttributeValue("height", height).setAttributeValue("nodetype", nodeType).setAttributeValue("version", "1"); } fmManager.saveObject(newFm); } } } /** * 获取指定阶段号的坐标 * @param tx * @param flowNo * @param flowunid * @return * @throws Exception */ public Map getCoordinate(JBOTransaction tx, String flowNo, String flowunid) throws Exception{ BizObjectManager ftManager = JBOFactory.getBizObjectManager(FLOW_TASK.CLASS_NAME); BizObjectManager fmManager = JBOFactory.getBizObjectManager(FLOW_MODEL.CLASS_NAME); List fts = ftManager.createQuery("objectno=:objectno and (endtime='' or endtime is null) ").setParameter("objectno",flowunid).getResultList(false); Map results = new HashMap(); if(fts.size()>0){ for(BizObject ft:fts){ String phaseNo = ft.getAttribute("phaseno").getString(); BizObject fm = fmManager.createQuery("flowno=:flowno and phaseno=:phaseno ").setParameter("flowno",flowNo).setParameter("phaseno",phaseNo).getSingleResult(false); String xcoordinate = fm.getAttribute("xcoordinate").getString(); String ycoordinate = fm.getAttribute("ycoordinate").getString(); if(xcoordinate.endsWith(".0")){ xcoordinate=xcoordinate.split("\\.")[0]; } if(ycoordinate.endsWith(".0")){ ycoordinate=ycoordinate.split("\\.")[0]; } results.put(phaseNo,xcoordinate+'@'+ycoordinate); } } return results; } /** * 保存流程图校验开始下一步,结束,否决的排序号为固定值 * @param graphJson * @return * @throws Exception */ public String checkFlowGraph(String graphJson) throws Exception{ String result = ""; JSONObject jsonObj = JSONDecoder.decode(graphJson); JSONObject states = (JSONObject)jsonObj.getValue("states"); JSONObject paths = (JSONObject)jsonObj.getValue("paths"); Map type_rect = new HashMap(); Map rect_phasno = new HashMap(); for(int i=0; i getCoordinates(JBOTransaction tx, String flowNo) throws Exception{ BizObjectManager fmManager = JBOFactory.getBizObjectManager(FLOW_MODEL.CLASS_NAME); List fms = fmManager.createQuery("flowno=:flowno and nodetype<>'END' ").setParameter("flowno",flowNo).getResultList(false); Map results = new HashMap(); if(fms.size()>0){ for(BizObject fm:fms){ String xcoordinate = fm.getAttribute("xcoordinate").getString(); String ycoordinate = fm.getAttribute("ycoordinate").getString(); if(xcoordinate.endsWith(".0")){ xcoordinate=xcoordinate.split("\\.")[0]; } if(ycoordinate.endsWith(".0")){ ycoordinate=ycoordinate.split("\\.")[0]; } results.put(fm.getAttribute("phaseno").getString(),xcoordinate+'@'+ycoordinate); } } return results; } }