2020
2121package com .arangodb .springframework .repository .query ;
2222
23- import java .lang .reflect .Method ;
2423import java .util .Collection ;
25- import java .util .HashMap ;
2624import java .util .LinkedList ;
2725import java .util .List ;
28- import java .util .Map ;
2926import java .util .Optional ;
3027import java .util .Set ;
3128import java .util .Spliterators ;
3229import java .util .stream .Collectors ;
3330import java .util .stream .StreamSupport ;
3431
35- import com .arangodb .springframework .core .mapping .TransactionMappingContext ;
3632import com .fasterxml .jackson .databind .JsonNode ;
3733import org .springframework .data .domain .Page ;
3834import org .springframework .data .domain .PageImpl ;
@@ -66,29 +62,6 @@ public class ArangoResultConverter {
6662 private final ArangoOperations operations ;
6763 private final Class <?> domainClass ;
6864
69- private static Map <Object , Method > TYPE_MAP = new HashMap <>();
70-
71- /**
72- * Build static map of all supported return types and the method used to convert them
73- */
74- static {
75- try {
76- TYPE_MAP .put (List .class , ArangoResultConverter .class .getMethod ("convertList" ));
77- TYPE_MAP .put (Iterable .class , ArangoResultConverter .class .getMethod ("convertList" ));
78- TYPE_MAP .put (Collection .class , ArangoResultConverter .class .getMethod ("convertList" ));
79- TYPE_MAP .put (Page .class , ArangoResultConverter .class .getMethod ("convertPage" ));
80- TYPE_MAP .put (Slice .class , ArangoResultConverter .class .getMethod ("convertPage" ));
81- TYPE_MAP .put (Set .class , ArangoResultConverter .class .getMethod ("convertSet" ));
82- TYPE_MAP .put (ArangoCursor .class , ArangoResultConverter .class .getMethod ("convertArangoCursor" ));
83- TYPE_MAP .put (GeoResult .class , ArangoResultConverter .class .getMethod ("convertGeoResult" ));
84- TYPE_MAP .put (GeoResults .class , ArangoResultConverter .class .getMethod ("convertGeoResults" ));
85- TYPE_MAP .put (GeoPage .class , ArangoResultConverter .class .getMethod ("convertGeoPage" ));
86- TYPE_MAP .put (Optional .class , ArangoResultConverter .class .getMethod ("convertOptional" ));
87- TYPE_MAP .put ("array" , ArangoResultConverter .class .getMethod ("convertArray" ));
88- } catch (final NoSuchMethodException e ) {
89- e .printStackTrace ();
90- }
91- }
9265
9366 /**
9467 * @param accessor
@@ -112,18 +85,42 @@ public ArangoResultConverter(final ArangoParameterAccessor accessor, final Arang
11285 */
11386 public Object convertResult (final Class <?> type ) {
11487 try {
115- if (type .isArray ()) {
116- return TYPE_MAP .get ("array" ).invoke (this );
117- }
118- if (!TYPE_MAP .containsKey (type )) {
119- return getNext (result );
120- }
121- return TYPE_MAP .get (type ).invoke (this );
88+ return convert (type );
12289 } catch (final Exception e ) {
12390 throw new MappingException (String .format ("Can't convert result to type %s!" , type .getName ()), e );
12491 }
12592 }
12693
94+ private Object convert (Class <?> type ) {
95+ if (type .isArray ()) {
96+ return convertArray ();
97+ } else if (List .class .equals (type )) {
98+ return convertList ();
99+ } else if (Iterable .class .equals (type )) {
100+ return convertList ();
101+ } else if (Collection .class .equals (type )) {
102+ return convertList ();
103+ } else if (Page .class .equals (type )) {
104+ return convertPage ();
105+ } else if (Slice .class .equals (type )) {
106+ return convertPage ();
107+ } else if (Set .class .equals (type )) {
108+ return convertSet ();
109+ } else if (ArangoCursor .class .equals (type )) {
110+ return convertArangoCursor ();
111+ } else if (GeoResult .class .equals (type )) {
112+ return convertGeoResult ();
113+ } else if (GeoResults .class .equals (type )) {
114+ return convertGeoResults ();
115+ } else if (GeoPage .class .equals (type )) {
116+ return convertGeoPage ();
117+ } else if (Optional .class .equals (type )) {
118+ return convertOptional ();
119+ } else {
120+ return getNext (result );
121+ }
122+ }
123+
127124 /**
128125 * Creates a Set return type from the given cursor
129126 *
@@ -142,20 +139,23 @@ private Set<?> buildSet(final ArangoCursor<?> cursor) {
142139 */
143140 private GeoResult <?> buildGeoResult (final ArangoCursor <?> cursor ) {
144141 GeoResult <?> geoResult = null ;
142+ // FIXME
145143 while (cursor .hasNext () && geoResult == null ) {
146144 final Object object = cursor .next ();
147145 if (!(object instanceof JsonNode )) {
146+ // FIXME
148147 continue ;
149148 }
150149
151150 final JsonNode slice = (JsonNode ) object ;
152151 final JsonNode distSlice = slice .get ("_distance" );
153152 final Double distanceInMeters = distSlice .isDouble () ? distSlice .doubleValue () : null ;
154153 if (distanceInMeters == null ) {
154+ // FIXME
155155 continue ;
156156 }
157157
158- final Object entity = operations .getConverter ().read (domainClass , slice , TransactionMappingContext . EMPTY );
158+ final Object entity = operations .getConverter ().read (domainClass , slice );
159159 final Distance distance = new Distance (distanceInMeters / 1000 , Metrics .KILOMETERS );
160160 geoResult = new GeoResult <>(entity , distance );
161161 }
@@ -170,17 +170,19 @@ private GeoResult<?> buildGeoResult(final ArangoCursor<?> cursor) {
170170 */
171171 private GeoResult <?> buildGeoResult (final Object object ) {
172172 if (object == null || !(object instanceof JsonNode )) {
173+ // FIXME
173174 return null ;
174175 }
175176
176177 final JsonNode slice = (JsonNode ) object ;
177178 final JsonNode distSlice = slice .get ("_distance" );
178179 final Double distanceInMeters = distSlice .isDouble () ? distSlice .doubleValue () : null ;
179180 if (distanceInMeters == null ) {
181+ // FIXME
180182 return null ;
181183 }
182184
183- final Object entity = operations .getConverter ().read (domainClass , slice , TransactionMappingContext . EMPTY );
185+ final Object entity = operations .getConverter ().read (domainClass , slice );
184186 final Distance distance = new Distance (distanceInMeters / 1000 , Metrics .KILOMETERS );
185187 return new GeoResult <>(entity , distance );
186188 }
0 commit comments