/src/main/java/org/neo4j/tutorial/CharacterBuilder.java

https://github.com/klauern/neo4j-tutorial · Java · 318 lines · 272 code · 46 blank · 0 comment · 34 complexity · eb82f78c70004515574849e5aa6ed2b2 MD5 · raw file

  1. package org.neo4j.tutorial;
  2. import static org.neo4j.tutorial.DatabaseHelper.ensureRelationshipInDb;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.HashSet;
  6. import java.util.List;
  7. import java.util.Map;
  8. import org.neo4j.graphdb.GraphDatabaseService;
  9. import org.neo4j.graphdb.Node;
  10. public class CharacterBuilder
  11. {
  12. private final String characterName;
  13. private HashSet<String> species;
  14. private boolean companion = false;
  15. private String[] loverNames;
  16. private String planet;
  17. private String[] things;
  18. private boolean enemy;
  19. private boolean ally;
  20. private ArrayList<String> actors = new ArrayList<String>();
  21. private HashMap<String, Integer> startDates = new HashMap<String, Integer>();
  22. public static CharacterBuilder character( String characterName )
  23. {
  24. return new CharacterBuilder( characterName );
  25. }
  26. public CharacterBuilder( String characterName )
  27. {
  28. this.characterName = characterName;
  29. }
  30. public CharacterBuilder isA( String speciesString )
  31. {
  32. if ( species == null )
  33. {
  34. species = new HashSet<String>();
  35. }
  36. species.add( speciesString );
  37. return this;
  38. }
  39. public CharacterBuilder isCompanion()
  40. {
  41. companion = true;
  42. return this;
  43. }
  44. public void fact( GraphDatabaseService db )
  45. {
  46. Node characterNode = ensureCharacterIsInDb( characterName, db );
  47. Node theDoctor = db.index()
  48. .forNodes( "characters" )
  49. .get( "character", "Doctor" )
  50. .getSingle();
  51. if ( species != null )
  52. {
  53. for ( String speciesString : species )
  54. {
  55. ensureRelationshipInDb( characterNode, DoctorWhoRelationships.IS_A,
  56. SpeciesBuilder.ensureSpeciesInDb( speciesString, db ) );
  57. }
  58. }
  59. if ( companion )
  60. {
  61. ensureCompanionRelationshipInDb( characterNode, db );
  62. }
  63. if ( enemy )
  64. {
  65. ensureEnemyOfRelationshipInDb( characterNode, db );
  66. }
  67. if ( ally )
  68. {
  69. ensureRelationshipInDb( characterNode, DoctorWhoRelationships.ALLY_OF, theDoctor );
  70. }
  71. if ( loverNames != null )
  72. {
  73. ensureLoversInDb( characterNode, loverNames, db );
  74. }
  75. if ( planet != null )
  76. {
  77. ensurePlanetInDb( characterNode, planet, db );
  78. }
  79. if ( things != null )
  80. {
  81. ensureThingsInDb( characterNode, things, db );
  82. }
  83. if ( actors != null )
  84. {
  85. ensureActorsInDb( characterNode, actors, db );
  86. }
  87. }
  88. public static void ensureAllyOfRelationshipInDb( Node allyNode, GraphDatabaseService db )
  89. {
  90. Node theDoctor = db.index()
  91. .forNodes( "characters" )
  92. .get( "character", "Doctor" )
  93. .getSingle();
  94. ensureRelationshipInDb( allyNode, DoctorWhoRelationships.ALLY_OF, theDoctor );
  95. ensureRelationshipInDb( theDoctor, DoctorWhoRelationships.ALLY_OF, allyNode );
  96. }
  97. public static void ensureEnemyOfRelationshipInDb( Node enemyNode, GraphDatabaseService db )
  98. {
  99. Node theDoctor = db.index()
  100. .forNodes( "characters" )
  101. .get( "character", "Doctor" )
  102. .getSingle();
  103. ensureRelationshipInDb( enemyNode, DoctorWhoRelationships.ENEMY_OF, theDoctor );
  104. ensureRelationshipInDb( theDoctor, DoctorWhoRelationships.ENEMY_OF, enemyNode );
  105. }
  106. public static void ensureCompanionRelationshipInDb( Node companionNode, GraphDatabaseService db )
  107. {
  108. Node theDoctor = db.index()
  109. .forNodes( "characters" )
  110. .get( "character", "Doctor" )
  111. .getSingle();
  112. ensureRelationshipInDb( companionNode, DoctorWhoRelationships.COMPANION_OF, theDoctor );
  113. }
  114. public void ensureActorsInDb( Node characterNode, List<String> actors, GraphDatabaseService db )
  115. {
  116. Node previousActorNode = null;
  117. for ( String actor : actors )
  118. {
  119. Node theActorNode = db.index()
  120. .forNodes( "actors" )
  121. .get( "actor", actor )
  122. .getSingle();
  123. if ( theActorNode == null )
  124. {
  125. theActorNode = db.createNode();
  126. theActorNode.setProperty( "actor", actor );
  127. db.index()
  128. .forNodes( "actors" )
  129. .add( theActorNode, "actor", actor );
  130. }
  131. ensureRelationshipInDb( theActorNode, DoctorWhoRelationships.PLAYED, characterNode );
  132. db.index()
  133. .forNodes( "actors" )
  134. .add( theActorNode, "actor", actor );
  135. if ( previousActorNode != null )
  136. {
  137. ensureRelationshipInDb( previousActorNode, DoctorWhoRelationships.REGENERATED_TO, theActorNode,
  138. map( "year", startDates.get( actor ) ) );
  139. }
  140. previousActorNode = theActorNode;
  141. }
  142. }
  143. private Map<String, Object> map( String key, Integer value )
  144. {
  145. HashMap<String, Object> result = new HashMap<String, Object>();
  146. if ( value != null )
  147. {
  148. result.put( key, value );
  149. }
  150. return result;
  151. }
  152. private static void ensureThingsInDb( Node characterNode, String[] things, GraphDatabaseService db )
  153. {
  154. for ( String thing : things )
  155. {
  156. ensureRelationshipInDb( characterNode, DoctorWhoRelationships.OWNS, ensureThingInDb( thing, db ) );
  157. }
  158. }
  159. private static Node ensureThingInDb( String thing, GraphDatabaseService database )
  160. {
  161. Node theThingNode = database.index()
  162. .forNodes( "things" )
  163. .get( "thing", thing )
  164. .getSingle();
  165. if ( theThingNode == null )
  166. {
  167. theThingNode = database.createNode();
  168. theThingNode.setProperty( "thing", thing );
  169. ensureThingIsIndexed( theThingNode, database );
  170. }
  171. return theThingNode;
  172. }
  173. private static void ensureThingIsIndexed( Node thingNode, GraphDatabaseService database )
  174. {
  175. database.index()
  176. .forNodes( "things" )
  177. .add( thingNode, "thing", thingNode.getProperty( "thing" ) );
  178. }
  179. private static Node ensurePlanetInDb( Node characterNode, String planet, GraphDatabaseService database )
  180. {
  181. Node thePlanetNode = database.index()
  182. .forNodes( "planets" )
  183. .get( "planet", planet )
  184. .getSingle();
  185. if ( thePlanetNode == null )
  186. {
  187. thePlanetNode = database.createNode();
  188. thePlanetNode.setProperty( "planet", planet );
  189. ensurePlanetIsIndexed( thePlanetNode, database );
  190. }
  191. ensureRelationshipInDb( characterNode, DoctorWhoRelationships.COMES_FROM, thePlanetNode );
  192. return thePlanetNode;
  193. }
  194. private static void ensurePlanetIsIndexed( Node thePlanetNode, GraphDatabaseService database )
  195. {
  196. database.index()
  197. .forNodes( "planets" )
  198. .add( thePlanetNode, "planet", thePlanetNode.getProperty( "planet" ) );
  199. }
  200. public static Node ensureCharacterIsInDb( String name, GraphDatabaseService db )
  201. {
  202. Node theCharacterNode = db.index()
  203. .forNodes( "characters" )
  204. .get( "character", name )
  205. .getSingle();
  206. if ( theCharacterNode == null )
  207. {
  208. theCharacterNode = db.createNode();
  209. theCharacterNode.setProperty( "character", name );
  210. ensureCharacterIsIndexed( theCharacterNode, db );
  211. }
  212. return theCharacterNode;
  213. }
  214. private static void ensureCharacterIsIndexed( Node characterNode, GraphDatabaseService database )
  215. {
  216. if ( database.index()
  217. .forNodes( "characters" )
  218. .get( "character", characterNode.getProperty( "character" ) )
  219. .getSingle() == null )
  220. {
  221. database.index()
  222. .forNodes( "characters" )
  223. .add( characterNode, "character", characterNode.getProperty( "character" ) );
  224. }
  225. }
  226. private static void ensureLoversInDb( Node characterNode, String[] loverNames, GraphDatabaseService db )
  227. {
  228. for ( String lover : loverNames )
  229. {
  230. ensureRelationshipInDb( characterNode, DoctorWhoRelationships.LOVES, ensureCharacterIsInDb( lover, db ) );
  231. }
  232. }
  233. public CharacterBuilder loves( String... loverNames )
  234. {
  235. this.loverNames = loverNames;
  236. return this;
  237. }
  238. public CharacterBuilder isFrom( String planet )
  239. {
  240. this.planet = planet;
  241. return this;
  242. }
  243. public CharacterBuilder owns( String... things )
  244. {
  245. this.things = things;
  246. return this;
  247. }
  248. public CharacterBuilder isEnemy()
  249. {
  250. this.enemy = true;
  251. return this;
  252. }
  253. public CharacterBuilder isAlly()
  254. {
  255. ally = true;
  256. return this;
  257. }
  258. public CharacterBuilder regeneration( String... actors )
  259. {
  260. for ( String actor : actors )
  261. {
  262. this.actors.add( actor );
  263. }
  264. return this;
  265. }
  266. public CharacterBuilder regeneration( String actor, int year )
  267. {
  268. this.actors.add( actor );
  269. this.startDates.put( actor, new Integer( year ) );
  270. return this;
  271. }
  272. }