|
1 | | -/** |
2 | | - * Copyright (c) 2002-2012 "Neo Technology," |
3 | | - * Network Engine for Objects in Lund AB [http://neotechnology.com] |
4 | | - * |
5 | | - * This file is part of Neo4j. |
6 | | - * |
7 | | - * Neo4j is free software: you can redistribute it and/or modify |
8 | | - * it under the terms of the GNU General Public License as published by |
9 | | - * the Free Software Foundation, either version 3 of the License, or |
10 | | - * (at your option) any later version. |
11 | | - * |
12 | | - * This program is distributed in the hope that it will be useful, |
13 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | - * GNU General Public License for more details. |
16 | | - * |
17 | | - * You should have received a copy of the GNU General Public License |
18 | | - * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | | - */ |
20 | | -package org.neo4j.rest.graphdb.traversal; |
21 | | - |
22 | | -import java.util.Collection; |
23 | | -import java.util.List; |
24 | | -import java.util.Map; |
25 | | - |
26 | | -import org.neo4j.graphdb.Node; |
27 | | -import org.neo4j.graphdb.Path; |
28 | | -import org.neo4j.graphdb.Relationship; |
29 | | -import org.neo4j.helpers.collection.IterableWrapper; |
30 | | -import org.neo4j.rest.graphdb.RequestResult; |
31 | | -import org.neo4j.rest.graphdb.RestAPI; |
32 | | -import org.neo4j.rest.graphdb.converter.RestResultConverter; |
33 | | -import org.neo4j.rest.graphdb.converter.TypeInformation; |
34 | | -import org.neo4j.rest.graphdb.entity.RestNode; |
35 | | -import org.neo4j.rest.graphdb.entity.RestRelationship; |
36 | | - |
37 | | -import static org.neo4j.helpers.collection.IteratorUtil.lastOrNull; |
38 | | - |
39 | | -/** |
40 | | - * @author Michael Hunger |
41 | | - * @since 03.02.11 |
42 | | - */ |
43 | | -public class RestPathParser implements RestResultConverter { |
44 | | - |
45 | | - private RestAPI restAPI; |
46 | | - |
47 | | - public RestPathParser(RestAPI restAPI) { |
48 | | - this.restAPI = restAPI; |
49 | | - } |
50 | | - |
51 | | - public static Path parse(Map path, final RestAPI restApi) { |
52 | | - TypeInformation typeInfo = new TypeInformation(path.get("nodes")); |
53 | | - RestPathParser restPathParser = new RestPathParser(restApi); |
54 | | - if (restPathParser.isFullPath(typeInfo)){ |
55 | | - return restPathParser.parseFullPath(path, restApi); |
56 | | - } |
57 | | - if (restPathParser.isPath(typeInfo)){ |
58 | | - return restPathParser.parsePath(path, restApi); |
59 | | - } |
60 | | - |
61 | | - throw new IllegalArgumentException("params map contained illegal type "+typeInfo.getGenericArguments()[0]); |
62 | | - } |
63 | | - |
64 | | - private boolean isPath(TypeInformation typeInfo) { |
65 | | - return typeInfo.getGenericArguments()[0].equals(String.class); |
66 | | - } |
67 | | - |
68 | | - private boolean isFullPath(TypeInformation typeInfo) { |
69 | | - return Map.class.isAssignableFrom(typeInfo.getGenericArguments()[0]); |
70 | | - } |
71 | | - |
72 | | - |
73 | | - private Path parseFullPath(Map path, final RestAPI restApi) { |
74 | | - final Collection<Map<?, ?>> nodesData = (Collection<Map<?, ?>>) path.get("nodes"); |
75 | | - final Collection<Map<?, ?>> relationshipsData = (Collection<Map<?, ?>>) path.get("relationships"); |
76 | | - final Map<?, ?> lastRelationshipData = lastMap(relationshipsData); |
77 | | - final Map<?, ?> startData = (Map<?, ?>) path.get("start"); |
78 | | - final Map<?, ?> endData = (Map<?, ?>) path.get("end"); |
79 | | - final Integer length = (Integer) path.get("length"); |
80 | | - |
81 | | - return new SimplePath( |
82 | | - new RestNode(startData,restApi), |
83 | | - new RestNode(endData,restApi), |
84 | | - new RestRelationship(lastRelationshipData,restApi), |
85 | | - length, |
86 | | - new IterableWrapper<Node, Map<?,?>>(nodesData) { |
87 | | - @Override |
88 | | - protected Node underlyingObjectToObject(Map<?, ?> data) { |
89 | | - return new RestNode(data,restApi); |
90 | | - } |
91 | | - }, |
92 | | - new IterableWrapper<Relationship, Map<?,?>>(relationshipsData) { |
93 | | - @Override |
94 | | - protected Relationship underlyingObjectToObject(Map<?, ?> data) { |
95 | | - return new RestRelationship(data,restApi); |
96 | | - } |
97 | | - }); |
98 | | - } |
99 | | - |
100 | | - private Path parsePath(Map path, final RestAPI restApi){ |
101 | | - final Collection<String> nodesData = (Collection<String>) path.get("nodes"); |
102 | | - final Collection<String> relationshipsData = (Collection<String>) path.get("relationships"); |
103 | | - final String lastRelationshipData = lastElement(relationshipsData); |
104 | | - final String startData = (String) path.get("start"); |
105 | | - final String endData = (String) path.get("end"); |
106 | | - final Integer length = (Integer) path.get("length"); |
107 | | - return new SimplePath( |
108 | | - new RestNode(startData,restApi), |
109 | | - new RestNode(endData,restApi), |
110 | | - new RestRelationship(lastRelationshipData,restApi), |
111 | | - length, |
112 | | - new IterableWrapper<Node, String>(nodesData) { |
113 | | - @Override |
114 | | - protected Node underlyingObjectToObject(String data) { |
115 | | - return new RestNode(data,restApi); |
116 | | - } |
117 | | - }, |
118 | | - new IterableWrapper<Relationship, String>(relationshipsData) { |
119 | | - @Override |
120 | | - protected Relationship underlyingObjectToObject(String data) { |
121 | | - return new RestRelationship(data,restApi); |
122 | | - } |
123 | | - }); |
124 | | - } |
125 | | - |
126 | | - private String lastElement(Collection<String> collection){ |
127 | | - return lastOrNull(collection); |
128 | | - } |
129 | | - |
130 | | - private Map<?, ?> lastMap(Collection<Map<?, ?>> collection) { |
131 | | - if (collection.isEmpty()) return null; |
132 | | - if (collection instanceof List) { |
133 | | - List<Map<?,?>> list = (List<Map<?,?>>) collection; |
134 | | - return list.get(list.size()-1); |
135 | | - } |
136 | | - Map<?, ?> result = null; |
137 | | - for (Map<?, ?> value : collection) { |
138 | | - result=value; |
139 | | - } |
140 | | - return result; |
141 | | - } |
142 | | - |
143 | | - @Override |
144 | | - public Object convertFromRepresentation(RequestResult value) { |
145 | | - return parse(value.toMap(), this.restAPI); |
146 | | - } |
147 | | -} |
| 1 | +/** |
| 2 | + * Copyright (c) 2002-2012 "Neo Technology," |
| 3 | + * Network Engine for Objects in Lund AB [http://neotechnology.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Neo4j is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | +package org.neo4j.rest.graphdb.traversal; |
| 21 | + |
| 22 | +import java.util.Collection; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | + |
| 26 | +import org.neo4j.graphdb.Node; |
| 27 | +import org.neo4j.graphdb.Path; |
| 28 | +import org.neo4j.graphdb.Relationship; |
| 29 | +import org.neo4j.helpers.collection.IterableWrapper; |
| 30 | +import org.neo4j.helpers.collection.IteratorUtil; |
| 31 | +import org.neo4j.rest.graphdb.RequestResult; |
| 32 | +import org.neo4j.rest.graphdb.RestAPI; |
| 33 | +import org.neo4j.rest.graphdb.converter.RestResultConverter; |
| 34 | +import org.neo4j.rest.graphdb.converter.TypeInformation; |
| 35 | +import org.neo4j.rest.graphdb.entity.RestNode; |
| 36 | +import org.neo4j.rest.graphdb.entity.RestRelationship; |
| 37 | + |
| 38 | +/** |
| 39 | + * @author Michael Hunger |
| 40 | + * @since 03.02.11 |
| 41 | + */ |
| 42 | +public class RestPathParser implements RestResultConverter { |
| 43 | + |
| 44 | + private RestAPI restAPI; |
| 45 | + |
| 46 | + public RestPathParser(RestAPI restAPI) { |
| 47 | + this.restAPI = restAPI; |
| 48 | + } |
| 49 | + |
| 50 | + public static Path parse(Map path, final RestAPI restApi) { |
| 51 | + TypeInformation typeInfo = new TypeInformation(path.get("nodes")); |
| 52 | + RestPathParser restPathParser = new RestPathParser(restApi); |
| 53 | + if (restPathParser.isFullPath(typeInfo)){ |
| 54 | + return restPathParser.parseFullPath(path, restApi); |
| 55 | + } |
| 56 | + if (restPathParser.isPath(typeInfo)){ |
| 57 | + return restPathParser.parsePath(path, restApi); |
| 58 | + } |
| 59 | + |
| 60 | + throw new IllegalArgumentException("params map contained illegal type "+typeInfo.getGenericArguments()[0]); |
| 61 | + } |
| 62 | + |
| 63 | + private boolean isPath(TypeInformation typeInfo) { |
| 64 | + return typeInfo.getGenericArguments()[0].equals(String.class); |
| 65 | + } |
| 66 | + |
| 67 | + private boolean isFullPath(TypeInformation typeInfo) { |
| 68 | + return Map.class.isAssignableFrom(typeInfo.getGenericArguments()[0]); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + private Path parseFullPath(Map path, final RestAPI restApi) { |
| 73 | + final Collection<Map<?, ?>> nodesData = (Collection<Map<?, ?>>) path.get("nodes"); |
| 74 | + final Collection<Map<?, ?>> relationshipsData = (Collection<Map<?, ?>>) path.get("relationships"); |
| 75 | + final Map<?, ?> lastRelationshipData = lastMap(relationshipsData); |
| 76 | + final Map<?, ?> startData = (Map<?, ?>) path.get("start"); |
| 77 | + final Map<?, ?> endData = (Map<?, ?>) path.get("end"); |
| 78 | + final Integer length = (Integer) path.get("length"); |
| 79 | + |
| 80 | + RestRelationship lastRelationship = lastRelationshipData == null ? null : new RestRelationship(lastRelationshipData, restApi); |
| 81 | + return new SimplePath( |
| 82 | + new RestNode(startData,restApi), |
| 83 | + new RestNode(endData,restApi), |
| 84 | + lastRelationship, |
| 85 | + length, |
| 86 | + new IterableWrapper<Node, Map<?,?>>(nodesData) { |
| 87 | + @Override |
| 88 | + protected Node underlyingObjectToObject(Map<?, ?> data) { |
| 89 | + return new RestNode(data,restApi); |
| 90 | + } |
| 91 | + }, |
| 92 | + new IterableWrapper<Relationship, Map<?,?>>(relationshipsData) { |
| 93 | + @Override |
| 94 | + protected Relationship underlyingObjectToObject(Map<?, ?> data) { |
| 95 | + return new RestRelationship(data,restApi); |
| 96 | + } |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + private Path parsePath(Map path, final RestAPI restApi){ |
| 101 | + final Collection<String> nodesData = (Collection<String>) path.get("nodes"); |
| 102 | + final Collection<String> relationshipsData = (Collection<String>) path.get("relationships"); |
| 103 | + final String lastRelationshipData = lastElement(relationshipsData); |
| 104 | + final String startData = (String) path.get("start"); |
| 105 | + final String endData = (String) path.get("end"); |
| 106 | + final Integer length = (Integer) path.get("length"); |
| 107 | + RestRelationship lastRelationship = lastRelationshipData == null ? null : new RestRelationship(lastRelationshipData, restApi); |
| 108 | + return new SimplePath( |
| 109 | + new RestNode(startData,restApi), |
| 110 | + new RestNode(endData,restApi), |
| 111 | + lastRelationship, |
| 112 | + length, |
| 113 | + new IterableWrapper<Node, String>(nodesData) { |
| 114 | + @Override |
| 115 | + protected Node underlyingObjectToObject(String data) { |
| 116 | + return new RestNode(data,restApi); |
| 117 | + } |
| 118 | + }, |
| 119 | + new IterableWrapper<Relationship, String>(relationshipsData) { |
| 120 | + @Override |
| 121 | + protected Relationship underlyingObjectToObject(String data) { |
| 122 | + return new RestRelationship(data,restApi); |
| 123 | + } |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + private String lastElement(Collection<String> collection){ |
| 128 | + return IteratorUtil.lastOrNull(collection); |
| 129 | + } |
| 130 | + |
| 131 | + private Map<?, ?> lastMap(Collection<Map<?, ?>> collection) { |
| 132 | + if (collection.isEmpty()) return null; |
| 133 | + if (collection instanceof List) { |
| 134 | + List<Map<?,?>> list = (List<Map<?,?>>) collection; |
| 135 | + return list.get(list.size()-1); |
| 136 | + } |
| 137 | + Map<?, ?> result = null; |
| 138 | + for (Map<?, ?> value : collection) { |
| 139 | + result=value; |
| 140 | + } |
| 141 | + return result; |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public Object convertFromRepresentation(RequestResult value) { |
| 146 | + return parse(value.toMap(), this.restAPI); |
| 147 | + } |
| 148 | +} |
0 commit comments