package edu.uci.ics.biggraph.servlet;

import java.net.URI;
import java.net.URISyntaxException;

/**
 * Translate AQL into valid URL. 
 */
public class URLGenerator {
    static String generate(String host, int port, RestAPI type, String revisedCmd) {
        return "http://" + host + ":" + new Integer(port).toString() + type.getQuery() 
                + "?" + type.getFrag() + revisedCmd;
    }
    
//    static String generate(String host, int port, RestAPI type, String cmd) {
//        URI uri = null;
//        
//        try {
//            uri = new URI("http", null, host, port, type.getQuery(), type.getFrag() + cmd, null);
//        } catch (URISyntaxException e) {
//            e.printStackTrace();
//        }
//        return uri != null ? uri.toASCIIString() : null;
//    }
    
    static String cmdParser(String cmd) {
        StringBuilder sb = new StringBuilder();
        int strlen = cmd.length();
        
        for (int i = 0; i < strlen; i++) {
            char c = cmd.charAt(i);
            
            switch (c) {
            case ' ':
            case '\t':
                sb.append("%20");
                while (i < strlen && 
                        (cmd.charAt(i) == ' ' || cmd.charAt(i) == '\t')) {
                    i++;
                }
                i--;
                break;
            case '\n':
            case '\r':
                break;
            default:
                sb.append(c);
                break;
            }
        }
        
        return sb.toString();
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String test = "use dataverse company;" + 
                      "for $l in dataset('Employee') return $l;";
        String test2 = "use dataverse company;" +
                       "insert into dataset Employee({ \"id\":123,\"name\":\"John Doe\"});";  
        String revised = cmdParser(test2);
        System.out.println(generate("localhost", 19002, RestAPI.UPDATE, revised));
    }

}

@@ -160,24 +160,95 @@
 rator %7B%0A    
+/**%0A     * Generate the final URL for HTTP request.%0A     */%0A    public 
 static Strin
@@ -359,20 +359,16 @@
 :%22 +
- new
  Integer
 (por
@@ -367,14 +367,8 @@
 eger
-(port)
 .toS
@@ -373,16 +373,20 @@
 oString(
+port
 ) + type
@@ -396,17 +396,16 @@
 tQuery()
- 
 %0A       
@@ -851,25 +851,110 @@
 //    %7D%0A
+%0A
     
+/**
 %0A    
+ * Parse the AQL by removing/translating necessary notations.%0A     */%0A    public 
 static S
@@ -1678,24 +1678,1276 @@
 ing();%0A    %7D
+%0A%0A    /**%0A     * Generate UPDATE command.%0A     * @param dataverse - the dataverse the required entry belongs to.%0A     * @param dataset - the dataset the required entry belongs to.%0A     * @param payload - items in this entry represented as strings.%0A     *                e.g.: %22%5C%22id%5C%22:123%22, %22%5C%22name%5C%22:%5C%22John Doe%5C%22%22.%0A     * @return the assembled command.%0A     */%0A    public static String update(String dataverse, String dataset, String%5B%5D payload) %7B%0A        StringBuilder sb = new StringBuilder();%0A%0A        sb.append(%22use dataverse %22 + dataverse + %22;%5Cn%22)%0A          .append(%22insert into dataset %22 + dataset + %22(%7B%22);%0A        for (String s : payload) %7B%0A            sb.append(s + %22,%22);%0A        %7D%0A        sb.append(%22%7D)%22);%0A%0A        return sb.toString();%0A    %7D%0A%0A    /**%0A     * Generate QUERY command.%0A     * @param dataverse - the dataverse the required entry belongs to.%0A     * @param dataset - the dataset the required entry belongs to.%0A     * @return the assembled command.%0A     */%0A    public static String query(String dataverse, String dataset) %7B%0A        StringBuilder sb = new StringBuilder();%0A%0A        sb.append(%22use dataverse %22 + dataverse + %22;%5Cn%22)%0A          .append(%22for $l in dataset('%22 + dataset + %22') return $l%22);%0A%0A        return sb.toString();%0A    %7D
 %0A    %0A    pu
@@ -3076,17 +3076,16 @@
 pany;%22 +
- 
 %0A       
@@ -3185,32 +3185,32 @@
 rse company;%22 +%0A
+
                 
@@ -3285,18 +3285,16 @@
 e%5C%22%7D);%22;
-  
 %0A       

add two AQL command generator.