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.i18nsystem.I18nizer;
39  import info.magnolia.icons.MagnoliaIcons;
40  import info.magnolia.ui.admincentral.shellapp.pulse.item.definition.ItemViewDefinition;
41  import info.magnolia.ui.admincentral.shellapp.pulse.item.registry.ItemViewDefinitionRegistry;
42  import info.magnolia.ui.api.app.SubAppContext;
43  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
44  import info.magnolia.ui.api.location.Location;
45  import info.magnolia.ui.api.message.Message;
46  import info.magnolia.ui.contentapp.browser.actions.ActionbarPresenter;
47  import info.magnolia.ui.contentapp.browser.actions.ActionbarView;
48  import info.magnolia.ui.ValueContext;
49  import info.magnolia.ui.contentapp.detail.AbstractDetailSubApp;
50  import info.magnolia.ui.contentapp.detail.ContentDetailSubApp;
51  import info.magnolia.ui.contentapp.detail.DetailDescriptor;
52  import info.magnolia.ui.editor.LocaleContext;
53  import info.magnolia.ui.form.definition.DefinitionConverter;
54  import info.magnolia.ui.ViewDefinition;
55  import info.magnolia.ui.editor.ItemProviderStrategy;
56  import info.magnolia.ui.editor.FormView;
57  import info.magnolia.ui.datasource.DatasourceDefinition;
58  import info.magnolia.ui.framework.message.MessagesManager;
59  
60  import javax.inject.Inject;
61  
62  import org.apache.commons.lang3.StringUtils;
63  
64  
65  /**
66   * Getting closer to what could be a generic NotificationDetailSubApp.
67   */
68  public class NotificationDetailSubApp extends AbstractDetailSubApp<NotificationFormView> {
69  
70      public static final String DEFAULT_VIEW = "admincentral:default";
71  
72      private final DetailDescriptor<Message, ?> subAppDescriptor;
73      private final ItemViewDefinitionRegistry itemViewDefinitionRegistry;
74      private final I18nizer i18nizer;
75      private final NotificationTypeValueProvider notificationTypeValueProvider;
76      private final String currentUserName;
77      private final ValueContext<Message> valueContext;
78      private final MessagesManager messagesManager;
79      private final DatasourceDefinition datasourceDefinition;
80      private final LocaleContext localeContext;
81      private final I18NAuthoringSupport i18NAuthoringSupport;
82      private ContentDetailSubApp.LocationContext locationContext;
83      private ItemProviderStrategy<Message, ContentDetailSubApp.DetailLocation> itemProviderStrategy;
84  
85      @Inject
86      protected NotificationDetailSubApp(SubAppContext subAppContext,
87                                         ItemViewDefinitionRegistry itemViewDefinitionRegistry,
88                                         I18nizer i18nizer,
89                                         MessagesManager messagesManager,
90                                         Context context,
91                                         DatasourceDefinition datasourceDefinition,
92                                         DetailDescriptor<Message, ?> descriptor,
93                                         ValueContext<Message> valueContext,
94                                         LocaleContext localeContext, I18NAuthoringSupport i18NAuthoringSupport) {
95          super(subAppContext, new NotificationFormView());
96          this.messagesManager = messagesManager;
97          this.subAppDescriptor = descriptor;
98          this.itemViewDefinitionRegistry = itemViewDefinitionRegistry;
99          this.i18nizer = i18nizer;
100         this.valueContext = valueContext;
101         this.localeContext = localeContext;
102         this.i18NAuthoringSupport = i18NAuthoringSupport;
103         this.notificationTypeValueProvider = new NotificationTypeValueProvider();
104         this.currentUserName = context.getUser().getName();
105         this.datasourceDefinition = datasourceDefinition;
106     }
107 
108     @Override
109     public NotificationFormView start(Location location) {
110         super.start(location);
111 
112         final NotificationFormView view = getView();
113 
114         this.locationContext = bindContext(ContentDetailSubApp.LocationContext.class);
115         this.locationContext.location().set(ContentDetailSubApp.DetailLocation.wrap(location));
116         this.itemProviderStrategy = create(subAppDescriptor.getItemProvider(), datasourceDefinition);
117         this.bindContext(LocaleContext.class).defaultLocale().set(getSubAppContext().getAuthoringLocale());
118 
119         itemProviderStrategy.read(ContentDetailSubApp.DetailLocation.wrap(location)).ifPresent(message -> {
120             valueContext.set(message);
121             messagesManager.clearMessage(currentUserName, message.getId());
122 
123             String itemView = StringUtils.defaultString(message.getView(), DEFAULT_VIEW);
124 
125             ItemViewDefinition itemViewDefinition = itemViewDefinitionRegistry.getProvider(itemView).get();
126             itemViewDefinition = i18nizer.decorate(itemViewDefinition);
127 
128             localeContext.populateFromI18NAuthoringSupport(message, i18NAuthoringSupport);
129 
130             FormView<Message> form = (FormView<Message>) create(DefinitionConverter.FORM.convert(itemViewDefinition.getForm()), localeContext);
131             view.setCaption(message.getSubject());
132             subAppDescriptor.setLabel(message.getSubject());
133             notificationTypeValueProvider.getIcon(message).ifPresent(this::updateViewAndSubAppDescriptorIcons);
134 
135             view.setSizeFull();
136             view.addStyleName("detail");
137             view.setMargin(false);
138             view.setSpacing(false);
139 
140             subAppDescriptor.setActions(itemViewDefinition.getActions());
141 
142             ActionbarPresenter actionbarPresenter = create(ActionbarPresenter.class, subAppDescriptor.getActions(), itemViewDefinition.getActionbar());
143             ActionbarView actionbar = (ActionbarView) create(
144                     ViewDefinition.builder()
145                             .withImplementationClass(ActionbarView.class)
146                             .withName("actionbar")
147                             .build(), itemViewDefinition.getActionbar(), actionbarPresenter);
148             view.setActionBar(actionbar);
149             view.setContent(form.asVaadinComponent());
150 
151             locationContext.location().observeNullable(l -> itemProviderStrategy.read(l).ifPresent(msg -> {
152                 valueContext.set(msg);
153                 form.populate(msg);
154                 view.setCaption(msg.getSubject());
155                 subAppDescriptor.setLabel(msg.getSubject());
156                 notificationTypeValueProvider.getIcon(msg).ifPresent(this::updateViewAndSubAppDescriptorIcons);
157             }));
158         });
159 
160         return view;
161     }
162 
163     private void updateViewAndSubAppDescriptorIcons(MagnoliaIcons icon) {
164         getView().setIcon(icon);
165         subAppDescriptor.setIcon(icon.getCssClass());
166     }
167 
168     @Override
169     public void locationChanged(Location location) {
170         super.locationChanged(location);
171         this.locationContext.location().set(ContentDetailSubApp.DetailLocation.wrap(location));
172     }
173 }