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.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
63
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 }