com.surveypanel.form.FormHandler.java Source code

Java tutorial

Introduction

Here is the source code for com.surveypanel.form.FormHandler.java

Source

/*
* SurveyPanel
* Copyright (C) 2009 Serge Tan Panza
* All rights reserved.
* License: GNU/GPL License v3 , see LICENSE.txt
* SurveyPanel is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
* 
*/
package com.surveypanel.form;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.AbstractHandler;

import com.surveypanel.js.JSManager;
import com.surveypanel.js.Result;

public class FormHandler extends AbstractHandler {

    private static FormFactory formFactory;
    private static JSManager jsManager;
    private static Server server;
    private static AbstractHandler handler;
    private static int port = 8181;
    private static Log log = LogFactory.getLog(FormHandler.class);
    private static long surveyId = 1;
    private static boolean quit, devMode = true;

    public static void main(String[] args) {
        if (handler == null) {
            handler = new FormHandler();
        }
        try {
            init();
            while (!quit) {
                Thread.sleep(1000);
            }
            shutDown();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch)
            throws IOException, ServletException {
        response.setContentType("text/html;charset=UTF-8");
        Map<String, String> values = new HashMap<String, String>();
        Map params = request.getParameterMap();
        try {
            for (Object key : params.keySet()) {
                Object object = params.get(key);
                if (object instanceof String) {
                    values.put((String) key, (String) object);
                } else if (object.getClass().isArray()) {
                    StringBuilder sb = new StringBuilder();
                    String[] value = (String[]) object;
                    for (String content : value) {
                        sb.append(content).append(",");
                    }
                    sb.deleteCharAt(sb.length() - 1);
                    values.put((String) key, sb.toString());
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (params.containsKey("quit")) {
            quit = true;
        }
        Form form = null;
        if (!values.containsKey("formId")) {
            form = formFactory.create(surveyId, devMode);
        } else {
            form = formFactory.load(values.get("formId"), surveyId, devMode);
        }
        form.setValues(values);
        form.setDevMode(devMode);
        Result execute;
        try {
            execute = (Result) jsManager.execute(form, "flow.execute(survey); save();");
            System.out.println(execute.getLogs());
            if (execute.hasError()) {
                response.getWriter().write(execute.getError());
            } else {
                response.getWriter().write(execute.getDisplay());
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().write(e.getMessage());
        }
        response.flushBuffer();
    }

    public static void init() throws Exception {

        formFactory = TestHelper.getDBFormFactory();
        formFactory.init(surveyId);
        jsManager = new JSManager(formFactory);

        log.info("start http server");
        server = new Server(port);
        server.setHandler(handler);
        server.start();

    }

    public static void shutDown() {
        try {
            log.info("stop http server");
            server.stop();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}