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