import("dispatch.{Dispatcher,PrefixMatcher,forward}");
import("fastJSON");
import("sqlbase.sqlcommon");

import("control.static_control");
import("control.editor_control");
import("control.run_control");

import("collab.collabroom_server");
import("collab.collab_server");
import("pad.model");
import("pad.dbwriter");

import("editor.workspace");

jimport("java.lang.System.out.println");

serverhandlers.startupHandler = function() {
    sqlcommon.init("org.apache.derby.jdbc.EmbeddedDriver",
                   "jdbc:derby:pads");
    
    model.onStartup();
    collab_server.onStartup();
    dbwriter.onStartup();
    collabroom_server.onStartup();
    workspace.onStartup();
};

serverhandlers.requestHandler = function() {
    // XXX maybe check some stuff?
    handlePath();
};

serverhandlers.tasks.writePad = function(padId) {
    dbwriter.taskWritePad(padId);
};
serverhandlers.tasks.reviseDocument = function(padId) {
    workspace.taskReviseDocument(padId);
};

serverhandlers.cometHandler = function(op, id, data) {
    if ( ! data) {
        collabroom_server.handleComet(op, id, data);
        return;
    }
    
    while (data[data.length-1] == '\u0000') {
        data = data.substr(0, data.length-1);
    }
    
    var wrapper;
    try {
        wrapper = fastJSON.parse(data);
    } catch (err) {
        try {
            wrapper = fastJSON.parse(data + '}');
        } catch (err) {
            println("[comet] invalid JSON");
            throw err;
        }
    }
    if (wrapper.type == "COLLABROOM") {
        collabroom_server.handleComet(op, id, wrapper.data);
    } else {
        println("[comet] incorrectly wrapped " + wrapper['type']);
    }
};

function handlePath() {
    response.neverCache();
    
    var get = new Dispatcher();
    get.addLocations([
        [PrefixMatcher('/static/'), forward(static_control)],
        ['/', editor_control.render_root],
        [/^\/run\/(\w+)\/(.+)$/, run_control.render_run],
        [/^\/test\/(\w+)\/(.+)$/, run_control.render_test],
        [/^\/delete\/(\w+)\/(.+)$/, editor_control.render_confirm_delete],
        [/^\/(\w+)\/?$/, editor_control.render_project],
        [/^\/(\w+)\/(.+)$/, editor_control.render_path]
    ]);
    
    var post = new Dispatcher();
    post.addLocations([
        [/^\/delete\/(\w+)\/(.+)$/, editor_control.delete_path],
        [/^\/(\w+)\/?$/, editor_control.create_project],
        [/^\/(\w+)\/(.+)$/, editor_control.create_path]
    ]);
    
    var dispatchers = { GET: get, POST: post };
    
    if (dispatchers[request.method].dispatch()) {
        return;
    }
    
    // XXX 404
}

@@ -1903,30 +1903,20 @@
    %5B
-/%5E%5C/run%5C/(%5Cw+)%5C/(.+)$/
+_file('run')
 , ru
@@ -1951,31 +1951,21 @@
    %5B
-/%5E%5C/test%5C/(%5Cw+)%5C/(.+)$/
+_file('test')
 , ru
@@ -2001,33 +2001,23 @@
    %5B
-/%5E%5C/delete%5C/(%5Cw+)%5C/(.+)$/
+_file('delete')
 , ed
@@ -2058,38 +2058,31 @@
 %5D,%0A        %5B
-/%5E%5C/(%5Cw+)%5C/?$/
+_proj()
 , editor_con
@@ -2112,33 +2112,23 @@
        %5B
-/%5E%5C/(%5Cw+)%5C/(.+)$/
+_file()
 , editor
@@ -2232,33 +2232,23 @@
    %5B
-/%5E%5C/delete%5C/(%5Cw+)%5C/(.+)$/
+_file('delete')
 , ed
@@ -2287,22 +2287,15 @@
    %5B
-/%5E%5C/(%5Cw+)%5C/?$/
+_proj()
 , ed
@@ -2337,25 +2337,15 @@
    %5B
-/%5E%5C/(%5Cw+)%5C/(.+)$/
+_file()
 , ed
@@ -2530,8 +2530,209 @@
 X 404%0A%7D%0A
+%0Afunction _proj(verb) %7B%0A  return new RegExp(%22%5E%22 + (verb ? %22/%22 + verb : %22%22) + %22/(%5B%5C%5Cw-%5C%5C.%5D+)/?$%22)%0A%7D%0A%0Afunction _file(verb) %7B%0A  return new RegExp(%22%5E%22 + (verb ? %22/%22 + verb : %22%22) + %22/(%5B%5C%5Cw-%5C%5C.%5D+)/(.+)$%22)%0A%7D%0A

Refactor dispatch regular expressions