|
24 | 24 |
|
25 | 25 | import java.lang.reflect.InvocationTargetException; |
26 | 26 | import java.lang.reflect.Type; |
27 | | -import java.util.ArrayList; |
28 | | -import java.util.Arrays; |
29 | 27 | import java.util.List; |
30 | 28 |
|
31 | 29 | import javax.annotation.Nullable; |
|
36 | 34 | import com.google.gson.JsonObject; |
37 | 35 | import com.google.gson.JsonParseException; |
38 | 36 | import com.google.gson.JsonParser; |
39 | | -import com.google.common.reflect.TypeToken; |
40 | 37 | import com.microsoft.graph.http.BaseCollectionPage; |
| 38 | +import com.microsoft.graph.http.BaseCollectionResponse; |
41 | 39 | import com.microsoft.graph.http.BaseRequestBuilder; |
42 | | -import com.microsoft.graph.http.IRequestBuilder; |
43 | 40 | import com.microsoft.graph.logger.ILogger; |
44 | 41 |
|
45 | 42 | /** Specialized serializer to handle collection pages */ |
46 | 43 | public class CollectionPageSerializer { |
47 | 44 |
|
48 | | - private static DefaultSerializer serializer; |
49 | | - /** length of the word "page" */ |
50 | | - private final static Integer pageLength = 4; |
51 | | - /** length of the word "collection" */ |
52 | | - private final static Integer collectionLength = 10; |
53 | | - /** length of the work "response" */ |
54 | | - private final static Integer responseLength = 8; |
| 45 | + private static DefaultSerializer serializer; |
| 46 | + /** length of the work "response" */ |
| 47 | + private final static int responseLength = 8; |
| 48 | + /** length of the work "page" */ |
| 49 | + private final static int pageLength = 4; |
55 | 50 |
|
56 | | - /** |
57 | | - * Not available for instantiation |
58 | | - */ |
59 | | - private CollectionPageSerializer() { |
60 | | - } |
| 51 | + /** |
| 52 | + * Not available for instantiation |
| 53 | + */ |
| 54 | + private CollectionPageSerializer() { |
| 55 | + } |
61 | 56 |
|
62 | | - /** |
| 57 | + /** |
63 | 58 | * Serializes an CollectionPage |
64 | 59 | * |
65 | 60 | * @param src the CollectionPage variable for serialization |
66 | 61 | * @param logger the logger |
67 | | - * @param <T1> the entity type for the collection |
68 | | - * @param <T2> the collection request builder interface type |
| 62 | + * @param <T1> the entity type for the collection |
| 63 | + * @param <T2> the collection request builder interface type |
69 | 64 | * @return JsonElement of CollectionPage |
70 | 65 | */ |
71 | | - @Nullable |
72 | | - public static <T1, T2 extends BaseRequestBuilder<T1>> JsonElement serialize(@Nonnull final BaseCollectionPage<T1, T2> src, @Nonnull final ILogger logger) { |
73 | | - if(src == null) { |
74 | | - return null; |
75 | | - } |
76 | | - JsonArray jsonArray = new JsonArray(); |
77 | | - List<T1> items = src.getCurrentPage(); |
78 | | - serializer = new DefaultSerializer(logger); |
79 | | - for(T1 item : items) { |
80 | | - final String json = serializer.serializeObject(item); |
81 | | - final JsonElement element = JsonParser.parseString(json); |
82 | | - if(element != null && element.isJsonObject()) { |
83 | | - final JsonObject jsonObject = element.getAsJsonObject(); |
84 | | - jsonArray.add(jsonObject); |
85 | | - } |
86 | | - } |
87 | | - return jsonArray; |
88 | | - } |
| 66 | + @Nullable |
| 67 | + public static <T1, T2 extends BaseRequestBuilder<T1>> JsonElement serialize(@Nonnull final BaseCollectionPage<T1, T2> src, @Nonnull final ILogger logger) { |
| 68 | + if(src == null) { |
| 69 | + return null; |
| 70 | + } |
| 71 | + JsonArray jsonArray = new JsonArray(); |
| 72 | + List<T1> items = src.getCurrentPage(); |
| 73 | + serializer = new DefaultSerializer(logger); |
| 74 | + for(T1 item : items) { |
| 75 | + final String json = serializer.serializeObject(item); |
| 76 | + final JsonElement element = JsonParser.parseString(json); |
| 77 | + if(element != null && element.isJsonObject()) { |
| 78 | + final JsonObject jsonObject = element.getAsJsonObject(); |
| 79 | + jsonArray.add(jsonObject); |
| 80 | + } |
| 81 | + } |
| 82 | + return jsonArray; |
| 83 | + } |
89 | 84 |
|
90 | | - /** |
| 85 | + /** |
91 | 86 | * Deserializes the JsonElement |
92 | 87 | * |
93 | 88 | * @param json the source CollectionPage's Json |
94 | | - * @param typeOfT The type of the CollectionPage to deserialize to |
| 89 | + * @param typeOfT The type of the CollectionPage to deserialize to |
95 | 90 | * @param logger the logger |
96 | | - * @param <T1> the entity type for the collection |
97 | | - * @param <T2> the collection request builder interface type |
| 91 | + * @param <T1> the entity type for the collection |
| 92 | + * @param <T2> the collection request builder interface type |
98 | 93 | * @throws JsonParseException the parse exception |
99 | 94 | * @return the deserialized CollectionPage |
100 | 95 | */ |
101 | | - @SuppressWarnings("unchecked") |
102 | | - @Nullable |
103 | | - public static <T1, T2 extends BaseRequestBuilder<T1>> BaseCollectionPage<T1, T2> deserialize(@Nonnull final JsonElement json, @Nonnull final Type typeOfT, @Nonnull final ILogger logger) throws JsonParseException { |
104 | | - if (json == null || !json.isJsonArray() || !typeOfT.getClass().equals(Class.class)) { |
105 | | - return null; |
106 | | - } |
107 | | - serializer = new DefaultSerializer(logger); |
108 | | - final JsonArray sourceArray = json.getAsJsonArray(); |
109 | | - final ArrayList<T1> list = new ArrayList<T1>(sourceArray.size()); |
110 | | - /** eg: com.microsoft.graph.requests.AttachmentCollectionPage */ |
111 | | - final String collectionPageClassCanonicalName = ((Class<?>)typeOfT).getName(); |
112 | | - /** eg: com.microsoft.graph.models.Attachment */ |
113 | | - final String baseEntityClassCanonicalName = collectionPageClassCanonicalName |
114 | | - .substring(0, collectionPageClassCanonicalName.length() - pageLength - collectionLength) |
115 | | - .replace("requests", "models"); |
116 | | - try { |
117 | | - final Class<?> baseEntityClass = Class.forName(baseEntityClassCanonicalName); |
118 | | - for(JsonElement sourceElement : sourceArray) { |
119 | | - if(sourceElement.isJsonObject()) { |
120 | | - final JsonObject sourceObject = sourceElement.getAsJsonObject(); |
121 | | - Class<?> entityClass = serializer.getDerivedClass(sourceObject, baseEntityClass); |
122 | | - if(entityClass == null) |
123 | | - entityClass = baseEntityClass; |
124 | | - final T1 targetObject = (T1)serializer.deserializeObject(sourceObject.toString(), entityClass); |
125 | | - ((IJsonBackedObject)targetObject).setRawObject(serializer, sourceObject); |
126 | | - list.add(targetObject); |
127 | | - } |
128 | | - } |
129 | | - /** eg: com.microsoft.graph.requests.AttachmentCollectionResponse */ |
130 | | - final String responseClassCanonicalName = collectionPageClassCanonicalName |
131 | | - .substring(0, collectionPageClassCanonicalName.length() - pageLength) + "Response"; |
132 | | - final Class<?> responseClass = Class.forName(responseClassCanonicalName); |
133 | | - final Object response = responseClass.getConstructor().newInstance(); |
134 | | - responseClass.getField("value").set(response, list); |
135 | | - final Class<?> collectionPageClass = Class.forName(collectionPageClassCanonicalName); |
136 | | - /** eg: com.microsoft.graph.requests.AttachmentCollectionRequestBuilder */ |
137 | | - final String responseBuilderCanonicalName = responseClassCanonicalName |
138 | | - .substring(0, responseClassCanonicalName.length() - responseLength) + "RequestBuilder"; |
139 | | - final Class<?> responseBuilderClass = Class.forName(responseBuilderCanonicalName); |
140 | | - return (BaseCollectionPage<T1, T2>)collectionPageClass.getConstructor(responseClass, responseBuilderClass).newInstance(response, null); |
141 | | - } catch(ClassNotFoundException ex) { |
142 | | - logger.logError("Could not find class during deserialization", ex); |
143 | | - } catch(NoSuchMethodException | InstantiationException | InvocationTargetException ex) { |
144 | | - logger.logError("Could not instanciate type during deserialization", ex); |
145 | | - } catch(NoSuchFieldException | IllegalAccessException ex) { |
146 | | - logger.logError("Unable to set field value during deserialization", ex); |
147 | | - } |
148 | | - return null; |
149 | | - } |
| 96 | + @SuppressWarnings("unchecked") |
| 97 | + @Nullable |
| 98 | + public static <T1, T2 extends BaseRequestBuilder<T1>> BaseCollectionPage<T1, T2> deserialize(@Nonnull final JsonElement json, @Nonnull final Type typeOfT, @Nonnull final ILogger logger) throws JsonParseException { |
| 99 | + if (json == null || !json.isJsonArray() || !typeOfT.getClass().equals(Class.class)) { |
| 100 | + return null; |
| 101 | + } |
| 102 | + serializer = new DefaultSerializer(logger); |
| 103 | + /** eg: com.microsoft.graph.requests.AttachmentCollectionPage */ |
| 104 | + final String collectionPageClassCanonicalName = ((Class<?>)typeOfT).getName(); |
| 105 | + try { |
| 106 | + /** eg: com.microsoft.graph.requests.AttachmentCollectionResponse */ |
| 107 | + final String responseClassCanonicalName = collectionPageClassCanonicalName |
| 108 | + .substring(0, collectionPageClassCanonicalName.length() - pageLength) + "Response"; |
| 109 | + final Class<?> responseClass = Class.forName(responseClassCanonicalName); |
| 110 | + final JsonObject responseJson = new JsonObject(); |
| 111 | + responseJson.add("value", json); |
| 112 | + final BaseCollectionResponse<T1> response = CollectionResponseSerializer.deserialize(responseJson, responseClass, logger); |
| 113 | + /** eg: com.microsoft.graph.requests.AttachmentCollectionRequestBuilder */ |
| 114 | + final String responseBuilderCanonicalName = responseClassCanonicalName |
| 115 | + .substring(0, responseClassCanonicalName.length() - responseLength) + "RequestBuilder"; |
| 116 | + final Class<?> responseBuilderClass = Class.forName(responseBuilderCanonicalName); |
| 117 | + final Class<?> collectionPageClass = Class.forName(collectionPageClassCanonicalName); |
| 118 | + return (BaseCollectionPage<T1, T2>)collectionPageClass.getConstructor(responseClass, responseBuilderClass).newInstance(response, null); |
| 119 | + } catch(ClassNotFoundException ex) { |
| 120 | + logger.logError("Could not find class during deserialization", ex); |
| 121 | + } catch(NoSuchMethodException | InstantiationException | InvocationTargetException ex) { |
| 122 | + logger.logError("Could not instanciate type during deserialization", ex); |
| 123 | + } catch(IllegalAccessException ex) { |
| 124 | + logger.logError("Unable to set field value during deserialization", ex); |
| 125 | + } |
| 126 | + return null; |
| 127 | + } |
150 | 128 | } |
0 commit comments