1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.ui.framework.ioc;
35
36 import info.magnolia.ui.api.app.AppContext;
37 import info.magnolia.ui.api.app.SubAppContext;
38
39 import java.io.Serializable;
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.Collections;
43 import java.util.List;
44 import java.util.Objects;
45
46 import com.google.common.collect.ImmutableList;
47 import com.vaadin.ui.UI;
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public interface UiContextReference extends Serializable {
70
71
72
73
74
75
76 String asString();
77
78
79
80
81
82
83 UiContextAnnotation getAnnotation();
84
85
86
87
88
89 default List<UiContextReference> getParentReferences() {
90 return Collections.emptyList();
91 }
92
93
94
95 static UiContextReference ofUi(UI ui) {
96 return new UiContextReferenceImpl(ui) {
97 @Override
98 public UiContextAnnotation getAnnotation() {
99 return UiAnnotations.forAdmincentral();
100 }
101 };
102 }
103
104 static UiContextReference ofM5Admincentral() {
105 return new UiContextReferenceImpl() {
106 @Override
107 public UiContextAnnotation getAnnotation() {
108 return UiAnnotations.forM5Admincentral();
109 }
110
111 @Override
112 public List<UiContextReference> getParentReferences() {
113 return Arrays.asList(new UiContextReferenceImpl() {
114 @Override
115 public UiContextAnnotation getAnnotation() {
116 return UiAnnotations.forAdmincentral();
117 }
118 });
119 }
120 };
121 }
122
123 static UiContextReference ofCurrentUi() {
124 return new UiContextReferenceImpl() {
125 @Override
126 public UiContextAnnotation getAnnotation() {
127 return UiAnnotations.forAdmincentral();
128 }
129 };
130 }
131
132
133
134
135
136
137
138
139
140 static UiContextReference ofApp(AppContext appContext) {
141 return new AppContextReference(appContext);
142 }
143
144
145
146
147
148
149
150
151
152 static UiContextReference ofSubApp(SubAppContext subAppContext) {
153 return new SubAppContextReference(subAppContext);
154 }
155
156 static UiContextReference ofView(String viewId, UiContextReference parentKey) {
157 return new ViewContextReference(viewId, parentKey);
158 }
159
160 static UiContextReference genericSubAppContextReference() {
161 return new UiContextReferenceImpl() {
162
163 @Override
164 public UiContextAnnotation getAnnotation() {
165 return UiAnnotations.forSubApps();
166 }
167
168 @Override
169 public List<UiContextReference> getParentReferences() {
170 final UiContextReference genericAppContextKey = genericAppContextReference();
171 return ImmutableList.<UiContextReference>builder().add(genericAppContextKey).addAll(genericAppContextKey.getParentReferences()).build();
172 }
173 };
174 }
175
176 static UiContextReference genericAppContextReference() {
177 return new UiContextReferenceImpl() {
178 @Override
179 public UiContextAnnotation getAnnotation() {
180 return UiAnnotations.forApps();
181 }
182
183 @Override
184 public List<UiContextReference> getParentReferences() {
185 return Arrays.asList(UiContextReference.ofCurrentUi());
186 }
187 };
188 }
189
190 static UiContextReference genericViewContextRefence() {
191 return new UiContextReferenceImpl() {
192 @Override
193 public UiContextAnnotation getAnnotation() {
194 return UiAnnotations.forViews();
195 }
196
197 @Override
198 public List<UiContextReference> getParentReferences() {
199 return Collections.emptyList();
200 }
201 };
202 }
203
204
205
206
207
208
209 class AppContextReference extends UiContextReferenceImpl {
210 private final AppContext appContext;
211
212 AppContextReference(AppContext appContext) {
213 this.appContext = appContext;
214 }
215
216 @Override
217 String getScopeId() {
218 return String.join(":", appName(), String.valueOf(appContext.hashCode()));
219 }
220
221 private String appName() {
222 return this.appContext.getName();
223 }
224
225 @Override
226 public UiContextAnnotation getAnnotation() {
227 return UiAnnotations.forApp(appName());
228 }
229
230 @Override
231 public List<UiContextReference> getParentReferences() {
232 final List<UiContextReference> parentReferenceList = new ArrayList<>();
233 parentReferenceList.add(genericAppContextReference());
234 if (AdmincentralFlavour.get().isM5()) {
235 parentReferenceList.add(UiContextReference.ofM5Admincentral());
236 }
237 parentReferenceList.add(UiContextReference.ofCurrentUi());
238 return parentReferenceList;
239 }
240 }
241
242
243
244
245 class SubAppContextReference extends UiContextReferenceImpl {
246
247 private final SubAppContext subAppContext;
248
249 SubAppContextReference(SubAppContext subAppContext) {
250 this.subAppContext = subAppContext;
251 }
252
253 @Override
254 protected String getScopeId() {
255 return String.join(":", appName(), subAppName(), String.valueOf(subAppContext.hashCode()));
256 }
257
258 private String subAppName() {
259 return subAppContext.getSubAppDescriptor().getName();
260 }
261
262 private String appName() {
263 return subAppContext.getAppContext().getName();
264 }
265
266 @Override
267 public UiContextAnnotation getAnnotation() {
268 return UiAnnotations.forSubApp(appName(), subAppName());
269 }
270
271 @Override
272 public List<UiContextReference> getParentReferences() {
273 final UiContextReference appContextKey = UiContextReference.ofApp(this.subAppContext.getAppContext());
274 return ImmutableList.<UiContextReference>builder().add(genericSubAppContextReference()).add(appContextKey).addAll(appContextKey.getParentReferences()).build();
275 }
276 }
277
278
279
280
281 class ViewContextReference extends UiContextReferenceImpl {
282
283 private final String viewId;
284 private final UiContextReference parentKey;
285
286 ViewContextReference(String viewId, UiContextReference parentKey) {
287 this.viewId = viewId;
288 this.parentKey = parentKey;
289 }
290
291 @Override
292 String getScopeId() {
293 return viewId;
294 }
295
296 @Override
297 public UiContextAnnotation getAnnotation() {
298 return new ViewImpl(viewId);
299 }
300
301 @Override
302 public List<UiContextReference> getParentReferences() {
303 return ImmutableList.<UiContextReference>builder().add(genericViewContextRefence()).add(parentKey).addAll(parentKey.getParentReferences()).build();
304 }
305
306 @Override
307 public String asString() {
308 return String.join(":", parentKey.asString(), getScopeId());
309 }
310 }
311
312
313
314
315 abstract class UiContextReferenceImpl implements UiContextReference {
316
317 private final String uiId;
318
319 UiContextReferenceImpl(UI ui) {
320 this.uiId = String.format("%s:%d", ui.getEmbedId(), ui.getUIId());
321 }
322
323 UiContextReferenceImpl() {
324 this(UI.getCurrent());
325 }
326
327 @Override
328 public String asString() {
329 return String.join(":", getUiInstancePrefix(), getScopeId());
330 }
331
332 String getUiInstancePrefix() {
333 return uiId;
334 }
335
336 String getScopeId() {
337 return "";
338 }
339
340 @Override
341 public boolean equals(Object o) {
342 if (this == o) return true;
343 if (!(o instanceof UiContextReference)) return false;
344 return Objects.equals(this.asString(), ((UiContextReference) o).asString());
345 }
346
347 @Override
348 public int hashCode() {
349 return Objects.hashCode(this.asString());
350 }
351
352 @Override
353 public String toString() {
354 return asString();
355 }
356 }
357 }