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.task.app.view.detail;
35  
36  import info.magnolia.context.Context;
37  import info.magnolia.i18nsystem.I18nizer;
38  import info.magnolia.icons.MagnoliaIcons;
39  import info.magnolia.registry.RegistrationException;
40  import info.magnolia.task.Task;
41  import info.magnolia.task.app.actions.ArchiveTasksAction;
42  import info.magnolia.task.definition.TaskDefinition;
43  import info.magnolia.task.definition.registry.TaskDefinitionRegistry;
44  import info.magnolia.ui.admincentral.shellapp.pulse.item.definition.ItemViewDefinition;
45  import info.magnolia.ui.admincentral.shellapp.pulse.item.registry.ItemViewDefinitionRegistry;
46  import info.magnolia.ui.api.app.SubAppContext;
47  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
48  import info.magnolia.ui.api.location.Location;
49  import info.magnolia.ui.contentapp.browser.actions.ActionbarPresenter;
50  import info.magnolia.ui.contentapp.browser.actions.ActionbarView;
51  import info.magnolia.ui.contentapp.detail.AbstractDetailSubApp;
52  import info.magnolia.ui.contentapp.detail.ContentDetailSubApp;
53  import info.magnolia.ui.datasource.DatasourceDefinition;
54  import info.magnolia.ui.editor.LocaleContext;
55  import info.magnolia.ui.form.definition.DefinitionConverter;
56  import info.magnolia.ui.UIComponent;
57  import info.magnolia.ui.ValueContext;
58  import info.magnolia.ui.ViewDefinition;
59  import info.magnolia.ui.editor.ItemProviderStrategy;
60  import info.magnolia.ui.editor.FormView;
61  
62  import java.text.MessageFormat;
63  import java.util.Optional;
64  
65  import javax.inject.Inject;
66  
67  import org.apache.commons.lang3.BooleanUtils;
68  import org.apache.commons.lang3.StringUtils;
69  import org.slf4j.Logger;
70  import org.slf4j.LoggerFactory;
71  
72  import lombok.SneakyThrows;
73  
74  /**
75   * TaskDetailSubApp.
76   */
77  public class TaskDetailSubApp extends AbstractDetailSubApp<TaskFormView> {
78  
79      private static final Logger log = LoggerFactory.getLogger(ArchiveTasksAction.class);
80  
81      private final TaskDetailDescriptor subAppDescriptor;
82      private final ItemViewDefinitionRegistry itemViewDefinitionRegistry;
83      private final I18nizer i18nizer;
84      private final TaskDefinitionRegistry taskDefinitionRegistry;
85      private ContentDetailSubApp.LocationContext locationContext;
86      private ItemProviderStrategy<Task, ContentDetailSubApp.DetailLocation> itemProviderStrategy;
87      private ValueContext<Task> valueContext;
88      private final LocaleContext localeContext;
89      private final I18NAuthoringSupport i18NAuthoringSupport;
90      private DatasourceDefinition datasourceDefinition;
91  
92      @Inject
93      protected TaskDetailSubApp(SubAppContext subAppContext,
94                                 ItemViewDefinitionRegistry itemViewDefinitionRegistry,
95                                 I18nizer i18nizer,
96                                 TaskDefinitionRegistry taskDefinitionRegistry,
97                                 DatasourceDefinition datasourceDefinition,
98                                 TaskDetailDescriptor detailDescriptor,
99                                 ValueContext<Task> valueContext, LocaleContext localeContext, I18NAuthoringSupport i18NAuthoringSupport) {
100         super(subAppContext, new TaskFormView());
101         this.itemViewDefinitionRegistry = itemViewDefinitionRegistry;
102         this.i18nizer = i18nizer;
103         this.taskDefinitionRegistry = taskDefinitionRegistry;
104         this.subAppDescriptor = detailDescriptor;
105         this.datasourceDefinition = datasourceDefinition;
106         this.valueContext = valueContext;
107         this.localeContext = localeContext;
108         this.i18NAuthoringSupport = i18NAuthoringSupport;
109     }
110 
111     @Override
112     @SneakyThrows(RegistrationException.class)
113     public TaskFormView start(Location location) {
114         super.start(location);
115 
116         final TaskFormView view = getView();
117 
118         this.locationContext = bindContext(ContentDetailSubApp.LocationContext.class);
119         this.locationContext.location().set(ContentDetailSubApp.DetailLocation.wrap(location));
120         this.itemProviderStrategy = create(subAppDescriptor.getItemProvider(), datasourceDefinition);
121         this.bindContext(LocaleContext.class).defaultLocale().set(getSubAppContext().getAuthoringLocale());
122 
123         Optional<Task> itemProvider = itemProviderStrategy.read(ContentDetailSubApp.DetailLocation.wrap(location));
124         if (itemProvider.isPresent()) {
125             Task task = itemProvider.get();
126             TaskDefinition taskDefinition = taskDefinitionRegistry.get(task.getName());
127 
128             String itemView = taskDefinition.getTaskView();
129             if (StringUtils.isEmpty(itemView)) {
130                 String workspace = (String) task.getContent().get("repository");
131                 String taskView = taskDefinition.getViewMapping().get(workspace);
132                 itemView = StringUtils.isNotBlank(taskView) ? taskView : taskDefinition.getViewMapping().get("default");
133             }
134 
135             ItemViewDefinition itemViewDefinition = itemViewDefinitionRegistry.get(itemView);
136             itemViewDefinition = i18nizer.decorate(itemViewDefinition);
137 
138             valueContext.set(task);
139 
140             localeContext.populateFromI18NAuthoringSupport(task, i18NAuthoringSupport);
141 
142             final FormView<Task> form = (FormView<Task>) create(DefinitionConverter.FORM.convert(itemViewDefinition.getForm()), localeContext);
143 
144             updateViewAndSubAppDescriptorIcons(taskDefinition, task);
145             view.setSizeFull();
146             view.addStyleName("detail");
147             view.setMargin(false);
148             view.setSpacing(false);
149             subAppDescriptor.setActions(itemViewDefinition.getActions());
150             ActionbarPresenter<Task> actionbarPresenter = create(ActionbarPresenter.class, subAppDescriptor.getActions(), itemViewDefinition.getActionbar(), valueContext);
151             view.bindInstance(UIComponent.class, getView());
152             ActionbarView actionbar = (ActionbarView) create(
153                     ViewDefinition.builder()
154                             .withImplementationClass(ActionbarView.class)
155                             .withName("actionbar")
156                             .build(), itemViewDefinition.getActionbar(), actionbarPresenter);
157             view.setActionBar(actionbar);
158             view.setContent(form.asVaadinComponent());
159 
160             locationContext.location().observeNullable(l -> itemProviderStrategy.read(ContentDetailSubApp.DetailLocation.wrap(location)).ifPresent(taskItem -> {
161                 form.populate(taskItem);
162                 valueContext.set(taskItem);
163                 try {
164                     TaskDefinition taskDef = taskDefinitionRegistry.get(taskItem.getName());
165                     updateViewAndSubAppDescriptorIcons(taskDef, taskItem);
166                 } catch (RegistrationException e) {
167                     log.debug("No task definition registered for name: {}", taskItem.getName());
168                 }
169             }));
170         }
171 
172         return view;
173     }
174 
175     @Override
176     public void locationChanged(Location location) {
177         super.locationChanged(location);
178         this.locationContext.location().set(ContentDetailSubApp.DetailLocation.wrap(location));
179     }
180 
181     @Override
182     public void stop() {
183         getView().destroy();
184     }
185 
186     private String taskTitle(TaskDefinition definition, Task task) {
187         String title = definition.getTitle();
188         if ("publish".equals(task.getName())) {
189             title = title + " {1} {2}";
190         } else {
191             return title;
192         }
193 
194         String workspace = (String) task.getContent().get(Context.ATTRIBUTE_REPOSITORY);
195         String path = (String) task.getContent().get(Context.ATTRIBUTE_PATH);
196         boolean isDeletion = BooleanUtils.toBooleanDefaultIfNull((Boolean) task.getContent().get("isDeletion"), false);
197         title = MessageFormat.format(title, BooleanUtils.toInteger(isDeletion), workspace, path);
198 
199         return title;
200     }
201 
202     private MagnoliaIcons taskIcon(Task task) {
203         if ("publish".equals(task.getName())) {
204             if (BooleanUtils.toBooleanDefaultIfNull((Boolean) task.getContent().get("isDeletion"), false)) {
205                 return MagnoliaIcons.UNPUBLISH;
206             }
207             return BooleanUtils.toBooleanDefaultIfNull((Boolean) task.getContent().get("recursive"), false) ? MagnoliaIcons.PUBLISH_INCL_SUB : MagnoliaIcons.PUBLISH;
208         }
209 
210         return MagnoliaIcons.WORK_ITEM;
211     }
212 
213     private void updateViewAndSubAppDescriptorIcons(TaskDefinition taskDefinition, Task taskItem) {
214         String title = taskTitle(taskDefinition, taskItem);
215         MagnoliaIcons newIcon = taskIcon(taskItem);
216         getView().setCaption(title);
217         getView().setIcon(newIcon);
218         subAppDescriptor.setLabel(title);
219         subAppDescriptor.setIcon(newIcon.getCssClass());
220     }
221 }