package wwwc.nees.joint.module.kao;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.openrdf.model.Statement;
import org.openrdf.rio.RDFHandler;
import org.openrdf.rio.RDFHandlerException;

/**
 * @author williams
 */
public class GraphQueryToJSONImpl implements RDFHandler {

    private JSONObject results;
    private JSONObject json_aux;
    private JSONObject results_removed;
    private List<String[]> triplesWithObjects;

    @Override
    public void startRDF() throws RDFHandlerException {
        results = new JSONObject();
        results_removed = new JSONObject();
        triplesWithObjects = new ArrayList<>();
    }

    @Override
    public void endRDF() throws RDFHandlerException {

        for (String[] triples : triplesWithObjects) {
            JSONObject object;
            try {
                if (results.has(triples[2])) {
                    object = (JSONObject) results.remove(triples[2]);
                    results_removed.put(triples[2], object);
                } else if (results_removed.has(triples[2])) {
                    object = (JSONObject) results_removed.getJSONObject(triples[2]);
                } else {
                    continue;
                }
                results.getJSONObject(triples[0]).put(triples[1], object);
            } catch (JSONException ex) {
                Logger.getLogger(GraphQueryToJSONImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    @Override
    public void handleNamespace(String prefix, String uri) throws RDFHandlerException {
    }

    @Override
    public void handleStatement(Statement stt) throws RDFHandlerException {
        String subject = stt.getSubject().stringValue();
        String predicate = stt.getPredicate().getLocalName();
        String object = stt.getObject().stringValue();
        //Validate the URI and it add to list of triples which possible represent an object
        try {

            new URL(object);
            if (!subject.equals(object)) {
                triplesWithObjects.add(new String[]{subject, predicate, object});
            }
        } catch (MalformedURLException ex) {
        }

        if (results.has(subject)) {
            try {
                results.getJSONObject(subject).accumulate(predicate, object);
            } catch (JSONException ex) {
                Logger.getLogger(GraphQueryToJSONImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            try {
                json_aux = new JSONObject();
                json_aux.accumulate(predicate, object);
                results.put(subject, json_aux);
            } catch (JSONException ex) {
                Logger.getLogger(GraphQueryToJSONImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    @Override
    public void handleComment(String string) throws RDFHandlerException {
    }

    public String getJSONAsString() {
        return results.toString();
    }
}

@@ -135,19 +135,94 @@
 va.util.
-Lis
+HashSet;%0Aimport java.util.Iterator;%0Aimport java.util.List;%0Aimport java.util.Se
 t;%0Aimpor
@@ -732,16 +732,51 @@
 jects;%0A%0A
+    private Set%3CString%3E areArray;%0A%0A
     @Ove
@@ -961,24 +961,60 @@
 ayList%3C%3E();%0A
+        areArray = new HashSet%3C%3E();%0A
     %7D%0A%0A    @
@@ -2304,17 +2304,16 @@
   try %7B%0A
-%0A
         
@@ -2524,24 +2524,55 @@
 %0A        %7D%0A%0A
+        JSONObject jsonObject;%0A
         if (
@@ -2620,32 +2620,45 @@
 %0A               
+ jsonObject =
  results.getJSON
@@ -2676,39 +2676,491 @@
 ect)
-.accumulate(predicate, object);
+;%0A                if (jsonObject.has(predicate)) %7B%0A                    //%0A                    converterObjectToArray(predicate);%0A                    //%0A                    areArray.add(predicate);%0A                    jsonObject.accumulate(predicate, object);%0A%0A                %7D else if (areArray.contains((String) predicate)) %7B%0A                    jsonObject.append(predicate, object);%0A                %7D else %7B%0A                    jsonObject.put(predicate, object);%0A                %7D
 %0A   
@@ -3366,20 +3366,22 @@
     json
-_aux
+Object
  = new J
@@ -3413,27 +3413,170 @@
     
-json_aux.accumulate
+if (areArray.contains((String) predicate)) %7B%0A                    jsonObject.append(predicate, object);%0A                %7D else %7B%0A                    jsonObject.put
 (pre
@@ -3584,32 +3584,50 @@
 icate, object);%0A
+                %7D%0A
                 
@@ -3651,20 +3651,22 @@
 ct, json
-_aux
+Object
 );%0A     
@@ -3821,24 +3821,631 @@
 %7D%0A        %7D%0A
+    %7D%0A%0A    public void converterObjectToArray(String predicate) throws JSONException %7B%0A        //%0A        Iterator%3CString%3E keys = results.keys();%0A        while (keys.hasNext()) %7B%0A            JSONObject jsonObject_aux = results.getJSONObject(keys.next());%0A            if (jsonObject_aux.has(predicate)) %7B%0A                try %7B%0A                    jsonObject_aux.getJSONArray(predicate);%0A                %7D catch (Exception e) %7B%0A                    Object value_temp = jsonObject_aux.remove(predicate);%0A                    jsonObject_aux.append(predicate, value_temp);%0A                %7D%0A            %7D%0A        %7D
 %0A    %7D%0A%0A    

Handle bug with JSONArray