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