Using the Neo4j Console in Tomcat and Embedded Mode

Last change 12/04/2014

The neo4j Console is just plain cool. You go through your db like a folder structure, call cypher queries and simply adapt properties BUTit only runs in standalone mode with jetty. But you can quickly use it in any other project with a servlet and a few lines of javascript code.

Prerequesites

You need a running servlet project that already uses neo4j. I am using the Spring-setup here. Apart from that you need:

The page

the Terminal Emulator just needs to be initialized. I will mount the neo4j servlet to /ajax/console and thus the code will look like this:
<script type="text/javascript"  src="<c:url value='/scripts/jquery.terminal-0.4.7.min.js'/>"> </script>
<script type="text/javascript">
$(document).ready(function(){
    $("#console").dialog({
        width : 577,
        height : 403,
        autoOpen: false,
        resize: function(e, ui) {
            var c = $("#console");
            $("#console").data("terminal").resize(c.width(), c.height());
        },
        open: function(e, ui) {
            if($("#console").data("init"))
                $("#console").data("terminal").focus();
        },
        closeOnEscape: false
    });

    $("#consoleBtn").click(function(){
        $("#console").dialog("open");
        var c = $("#console");
        if(!c.data("init")) {
            var term = c.append("<div/>").terminal("../ajax/console", {
                prompt: '>',
                name: 'console',
                greetings: "Start browsing in neo4j. For usage and commands type 'help'."
            });
            c.data("init", true);
            $("#console").data("terminal", term);
        }

    });
});
</script>
<img id="consoleBtn" title="open db console" src="<c:url value='/administration/neo4j.png'/>" style="cursor:pointer;display:block;margin-left:-7px;width:18px;margin-top:-160px"/>
<div title="Data Console" id="console" style="display:none"></div>

The Servlet

The servlet code is straightforward. First I need a Neo4jTemplate from spring. Note that I also initialize jackson for the deserialization of the terminal stuff.
    private Neo4jTemplate template;

    public void init(ServletConfig config) throws ServletException {
        super.init(config);

        // get the root web application context also used by "OpenSessionInViewFilter" for OMR lazy loading
        context = WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext());

        // the jackson stuff
        AnnotationIntrospector primary = new JacksonAnnotationIntrospector();
        AnnotationIntrospector secondary = new JaxbAnnotationIntrospector();
        AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary, secondary);
        mapper = new ObjectMapper();
        mapper.getDeserializationConfig().setAnnotationIntrospector(pair);
        mapper.getSerializationConfig().setAnnotationIntrospector(pair);
        mapper.getSerializationConfig().set(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);

        template = context.getBean(Neo4jTemplate.class);

    }

The rest consists of parsing the command and returning the correct info.

Site created with Corinis CCM