This repository has been archived by the owner on Oct 7, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ac469ef
Showing
11 changed files
with
601 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
web: sh target/bin/webapp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>org.neo4j.example</groupId> | ||
<artifactId>neo4j-movies</artifactId> | ||
<version>2.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
<name>Neo4j Movies Demo</name> | ||
<description>Neo4j Movies Demo App</description> | ||
<url>http://github.com/jexp/neo4j-movies</url> | ||
<properties> | ||
<neo4j.version>2.0.0-M06</neo4j.version> | ||
</properties> | ||
|
||
<repositories> | ||
<repository> | ||
<id>Spark repository</id> | ||
<url>http://www.sparkjava.com/nexus/content/repositories/spark/</url> | ||
</repository> | ||
<repository> | ||
<id>Neo4j releases</id> | ||
<url>http://m2.neo4j.org/content/repositories/releases</url> | ||
</repository> | ||
<repository> | ||
<id>Neo4j snapshots</id> | ||
<url>http://m2.neo4j.org/content/repositories/snapshots</url> | ||
</repository> | ||
<repository> | ||
<id>mandubian-mvn</id> | ||
<url>http://mandubian-mvn.googlecode.com/svn/trunk/mandubian-mvn/repository</url> | ||
</repository> | ||
</repositories> | ||
<dependencies> | ||
<!-- tag::jdbc-dependency[] --> | ||
<dependency> | ||
<groupId>org.neo4j</groupId> | ||
<artifactId>neo4j-jdbc</artifactId> | ||
<version>2.0.0-M06</version> | ||
</dependency> | ||
<!-- end::jdbc-dependency[] --> | ||
<dependency> | ||
<groupId>org.neo4j</groupId> | ||
<artifactId>neo4j</artifactId> | ||
<version>${neo4j.version}</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>org.neo4j</groupId> | ||
<artifactId>neo4j-jmx</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.eclipse.jetty</groupId> | ||
<artifactId>jetty-server</artifactId> | ||
<version>9.0.5.v20130815</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.eclipse.jetty</groupId> | ||
<artifactId>jetty-webapp</artifactId> | ||
<version>9.0.5.v20130815</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-core</artifactId> | ||
<version>1.8.4</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.neo4j</groupId> | ||
<artifactId>neo4j-kernel</artifactId> | ||
<version>${neo4j.version}</version> | ||
<type>test-jar</type> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
<version>1.6.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-log4j12</artifactId> | ||
<version>1.6.1</version> | ||
<scope>runtime</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>log4j</groupId> | ||
<artifactId>log4j</artifactId> | ||
<version>1.2.16</version> | ||
<scope>runtime</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.sparkjava</groupId> | ||
<artifactId>spark-core</artifactId> | ||
<version>1.1.1</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.google.code.gson</groupId> | ||
<artifactId>gson</artifactId> | ||
<version>2.2.4</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.11</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.1</version> | ||
<configuration> | ||
<source>1.7</source> | ||
<target>1.7</target> | ||
<encoding>UTF-8</encoding> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>appassembler-maven-plugin</artifactId> | ||
<version>1.1.1</version> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>assemble</goal> | ||
</goals> | ||
<configuration> | ||
<assembleDirectory>target</assembleDirectory> | ||
<extraJvmArguments>-Xmx512m</extraJvmArguments> | ||
<programs> | ||
<program> | ||
<mainClass>example.jdbc.movies.MovieServer</mainClass> | ||
<name>webapp</name> | ||
</program> | ||
</programs> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>exec-maven-plugin</artifactId> | ||
<version>1.2.1</version> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>java</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
<configuration> | ||
<mainClass>example.jdbc.movies.MovieServer</mainClass> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package example.jdbc.executor; | ||
|
||
import java.util.Iterator; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author Michael Hunger @since 22.10.13 | ||
*/ | ||
public interface CypherExecutor { | ||
Iterator<Map<String,Object>> query(String statement, Map<String,Object> params); | ||
} |
82 changes: 82 additions & 0 deletions
82
src/main/java/example/jdbc/executor/JdbcCypherExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package example.jdbc.executor; | ||
|
||
import java.sql.*; | ||
import java.util.*; | ||
|
||
/** | ||
* @author Michael Hunger @since 22.10.13 | ||
*/ | ||
public class JdbcCypherExecutor implements CypherExecutor { | ||
|
||
private final Connection conn; | ||
|
||
public JdbcCypherExecutor(String url) { | ||
this(url,null,null); | ||
} | ||
public JdbcCypherExecutor(String url,String username, String password) { | ||
try { | ||
conn = DriverManager.getConnection(url.replace("http://","jdbc:neo4j://"),username,password); | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public Iterator<Map<String, Object>> query(String query, Map<String, Object> params) { | ||
try { | ||
final PreparedStatement statement = conn.prepareStatement(query); | ||
setParameters(statement, params); | ||
final ResultSet result = statement.executeQuery(); | ||
return new Iterator<Map<String, Object>>() { | ||
|
||
boolean hasNext = result.next(); | ||
public List<String> columns; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return hasNext; | ||
} | ||
|
||
private List<String> getColumns() throws SQLException { | ||
if (columns != null) return columns; | ||
ResultSetMetaData metaData = result.getMetaData(); | ||
int count = metaData.getColumnCount(); | ||
List<String> cols = new ArrayList<>(count); | ||
for (int i = 1; i <= count; i++) cols.add(metaData.getColumnName(i)); | ||
return columns = cols; | ||
} | ||
|
||
@Override | ||
public Map<String, Object> next() { | ||
try { | ||
if (hasNext) { | ||
Map<String, Object> map = new LinkedHashMap<>(); | ||
for (String col : getColumns()) map.put(col, result.getObject(col)); | ||
hasNext = result.next(); | ||
if (!hasNext) { | ||
result.close(); | ||
statement.close(); | ||
} | ||
return map; | ||
} else throw new NoSuchElementException(); | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void remove() { | ||
} | ||
}; | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private void setParameters(PreparedStatement statement, Map<String, Object> params) throws SQLException { | ||
for (Map.Entry<String, Object> entry : params.entrySet()) { | ||
int index = Integer.parseInt(entry.getKey()); | ||
statement.setObject(index, entry.getValue()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package example.jdbc.movies; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import spark.Request; | ||
import spark.Response; | ||
import spark.Route; | ||
import spark.servlet.SparkApplication; | ||
|
||
import java.net.URI; | ||
import java.net.URLDecoder; | ||
|
||
import static spark.Spark.get; | ||
|
||
public class MovieRoutes implements SparkApplication { | ||
|
||
private Gson gson = new GsonBuilder().disableHtmlEscaping().create(); | ||
private MovieService service; | ||
|
||
public MovieRoutes(MovieService service) { | ||
this.service = service; | ||
} | ||
|
||
public void init() { | ||
get(new Route("/movie/:title") { | ||
public Object handle(Request request, Response response) { | ||
return gson.toJson(service.findMovie(URLDecoder.decode(request.params("title")))); | ||
} | ||
}); | ||
get(new Route("/search") { | ||
public Object handle(Request request, Response response) { | ||
return gson.toJson(service.search(request.queryParams("q"))); | ||
} | ||
}); | ||
get(new Route("/graph") { | ||
public Object handle(Request request, Response response) { | ||
int limit = request.queryParams("limit") != null ? Integer.valueOf(request.queryParams("limit")) : 100; | ||
return gson.toJson(service.graph(limit)); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package example.jdbc.movies; | ||
|
||
import example.jdbc.util.Util; | ||
|
||
import static spark.Spark.externalStaticFileLocation; | ||
import static spark.Spark.setPort; | ||
|
||
/** | ||
* @author Michael Hunger @since 22.10.13 | ||
*/ | ||
public class MovieServer { | ||
|
||
public static void main(String[] args) { | ||
setPort(Util.getWebPort()); | ||
externalStaticFileLocation("src/main/webapp"); | ||
final MovieService service = new MovieService(Util.getNeo4jUrl()); | ||
new MovieRoutes(service).init(); | ||
} | ||
} |
Oops, something went wrong.