com.jfaker.framework.flow.web.ProcessController.java Source code

Java tutorial

Introduction

Here is the source code for com.jfaker.framework.flow.web.ProcessController.java

Source

/*
 *  Copyright 2014-2015 snakerflow.com
 *  *
 *  * Licensed under the Apache License, Version 2.0 (the "License");
 *  * you may not use this file except in compliance with the License.
 *  * You may obtain a copy of the License at
 *  *
 *  *     http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  * See the License for the specific language governing permissions and
 *  * limitations under the License.
 *
 */
package com.jfaker.framework.flow.web;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.snaker.engine.access.Page;
import org.snaker.engine.access.QueryFilter;
import org.snaker.engine.entity.HistoryOrder;
import org.snaker.engine.entity.HistoryTask;
import org.snaker.engine.entity.Process;
import org.snaker.engine.entity.Task;
import org.snaker.engine.helper.AssertHelper;
import org.snaker.engine.helper.StreamHelper;
import org.snaker.engine.helper.StringHelper;
import org.snaker.engine.model.ProcessModel;

import com.jfaker.framework.flow.SnakerHelper;
import com.jfaker.framework.security.shiro.ShiroUtils;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.upload.UploadFile;

/**
 * ?
 * @author yuqs
 * @since 0.1
 */
public class ProcessController extends SnakerController {
    /**
     * ?
     */
    public void index() {
        QueryFilter filter = new QueryFilter();
        String displayName = getPara("displayName");
        if (StringHelper.isNotEmpty(displayName)) {
            filter.setDisplayName(displayName);
        }
        Page<Process> page = new Page<Process>();
        page.setPageNo(getParaToInt("pageNo", 1));
        engine.process().getProcesss(page, filter);
        setAttr("page", page);
        render("processDesigner.jsp");
    }

    /**
     * ??
     */
    public void init() {
        initFlows();
        redirect("/snaker/process");
    }

    /**
     * ??
     */
    public void deploy() {
        render("processDeploy.jsp");
    }

    /**
     * ?
     */
    public void add() {
        render("processAdd.jsp");
    }

    /**
     * ?[web?]
     */
    public void designer() {
        String processId = getPara(PARA_PROCESSID);
        if (StringUtils.isNotEmpty(processId)) {
            Process process = engine.process().getProcessById(processId);
            AssertHelper.notNull(process);
            ProcessModel processModel = process.getModel();
            if (processModel != null) {
                String json = SnakerHelper.getModelJson(processModel);
                setAttr("process", json);
            }
            setAttr("processId", processId);
        }
        render("processDesigner.jsp");
    }

    /**
     * ?
     */
    public void edit() {
        Process process = engine.process().getProcessById(getPara());
        setAttr("process", process);
        if (process.getDBContent() != null) {
            try {
                setAttr("content", StringHelper.textXML(new String(process.getDBContent(), "UTF-8")));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        render("processEdit.jsp");
    }

    /**
     * ??ID?
     */
    public void delete() {
        engine.process().undeploy(getPara());
        redirect("/snaker/process");
    }

    /**
     * ??
     */
    public void doFileDeploy() {
        InputStream input = null;
        try {
            String id = getPara("id");
            UploadFile file = getFile("snakerFile");
            input = new FileInputStream(file.getFile());
            if (StringUtils.isNotEmpty(id)) {
                engine.process().redeploy(id, input);
            } else {
                engine.process().deploy(input);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        redirect("/snaker/process");
    }

    /**
     * ??[web?]
     * @param model
     * @return
     */
    public void doStringDeploy() {
        InputStream input = null;
        try {
            String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
                    + SnakerHelper.convertXml(getPara("model"));
            System.out.println("model xml=\n" + xml);
            String id = getPara("id");
            input = StreamHelper.getStreamFromString(xml);
            if (StringUtils.isNotEmpty(id)) {
                engine.process().redeploy(id, input);
            } else {
                engine.process().deploy(input);
            }
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(false);
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        renderJson(true);
    }

    /**
     * ??
     */
    public void processStart() {
        startInstanceByName(getPara("processName"), null, ShiroUtils.getUsername(), null);
        redirect("/snaker/process");
    }

    /**
     * ??json?
     */
    public void json() {
        String processId = getPara(PARA_PROCESSID);
        String orderId = getPara(PARA_ORDERID);
        Process process = engine.process().getProcessById(processId);
        AssertHelper.notNull(process);
        ProcessModel model = process.getModel();
        Map<String, String> jsonMap = new HashMap<String, String>();
        if (model != null) {
            jsonMap.put("process", SnakerHelper.getModelJson(model));
        }
        if (StringUtils.isNotEmpty(orderId)) {
            List<Task> tasks = engine.query().getActiveTasks(new QueryFilter().setOrderId(orderId));
            List<HistoryTask> historyTasks = engine.query().getHistoryTasks(new QueryFilter().setOrderId(orderId));
            jsonMap.put("state", SnakerHelper.getStateJson(model, tasks, historyTasks));
        }
        //{"historyRects":{"rects":[{"paths":["TO 1"],"name":""},{"paths":["TO "],"name":"1"},{"paths":["TO 3","TO 4","TO 2"],"name":""}]}}
        renderJson(jsonMap);
    }

    /**
     * ??
     */
    public void display() {
        keepPara();
        String orderId = getPara(PARA_ORDERID);
        HistoryOrder order = engine.query().getHistOrder(orderId);
        setAttr("order", order);
        List<HistoryTask> tasks = engine.query().getHistoryTasks(new QueryFilter().setOrderId(orderId));
        setAttr("tasks", tasks);
        render("processView.jsp");
    }

    /**
     * ?
     */
    public void diagram() {
        keepPara();
        render("diagram.jsp");
    }

    public void validate() {
        render("processValidate.jsp");
    }

    /**
     * ???
     */
    public void getProcessTaskInfo() {
        Process process = engine.process().getProcessById(getPara(PARA_PROCESSID));
        //Process process = engine.process().getProcessById("23e85c95caf74a9eaef8cc9df9594448");
        String flowId = this.getPara("flowId");
        String flowName = this.getPara("flowName");
        setAttr("process", process);
        if (process.getDBContent() != null) {
            try {
                String content = new String(process.getDBContent(), "UTF-8");
                System.out.println(new String(process.getDBContent(), "UTF-8"));
                Document document = DocumentHelper.parseText(content);
                Element rootEle = document.getRootElement();
                List taskNodes = rootEle.elements("task");
                List<Object> list = new ArrayList<Object>();
                Map map = null;
                for (Iterator it = taskNodes.iterator(); it.hasNext();) {
                    map = new HashMap();
                    Element elm = (Element) it.next();
                    map.put("processId", process.getId());
                    map.put("processName", process.getName());
                    map.put("SUB_FLOW_ID", flowId);
                    map.put("FLOW_NAME", flowName);
                    map.put("tacheCode", elm.attribute("name").getText());
                    map.put("tachName", elm.attribute("displayName").getText());
                    list.add(map);

                }
                setAttr("tacheList", list);
                renderJson(list);
                //renderJson(JSONArray.fromObject(list).toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void insertFlowTache(String SUB_FLOW_ID, String processId, String tacheCode) {

        Db.update("INSERT INTO OA_FLOW_TACHE(FLOW_ID,PROCESS_ID,TACHE_ID)" + "  (" + "SELECT '" + xf_id
                + "' AS FORM_ID,'" + xf_name + "' AS FORM_NAME,'" + xf_content
                + "' AS FORM_CONTENT,'' AS FORM_TYPE,'' AS FORM_USER,SYSDATE() AS CREATE_DATE,'' AS COMMENTS from dual)B");
    }
}