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