Permalink
Browse files

First working version

  • Loading branch information...
1 parent 566fcf0 commit 6723f801e3b4a7f88d767cc3827db0f7b91d8405 @jawher committed Dec 23, 2009
Showing with 496 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +22 −0 LICENSE
  3. +67 −0 README
  4. +162 −0 pom.xml
  5. +83 −0 src/main/scala/org/neo4j/scala/Neo4jWrapper.scala
  6. +157 −0 src/test/scala/org/neo4j/scala/Neo4jWrapperTest.scala
View
@@ -0,0 +1,5 @@
+generated
+target
+.manager
+*~
+
View
22 LICENSE
@@ -0,0 +1,22 @@
+ The MIT License
+
+Copyright (c) 2009, Jawher Moussa and Martin Kleppmann
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+
View
67 README
@@ -0,0 +1,67 @@
+Neo4j resources library
+=======================
+
+The Neo4j resources library allows you to build an application which stores its data in
+the [http://neo4j.org/](Neo4j open source graph database) and exposes it through a
+domain-specific RESTful JSON API which you define. It is written in Scala and is intended
+to be used in other Scala projects, although you may be able to get it to work with
+other JVM-based langauges too. Projects using this library can build amazing databases
+with a minimum of boilerplate and unnecessary code.
+
+Please see the [Neo4j Scala template](http://github.com/ept/neo4j-scala-template/tree/master)
+as a starting point for your application based on the Neo4j resources library.
+
+
+Building
+--------
+
+You need a Java 5 (or newer) environment and Maven 2.0.9 (or newer) installed:
+
+ $ mvn --version
+ Maven version: 2.0.10
+ Java version: 1.6.0_03-p3
+ OS name: "darwin" version: "9.7.0" arch: "i386" Family: "unix"
+
+
+You should now be able to do a full build of `neo4j-resources`:
+
+ $ git clone git://github.com/jawher/neo4j-scala.git
+ $ cd neo4j-scala
+ $ mvn clean install
+
+To use this library in your projects, add the following to the `dependencies` section of your
+`pom.xml`:
+
+ <dependency>
+ <groupId>org.neo4j</groupId>
+ <artifactId>neo4j-scala</artifactId>
+ <version>0.9.0-SNAPSHOT</version>
+ </dependency>
+
+If you don't use Maven, take `target/neo4j-scala-0.9.0-SNAPSHOT.jar` and all of its dependencies, and add them to your classpath.
+
+To use the project in Eclipse, you must have the Eclipse Scala plugin installed.
+You should also do a full Maven build before using Eclipse, to ensure you have
+all the dependencies downloaded. Then you should be able to do
+"File -> Import -> General -> Existing Projects into Workspace"
+and be ready to go. Note that at the time of writing, the Eclipse Scala
+plugin appears to have a bug which causes it not to write any class files to the
+target directory.
+
+
+Troubleshooting
+---------------
+
+
+
+Using this library
+------------------
+
+
+
+
+License
+-------
+
+See `LICENSE` for details.
+
View
162 pom.xml
@@ -0,0 +1,162 @@
+<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</groupId>
+ <artifactId>neo4j-scala</artifactId>
+ <packaging>bundle</packaging>
+ <name>Neo4j Scala</name>
+ <version>0.9.0-SNAPSHOT</version>
+ <description>Scala wrapper for Neo4j Graph Database</description>
+ <url>http://github.com/jawher/neo4j-scala</url>
+ <inceptionYear>2009</inceptionYear>
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <scala.version>2.7.7</scala.version>
+ <neo4j.version>1.0-b10</neo4j.version>
+ </properties>
+
+ <developers>
+ <developer>
+ <id>jawher</id>
+ <name>Jawher Moussa</name>
+ <url>http://twitter.com/jawher</url>
+ <timezone>+1</timezone>
+ </developer>
+ <developer>
+ <id>martin</id>
+ <name>Martin Kleppmann</name>
+ <url>http://twitter.com/martinkl</url>
+ </developer>
+ </developers>
+
+ <licenses>
+ <license>
+ <name>The MIT License</name>
+ <url>http://www.opensource.org/licenses/mit-license.php</url>
+ </license>
+ </licenses>
+
+ <scm>
+ <connection>scm:git:git://github.com/jawher/neo4j-scala.git</connection>
+ <developerConnection>scm:git:git://github.com/jawher/neo4j-scala.git</developerConnection>
+ </scm>
+
+
+ <repositories>
+ <repository>
+ <id>scala-tools.org</id>
+ <name>Scala-Tools Maven2 Repository</name>
+ <url>http://scala-tools.org/repo-releases</url>
+ </repository>
+ <repository>
+ <id>neo4j-public-repository</id>
+ <url>http://m2.neo4j.org</url>
+ </repository>
+ </repositories>
+
+ <pluginRepositories>
+ <pluginRepository>
+ <id>scala-tools.org</id>
+ <name>Scala-Tools Maven2 Repository</name>
+ <url>http://scala-tools.org/repo-releases</url>
+ </pluginRepository>
+ </pluginRepositories>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.scala-lang</groupId>
+ <artifactId>scala-library</artifactId>
+ <version>${scala.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.7</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.scala-tools.testing</groupId>
+ <artifactId>specs</artifactId>
+ <version>1.6.1</version>
+ <scope>test</scope>
+ </dependency>
+
+ <!-- Neo4j graph database -->
+ <dependency>
+ <groupId>org.neo4j</groupId>
+ <artifactId>neo</artifactId>
+ <version>${neo4j.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.neo4j</groupId>
+ <artifactId>shell</artifactId>
+ <version>${neo4j.version}</version>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <sourceDirectory>src/main/scala</sourceDirectory>
+ <testSourceDirectory>src/test/scala</testSourceDirectory>
+ <plugins>
+ <plugin>
+ <groupId>org.scala-tools</groupId>
+ <artifactId>maven-scala-plugin</artifactId>
+ <version>2.12.2</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>compile</goal>
+ <goal>testCompile</goal>
+ </goals>
+ </execution>
+ </executions>
+ <configuration>
+ <charset>${project.build.sourceEncoding}</charset>
+ <scalaVersion>${scala.version}</scalaVersion>
+ <args>
+ <arg>-target:jvm-1.5</arg>
+ </args>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-eclipse-plugin</artifactId>
+ <configuration>
+ <downloadSources>true</downloadSources>
+ <buildcommands>
+ <buildcommand>ch.epfl.lamp.sdt.core.scalabuilder</buildcommand>
+ </buildcommands>
+ <additionalProjectnatures>
+ <projectnature>ch.epfl.lamp.sdt.core.scalanature</projectnature>
+ </additionalProjectnatures>
+ <classpathContainers>
+ <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
+ <classpathContainer>ch.epfl.lamp.sdt.launching.SCALA_CONTAINER</classpathContainer>
+ </classpathContainers>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <extensions>true</extensions>
+ <configuration>
+ <instructions>
+ <Export-Package>org.neo4j.scala</Export-Package>
+ </instructions>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ <reporting>
+ <plugins>
+ <plugin>
+ <groupId>org.scala-tools</groupId>
+ <artifactId>maven-scala-plugin</artifactId>
+ <configuration>
+ <scalaVersion>${scala.version}</scalaVersion>
+ </configuration>
+ </plugin>
+ </plugins>
+ </reporting>
+</project>
@@ -0,0 +1,83 @@
+package org.neo4j.scala
+
+import org.neo4j.api.core._
+
+/**
+ * Extend your class with this trait to get really neat new notation for creating
+ * new relationships. For example, ugly Java-esque code like:
+ * <pre>
+ * val knows = DynamicRelationshipType.withName("KNOWS")
+ * start.createRelationshipTo(intermediary, knows)
+ * intermediary.createRelationshipTo(end, knows)
+ * </pre>
+ *
+ * can be replaced with a beautiful Scala one-liner:
+ * <pre>start --> "KNOWS" --> intermediary --> "KNOWS" --> end</pre>
+ *
+ * Feel free to use this example to tell all your friends how awesome scala is :)
+ */
+trait Neo4jWrapper {
+
+ /**
+ * Execute instructions within a Neo4j transaction; rollback if exception is raised and
+ * commit otherwise; and return the return value from the operation.
+ */
+ def execInNeo4j[T<:Any](operation: NeoService => T)(implicit neo : NeoService): T = {
+ val tx = synchronized {
+ neo.beginTx
+ }
+ try {
+ val ret = operation(neo)
+ tx.success
+ return ret
+ } finally {
+ tx.finish
+ }
+ }
+
+ class NodeRelationshipMethods(node: Node) {
+
+ def -->(relType: RelationshipType) = new OutgoingRelationshipBuilder(node, relType)
+
+ // Create incoming relationship
+
+ def <--(relType: RelationshipType) = new IncomingRelationshipBuilder(node, relType)
+ }
+
+ // Half-way through building an outgoing relationship
+ class OutgoingRelationshipBuilder(fromNode: Node, relType: RelationshipType) {
+ def -->(toNode: Node) = {
+ fromNode.createRelationshipTo(toNode, relType)
+ new NodeRelationshipMethods(toNode)
+ }
+ }
+
+ // Half-way through building an incoming relationship
+ class IncomingRelationshipBuilder(toNode: Node, relType: RelationshipType) {
+ def <--(fromNode: Node) = {
+ fromNode.createRelationshipTo(toNode, relType)
+ new NodeRelationshipMethods(fromNode)
+ }
+ }
+
+ implicit def node2relationshipBuilder(node: Node) = new NodeRelationshipMethods(node)
+
+ implicit def string2RelationshipType(relType: String) = DynamicRelationshipType.withName(relType)
+
+ class RichPropertyContainer(propertyContainer: PropertyContainer) {
+ def apply(property: String) : Option[Any] = if(propertyContainer.hasProperty(property)) Some(propertyContainer.getProperty(property)) else None
+ def update(property: String, value: Any) : Unit = propertyContainer.setProperty(property, value)
+ }
+
+ implicit def propertyContainer2RichPropertyContainer(propertyContainer: PropertyContainer) = new RichPropertyContainer(propertyContainer)
+
+ implicit def fn2StopEvaluator(e : TraversalPosition => Boolean) =
+ new StopEvaluator() {
+ def isStopNode(traversalPosition : TraversalPosition) = e(traversalPosition)
+ }
+
+ implicit def fn2ReturnableEvaluator(e : TraversalPosition => Boolean) =
+ new ReturnableEvaluator () {
+ def isReturnableNode(traversalPosition : TraversalPosition) = e(traversalPosition)
+ }
+}
Oops, something went wrong.

0 comments on commit 6723f80

Please sign in to comment.