EMlab-generation Documentation  1.0
Documentation of the EMLab-Generation model.
GenericRepository.java
1 /*******************************************************************************
2  * Copyright 2012 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 package emlab.gen.repository;
17 
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.NoSuchElementException;
21 import java.util.Random;
22 
23 import javax.script.ScriptEngine;
24 
25 import org.neo4j.graphdb.traversal.TraversalDescription;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.data.neo4j.aspects.core.NodeBacked;
30 import org.springframework.data.neo4j.repository.GraphRepository;
31 import org.springframework.data.neo4j.support.Neo4jTemplate;
32 import org.springframework.stereotype.Repository;
33 
34 import com.tinkerpop.blueprints.pgm.Vertex;
35 import com.tinkerpop.blueprints.pgm.impls.neo4j.Neo4jGraph;
36 import com.tinkerpop.pipes.AbstractPipe;
37 import com.tinkerpop.pipes.Pipe;
38 import com.tinkerpop.pipes.util.Pipeline;
39 import com.tinkerpop.pipes.util.SingleIterator;
40 
42 
43 @Repository
44 public class GenericRepository {
45 
46  Logger logger = LoggerFactory.getLogger(GenericRepository.class);;
47 
48  ScriptEngine engine = null;
49 
50  @Autowired
51  Neo4jTemplate template;
52 
53  private <T extends NodeBacked> GraphRepository<T> finder(Class<T> clazz) {
54  return this.template.repositoryFor(clazz);
55  }
56 
57  public <T extends NodeBacked> Iterable<T> findAllByPropertyValue(Class<T> clazz, String property, Object value) {
58  return finder(clazz).findAllByPropertyValue(property, value);
59  }
60 
61  public <T extends NodeBacked> T findByPropertyValue(Class<T> clazz, String property, Object value) {
62  return finder(clazz).findByPropertyValue(property, value);
63  }
64 
65  public <T extends NodeBacked> Iterable<T> findAll(Class<T> clazz) {
66  return finder(clazz).findAll();
67  }
68 
69  public <T extends NodeBacked> Iterable<T> findAllAtRandom(Class<T> clazz) {
70  List<T> list = Utils.asList(findAll(clazz));
71  Collections.shuffle(list, new Random());
72  return list;
73  }
74 
75  public <T extends NodeBacked> T findOneAtRandom(Class<T> clazz) {
76  List<T> list = Utils.asList(findAll(clazz));
77  Collections.shuffle(list, new Random());
78  return list.get(0);
79  }
80 
81  public <T extends NodeBacked> T findFirst(Class<T> clazz) {
82  if (finder(clazz).findAll().iterator().hasNext()) {
83  return finder(clazz).findAll().iterator().next();
84  } else {
85  return null;
86  }
87  }
88 
89  public <T extends NodeBacked> T findById(Class<T> clazz, long id) {
90  return finder(clazz).findOne(id);
91  }
92 
93  public <T extends NodeBacked, E extends NodeBacked> Iterable<T> findAllByTraversal(Class<T> clazz, E startNode,
94  TraversalDescription traversalDescription) {
95  return finder(clazz).findAllByTraversal(startNode, traversalDescription);
96  }
97 
98  public <T extends NodeBacked, E extends NodeBacked> Iterable<T> findAllByPipe(Class<T> clazz, E startNode, Pipe<Vertex, Vertex> pipe) {
99  Vertex startVertex = getVertex(startNode);
100  Pipe<Vertex, T> typed = new MappingPipe<T>(clazz);
101  Pipe<Vertex, T> emit = new Pipeline<Vertex, T>(pipe, typed);
102  emit.setStarts(new SingleIterator<Vertex>(startVertex));
103  return emit;
104  }
105 
106  public <T extends NodeBacked> Vertex getVertex(T e) {
107  return (new Neo4jGraph(template.getGraphDatabaseService())).getVertex(e.getNodeId());
108  }
109 
110  class MappingPipe<T extends NodeBacked> extends AbstractPipe<Vertex, T> implements Pipe<Vertex, T> {
111 
112  Class<T> genericClass;
113 
114  public MappingPipe(Class<T> clazz) {
115  super();
116  genericClass = clazz;
117  }
118 
119  @Override
120  protected T processNextStart() throws NoSuchElementException {
121  Vertex v = this.starts.next();
122  return findById(genericClass, (Long) v.getId());
123  }
124 
125  }
126 
127 }