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