1111 * Contributors:
1212 *
1313 * Otavio Santana
14+ * Michele Rastelli
1415 */
1516package org .eclipse .jnosql .databases .arangodb .communication ;
1617
1718
1819import com .arangodb .ArangoDB ;
19- import com .arangodb .entity .BaseDocument ;
2020import 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 ;
2229import org .eclipse .jnosql .communication .ValueUtil ;
2330import org .eclipse .jnosql .communication .semistructured .CommunicationEntity ;
2431import org .eclipse .jnosql .communication .semistructured .Element ;
2532
26- import java .util .ArrayList ;
2733import java .util .Collection ;
28- import java .util .HashMap ;
34+ import java .util .Collections ;
2935import java .util .List ;
3036import java .util .Map ;
3137import java .util .Objects ;
32- import java .util .function .Function ;
3338import java .util .logging .Level ;
3439import java .util .logging .Logger ;
3540
36- import static java .util .Collections .singletonMap ;
3741import static java .util .stream .Collectors .toList ;
3842import static java .util .stream .StreamSupport .stream ;
3943
@@ -47,9 +51,6 @@ public final class ArangoDBUtil {
4751 public static final String REV = "_rev" ;
4852 private static final Logger LOGGER = Logger .getLogger (ArangoDBUtil .class .getName ());
4953
50- private static final Function <Map .Entry <?, ?>, Element > ENTRY_DOCUMENT = entry ->
51- Element .of (entry .getKey ().toString (), entry .getValue ());
52-
5354 private ArangoDBUtil () {
5455 }
5556
@@ -71,101 +72,100 @@ public static void checkCollection(String bucketName, ArangoDB arangoDB, String
7172 List <String > collections = arangoDB .db (bucketName )
7273 .getCollections ().stream ()
7374 .map (CollectionEntity ::getName )
74- .collect ( toList () );
75+ .toList ();
7576 if (!collections .contains (namespace )) {
7677 arangoDB .db (bucketName ).createCollection (namespace );
7778 }
7879 }
7980
81+ static CommunicationEntity toEntity (JsonObject jsonObject ) {
82+ List <Element > documents = toDocuments (jsonObject );
8083
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 ];
9189 return CommunicationEntity .of (collection , documents );
9290 }
9391
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 ());
10394 }
10495
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 ());
122100 }
123101
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 ( );
128106 }
129107
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+ };
145118 }
146119
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 ));
153128 }
154- return map ;
129+ return builder . build () ;
155130 }
156131
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+ }
165165 }
166166
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 );
169169 }
170170
171171}
0 commit comments