Biblioteca Java - Blame information for rev 18
Subversion Repositories:
(root)/Frameworks and Technologies/Neo4J Samples/Neo4JTutorial/src/main/java/com/linkscreens/graphsin/repository/PersonRepository.java
Rev | Author | Line No. | Line |
---|---|---|---|
18 | mihai | 1 | /** |
2 | * Licensed to Neo Technology under one or more contributor | ||
3 | * license agreements. See the NOTICE file distributed with | ||
4 | * this work for additional information regarding copyright | ||
5 | * ownership. Neo Technology licenses this file to you under | ||
6 | * the Apache License, Version 2.0 (the "License"); you may | ||
7 | * not use this file except in compliance with the License. | ||
8 | * You may obtain a copy of the License at | ||
9 | * | ||
10 | * http://www.apache.org/licenses/LICENSE-2.0 | ||
11 | * | ||
12 | * Unless required by applicable law or agreed to in writing, | ||
13 | * software distributed under the License is distributed on an | ||
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
15 | * KIND, either express or implied. See the License for the | ||
16 | * specific language governing permissions and limitations | ||
17 | * under the License. | ||
18 | */ | ||
19 | package com.linkscreens.graphsin.repository; | ||
20 | |||
21 | import java.util.ArrayList; | ||
22 | |||
23 | import org.neo4j.graphdb.Direction; | ||
24 | import org.neo4j.graphdb.GraphDatabaseService; | ||
25 | import org.neo4j.graphdb.Node; | ||
26 | import org.neo4j.graphdb.Relationship; | ||
27 | import org.neo4j.graphdb.index.Index; | ||
28 | import org.neo4j.graphdb.index.IndexHits; | ||
29 | import org.neo4j.helpers.collection.IterableWrapper; | ||
30 | |||
31 | public class PersonRepository | ||
32 | { | ||
33 | private final GraphDatabaseService graphDb; | ||
34 | private final Index<Node> index; | ||
35 | |||
36 | public PersonRepository( GraphDatabaseService graphDb, Index<Node> index ) | ||
37 | { | ||
38 | this.graphDb = graphDb; | ||
39 | this.index = index; | ||
40 | } | ||
41 | |||
42 | public PersonNode createPerson( String name ) throws Exception | ||
43 | { | ||
44 | Node newPersonNode = graphDb.createNode(); | ||
45 | Node alreadyExist = index.get( PersonNode.EMAIL, name ).getSingle(); | ||
46 | if ( alreadyExist != null ) | ||
47 | { | ||
48 | throw new Exception( "Person with this name already exists " ); | ||
49 | } | ||
50 | newPersonNode.setProperty( PersonNode.EMAIL, name ); | ||
51 | index.add( newPersonNode, PersonNode.EMAIL, name ); | ||
52 | return new PersonNode( newPersonNode ); | ||
53 | } | ||
54 | |||
55 | |||
56 | public PersonNode getPersonByEmail( String name ) | ||
57 | { | ||
58 | Node personNode = index.get( PersonNode.EMAIL, name ).getSingle(); | ||
59 | if ( personNode == null ) | ||
60 | { | ||
61 | throw new IllegalArgumentException( "Person[" + name | ||
62 | + "] not found" ); | ||
63 | } | ||
64 | return new PersonNode( personNode ); | ||
65 | } | ||
66 | |||
67 | public void deletePerson( PersonNode person ) | ||
68 | { | ||
69 | Node personNode = person.getUnderlyingNode(); | ||
70 | index.remove( personNode, PersonNode.EMAIL, person.getName() ); | ||
71 | for ( PersonNode friend : person.getFriends() ) | ||
72 | { | ||
73 | person.removeFriend( friend ); | ||
74 | } | ||
75 | personNode.getSingleRelationship( RelTypes.A_PERSON, Direction.INCOMING ).delete(); | ||
76 | |||
77 | for ( StatusUpdateNode status : person.getStatus() ) | ||
78 | { | ||
79 | Node statusNode = status.getUnderlyingNode(); | ||
80 | for ( Relationship r : statusNode.getRelationships() ) | ||
81 | { | ||
82 | r.delete(); | ||
83 | } | ||
84 | statusNode.delete(); | ||
85 | } | ||
86 | |||
87 | personNode.delete(); | ||
88 | } | ||
89 | |||
90 | /** | ||
91 | * Updated to retrevive all nodes based on index and not based on reference node which is deprecated because of performance | ||
92 | * issues. See: http://stackoverflow.com/questions/15088616/is-concept-of-reference-node-in-neo4j-still-used-or-deprecated | ||
93 | * @return | ||
94 | */ | ||
95 | |||
96 | public Iterable<PersonNode> getAllPersons() | ||
97 | { | ||
98 | |||
99 | IndexHits<Node> all = index.query("*:*"); | ||
100 | ArrayList<PersonNode> list = new ArrayList(); | ||
101 | while(all.hasNext()) | ||
102 | list.add(new PersonNode(all.next())); | ||
103 | return list; | ||
104 | } | ||
105 | } |