1 /**
2 * This file Copyright (c) 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;
35
36 import info.magnolia.ui.framework.datasource.DatasourceHolder;
37 import info.magnolia.ui.framework.ioc.Destructible;
38 import info.magnolia.ui.framework.ioc.ViewComponentProvider;
39 import info.magnolia.ui.framework.ioc.ViewContextKeyRegistry;
40 import info.magnolia.objectfactory.ComponentProvider;
41 import info.magnolia.ui.api.view.View;
42 import info.magnolia.ui.framework.ioc.BeanStore;
43 import info.magnolia.ui.framework.ioc.CurrentUiContextReference;
44 import info.magnolia.ui.framework.ioc.SessionStore;
45 import info.magnolia.ui.framework.ioc.UiContextReference;
46 import info.magnolia.util.Util;
47
48 import java.util.Optional;
49
50 import com.vaadin.ui.Component;
51 import com.vaadin.ui.ComponentContainer;
52
53 /**
54 * View interface enhanced with some IoC-like capabilities based on the idea
55 * that each view is bound the {@link BeanStore} and is able to store objects
56 * inside of it, access the parent stores etc.
57 */
58 public interface UiFrameworkView extends View {
59
60 /**
61 * Convenience method implementation.
62 */
63 @Override
64 default Component asVaadinComponent() {
65 if (this instanceof Component) {
66 return (Component) this;
67 } else {
68 throw new RuntimeException(String.format("%s was expected to be an instance of Component but it's not", this.getClass().getName()));
69 }
70 }
71
72 /**
73 * Cleans up the related bean store, un-registers this
74 * view from the {@link ViewContextKeyRegistry view registry},
75 * detaches the from the parent Vaadin component,
76 * recursively destroys the child views.
77 */
78 default void destroy() {
79 Optional.ofNullable(asVaadinComponent().getParent())
80 .filter(ComponentContainer.class::isInstance)
81 .map(ComponentContainer.class::cast)
82 .ifPresent(parent -> parent.removeComponent(this.asVaadinComponent()));
83
84 final ViewContextKeyRegistry viewContextRegistry = ViewContextKeyRegistry.access();
85
86 viewContextRegistry.getChildViews(getCurrentViewReference()).forEach(UiFrameworkView::destroy);
87
88 // this is important to first clean-up the bean store
89 // and only then - un-register the the current view
90 // because otherwise the store will be in-accessible
91 // via view context registry
92 final BeanStore beanStore = accessViewBeanStore();
93 beanStore.forEach((key, instance) -> {
94 if (instance instanceof Destructible) {
95 ((Destructible) instance).destroy();
96 }
97 });
98
99 beanStore.clear();
100
101 viewContextRegistry.unRegister(this);
102 }
103
104 /**
105 * Put an instance into the view's bean storage, making it
106 * injectable with {@link ViewComponentProvider}.
107 *
108 * @param <T>
109 * type of the shared instance
110 */
111 default <T> void bindInstance(Class<T> type, T instance) {
112 accessViewBeanStore().put(type, instance);
113 }
114
115 /**
116 * Create and bind an instance of the view context. The provided argument is typically an interface,
117 * whose implementation is auto-generated on the fly.
118 *
119 * The instance of the view context is the stored in the view's bean storage and share
120 * in similar fashion link {@link #bindInstance(Class, Object)} does.
121 *
122 * @param <T>
123 * type of the view context
124 */
125 default <T extends ViewContext> T bindContext(Class<? extends T> contextClass) {
126 final T context = new ViewContextProxy().createViewContext(contextClass);
127 accessViewBeanStore().put(contextClass, context);
128 return context;
129 }
130
131 /**
132 * Shares datasource definition information with the sub-views and components.
133 * Datasource definition provides framework with the hints regarding which
134 * implementation to choose for the domain-specific interfaces.
135 *
136 * @see info.magnolia.ui.framework.datasource.DatasourceComponent
137 * @see info.magnolia.ui.framework.datasource.DatasourceBundle
138 * @see info.magnolia.ui.framework.datasource.DatasourceSupport
139 */
140 default void bindDatasourceDefinition(Object definition) {
141 accessViewBeanStore().put(DatasourceHolder.class, new DatasourceHolder(definition));
142 }
143
144 /**
145 * Convenience wrapper around {@link ComponentProvider} capabilities.
146 * Creates and instance of a type specified by a passed {@link WithImplementation} instance.
147 *
148 * @param <T>
149 * type of an instance to be created
150 */
151 default <T> T create(WithImplementation<T> definition, Object... args) {
152 return getComponentProvider().newInstance(definition.getImplementationClass(), Util.appendToArray(args, definition));
153 }
154
155 /**
156 * Convenience wrapper around {@link ComponentProvider} capabilities.
157 *
158 * @param <T>
159 * type of an instance to be created
160 */
161 default <T> T create(Class type, Object... args) {
162 //noinspection unchecked
163 return getComponentProvider().newInstance((Class<T>) type, args);
164 }
165
166 /**
167 * Get {@link ViewProvider} utility.
168 */
169 default ViewProvider getViewProvider() {
170 return new ViewProviderImpl(getCurrentViewReference());
171 }
172
173 default ComponentProvider getComponentProvider() {
174 return new ViewComponentProvider(getCurrentViewReference());
175 }
176
177 default UiContextReference getCurrentViewReference() {
178 return ViewContextKeyRegistry.access()
179 .lookUp(this)
180 .orElseGet(() -> CurrentUiContextReference.get().getUiContextReference());
181 }
182
183 default BeanStore accessViewBeanStore() {
184 return SessionStore.access().getBeanStore(getCurrentViewReference());
185 }
186 }