1 package com.atlassian.marketplace.client.impl;
2
3 import java.lang.reflect.Field;
4 import java.net.URI;
5 import java.util.Collection;
6 import java.util.Map;
7
8 import com.atlassian.fugue.Option;
9 import com.atlassian.marketplace.client.model.AddonCategorySummary;
10 import com.atlassian.marketplace.client.model.AddonReference;
11 import com.atlassian.marketplace.client.model.AddonSummary;
12 import com.atlassian.marketplace.client.model.AddonVersionSummary;
13 import com.atlassian.marketplace.client.model.Application;
14 import com.atlassian.marketplace.client.model.ApplicationVersion;
15 import com.atlassian.marketplace.client.model.Entity;
16 import com.atlassian.marketplace.client.model.ErrorDetail;
17 import com.atlassian.marketplace.client.model.LicenseType;
18 import com.atlassian.marketplace.client.model.Links;
19 import com.atlassian.marketplace.client.model.Product;
20 import com.atlassian.marketplace.client.model.RequiredLink;
21 import com.atlassian.marketplace.client.model.VendorSummary;
22
23 import com.google.common.collect.ImmutableList;
24
25 import static com.atlassian.fugue.Option.none;
26 import static com.atlassian.fugue.Option.some;
27 import static com.google.common.base.Preconditions.checkNotNull;
28
29
30
31
32
33 public abstract class InternalModel
34 {
35 public static Addons addons(Links links, ImmutableList<AddonSummary> items, int count)
36 {
37 return makeCollectionRep(Addons.class, links, items, some(count));
38 }
39
40 public static AddonCategories addonCategories(Links links, ImmutableList<AddonCategorySummary> items)
41 {
42 return makeCollectionRep(AddonCategories.class, links, items, none(Integer.class));
43 }
44
45 public static AddonReferences addonReferences(Links links, ImmutableList<AddonReference> items, int count)
46 {
47 return makeCollectionRep(AddonReferences.class, links, items, some(count));
48 }
49
50 public static AddonVersions addonVersions(Links links, ImmutableList<AddonVersionSummary> items, int count)
51 {
52 return makeCollectionRep(AddonVersions.class, links, items, some(count));
53 }
54
55 public static Applications applications(Links links, ImmutableList<Application> items)
56 {
57 return makeCollectionRep(Applications.class, links, items, none(Integer.class));
58 }
59
60 public static ApplicationVersions applicationVersions(Links links, ImmutableList<ApplicationVersion> items, int count)
61 {
62 return makeCollectionRep(ApplicationVersions.class, links, items, some(count));
63 }
64
65 public static ErrorDetails errorDetails(Iterable<ErrorDetail> errorDetails)
66 {
67 return new ErrorDetails(errorDetails);
68 }
69
70 public static MinimalLinks minimalLinks(Links links)
71 {
72 return new MinimalLinks(links);
73 }
74
75 public static Products products(Links links, ImmutableList<Product> items, int count)
76 {
77 return makeCollectionRep(Products.class, links, items, some(count));
78 }
79
80 public static Vendors vendors(Links links, ImmutableList<VendorSummary> items, int count)
81 {
82 return makeCollectionRep(Vendors.class, links, items, some(count));
83 }
84
85
86
87
88 public static abstract class EntityCollection<T> implements Entity
89 {
90 private Links _links;
91 private Integer count;
92 @RequiredLink(rel = "self") private URI selfUri;
93
94 protected EntityCollection()
95 {
96 }
97
98 protected EntityCollection(Links links, int count)
99 {
100 this._links = links;
101 this.count = count;
102 }
103
104
105
106
107 public Links getLinks()
108 {
109 return _links;
110 }
111
112 public URI getSelfUri()
113 {
114 return selfUri;
115 }
116
117
118
119
120 public int getCount()
121 {
122 return count;
123 }
124
125
126
127
128 public abstract Iterable<T> getItems();
129 }
130
131 public static class AddonCategories
132 {
133 public Links _links;
134 public Embedded _embedded;
135
136 public static class Embedded
137 {
138 public ImmutableList<AddonCategorySummary> categories;
139 }
140 }
141
142 public static class AddonReferences extends EntityCollection<AddonReference>
143 {
144 private Embedded _embedded;
145
146 public Iterable<AddonReference> getItems()
147 {
148 return _embedded.addons;
149 }
150
151 public static class Embedded
152 {
153 private ImmutableList<AddonReference> addons;
154 }
155 }
156
157 public static class AddonVersions extends EntityCollection<AddonVersionSummary>
158 {
159 private Embedded _embedded;
160
161 public Iterable<AddonVersionSummary> getItems()
162 {
163 return _embedded.versions;
164 }
165
166 public static class Embedded
167 {
168 private ImmutableList<AddonVersionSummary> versions;
169 }
170 }
171
172 public static class Addons extends EntityCollection<AddonSummary>
173 {
174 private Embedded _embedded;
175
176 public Iterable<AddonSummary> getItems()
177 {
178 return _embedded.addons;
179 }
180
181 public static class Embedded
182 {
183 private ImmutableList<AddonSummary> addons;
184 }
185 }
186
187 public static class ApplicationVersions extends EntityCollection<ApplicationVersion>
188 {
189 private Embedded _embedded;
190
191 public Iterable<ApplicationVersion> getItems()
192 {
193 return _embedded.versions;
194 }
195
196 public static class Embedded
197 {
198 private Collection<ApplicationVersion> versions;
199 }
200 }
201
202 public static class Applications
203 {
204 public Links _links;
205 public Embedded _embedded;
206
207 public static class Embedded
208 {
209 public ImmutableList<Application> applications;
210 }
211 }
212
213 public static class ErrorDetails
214 {
215 ImmutableList<ErrorDetail> errors;
216
217 ErrorDetails(Iterable<ErrorDetail> errorDetails)
218 {
219 this.errors = ImmutableList.copyOf(errorDetails);
220 }
221 }
222
223 public static class LicenseTypes
224 {
225 private Embedded _embedded;
226
227 public Iterable<LicenseType> getItems()
228 {
229 return _embedded.types;
230 }
231
232 public static class Embedded
233 {
234 public ImmutableList<LicenseType> types;
235 }
236 }
237
238 public static class MinimalLinks
239 {
240 private Links _links;
241
242 public MinimalLinks(Links _links)
243 {
244 this._links = checkNotNull(_links);
245 }
246
247 public Links getLinks()
248 {
249 return _links;
250 }
251 }
252
253 public static class Products extends EntityCollection<Product>
254 {
255 private Embedded _embedded;
256
257 public Iterable<Product> getItems()
258 {
259 return _embedded.products;
260 }
261
262 public static class Embedded
263 {
264 private ImmutableList<Product> products;
265 }
266 }
267
268 public static class Vendors extends EntityCollection<VendorSummary>
269 {
270 private Embedded _embedded;
271
272 public Iterable<VendorSummary> getItems()
273 {
274 return _embedded.vendors;
275 }
276
277 public static class Embedded
278 {
279 private ImmutableList<VendorSummary> vendors;
280 }
281 }
282
283
284 private static <A> A makeCollectionRep(Class<A> repClass, Links links, ImmutableList<?> items, Option<Integer> count)
285 {
286 Map<String, Field> fields = EntityValidator.getClassFields(repClass);
287 try
288 {
289 A instance = (A) repClass.getConstructor().newInstance();
290
291 fields.get("_links").set(instance, links);
292 for (int c: count)
293 {
294 if (fields.get("count") != null)
295 {
296 fields.get("count").set(instance, c);
297 }
298 }
299
300 Field f = fields.get("_embedded");
301 Class<?> ec = f.getType();
302 Object e = ec.getConstructor().newInstance();
303 Field eif = ec.getDeclaredFields()[0];
304 eif.setAccessible(true);
305 eif.set(e, items);
306 f.set(instance, e);
307
308 return instance;
309 }
310 catch (Exception e)
311 {
312 throw new RuntimeException(e);
313 }
314 }
315 }