controllers.ServiceController.java Source code

Java tutorial

Introduction

Here is the source code for controllers.ServiceController.java

Source

/**
 * Tern Framework.
 * 
 * @author fancimage
 * @Copyright 2010 qiao_xf@163.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
 */

package controllers;

import com.opensymphony.workflow.loader.DTDEntityResolver;
import com.tern.dao.Record;
import com.tern.iap.AppContext;
import com.tern.iap.util.ActionResult;
import com.tern.util.Trace;
import com.tern.util.config;
import com.tern.web.ControllerException;
import com.tern.web.HttpStream;
import com.tern.web.Route;
import com.tern.web.routes.HttpMethod;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.*;

@Route("/service/$appName/*")
public class ServiceController extends DataResourceController {
    public ServiceController() {
        this.modelName = "service";
    }

    public String show(int id) {
        model = getModel();

        Record record = model.find(id); //retrive
        request.setAttribute("model", model);
        request.setAttribute("record", record);

        return "service/design";
    }

    private void flushFile(File file) {
        HttpStream stream = this.getStream();

        try {
            InputStream inStream = new FileInputStream(file);
            byte[] buf = new byte[4096];
            int readLength;
            while (((readLength = inStream.read(buf)) != -1)) {
                stream.append(buf, 0, readLength);
            }
            inStream.close();
        } catch (Exception e) {
            Trace.write(Trace.Error, e, "read osworkflow definition.");
        }
    }

    @Route("/%1/define")
    public void define(int id) {
        model = getModel();
        Record record = model.find(id); //retrive
        if (record == null) {
            throw new ControllerException(this, "??");
        }

        response.setContentType("text/xml");
        response.setCharacterEncoding("utf-8");
        response.reset();

        HttpStream stream = this.getStream();

        /*??XML*/
        AppContext ctx = AppContext.getAppContext(appName);
        String path = ctx.getResourcePath() + "/models/process/" + record.getString("tname") + ".xml";
        File xmlFile = new File(path);
        if (!xmlFile.exists()) {
            path = config.getConfigurationPath() + "/models/process.template.xml";
            xmlFile = new File(path);
        }
        if (!xmlFile.exists()) {
            //
            stream.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
            stream.append(
                    "<!DOCTYPE workflow PUBLIC \"-//OpenSymphony Group//DTD OSWorkflow 2.6//EN\" \"http://www.opensymphony.com/osworkflow/workflow_2_8.dtd\"[]>\n");
            stream.append(
                    "<workflow>\n<initial-actions>\n<action id=\"0\" name=\"\">\n<results><unconditional-result old-status=\"Finished\" status=\"doing\" step=\"1\" /></results>\n</action>\n</initial-actions>\n");
            stream.append("<steps>\n<step id=\"1\" name=\"?\">\n</step>\n</steps>\n");
            stream.append("</workflow>");
        } else {
            flushFile(xmlFile);
        }
    }

    private boolean writeBodyToFile(String filename) {
        String xmlFile = filename + ".xml";
        String jsonFile = filename + ".shape.json";
        try {
            BufferedReader br = request.getReader();

            boolean hasJson = false;
            StringBuffer buf = new StringBuffer();
            //Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(xmlFile),"utf-8"));

            String inputLine;
            while ((inputLine = br.readLine()) != null) {
                if (inputLine.equals("===boundary===")) {
                    /*?shapes*/
                    //writer.close();
                    //writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(jsonFile),"utf-8"));

                    SAXReader reader = new SAXReader();
                    reader.setEntityResolver(new DTDEntityResolver());
                    Document document = reader.read(new StringReader(buf.toString()));
                    OutputFormat format = new OutputFormat(" ", true);
                    XMLWriter writer = new XMLWriter(
                            new BufferedWriter(new OutputStreamWriter(new FileOutputStream(xmlFile), "utf-8")),
                            format);
                    writer.write(document);
                    writer.flush();
                    writer.close();

                    hasJson = true;
                    buf = new StringBuffer();
                    continue;
                }

                buf.append(inputLine).append("\n");

                //writer.write(inputLine);
                //writer.write("\n");
            }
            //writer.close();
            br.close();

            if (!hasJson) {
                Trace.write(Trace.Error, "write osworkflow: no json-shape define!");
                return false;
            }

            String jsonString = buf.toString();
            jsonString = formatJsonStrings(jsonString);
            if (jsonString == null) {
                Trace.write(Trace.Error, "write osworkflow[json]: " + buf.toString());
                return false;
            }

            Writer jsonWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(jsonFile), "utf-8"));
            jsonWriter.write(jsonString);
            jsonWriter.close();

            return true;
        } catch (DocumentException de) {
            Trace.write(Trace.Error, de, "write osworkflow[xml].");
        } catch (IOException e) {
            Trace.write(Trace.Error, e, "write osworkflow.");
        }

        return false;
    }

    @Route(value = "/%1/define/update", method = HttpMethod.POST)
    public void defineSave(int id) {
        model = getModel();
        Record record = model.find(id); //retrive
        if (record == null) {
            throw new ControllerException(this, "??");
        }

        ActionResult r = new ActionResult();
        this.setViewObject(r);

        AppContext ctx = AppContext.getAppContext(appName);
        String path = ctx.getResourcePath() + "/models/process/" + record.getString("tname");

        if (!writeBodyToFile(path)) {
            r.setResult(2, "save failed");
        }
    }

    @Route("/%1/shape")
    public void shape(int id) {
        /*?*/
        model = getModel();
        Record record = model.find(id); //retrive
        if (record == null) {
            throw new ControllerException(this, "??");
        }

        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        response.reset();

        HttpStream stream = this.getStream();

        /*??XML*/
        AppContext ctx = AppContext.getAppContext(appName);
        String path = ctx.getResourcePath() + "/models/process/" + record.getString("tname") + ".shape.json";
        File file = new File(path);
        if (!file.exists()) {
            stream.append("{}"); //
        } else {
            flushFile(file);
        }
    }

    private static String formatJsonStrings(String json) {
        StringBuffer result = new StringBuffer();

        int length = json.length();
        int number = 0;
        char key = 0;
        boolean inString = false;

        //??
        for (int i = 0; i < length; i++) {
            //1???
            key = json.charAt(i);

            if (key == '"') {
                inString = !inString;
                result.append(key);
                continue;
            }

            if (inString) {
                result.append(key);
                continue;
            }

            //2?????????
            if ((key == '[') || (key == '{')) {
                //1?????
                if ((i - 1 > 0) && (json.charAt(i - 1) == ':')) {
                    result.append('\n');
                    result.append(indent(number));
                }

                //2??
                result.append(key);

                //3??????????
                result.append('\n');

                //4???????
                number++;
                result.append(indent(number));

                //5
                continue;
            }

            //3?????????
            if ((key == ']') || (key == '}')) {
                //1??????????
                result.append('\n');

                //2????????
                number--;
                result.append(indent(number));

                //3??
                result.append(key);

                //4???????
                if (((i + 1) < length) && (json.charAt(i + 1) != ',')) {
                    result.append('\n');
                }

                //5
                continue;
            }

            //4?????????
            if ((key == ',')) {
                result.append(key);
                result.append('\n');
                result.append(indent(number));
                continue;
            }

            //5???
            result.append(key);
        }

        if (inString) {
            return null;
        }

        return result.toString();
    }

    private static String indent(int number) {
        StringBuffer result = new StringBuffer();
        for (int i = 0; i < number; i++) {
            result.append("   ");
        }
        return result.toString();
    }
}