View Javadoc
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.admincentral.apps.notifications.view.detail;
35  
36  import info.magnolia.admincentral.apps.notifications.view.column.NotificationTypeValueProvider;
37  import info.magnolia.context.Context;
38  import info.magnolia.icons.MagnoliaIcons;
39  import info.magnolia.ui.api.app.SubAppContext;
40  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
41  import info.magnolia.ui.api.location.Location;
42  import info.magnolia.ui.api.message.Message;
43  import info.magnolia.ui.contentapp.browser.actions.ActionbarPresenter;
44  import info.magnolia.ui.contentapp.browser.actions.ActionbarView;
45  import info.magnolia.ui.contentapp.browser.context.ValueContext;
46  import info.magnolia.ui.contentapp.detail.ContentDetailSubApp;
47  import info.magnolia.ui.form.LocaleContext;
48  import info.magnolia.ui.framework.ViewDefinition;
49  import info.magnolia.ui.framework.app.BaseSubApp;
50  import info.magnolia.ui.framework.databinding.ItemProviderStrategy;
51  import info.magnolia.ui.framework.databinding.view.EditorView;
52  import info.magnolia.ui.framework.databinding.view.FormView;
53  import info.magnolia.ui.framework.datasource.definition.DatasourceDefinition;
54  import info.magnolia.ui.framework.message.MessagesManager;
55  
56  import java.util.Locale;
57  import java.util.Optional;
58  
59  import javax.inject.Inject;
60  
61  /**
62   * Getting closer to what could be a generic NotificationDetailSubApp.
63   * @param <T>
64   */
65  public class NotificationDetailSubApp<T> extends BaseSubApp<NotificationFormView> {
66  
67      private final NotificationDetailDescriptor<T, ?> subAppDescriptor;
68      private final I18NAuthoringSupport i18NAuthoringSupport;
69      private final NotificationTypeValueProvider notificationTypeValueProvider;
70      private final String currentUserName;
71      private final ValueContext<T> valueContext;
72      private final MessagesManager messagesManager;
73      private final DatasourceDefinition datasourceDefinition;
74      private ContentDetailSubApp.LocationContext locationContext;
75      private ItemProviderStrategy<T> itemProviderStrategy;
76  
77      @Inject
78      protected NotificationDetailSubApp(SubAppContext subAppContext,
79                                         I18NAuthoringSupport i18NAuthoringSupport,
80                                         MessagesManager messagesManager,
81                                         Context context,
82                                         DatasourceDefinition datasourceDefinition,
83                                         NotificationDetailDescriptor<T, ?> descriptor,
84                                         ValueContext<T> valueContext) {
85          super(subAppContext, new NotificationFormView());
86          this.messagesManager = messagesManager;
87          this.subAppDescriptor = descriptor;
88          this.i18NAuthoringSupport = i18NAuthoringSupport;
89          this.valueContext = valueContext;
90          this.notificationTypeValueProvider = new NotificationTypeValueProvider();
91          this.currentUserName = context.getUser().getName();
92          this.datasourceDefinition = datasourceDefinition; 
93      }
94  
95      @Override
96      public NotificationFormView start(Location location) {
97          super.start(location);
98  
99          final NotificationFormView view = getView();
100 
101         ActionbarPresenter actionbarPresenter = create(ActionbarPresenter.class, subAppDescriptor.getActions(), subAppDescriptor.getActionbar());
102         bindInstance(ActionbarPresenter.class, actionbarPresenter);
103 
104         final LocaleContext localeContext = bindContext(LocaleContext.class);
105         final Locale currentLocale = getSubAppContext().getAuthoringLocale() != null ?
106                 getSubAppContext().getAuthoringLocale() :
107                 i18NAuthoringSupport.getDefaultLocale();
108         localeContext.current().set(currentLocale);
109 
110         this.locationContext = bindContext(ContentDetailSubApp.LocationContext.class);
111         this.locationContext.location().set(ContentDetailSubApp.DetailLocation.wrap(location));
112         this.itemProviderStrategy = create(subAppDescriptor.getItemProvider(), datasourceDefinition);
113 
114         bindInstance(ItemProviderStrategy.class, itemProviderStrategy);
115 
116         Optional<T> itemProvider = itemProviderStrategy.read();
117         if (itemProvider.isPresent()) {
118             Message message = (Message) itemProvider.get();
119             valueContext.set(itemProvider.get());
120             messagesManager.clearMessage(currentUserName, message.getId());
121 
122             final FormView<T> form = (FormView<T>) create(subAppDescriptor.getForm());
123             bindInstance(EditorView.class, form);
124             view.setCaption(message.getSubject());
125             subAppDescriptor.setLabel(message.getSubject());
126             notificationTypeValueProvider.getIcon(message).ifPresent(this::updateViewAndSubAppDescriptorIcons);
127 
128             view.setSizeFull();
129             view.addStyleName("detail");
130             view.setMargin(false);
131             view.setSpacing(false);
132             @SuppressWarnings("unchecked") ActionbarView actionbar = (ActionbarView) create(
133                     ViewDefinition.builder()
134                             .withImplementationClass(ActionbarView.class)
135                             .withName("actionbar")
136                             .build(), subAppDescriptor.getActionbar());
137             view.setActionBar(actionbar);
138             view.setContent(form.asVaadinComponent());
139 
140             locationContext.location().observeNullable(l -> itemProviderStrategy.read().ifPresent(msg -> {
141                 valueContext.set(msg);
142                 form.populate(msg);
143                 Message castedMsg = (Message) msg;
144                 view.setCaption(castedMsg.getSubject());
145                 subAppDescriptor.setLabel(castedMsg.getSubject());
146                 notificationTypeValueProvider.getIcon(castedMsg).ifPresent(this::updateViewAndSubAppDescriptorIcons);
147             }));
148             localeContext.current().observeNullable(locale -> view.setContent(form.getLayout(locale)));
149         }
150 
151         return view;
152     }
153 
154     private void updateViewAndSubAppDescriptorIcons(MagnoliaIcons icon) {
155         getView().setIcon(icon);
156         subAppDescriptor.setIcon(icon.getCssClass());
157     }
158 
159 
160     @Override
161     public void locationChanged(Location location) {
162         super.locationChanged(location);
163         this.locationContext.location().set(ContentDetailSubApp.DetailLocation.wrap(location));
164     }
165 }