Neo4j REST Server, GWT and JIT Part 2

This is a Proof of Concept (from GTUG BootCamp 2010), which is: Try to code a complete client side data-load (from some REST services) and graph visualization using Neo4j REST server, the Java Script Visualization Framework JIT and Google Web Toolkit.

This is a followup to Part 1 that stopped by loading all Nodes of the graph. Next is to load all relations.

        void loadAllRelations(ArrayList listOfRelations) {
            ArrayList listOfRelationsClone = (ArrayList) listOfRelations.clone();
            for (Relation rel : listOfRelationsClone)
                loadRelation(rel, listOfRelations);
        }

Same Idea here: loadAllRelations calles loadRelation for every Relation stored in listOfRelations.

        void loadRelation(final Relation rel, final ArrayList listOfRelations) {
            final JSONObject n1o = hasNode(rel.n1);
            final JSONObject n2o = hasNode(rel.n2);
            final String n1 = rel.n1;
            final String n2 = rel.n2;

            String url = "neo4j-rest/relationship/" + rel.relation;

            RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(url));
            builder.setHeader("Accept", "application/json");
            try {
                builder.sendRequest(null, new RequestCallback() {
                    public void onError(Request request, Throwable exception) {
                    }

                    public void onResponseReceived(Request request, Response response) {
                        if (200 == response.getStatusCode()) {

                            JSONObject json = new JSONObject(buildJavaScriptObject(response.getText()));
                            String relationType = json.get("type").toString();
                            JSONArray adjacenciesArrayN1 = getAdjacenciesArray(n1o);

                            fillAdjacenciesArray(adjacenciesArrayN1, n1, n2, "#C74243", relationType);
                            listOfRelations.remove(rel);
                        }
                    }
                });
            } catch (RequestException e) {
            }
        }

The callback removes the loaded relation from the listOfRelations. So that another method can wait for all loaded relations:

       void waitForLoadingRelationsAndCallCallBack(final ArrayList listOfRelations, final AsyncCallback callBack) {
            Timer t = new Timer() {
                @Override
                public void run() {
                    if (!listOfRelations.isEmpty()) {
                        waitForLoadingRelationsAndCallCallBack(listOfRelations, callBack);
                        return;
                    }
                    callBack.onSuccess(graphArray.toString());
                }
            };
            t.schedule(500);
        }

If its all done the onSuccess method is called, the last and final callback to indicate all is loaded.

What we achieved with this is create a JIT graph structure. I have created a GWT Application where you can press a Button to start the “download” of the graph. The code with all the other required method is here. Till now you can copy and paste the JSON object and use it for the JavaScript loadJSON function.

function init() {
    // init data
    var json = [. . .];
    // end
    // init ForceDirected
    var fd = new $jit.ForceDirected({
        //id of the visualization container
        injectInto: 'infovis',
   . . .
   fd.loadJSON(json);

The next thing to do is to create the graph automatically after clicking the button. So this will be part 3 ;-).

Comments are closed.