EMlab-generation Documentation  1.0
Documentation of the EMLab-Generation model.
AbstractRepository.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.lang.reflect.ParameterizedType;
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.NoSuchElementException;
22 import java.util.Random;
23 
24 import javax.script.ScriptEngine;
25 
26 import org.apache.log4j.Logger;
27 import org.neo4j.graphdb.traversal.TraversalDescription;
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 
33 import com.tinkerpop.blueprints.pgm.Vertex;
34 import com.tinkerpop.blueprints.pgm.impls.neo4j.Neo4jGraph;
35 import com.tinkerpop.pipes.AbstractPipe;
36 import com.tinkerpop.pipes.Pipe;
37 import com.tinkerpop.pipes.util.Pipeline;
38 import com.tinkerpop.pipes.util.SingleIterator;
39 
41 
42 public abstract class AbstractRepository<T extends NodeBacked> {
43 
44  Logger logger = Logger.getLogger(AbstractRepository.class);
45 
46  ScriptEngine engine = null;
47 
48  @Autowired
49  private Neo4jTemplate template;
50 
51  private GraphRepository<T> finder() {
52  return this.template.repositoryFor(getActualType());
53  }
54 
55  /*
56  * Finder methods
57  */
58 
59  public Iterable<T> findAllByPropertyValue(String property, Object value) {
60  return finder().findAllByPropertyValue(property, value);
61  }
62 
63  public T findByPropertyValue(String property, Object value) {
64  return finder().findByPropertyValue(property, value);
65  }
66 
67  public Iterable<T> findAll() {
68  return finder().findAll();
69  }
70 
71  public Iterable<T> findAllAtRandom() {
72  List<T> list = Utils.asList(findAll());
73  Collections.shuffle(list, new Random());
74  return list;
75  }
76 
77  public T findById(long id) {
78  return finder().findOne(id);
79  }
80 
81  public long count() {
82  return finder().count();
83  }
84 
85  public <E extends NodeBacked> Iterable<T> findAllByTraversal(E startNode, TraversalDescription traversalDescription) {
86  return finder().findAllByTraversal(startNode, traversalDescription);
87  }
88 
89  public <E extends NodeBacked> Iterable<T> findAllByPipe(E startNode, Pipe<Vertex, Vertex> pipe) {
90  Vertex startVertex = getVertex(startNode);
91  Pipe<Vertex, T> typed = new MappingPipe();
92  Pipe<Vertex, T> emit = new Pipeline<Vertex, T>(pipe, typed);
93  emit.setStarts(new SingleIterator<Vertex>(startVertex));
94  return emit;
95  }
96 
97  @SuppressWarnings("unchecked")
98  public Class<T> getActualType() {
99  ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
100  return (Class<T>) parameterizedType.getActualTypeArguments()[0];
101  }
102 
103  public <E extends NodeBacked> Vertex getVertex(E e) {
104  return (new Neo4jGraph(template.getGraphDatabaseService())).getVertex(e.getNodeId());
105  }
106 
107  class MappingPipe extends AbstractPipe<Vertex, T> implements Pipe<Vertex, T> {
108  @Override
109  protected T processNextStart() throws NoSuchElementException {
110  Vertex v = this.starts.next();
111  return finder().findOne((Long) v.getId());
112  }
113  }
114 
115 }