View Javadoc
1   /**
2    * This file Copyright (c) 2014-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.admincentral.shellapp.pulse.task.action;
35  
36  import static com.vaadin.server.Sizeable.Unit.PERCENTAGE;
37  
38  import info.magnolia.context.Context;
39  import info.magnolia.objectfactory.Components;
40  import info.magnolia.task.Task;
41  import info.magnolia.task.TasksManager;
42  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
43  import info.magnolia.ui.DialogBuilder;
44  import info.magnolia.ui.admincentral.shellapp.pulse.task.DefaultTaskDetailPresenter;
45  import info.magnolia.ui.api.action.ActionDefinition;
46  import info.magnolia.ui.api.context.UiContext;
47  import info.magnolia.ui.api.shell.Shell;
48  import info.magnolia.ui.contentapp.browser.context.ValueContext;
49  import info.magnolia.ui.dialog.DialogDefinitionRegistry;
50  import info.magnolia.ui.dialog.definition.FormDialogDefinition;
51  import info.magnolia.ui.dialog.formdialog.FormDialogPresenter;
52  import info.magnolia.ui.form.EditorCallback;
53  import info.magnolia.ui.form.definition.DefinitionConverter;
54  import info.magnolia.ui.form.definition.TabDefinition;
55  import info.magnolia.ui.field.FieldDefinition;
56  import info.magnolia.ui.field.factory.FormFieldFactory;
57  import info.magnolia.ui.framework.datasource.components.SelectedItems;
58  import info.magnolia.ui.vaadin.overlay.MessageStyleTypeEnum;
59  
60  import java.util.ArrayList;
61  import java.util.HashMap;
62  import java.util.List;
63  import java.util.Map;
64  import java.util.Optional;
65  
66  import com.vaadin.ui.Button;
67  import com.vaadin.ui.Component;
68  import com.vaadin.ui.FormLayout;
69  import com.vaadin.ui.Window;
70  import com.vaadin.v7.data.util.ObjectProperty;
71  import com.vaadin.v7.data.util.PropertysetItem;
72  
73  /**
74   * Action used to intercept the reject task workflow by a dialog asking for a comment.
75   */
76  public class RejectTaskAction extends AbstractTaskAction<RejectTaskActionDefinition> {
77  
78      private final FormDialogPresenter formDialogPresenter;
79      private final UiContext uiContext;
80      private final DialogDefinitionRegistry dialogDefinitionRegistry;
81      private final FormFieldFactory formFieldFactory;
82  
83      private Window dialogWindow;
84  
85      public RejectTaskAction(RejectTaskActionDefinition definition, SelectedItems<Task> selectedItems, TasksManager taskManager, Shell shell, DialogDefinitionRegistry dialogDefinitionRegistry, FormFieldFactory formFieldFactory, ValueContext<Task> valueContext) {
86          super(definition, selectedItems.getFirst().orElse(null), taskManager, shell, valueContext);
87          this.formDialogPresenter = null;
88          this.uiContext = null;
89          this.dialogDefinitionRegistry = dialogDefinitionRegistry;
90          this.formFieldFactory = formFieldFactory;
91      }
92  
93      /**
94       * @deprecated since 6.0. Use {@link #RejectTaskAction(RejectTaskActionDefinition, info.magnolia.ui.framework.datasource.components.SelectedItems, info.magnolia.task.TasksManager, info.magnolia.ui.api.shell.Shell, info.magnolia.ui.dialog.DialogDefinitionRegistry, info.magnolia.ui.field.factory.FormFieldFactory, info.magnolia.ui.contentapp.browser.context.ValueContext)} instead.
95       */
96      @Deprecated
97      public RejectTaskAction(RejectTaskActionDefinition definition, Task task, TasksManager taskManager, DefaultTaskDetailPresenter taskPresenter, FormDialogPresenter formDialogPresenter, UiContext uiContext, Shell shell) {
98          super(definition, task, taskManager, taskPresenter, shell);
99          this.formDialogPresenter = formDialogPresenter;
100         this.uiContext = uiContext;
101         dialogDefinitionRegistry = null;
102         formFieldFactory = null;
103     }
104 
105     @Override
106     protected void executeTask(final TasksManager taskManager, final Task task) {
107 
108         final PropertysetItem propertysetItem = new PropertysetItem();
109         propertysetItem.addItemProperty(Context.ATTRIBUTE_COMMENT, new ObjectProperty<String>(null, String.class));
110 
111         if (getTaskPresenter() == null) {
112             dialogDefinitionRegistry.getAllDefinitions().stream()
113                     .filter(definition -> definition.getId() != null && definition.getId().equalsIgnoreCase(getDefinition().getDialogName()))
114                     .findFirst()
115                     .ifPresent(dialogDefinition -> {
116                                 FormLayout formLayout = new FormLayout();
117 
118                                 ((FormDialogDefinition) dialogDefinition)
119                                         .getForm()
120                                         .getTabs()
121                                         .stream()
122                                         .map(TabDefinition::getFields)
123                                         .flatMap(List::stream)
124                                         .map(DefinitionConverter.FIELD::convert)
125                                         .map((FieldDefinition definition) -> (Component) formFieldFactory.createField(definition, Components.getComponent(I18NAuthoringSupport.class).getDefaultLocale()))
126                                         .forEach(field -> formLayout.addComponent((Component) field));
127 
128                                 dialogWindow = DialogBuilder.dialog()
129                                         .withTitle(dialogDefinition.getLabel())
130                                         .withContent(formLayout)
131                                         .withActions(dialogActions(dialogDefinition.getActions(), propertysetItem, taskManager, task))
132                                         .buildAndOpen();
133 
134                                 dialogWindow.setWidth(95, PERCENTAGE);
135                                 dialogWindow.addStyleNames("dialog");
136                             }
137                     );
138         } else {
139             formDialogPresenter.start(propertysetItem, getDefinition().getDialogName(), uiContext, new EditorCallback() {
140                 @Override
141                 public void onSuccess(String actionName) {
142                     RejectTaskAction.this.onSuccess(propertysetItem, taskManager, task);
143 
144                     formDialogPresenter.closeDialog();
145 
146                     getTaskPresenter().onNavigateToList();
147                 }
148 
149                 @Override
150                 public void onCancel() {
151                     formDialogPresenter.closeDialog();
152                 }
153             });
154         }
155     }
156 
157     private void onSuccess(final PropertysetItem propertysetItem, final TasksManager taskManager, final Task task) {
158         Object comment = propertysetItem.getItemProperty(Context.ATTRIBUTE_COMMENT).getValue();
159 
160         Map<String, Object> result = new HashMap<String, Object>();
161         result.put(ACTOR_ID, task.getActorId());
162         result.put(DECISION, getDefinition().getDecision());
163 
164         if (comment != null) {
165             result.put(Context.ATTRIBUTE_COMMENT, comment);
166         }
167 
168         taskManager.resolve(task.getId(), result);
169         log.debug("About to reject human task named [{}]", task.getName());
170 
171         updateView();
172 
173         getShell().openNotification(MessageStyleTypeEnum.INFO, true, getDefinition().getSuccessMessage());
174     }
175 
176     private List<Component> dialogActions(Map<String, ActionDefinition> actionDefinitionMap, final PropertysetItem propertysetItem, final TasksManager taskManager, final Task task) {
177         List<Component> actions = new ArrayList<>();
178         actionDefinitionMap.forEach((name, actionDefinition) -> {
179             String caption = Optional.ofNullable(actionDefinition.getLabel()).orElse(actionDefinition.getName());
180             Button button = new Button(caption);
181             button.addStyleName(actionDefinition.getName());
182             if ("commit".equals(actionDefinition.getName())) {
183                 button.addClickListener(e -> {
184                     onSuccess(propertysetItem, taskManager, task);
185                     dialogWindow.close();
186                 });
187             } else {
188                 button.addClickListener(e -> dialogWindow.close());
189             }
190             actions.add(button);
191         });
192 
193         return actions;
194     }
195 }