11
11
* Contributors:
12
12
*
13
13
* Otavio Santana
14
+ * Michele Rastelli
14
15
*/
15
16
package org .eclipse .jnosql .databases .arangodb .communication ;
16
17
17
18
18
19
import com .arangodb .ArangoDB ;
19
- import com .arangodb .entity .BaseDocument ;
20
20
import com .arangodb .entity .CollectionEntity ;
21
- import org .eclipse .jnosql .communication .Value ;
21
+ import jakarta .json .Json ;
22
+ import jakarta .json .JsonArray ;
23
+ import jakarta .json .JsonArrayBuilder ;
24
+ import jakarta .json .JsonNumber ;
25
+ import jakarta .json .JsonObject ;
26
+ import jakarta .json .JsonObjectBuilder ;
27
+ import jakarta .json .JsonString ;
28
+ import jakarta .json .JsonValue ;
22
29
import org .eclipse .jnosql .communication .ValueUtil ;
23
30
import org .eclipse .jnosql .communication .semistructured .CommunicationEntity ;
24
31
import org .eclipse .jnosql .communication .semistructured .Element ;
25
32
26
- import java .util .ArrayList ;
27
33
import java .util .Collection ;
28
- import java .util .HashMap ;
34
+ import java .util .Collections ;
29
35
import java .util .List ;
30
36
import java .util .Map ;
31
37
import java .util .Objects ;
32
- import java .util .function .Function ;
33
38
import java .util .logging .Level ;
34
39
import java .util .logging .Logger ;
35
40
36
- import static java .util .Collections .singletonMap ;
37
41
import static java .util .stream .Collectors .toList ;
38
42
import static java .util .stream .StreamSupport .stream ;
39
43
@@ -47,9 +51,6 @@ public final class ArangoDBUtil {
47
51
public static final String REV = "_rev" ;
48
52
private static final Logger LOGGER = Logger .getLogger (ArangoDBUtil .class .getName ());
49
53
50
- private static final Function <Map .Entry <?, ?>, Element > ENTRY_DOCUMENT = entry ->
51
- Element .of (entry .getKey ().toString (), entry .getValue ());
52
-
53
54
private ArangoDBUtil () {
54
55
}
55
56
@@ -71,101 +72,100 @@ public static void checkCollection(String bucketName, ArangoDB arangoDB, String
71
72
List <String > collections = arangoDB .db (bucketName )
72
73
.getCollections ().stream ()
73
74
.map (CollectionEntity ::getName )
74
- .collect ( toList () );
75
+ .toList ();
75
76
if (!collections .contains (namespace )) {
76
77
arangoDB .db (bucketName ).createCollection (namespace );
77
78
}
78
79
}
79
80
81
+ static CommunicationEntity toEntity (JsonObject jsonObject ) {
82
+ List <Element > documents = toDocuments (jsonObject );
80
83
81
- static CommunicationEntity toEntity (BaseDocument document ) {
82
- Map <String , Object > properties = document .getProperties ();
83
- List <Element > documents = properties .keySet ().stream ()
84
- .map (k -> toDocument (k , properties ))
85
- .collect (toList ());
86
-
87
- documents .add (Element .of (KEY , document .getKey ()));
88
- documents .add (Element .of (ID , document .getId ()));
89
- documents .add (Element .of (REV , document .getRevision ()));
90
- String collection = document .getId ().split ("/" )[0 ];
84
+ String id = jsonObject .getString (ID );
85
+ documents .add (Element .of (KEY , jsonObject .getString (KEY )));
86
+ documents .add (Element .of (ID , id ));
87
+ documents .add (Element .of (REV , jsonObject .getString (REV )));
88
+ String collection = id .split ("/" )[0 ];
91
89
return CommunicationEntity .of (collection , documents );
92
90
}
93
91
94
- static BaseDocument getBaseDocument (CommunicationEntity entity ) {
95
- Map <String , Object > map = new HashMap <>();
96
- for (Element document : entity .elements ()) {
97
- if (KEY .equals (document .name ()) && Objects .isNull (document .get ())) {
98
- continue ;
99
- }
100
- map .put (document .name (), convert (document .value ()));
101
- }
102
- return new BaseDocument (map );
92
+ static JsonObject toJsonObject (CommunicationEntity entity ) {
93
+ return toJsonObject (entity .elements ());
103
94
}
104
95
105
- private static Element toDocument (String key , Map <String , Object > properties ) {
106
- Object value = properties .get (key );
107
- if (value instanceof Map map ) {
108
- return Element .of (key , map .keySet ()
109
- .stream ().map (k -> toDocument (k .toString (), map ))
110
- .collect (toList ()));
111
- }
112
- if (isADocumentIterable (value )) {
113
- List <List <Element >> documents = new ArrayList <>();
114
- for (Object object : Iterable .class .cast (value )) {
115
- Map <?, ?> map = Map .class .cast (object );
116
- documents .add (map .entrySet ().stream ().map (ENTRY_DOCUMENT ).collect (toList ()));
117
- }
118
- return Element .of (key , documents );
119
-
120
- }
121
- return Element .of (key , value );
96
+ private static List <Element > toDocuments (JsonObject object ) {
97
+ return object .entrySet ().stream ()
98
+ .map (it -> Element .of (it .getKey (), toDocuments (it .getValue ())))
99
+ .collect (toList ());
122
100
}
123
101
124
- private static boolean isADocumentIterable ( Object value ) {
125
- return Iterable . class . isInstance ( value ) &&
126
- stream ( Iterable . class . cast ( value ). spliterator (), false )
127
- . allMatch ( Map . class :: isInstance );
102
+ private static List <?> toDocuments ( JsonArray array ) {
103
+ return array . stream ()
104
+ . map ( ArangoDBUtil :: toDocuments )
105
+ . toList ( );
128
106
}
129
107
130
- private static Object convert (Value value ) {
131
- Object val = ValueUtil .convert (value );
132
-
133
- if (Element .class .isInstance (val )) {
134
- Element document = Element .class .cast (val );
135
- return singletonMap (document .name (), convert (document .value ()));
136
- }
137
- if (isSudDocument (val )) {
138
- return getMap (val );
139
- }
140
- if (isSudDocumentList (val )) {
141
- return stream (Iterable .class .cast (val ).spliterator (), false )
142
- .map (ArangoDBUtil ::getMap ).collect (toList ());
143
- }
144
- return val ;
108
+ private static Object toDocuments (JsonValue value ) {
109
+ return switch (value .getValueType ()) {
110
+ case OBJECT -> toDocuments (value .asJsonObject ());
111
+ case ARRAY -> toDocuments (value .asJsonArray ());
112
+ case STRING -> ((JsonString ) value ).getString ();
113
+ case NUMBER -> ((JsonNumber ) value ).numberValue ();
114
+ case TRUE -> true ;
115
+ case FALSE -> false ;
116
+ case NULL -> null ;
117
+ };
145
118
}
146
119
147
- private static Object getMap (Object val ) {
148
- Iterable <?> iterable = Iterable .class .cast (val );
149
- Map <Object , Object > map = new HashMap <>();
150
- for (Object item : iterable ) {
151
- var document = cast (item );
152
- map .put (document .name (), document .get ());
120
+ private static JsonObject toJsonObject (Iterable <Element > elements ) {
121
+ JsonObjectBuilder builder = Json .createObjectBuilder ();
122
+ for (Element document : elements ) {
123
+ if (KEY .equals (document .name ()) && Objects .isNull (document .get ())) {
124
+ continue ;
125
+ }
126
+ Object value = ValueUtil .convert (document .value ());
127
+ builder .add (document .name (), toJsonValue (value ));
153
128
}
154
- return map ;
129
+ return builder . build () ;
155
130
}
156
131
157
- private static boolean isSudDocumentList (Object value ) {
158
- return value instanceof Iterable && stream (Iterable .class .cast (value ).spliterator (), false ).
159
- allMatch (d -> d instanceof Iterable && isSudDocument (d ));
160
- }
161
-
162
- private static boolean isSudDocument (Object value ) {
163
- return value instanceof Iterable && stream (Iterable .class .cast (value ).spliterator (), false ).
164
- allMatch (Element .class ::isInstance );
132
+ @ SuppressWarnings ("unchecked" )
133
+ private static JsonValue toJsonValue (Object value ) {
134
+ if (value instanceof Element document ) {
135
+ return toJsonObject (Collections .singletonList (document ));
136
+ } else if (value instanceof Iterable <?> iterable ) {
137
+ if (isSubDocument (iterable )) {
138
+ return toJsonObject ((Iterable <Element >) iterable );
139
+ } else {
140
+ JsonArrayBuilder builder = Json .createArrayBuilder ();
141
+ for (Object it : iterable ) {
142
+ builder .add (toJsonValue (it ));
143
+ }
144
+ return builder .build ();
145
+ }
146
+ } else if (value instanceof Map <?, ?> map ) {
147
+ JsonObjectBuilder builder = Json .createObjectBuilder ();
148
+ for (Map .Entry <?, ?> e : map .entrySet ()) {
149
+ builder .add ((String ) e .getKey (), toJsonValue (e .getValue ()));
150
+ }
151
+ return builder .build ();
152
+ } else if (Objects .isNull (value )) {
153
+ return JsonValue .NULL ;
154
+ } else if (value instanceof Number number ) {
155
+ return Json .createValue (number );
156
+ } else if (value instanceof String string ) {
157
+ return Json .createValue (string );
158
+ } else if (Boolean .TRUE .equals (value )) {
159
+ return JsonValue .TRUE ;
160
+ } else if (Boolean .FALSE .equals (value )) {
161
+ return JsonValue .FALSE ;
162
+ } else {
163
+ throw new IllegalArgumentException ("Unsupported type: " + value .getClass ());
164
+ }
165
165
}
166
166
167
- private static Element cast ( Object document ) {
168
- return Element . class . cast ( document );
167
+ private static boolean isSubDocument ( Iterable <?> iterable ) {
168
+ return stream ( iterable . spliterator (), false ). allMatch ( Element . class :: isInstance );
169
169
}
170
170
171
171
}
0 commit comments