Java Activiti (2)--Basic depth process Add, delete, complete tasks, etc.

Source: Internet
Author: User

One, process implementation

public class Activitiactiontest {private Processengine processengine = Processengines.getdefaultprocessengine (); 1.1. CREATE table public static void Createtabelauto () {//Create object Processengineconfiguration conf = Processeng
        Ineconfiguration.createstandaloneprocessengineconfiguration ();
        String Driver = Propertiesutil.getvalue ("Jdbc_driver", "conf/jdbc.properties");
        String url = propertiesutil.getvalue ("Jdbc_url", "conf/jdbc.properties");
        String username = propertiesutil.getvalue ("Jdbc_username", "conf/jdbc.properties");
        String Password = propertiesutil.getvalue ("Jdbc_password", "conf/jdbc.properties");
        Set up Database Conf.setjdbcdriver (driver);
        Conf.setjdbcurl (URL);
        Conf.setjdbcusername (username);
        Conf.setjdbcpassword (password);

        Auto-Build Table Conf.setdatabaseschemaupdate ("true");
    Create engine Processengine Processengine = Conf.buildprocessengine (); //1.2, CREATE TABLE, ProcessenginecoNfiguration the database entity bean public static void Createtabelbyxml () {//Create object String resource = "Activiti-conte
        Xt2.xml ";
        String beanname = "Processengineconfiguration"; Processengineconfiguration conf = Processengineconfiguration.createprocessengineconfigurationfromresource (
        Resource, beanname);
    Create engine Processengine Processengine = Conf.buildprocessengine (); }//2, deployment process definition @Test public void Deployleave () throws Exception {Deploymentbuilder builder = Processe
        Ngine.getrepositoryservice (). Createdeployment ();
Mode 1//Builder.addclasspathresource ("FLOW/LEAVE.BPMN");
Builder.addclasspathresource ("Flow/leave.png");
        Deployment deploy = Builder.deploy ();
        Builder.addclasspathresource ("Flow/baoxiao.bmp");
        Builder.addclasspathresource ("Flow/baoxiao.png");
        Builder.name ("Reimbursement process Deployment");
        Deployment deploy = Builder.deploy (); Mode 2//Zipinputstream ZIPINPUTSTReam = new Zipinputstream (This.getclass (). getClassLoader (). getResourceAsStream ("Flow/leave.zip"));
Builder.addzipinputstream (Zipinputstream);
Builder.name ("Leave process Deployment");
        Deployment deploy = Builder.deploy ();
    System.out.println ("deploy.getid () = =" + Deploy.getid ());  //3.1, query deployment table @Test public void querydeployments () throws Exception {Deploymentquery deploymentquery
        = Processengine.getrepositoryservice (). Createdeploymentquery ();
        list<deployment> list = Deploymentquery.list ();
        for (deployment d:list) {System.out.println (D.getid () + ":" + d.getname ());  //3.2, delete process deployment ID @Test public void deletedeployment () throws Exception {String Deploymentid =
        "1";
        Processengine.getrepositoryservice (). Deletedeployment (Deploymentid);
    cascading deletes Processengine.getrepositoryservice (). Deletedeployment (Deploymentid, true); //3.3, querying for deployment file information and loss based on the ID of the process deploymentThe inflow @Test public void Getdeployfile () throws Exception {String Deploymentid = "10001";
        list<string> names = Processengine.getrepositoryservice (). Getdeploymentresourcenames (DeploymentId);
            for (String f:names) {System.out.println ("f===" + f);
            InputStream InputStream = Processengine.getrepositoryservice (). getResourceAsStream (DeploymentId, F);
Save the file to local//FileOutputStream FOS = new FileOutputStream (new file ("d:\\" + f));
int len =-1;
Byte[] B = new byte[1024];            while (len = Inputstream.read ())!=-1) {//Fos.write (b, 0, Len);//}//
Fos.flush ();
            Fos.close ();
            Fileutils.copyinputstreamtofile (InputStream, New File ("d:\\" + f));
        Inputstream.close (); //3.4, query the deployment file picture based on the process deployment ID @Test public void getdeploypng () throws Exception {String Processde Finitionid = "Leaveflow:4:10004";
        InputStream InputStream = Processengine.getrepositoryservice (). Getprocessdiagram (Processdefinitionid);
        Fileutils.copyinputstreamtofile (InputStream, New File ("D:\\leave.png"));
    Inputstream.close (); //4.1, get the process-defined ID @Test public void queryprocessdefinition () throws Exception {Processdefinitionquer
        Y query = Processengine.getrepositoryservice (). Createprocessdefinitionquery ();
        Add Filter Condition Query.processdefinitionkey ("Baoxiaoflow");
        Add sort query.orderbyprocessdefinitionversion (). ASC ();
        Paging query.listpage (0, 10);
        list<processdefinition> processdefinitions = Query.list ();
        for (ProcessDefinition p:processdefinitions) {System.out.println ("p.getid () = =" + P.getid ()); //4.1.1, querying the latest process definition @Test public void querylatestprocessdefinition () throws Exception {PROCESSD Efinitionquery query = Processengine.getrepositoryService (). Createprocessdefinitionquery ();
        Add sort query.orderbyprocessdefinitionversion (). ASC ();
        list<processdefinition> list = Query.list ();
        map<string, processdefinition> map = new hashmap<string, processdefinition> (); for (ProcessDefinition pd:list) {map.put (Pd.getkey (), PD);//key different leaveflow, baoxiaoflow} S
        YSTEM.OUT.PRINTLN (map);
        arraylist<processdefinition> lastlist = new arraylist<processdefinition> (Map.values ());
        for (ProcessDefinition pd:lastlist) {System.out.println (Pd.getname () + ":" + pd.getversion ()); }//4.2, deletes the ID of the process deployment because the deployment and definition are principal-associate relationships, deleting the master table deletes the process definition @Test public void deleteprocessdefinition () throws Excep
        tion {String Deploymentid = "1";
        Processengine.getrepositoryservice (). Deletedeployment (Deploymentid);
  cascading deletes Processengine.getrepositoryservice (). Deletedeployment (Deploymentid, true);  //4.3, START process instance @Test public void Startprocessinstancebyid () throws Exception {String proc based on the ID of the process definition
        Essdefinitionid = "leaveflow:4:10004"; ProcessInstance processinstance = Processengine.getruntimeservice (). Startprocessinstancebyid (ProcessDefinitionId)
        ;
    System.out.println ("processinstance.getid () = =" + Processinstance.getid ());
        //4.4, starting the process instance based on the key defined by the process, the highest @Test public void Startprocessinstancebykey () throws Exception {
        String Processdefinitionkey = "Leaveflow"; ProcessInstance processinstance = Processengine.getruntimeservice (). Startprocessinstancebykey (
        Processdefinitionkey);
        System.out.println ("processinstance.getid () = =" + Processinstance.getid ());
    System.out.println ("processinstance.getprocessdefinitionid () = =" + Processinstance.getprocessdefinitionid ()); @Test public void Querys () throws Exception {list<processdefinition> processdefinitions = Proces SenginE.getrepositoryservice (). Createprocessdefinitionquery (). List ();
        list<deployment> deployments = Processengine.getrepositoryservice (). Createdeploymentquery (). List (); list<processinstance> processinstances = Processengine.getruntimeservice (). Createprocessinstancequery ().
        List ();

    list<task> tasks = Processengine.gettaskservice (). Createtaskquery (). List (); //5.1, query process instance, corresponding excution table @Test public void Queryprocessinstance () throws Exception {Processinstanc
        Equery query = Processengine.getruntimeservice (). Createprocessinstancequery ();
        Query.processdefinitionid ("Leaveflow");
        Query.orderbyprocessdefinitionkey (). DESC ();
        Query.listpage (0,2);
        list<processinstance> processinstances = Query.list (); for (ProcessInstance pi:processinstances) {//getactivityid activity ID System.out.println (Pi.getid () +
        ":" + Pi.getactivityid ());
   //5.2 to end the process instance while closing the task @Test public void Endprocessinstance () throws Exception {String Processinstanceid = "17504";
    Processengine.getruntimeservice (). Deleteprocessinstance (Processinstanceid, "OK"); 
        //6.1, according to the process instance, query the task list to show that a process definition corresponds to multiple process instances, one process instance corresponds to multiple task lists @Test public void Getprocesstask () throws Exception {
        String Processinstanceid = "5001";
        1//string assignee = "Tom";
        2 String assignee = "Jack";
        3//string Assignee = "Smith";
        Task List Taskquery taskquery = Processengine.gettaskservice (). Createtaskquery ();
        Query Tom's Task List Taskquery.taskassignee (assignee);
        Taskquery.orderbytaskcreatetime (). DESC ();
        list<task> tasks = Taskquery.list ();
        for (Task task:tasks) {System.out.println (Task.getid () + ":" + task.getname ()); }//6.2, processing tasks, processing completed, the third step will not find the task of Tom, ran to the next Jack's task, 5 and 6 are mutually repeated @Test public void Dealprocesstask () throws Exception {//string taskId = "12506";//Submit String taskId = "22502";
    The project manager approves Processengine.gettaskservice (). complete (taskId); //6.3, step down, skip current approval @Test public void Nextprocesstask () throws Exception {//string taskId = "12506 ";  Commit String Executionid = "20002";
    The project manager approves//processengine.getruntimeservice (). signal (Executionid); }
}

Two, basic connection database file Activiti-context.xml

<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:context= "http://www.springframework.org/ Schema/context "xmlns:tx=" Http://www.springframework.org/schema/tx "xmlns:xsi=" http://www.w3.org/2001/ Xmlschema-instance "xsi:schemalocation=" Http://www.springframework.org/schema/beans http://ww 
                        W.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context-2.5.xsd http ://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "&
    Gt <!--Configure the process engine configuration object--> <bean id= "Processengineconfiguration class=" Org.activiti.engine.impl.cfg.Standal Oneprocessengineconfiguration "> <property name=" jdbcdriver "value=" Com.mysql.jdbc.Driver "/> < Property Name= "Jdbcurl" value= "Jdbc:mysql:activiti_1110 "/> <property name=" jdbcusername "value=" "root"/> <property name= "JdbcPasswor" D "value=" root "/> <property name=" Databaseschemaupdate "value=" true "/> </bean> <!--with Place a process engine factory bean to create the process engine object--> <bean id= "Processengine" class= "Org.activiti.spring.ProcessEngineFactoryBean" > <!--inject the process engine configuration object through the Set method--> <property name= "Processengineconfiguration ref=" Processenginecon Figuration "/> </bean> </beans>

Three, reimbursement process XML

<?xml version= "1.0" encoding= "UTF-8" standalone= "yes"?> <definitions "xmlns=" 20100524/model "xmlns:activiti=" HTTP://ACTIVITI.ORG/BPMN "xmlns:bpmndi=" Http://www.omg.org/spec/BPMN/20100524/DI "Xmlns:omgdc=" Http://www.omg.org/spec/DD/20100524/DC "xmlns:omgdi=" Http://www.omg.org/spec/DD/20100524/DI "xmlns : tns= "Http://www.activiti.org/test" xmlns:xsd= "Http://www.w3.org/2001/XMLSchema" xmlns:xsi= "http://www.w3.org/" 2001/xmlschema-instance "expressionlanguage=" Http://www.w3.org/1999/XPath "id=" m1505312057485 "Name=" " Targetnamespace= "Http://www.activiti.org/test" typelanguage= "Http://www.w3.org/2001/XMLSchema" > <process ID = "Baoxiaoflow" isclosed= "false" isexecutable= "true" Name= "Baoxiaoflow" processtype= "None" > <startevent id= " Startid "Name=" "Start"/> <endevent id= "Endid" name= "End"/> <usertask "Tom" activiti:assignee= Sync= "false" activiti:exclusive= "true" id= "Processsubmitid" name= "submit claim"/> <usertasK activiti:assignee= "Jack" activiti:exclusive= "true" id= "Processsupervisorid" Name= "manager approves"/> <sequenceflow id= " _6 "sourceref=" Startid "targetref=" Processsubmitid "/> <sequenceflow id=" _7 "sourceref=" ProcessSubmitId " targetref= "Processsupervisorid"/> <usertask activiti:assignee= "Smith" activiti:exclusive= "true" id= " Processmanagerid "Name=" general manager approval "/> <sequenceflow id=" _10 "sourceref=" Processsupervisorid "targetRef=" Processmanagerid "/> <sequenceflow id=" _11 sourceref= "Processmanagerid" targetref= "EndId"/> <sequenceF Low id= "Startid-processsubmitid" sourceref= "Startid" targetref= "Processsubmitid"/> </process> <bpmndi: Bpmndiagram documentation= "background= #FFFFFF; count=1;horizontalcount=1;orientation=0;width=842.4;height=1195.2 ; imageablewidth=832.4;imageableheight=1185.2;imageablex=5.0;imageabley=5.0 "id=" Diagram-_1 "name=" New Diagram " > <bpmndi:bpmnplane bpmnelement= "Baoxiaoflow" > <bpmndi:bpmnshape Bpmnelement= "Startid" id= "Shape-startid" > <omgdc:bounds height= "32.0" width= "32.0" x= "390.0" y= "115.0"/> <bpmndi:BPMNLabel> <omgdc:bounds height= "32.0" width= "32.0" x= "0.0" y= "0.0"/> </
        bpmndi:bpmnlabel> </bpmndi:BPMNShape> <bpmndi:bpmnshape bpmnelement= "Endid" id= "Shape-endid" > <omgdc:bounds height= "32.0" width= "32.0" x= "400.0" y= "495.0"/> <bpmndi:BPMNLabel> ; Omgdc:bounds height= "32.0" width= "32.0" x= "0.0" y= "0.0"/> </bpmndi:BPMNLabel> </bpmndi:bpmnshap e> <bpmndi:bpmnshape bpmnelement= "Processsubmitid" id= "Shape-processsubmitid" > <omgdc:Bounds Hei  ght= "55.0" width= "85.0" x= "370.0" y= "215.0"/> <bpmndi:BPMNLabel> <omgdc:bounds height= "55.0" Width= "85.0&
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.