Biblioteca Java - Blame information for rev 18
Subversion Repositories:
(root)/Frameworks and Technologies/Neo4J Samples/Neo4JTutorial/src/main/java/com/linkscreens/graphsin/network/SocialNetworkServiceImpl.java
Rev | Author | Line No. | Line |
---|---|---|---|
18 | mihai | 1 | package com.linkscreens.graphsin.network; |
2 | |||
3 | import java.util.ArrayList; | ||
4 | import java.util.Iterator; | ||
5 | |||
6 | import org.neo4j.graphdb.GraphDatabaseService; | ||
7 | import org.neo4j.graphdb.Node; | ||
8 | import org.neo4j.graphdb.Transaction; | ||
9 | import org.neo4j.graphdb.factory.GraphDatabaseFactory; | ||
10 | import org.neo4j.graphdb.index.Index; | ||
11 | import org.neo4j.helpers.collection.IteratorUtil; | ||
12 | |||
13 | import com.linkscreens.graphsin.model.*; | ||
14 | import com.linkscreens.graphsin.repository.AlbumNode; | ||
15 | import com.linkscreens.graphsin.repository.AlbumRepository; | ||
16 | import com.linkscreens.graphsin.repository.CommentNode; | ||
17 | import com.linkscreens.graphsin.repository.MediaItemNode; | ||
18 | import com.linkscreens.graphsin.repository.MediaItemRepository; | ||
19 | import com.linkscreens.graphsin.repository.PersonNode; | ||
20 | import com.linkscreens.graphsin.repository.PersonRepository; | ||
21 | import com.linkscreens.graphsin.repository.StatusUpdateNode; | ||
22 | |||
23 | public class SocialNetworkServiceImpl implements SocialNetworkService { | ||
24 | GraphDatabaseService graphDb; | ||
25 | PersonRepository personRepository; | ||
26 | AlbumRepository albumRepository; | ||
27 | MediaItemRepository mediaRepository; | ||
28 | |||
29 | public SocialNetworkServiceImpl(String src_dir) throws Exception { | ||
30 | setup(src_dir); | ||
31 | } | ||
32 | |||
33 | private void setup(String storeDir) throws Exception | ||
34 | { | ||
35 | this.graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( storeDir ); | ||
36 | try ( Transaction tx = graphDb.beginTx() ) | ||
37 | { | ||
38 | Index<Node> index = graphDb.index().forNodes( "nodes" ); | ||
39 | personRepository = new PersonRepository( graphDb, index ); | ||
40 | |||
41 | Index<Node> indexA = graphDb.index().forNodes( "albums" ); | ||
42 | albumRepository = new AlbumRepository(graphDb, indexA); | ||
43 | |||
44 | Index<Node> indexM = graphDb.index().forNodes( "mediaitems" ); | ||
45 | mediaRepository = new MediaItemRepository(graphDb, indexM); | ||
46 | |||
47 | } | ||
48 | } | ||
49 | |||
50 | /* (non-Javadoc) | ||
51 | * @see com.linkscreens.graphsin.network.SocialNetwork#addStatus(java.lang.String, java.lang.String) | ||
52 | */ | ||
53 | @Override | ||
54 | public void addStatus(String name, String text){ | ||
55 | try ( Transaction tx = graphDb.beginTx() ) | ||
56 | { | ||
57 | PersonNode p = personRepository.getPersonByEmail(name); | ||
58 | p.addStatus(text); | ||
59 | } | ||
60 | } | ||
61 | |||
62 | /* (non-Javadoc) | ||
63 | * @see com.linkscreens.graphsin.network.SocialNetwork#createFriend(java.lang.String, java.lang.String) | ||
64 | */ | ||
65 | @Override | ||
66 | public void createFriend(String name1, String name2){ | ||
67 | try ( Transaction tx = graphDb.beginTx() ) | ||
68 | { | ||
69 | PersonNode p1 = this.personRepository.getPersonByEmail(name1); | ||
70 | PersonNode p2 = this.personRepository.getPersonByEmail(name2); | ||
71 | p1.addFriend(p2); | ||
72 | } | ||
73 | } | ||
74 | |||
75 | /* (non-Javadoc) | ||
76 | * @see com.linkscreens.graphsin.network.SocialNetwork#getFriendRecommandation(java.lang.String) | ||
77 | */ | ||
78 | @Override | ||
79 | public Person getFriendRecommandation(String name){ | ||
80 | Person reco = null; | ||
81 | try ( Transaction tx = graphDb.beginTx() ) | ||
82 | { | ||
83 | PersonNode p = this.personRepository.getPersonByEmail(name); | ||
84 | PersonNode recommendation = IteratorUtil.single( p.getFriendRecommendation( 1 ).iterator() ); | ||
85 | reco = new Person(recommendation.getName()); | ||
86 | } | ||
87 | return reco; | ||
88 | } | ||
89 | |||
90 | /* (non-Javadoc) | ||
91 | * @see com.linkscreens.graphsin.network.SocialNetwork#getFriendsOf(java.lang.String) | ||
92 | */ | ||
93 | @Override | ||
94 | public ArrayList<Person> getFriendsOf(String name){ | ||
95 | |||
96 | ArrayList<Person> friends = new ArrayList<>(); | ||
97 | try ( Transaction tx = graphDb.beginTx() ) | ||
98 | { | ||
99 | PersonNode p = personRepository.getPersonByEmail(name); | ||
100 | for (Iterator iterator = p.getFriends().iterator(); iterator.hasNext();) { | ||
101 | PersonNode person = (PersonNode) iterator.next(); | ||
102 | friends.add(new Person(person.getName())); | ||
103 | } | ||
104 | } | ||
105 | return friends; | ||
106 | } | ||
107 | |||
108 | @Override | ||
109 | public Person getPersonByEMail(String email) { | ||
110 | Person p = null; | ||
111 | try ( Transaction tx = graphDb.beginTx() ) | ||
112 | { | ||
113 | PersonNode node = personRepository.getPersonByEmail(email); | ||
114 | p = new Person(node.getName()); | ||
115 | } | ||
116 | return p; | ||
117 | } | ||
118 | |||
119 | /* (non-Javadoc) | ||
120 | * @see com.linkscreens.graphsin.network.SocialNetwork#getAllPersons() | ||
121 | */ | ||
122 | @Override | ||
123 | public ArrayList<Person> getAllPersons(){ | ||
124 | ArrayList<Person> persons = new ArrayList<>(); | ||
125 | try ( Transaction tx = graphDb.beginTx() ) | ||
126 | { | ||
127 | for (Iterator iterator =personRepository.getAllPersons().iterator(); iterator.hasNext();) { | ||
128 | PersonNode person = (PersonNode) iterator.next(); | ||
129 | persons.add(new Person(person.getName())); | ||
130 | } | ||
131 | } | ||
132 | return persons; | ||
133 | } | ||
134 | |||
135 | /* (non-Javadoc) | ||
136 | * @see com.linkscreens.graphsin.network.SocialNetwork#retrieveStatusUpdates(java.lang.String) | ||
137 | */ | ||
138 | @Override | ||
139 | public ArrayList<StatusUpdate> retrieveFriendsStatusUpdates(String name) throws Exception | ||
140 | { | ||
141 | PersonNode person; | ||
142 | |||
143 | ArrayList<StatusUpdate> updates = new ArrayList<StatusUpdate>(); | ||
144 | try ( Transaction tx = graphDb.beginTx() ) | ||
145 | { | ||
146 | |||
147 | person = this.personRepository.getPersonByEmail(name); | ||
148 | Iterator<StatusUpdateNode> i = person.friendStatuses(); | ||
149 | while(i.hasNext()){ | ||
150 | StatusUpdateNode su = i.next(); | ||
151 | //updates.add(su.getPerson().getName()+":+"+su.getDate().toString()+":"+su.getStatusText()); | ||
152 | updates.add( | ||
153 | new StatusUpdate(su.getStatusText(), su.getDate()) | ||
154 | ); | ||
155 | } | ||
156 | |||
157 | } | ||
158 | |||
159 | return updates; | ||
160 | } | ||
161 | |||
162 | public ArrayList<StatusUpdate> retreivePersonStatusUpdates(String email){ | ||
163 | ArrayList<StatusUpdate> list = new ArrayList<StatusUpdate>(); | ||
164 | try ( Transaction tx = graphDb.beginTx() ) | ||
165 | { | ||
166 | PersonNode person = personRepository.getPersonByEmail(email); | ||
167 | Iterator<StatusUpdateNode> i = person.getStatus().iterator(); | ||
168 | while(i.hasNext()){ | ||
169 | StatusUpdateNode su = i.next(); | ||
170 | StatusUpdate u = new StatusUpdate(su.getStatusText(), su.getDate()); | ||
171 | list.add(u); | ||
172 | } | ||
173 | tx.success(); | ||
174 | } | ||
175 | return list; | ||
176 | } | ||
177 | |||
178 | /* (non-Javadoc) | ||
179 | * @see com.linkscreens.graphsin.network.SocialNetwork#retreivePersonMediaAlbums(java.lang.String) | ||
180 | */ | ||
181 | @Override | ||
182 | public ArrayList<Album> retreivePersonMediaAlbums(String email){ | ||
183 | PersonNode person; | ||
184 | |||
185 | ArrayList<Album> list = new ArrayList<Album>(); | ||
186 | try ( Transaction tx = graphDb.beginTx() ) | ||
187 | { | ||
188 | person = this.personRepository.getPersonByEmail(email); | ||
189 | Iterator<AlbumNode> i = person.getAlbums().iterator(); | ||
190 | while(i.hasNext()){ | ||
191 | AlbumNode su = i.next(); | ||
192 | list.add(new Album(su.getId(),su.getTitle())); | ||
193 | } | ||
194 | } | ||
195 | return list; | ||
196 | |||
197 | } | ||
198 | |||
199 | /* (non-Javadoc) | ||
200 | * @see com.linkscreens.graphsin.network.SocialNetwork#getMediaItems(java.lang.String) | ||
201 | */ | ||
202 | @Override | ||
203 | public ArrayList<MediaItem> getMediaItems(String albumId){ | ||
204 | ArrayList<MediaItem> list = new ArrayList(); | ||
205 | try ( Transaction tx = graphDb.beginTx() ) | ||
206 | { | ||
207 | AlbumNode album = albumRepository.getAlbumById(albumId); | ||
208 | Iterator<MediaItemNode> i = album.getMediaItems().iterator(); | ||
209 | while(i.hasNext()){ | ||
210 | MediaItemNode item = i.next(); | ||
211 | list.add(new MediaItem(item.getId(),item.getDescription())); | ||
212 | } | ||
213 | } | ||
214 | return list; | ||
215 | } | ||
216 | |||
217 | /* (non-Javadoc) | ||
218 | * @see com.linkscreens.graphsin.network.SocialNetwork#getMediaItemComments(java.lang.String) | ||
219 | */ | ||
220 | @Override | ||
221 | public ArrayList<Comment> getMediaItemComments(String itemId){ | ||
222 | ArrayList<Comment> list = new ArrayList(); | ||
223 | try ( Transaction tx = graphDb.beginTx() ) | ||
224 | { | ||
225 | MediaItemNode item = this.mediaRepository.getMediaItemById(itemId); | ||
226 | Iterator<CommentNode> i = item.getComments().iterator(); | ||
227 | while(i.hasNext()){ | ||
228 | CommentNode c = i.next(); | ||
229 | list.add(new Comment(c.getCommentText(),c.getDate())); | ||
230 | } | ||
231 | } | ||
232 | return list; | ||
233 | } | ||
234 | |||
235 | } |