View Javadoc
1   /**
2    * This file Copyright (c) 2017-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * A simple abstraction for the UI context identification. Useful in the two following scenarios:
51   * <p>
52   * Used by {@link SessionStore} as the key of a {@link BeanStore} containing
53   * scoped objects (see {@link UiContextReference#asString() method}).
54   * </p>
55   * <p>
56   * Helps to resolve the {@link com.google.inject.BindingAnnotation Guice Binding Annotation} corresponding
57   * to the UI context, which provides access to context-specific type bindings
58   * (orchestrated by e.g. {@link UiContextApplyingProvider}).
59   * </p>
60   * <p>
61   * Also provides a list of the keys that are more 'general' than the current.  
62   * </p>
63   *
64   * @see UiContextApplyingProvider
65   * @see CurrentUiContextReference
66   * @see SessionStore
67   * @see UiScopes
68   */
69  public interface UiContextReference extends Serializable {
70  
71      /**
72       * Get String representation of UI context ref; useful
73       * when the reference needs to be used as a key in a hash map
74       * (e.g. in {@link BeanStore}).
75       */
76      String asString();
77  
78      /**
79       * Get {@link UiContextAnnotation} which corresponds to
80       * this UI context reference. Such annotation is also a
81       * {@link com.google.inject.BindingAnnotation} used by Guice.
82       */
83      UiContextAnnotation getAnnotation();
84  
85      /**
86       * Get all the ('broader') references that can be resolved from
87       * the current one.
88       */
89      default List<UiContextReference> getParentReferences() {
90          return Collections.emptyList();
91      }
92  
93      // UI context reference factory methods
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      * Creates a UI context reference bound to a concrete instance of an app.
134      * All the app scoped objects relevant to that app will be resolvable through
135      * this reference.
136      *
137      * @param appContext concrete instance of a app context
138      * @return ui context reference bound to the app instance
139      */
140     static UiContextReference ofApp(AppContext appContext) {
141         return new AppContextReference(appContext);
142     }
143 
144     /**
145      * Creates a UI context reference bound to a concrete instance of a sub-app.
146      * All the scoped objects relevant to that sub-app will be resolvable through
147      * this reference.
148      *
149      * @param subAppContext concrete instance of a sub-app context
150      * @return ui context reference bound to sub-app instance
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     // Implementations of the UI context keys
205 
206     /**
207      * App specific UI context reference.
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      * Sub-app specific UI context reference.
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      * View-specific UI context reference implementation.
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      * Base {@link UiContextReference} implementation.
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 }