Permalink
Browse files

Added some sample code over a simple domain model. Changed pom to inc…

…lude dependencies for SDG and a Main
  • Loading branch information...
Chris Gioran
Chris Gioran committed Mar 1, 2011
1 parent 775ff2b commit d3ea97d974158d7a777f32c5a299a21abeb650fb
View
@@ -290,6 +290,64 @@
<target>1.6</target>
</configuration>
</plugin>
+
+ <plugin>
+ <!-- required to resolve aspectj-enhanced class features -->
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>aspectj-maven-plugin</artifactId>
+ <version>1.0</version>
+ <configuration>
+ <outxml>true</outxml>
+ <aspectLibraries>
+ <aspectLibrary>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-aspects</artifactId>
+ </aspectLibrary>
+ <aspectLibrary>
+ <groupId>org.springframework.data</groupId>
+ <artifactId>spring-data-neo4j</artifactId>
+ </aspectLibrary>
+ </aspectLibraries>
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ <executions>
+ <execution>
+ <goals>
+ <goal>compile</goal>
+ <goal>test-compile</goal>
+ </goals>
+ </execution>
+ </executions>
+ <dependencies>
+ <dependency>
+ <groupId>org.aspectj</groupId>
+ <artifactId>aspectjrt</artifactId>
+ <version>${aspectj.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.aspectj</groupId>
+ <artifactId>aspectjtools</artifactId>
+ <version>${aspectj.version}</version>
+ </dependency>
+ </dependencies>
+ </plugin>
+
+ <plugin>
+ <!-- Execute the main class -->
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>exec-maven-plugin</artifactId>
+ <executions>
+ <execution>
+ <goals>
+ <goal>exec</goal>
+ </goals>
+ </execution>
+ </executions>
+ <configuration>
+ <mainClass>org.neo4j.spring.example.proteins.Main</mainClass>
+ </configuration>
+ </plugin>
</plugins>
</build>
@@ -0,0 +1,28 @@
+package org.neo4j.spring.example.proteins;
+
+import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * Hello world(s)!
+ * <p/>
+ * An example application for exploring Spring Data Graph.
+ */
+public class Main
+{
+
+ public static void main( String[] args ) throws Exception
+ {
+ ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext(
+ "/spring/proteinsContext.xml" );
+
+ ProteinClassRepository classRepo = applicationContext.getBean( ProteinClassRepository.class );
+
+ Iterable<ProteinClass> proteinClasses = classRepo.createProteinClasses();
+
+ for ( ProteinClass clazz : proteinClasses )
+ System.out.println( "At home on: " + clazz );
+
+ applicationContext.close();
+ }
+}
@@ -0,0 +1,27 @@
+package org.neo4j.spring.example.proteins;
+
+import org.apache.commons.collections.EnumerationUtils;
+import org.springframework.data.annotation.Indexed;
+import org.springframework.data.graph.annotation.NodeEntity;
+import org.springframework.data.graph.annotation.RelatedTo;
+import org.springframework.data.graph.core.Direction;
+
+@NodeEntity
+public class Protein
+{
+ @Indexed( indexName = "proteins" )
+ private String name;
+
+ @RelatedTo( type = "BELONGS_TO_CLASS", elementClass = ProteinClass.class, direction = Direction.OUTGOING )
+ private ProteinClass belongsToFamily;
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public void setName( String name )
+ {
+ this.name = name;
+ }
+}
@@ -0,0 +1,67 @@
+package org.neo4j.spring.example.proteins;
+
+import java.util.Collections;
+import java.util.Set;
+
+import org.springframework.data.annotation.Indexed;
+import org.springframework.data.graph.annotation.NodeEntity;
+import org.springframework.data.graph.annotation.RelatedTo;
+import org.springframework.data.graph.core.Direction;
+import org.springframework.data.graph.core.NodeBacked;
+
+@NodeEntity
+public class ProteinClass
+{
+
+ @Indexed( indexName = "protein-classes" )
+ private String name;
+
+ @RelatedTo( type = "SUBCLASS_OF", elementClass = ProteinClass.class, direction = Direction.OUTGOING )
+ private ProteinClass superClass;
+
+ @RelatedTo( type = "SUBCLASS_OF", elementClass = ProteinClass.class, direction = Direction.INCOMING )
+ private Set<ProteinClass> subclasses;
+
+ @RelatedTo( type = "INTERACTS_WITH_CLASS", elementClass = ProteinClass.class, direction = Direction.BOTH )
+ private Set<ProteinClass> interactions;
+
+ @RelatedTo( type = "BELONGS_TO_CLASS", elementClass = Protein.class, direction = Direction.INCOMING )
+ private Set<Protein> members;
+
+ public ProteinClass( String name )
+ {
+ this.name = name;
+ }
+
+ public ProteinClass() {}
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public Set<Protein> getMembers()
+ {
+ return members;
+ }
+
+ public ProteinClass getSuperClass()
+ {
+ return superClass;
+ }
+
+ public void setSuperClass( ProteinClass superClass )
+ {
+ this.superClass = superClass;
+ }
+
+ public Set<ProteinClass> getSubclasses()
+ {
+ return subclasses;
+ }
+
+ public Set<ProteinClass> getInteractions()
+ {
+ return interactions;
+ }
+}
@@ -0,0 +1,125 @@
+package org.neo4j.spring.example.proteins;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.util.ArrayList;
+import java.util.Collection;
+
+import javax.sql.XAConnection;
+import javax.sql.XADataSource;
+import javax.transaction.Transaction;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.graph.neo4j.finder.FinderFactory;
+import org.springframework.data.graph.neo4j.finder.NodeFinder;
+import org.springframework.stereotype.Repository;
+import org.springframework.transaction.jta.JtaTransactionManager;
+
+@Repository
+public class ProteinClassRepository
+{
+ @Autowired
+ private FinderFactory finderFactory;
+
+ @Autowired
+ private XADataSource dataSource;
+
+ @Autowired
+ private JtaTransactionManager tm;
+
+ public Collection<ProteinClass> createProteinClasses() throws Exception
+ {
+ tm.getTransactionManager().begin();
+ Transaction tx = tm.getTransactionManager().getTransaction();
+
+ ArrayList<ProteinClass> proteinClasses = new ArrayList<ProteinClass>();
+ try
+ {
+ XAConnection xaConn = dataSource.getXAConnection();
+ Connection conn = xaConn.getConnection();
+ tx.enlistResource( xaConn.getXAResource() );
+ PreparedStatement insertStmt = conn.prepareStatement(
+ "insert into Node values(?)" );
+ ProteinClass root1 = new ProteinClass( "root1" );
+ proteinClasses.add( root1 );
+ insertStmt.setLong( 1, root1.getUnderlyingState().getId() );
+ insertStmt.execute();
+
+ ProteinClass root2 = new ProteinClass( "root2" );
+ proteinClasses.add( root2 );
+
+ insertStmt.setLong( 1, root2.getUnderlyingState().getId() );
+ insertStmt.execute();
+
+ ProteinClass firstLevel1 = new ProteinClass( "firstLevel1" );
+ root1.getSubclasses().add( firstLevel1 );
+ firstLevel1.setSuperClass( root1 );
+ proteinClasses.add( firstLevel1 );
+
+ ProteinClass firstLevel2 = new ProteinClass( "firstLevel2" );
+ root1.getSubclasses().add( firstLevel2 );
+ firstLevel2.setSuperClass( root1 );
+ proteinClasses.add( firstLevel2 );
+
+ ProteinClass firstLevel3 = new ProteinClass( "firstLevel3" );
+ root2.getSubclasses().add( firstLevel3 );
+ firstLevel3.setSuperClass( root2 );
+ proteinClasses.add( firstLevel3 );
+
+ ProteinClass leaf1 = new ProteinClass( "leaf1" );
+ firstLevel1.getSubclasses().add( leaf1 );
+ leaf1.setSuperClass( firstLevel1 );
+ proteinClasses.add( leaf1 );
+
+ ProteinClass leaf2 = new ProteinClass( "leaf2" );
+ firstLevel1.getSubclasses().add( leaf2 );
+ leaf2.setSuperClass( firstLevel1 );
+ proteinClasses.add( leaf2 );
+
+ ProteinClass leaf3 = new ProteinClass( "leaf3" );
+ firstLevel1.getSubclasses().add( leaf3 );
+ leaf3.setSuperClass( firstLevel1 );
+ proteinClasses.add( leaf3 );
+
+ ProteinClass leaf4 = new ProteinClass( "leaf4" );
+ firstLevel2.getSubclasses().add( leaf4 );
+ leaf4.setSuperClass( firstLevel1 );
+ proteinClasses.add( leaf4 );
+
+ tx.commit();
+ }
+ catch ( Exception e )
+ {
+ e.printStackTrace();
+ tx.rollback();
+ throw new RuntimeException( e );
+ }
+ return proteinClasses;
+
+ }
+
+ public ProteinClass findProteinClassIdentifiedBy( long id )
+ {
+ return finder().findById( id );
+ }
+
+ private NodeFinder<ProteinClass> finder()
+ {
+ return finderFactory.createNodeEntityFinder( ProteinClass.class );
+ }
+
+ public Iterable<ProteinClass> findAllProteinClasses()
+ {
+ return finder().findAll();
+ }
+
+ public long countProteinClasses()
+ {
+ return finder().count();
+ }
+
+ public ProteinClass findProteinClassNamed( String name )
+ {
+ return finder().findByPropertyValue( null, "name", name );
+ }
+}
@@ -0,0 +1,8 @@
+package org.neo4j.spring.example.proteins;
+
+import org.neo4j.graphdb.RelationshipType;
+
+public enum ProteinRelationships implements RelationshipType
+{
+ SUBCLASS_OF, BELONGS_TO_CLASS, INTERACTS_WITH_CLASS
+}
@@ -0,0 +1,47 @@
+package org.neo4j.spring.example.proteins;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.neo4j.graphdb.GraphDatabaseService;
+import org.neo4j.kernel.Config;
+import org.neo4j.kernel.EmbeddedGraphDatabase;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.graph.neo4j.config.AbstractNeo4jConfiguration;
+
+/**
+ * A custom Neo4jConfiguration
+ */
+@Configuration
+public class ProteinsNeo4jConfiguration extends AbstractNeo4jConfiguration
+{
+ private static String storeDir = "target/neo4j-proteins";
+
+ @Override
+ public boolean isUsingCrossStorePersistence() {
+ return false;
+ }
+
+ // ABKNOTE
+ // - @Bean duplicated in overridden parent method. why both places?
+ // - destroyMethod does not seem to do anything.
+ //@Bean(destroyMethod = "shutdown")
+
+// @Bean
+// String someBean() {
+// return "DUMMY";
+// }
+
+ @Override
+ public GraphDatabaseService graphDatabaseService() {
+ Map<String, String> config = new HashMap<String, String>();
+ config.put( Config.TXMANAGER_IMPLEMENTATION, "spring-jta" );
+ return new EmbeddedGraphDatabase( storeDir, config );
+ }
+
+ public String getStoreDir()
+ {
+ return storeDir;
+ }
+}
+
Oops, something went wrong.

0 comments on commit d3ea97d

Please sign in to comment.