package edu.uci.ics.biggraph.outputformat;

import java.io.IOException;

import edu.uci.ics.biggraph.servlet.URLGenerator;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;

import edu.uci.ics.biggraph.io.IntWritable;
import edu.uci.ics.biggraph.io.VLongArrayListWritable;
import edu.uci.ics.biggraph.io.VLongWritable;
import edu.uci.ics.pregelix.api.graph.Vertex;
import edu.uci.ics.pregelix.api.io.text.TextVertexOutputFormat.TextVertexWriter;

import edu.uci.ics.biggraph.servlet.*;

public class SocialSuggestionWriter extends 
TextVertexWriter<VLongWritable, VLongArrayListWritable, IntWritable>{

    public SocialSuggestionWriter(RecordWriter<Text, Text> arg0) {
        super(arg0);
    }

    @Override
    public void writeVertex(
            Vertex<VLongWritable, VLongArrayListWritable, IntWritable, ?> vertex)
            throws IOException, InterruptedException {
        VLongArrayListWritable val = vertex.getVertexValue();
        String nodeID = vertex.getVertexId().toString();
        String nodeVal = val.toString();

        getRecordWriter().write(new Text(nodeID), new Text(nodeVal));

        // Assemble vertex payload as part of AQL UPDATE command.
        String[] items = new String[2];
        items[0] = "\"node_id\":" + nodeID;
        items[1] = "\"suggested_friends\":" + nodeVal; // XXX: NOT SURE!

        String aql = URLGenerator.update("Tasks", "TaskThreeType", items);
        aql = URLGenerator.cmdParser(aql);
        String url = URLGenerator.generate("localhost", 19002, RestAPI.UPDATE, aql);
        Commander.sendGet(url); // no payload to get
    }

}

@@ -1145,17 +1145,28 @@
        /
-/
+*%0A         *
  Assembl
@@ -1225,38 +1225,240 @@
     
-String%5B%5D items = new String%5B2%5D
+ * Find out the fields format:%0A         * @see https://docs.google.com/document/d/1HvSHJrj2qdY6Zr8wCQRlMVbTfrFzz7qnOhqEed3J4DQ/edit%0A         */%0A        String%5B%5D items = new String%5B2%5D;%0A        StringBuilder vals = new StringBuilder()
 ;%0A  
@@ -1511,19 +1511,20 @@
     
-items%5B1%5D = 
+vals.append(
 %22%5C%22s
@@ -1550,34 +1550,334 @@
 %22 + 
-nodeVal; // XXX: NOT SURE!
+%22%7B%7B%22);%0A        String%5B%5D ss = nodeVal.split(%22 %22);%0A        for (int i = 0; i %3C ss.length; i++) %7B%0A            vals.append(ss%5Bi%5D);%0A            if (i != ss.length - 1) %7B%0A                vals.append(%22,%22);%0A            %7D%0A        %7D // XXX: should we put %22null%22 afterwards?%0A        vals.append(%22%7D%7D%22);%0A        items%5B1%5D = vals.toString();
 %0A%0A  

Change the format of unorderedList