1 package eu.ehri.project.core.impl.neo4j;
2
3
4 import com.tinkerpop.blueprints.Edge;
5 import com.tinkerpop.blueprints.Element;
6 import com.tinkerpop.blueprints.Vertex;
7 import com.tinkerpop.blueprints.util.ElementHelper;
8 import org.neo4j.graphdb.Node;
9 import org.neo4j.graphdb.PropertyContainer;
10 import org.neo4j.graphdb.Relationship;
11
12 import java.lang.reflect.Array;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.HashSet;
16 import java.util.Iterator;
17 import java.util.Set;
18
19
20 abstract class Neo4j2Element implements Element {
21
22 protected final Neo4j2Graph graph;
23 protected PropertyContainer rawElement;
24
25 public Neo4j2Element(Neo4j2Graph graph) {
26 this.graph = graph;
27 }
28
29 public <T> T getProperty(String key) {
30 this.graph.autoStartTransaction(false);
31 if (this.rawElement.hasProperty(key))
32 return (T) tryConvertCollectionToArrayList(this.rawElement.getProperty(key));
33 else
34 return null;
35 }
36
37 public void setProperty(String key, Object value) {
38 ElementHelper.validateProperty(this, key, value);
39 this.graph.autoStartTransaction(true);
40
41 this.rawElement.setProperty(key, tryConvertCollectionToArray(value));
42 }
43
44 public <T> T removeProperty(String key) {
45 if (!this.rawElement.hasProperty(key))
46 return null;
47 else {
48 this.graph.autoStartTransaction(true);
49 return (T) this.rawElement.removeProperty(key);
50 }
51 }
52
53 public Set<String> getPropertyKeys() {
54 this.graph.autoStartTransaction(false);
55 Set<String> keys = new HashSet<String>();
56 for (String key : this.rawElement.getPropertyKeys()) {
57 keys.add(key);
58 }
59 return keys;
60 }
61
62 public int hashCode() {
63 return this.getId().hashCode();
64 }
65
66 public PropertyContainer getRawElement() {
67 return this.rawElement;
68 }
69
70 public Object getId() {
71 this.graph.autoStartTransaction(false);
72 if (this.rawElement instanceof Node) {
73 return ((Node) this.rawElement).getId();
74 } else {
75 return ((Relationship) this.rawElement).getId();
76 }
77 }
78
79 public void remove() {
80 if (this instanceof Vertex)
81 this.graph.removeVertex((Vertex) this);
82 else
83 this.graph.removeEdge((Edge) this);
84 }
85
86 public boolean equals(Object object) {
87 return ElementHelper.areEqual(this, object);
88 }
89
90 private Object tryConvertCollectionToArray(Object value) {
91 if (value instanceof Collection<?>) {
92
93
94 try {
95 Collection<?> collection = (Collection<?>) value;
96 Object[] array = null;
97 Iterator<?> objects = collection.iterator();
98 for (int i = 0; objects.hasNext(); i++) {
99 Object object = objects.next();
100 if (array == null) {
101 array = (Object[]) Array.newInstance(object.getClass(), collection.size());
102 }
103
104 array[i] = object;
105 }
106 return array;
107 } catch (ArrayStoreException ase) {
108
109 return value;
110 }
111 } else {
112 return value;
113 }
114 }
115
116 private Object tryConvertCollectionToArrayList(Object value) {
117 if (value.getClass().isArray()) {
118
119 try {
120 ArrayList<Object> list = new ArrayList<Object>();
121 int arrlength = Array.getLength(value);
122 for (int i = 0; i < arrlength; i++) {
123 Object object = Array.get(value, i);
124 list.add(object);
125 }
126 return list;
127 } catch (Exception e) {
128
129 return value;
130 }
131 } else {
132 return value;
133 }
134 }
135 }