View Javadoc
1   /**
2    * This file Copyright (c) 2012-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.dialog.formdialog;
35  
36  import info.magnolia.i18nsystem.I18nizer;
37  import info.magnolia.i18nsystem.SimpleTranslator;
38  import info.magnolia.objectfactory.ComponentProvider;
39  import info.magnolia.ui.api.action.ActionDefinition;
40  import info.magnolia.ui.api.app.SubAppContext;
41  import info.magnolia.ui.api.availability.AvailabilityChecker;
42  import info.magnolia.ui.api.context.UiContext;
43  import info.magnolia.ui.api.overlay.OverlayCloser;
44  import info.magnolia.ui.dialog.BaseDialogPresenter;
45  import info.magnolia.ui.dialog.Dialog;
46  import info.magnolia.ui.dialog.DialogView;
47  import info.magnolia.ui.dialog.actionarea.DialogActionExecutor;
48  import info.magnolia.ui.dialog.definition.FormDialogDefinition;
49  import info.magnolia.ui.dialog.registry.DialogDefinitionRegistry;
50  import info.magnolia.ui.form.EditorCallback;
51  import info.magnolia.ui.form.EditorValidator;
52  import info.magnolia.ui.form.FormPresenter;
53  import info.magnolia.ui.vaadin.integration.contentconnector.ContentConnector;
54  
55  import java.util.ArrayList;
56  import java.util.Arrays;
57  import java.util.Iterator;
58  import java.util.List;
59  import java.util.Locale;
60  import java.util.Optional;
61  
62  import javax.inject.Inject;
63  
64  import org.apache.commons.lang3.StringUtils;
65  import org.slf4j.Logger;
66  import org.slf4j.LoggerFactory;
67  
68  import com.vaadin.v7.data.Item;
69  import com.vaadin.v7.data.Property;
70  
71  /**
72   * Presenter for forms opened inside dialogs.
73   */
74  public class FormDialogPresenterImpl extends BaseDialogPresenter implements FormDialogPresenter, EditorValidator {
75  
76      private static final Logger log = LoggerFactory.getLogger(FormDialogPresenterImpl.class);
77  
78      private EditorCallback callback;
79  
80      private final DialogDefinitionRegistry dialogDefinitionRegistry;
81  
82      private ContentConnector contentConnector;
83  
84      private final AvailabilityChecker checker;
85  
86      private final FormPresenter formPresenter;
87  
88      private final FormView formView;
89  
90      private Item item;
91  
92      @Inject
93      public FormDialogPresenterImpl(final DialogDefinitionRegistry dialogDefinitionRegistry, ComponentProvider componentProvider, DialogActionExecutor executor, FormView view, I18nizer i18nizer, SimpleTranslator i18n, AvailabilityChecker checker, ContentConnector contentConnector, FormPresenter formPresenter) {
94          super(componentProvider, executor, view, i18nizer, i18n);
95          this.dialogDefinitionRegistry = dialogDefinitionRegistry;
96          this.checker = checker;
97          this.contentConnector = contentConnector;
98          this.formPresenter = formPresenter;
99          this.componentProvider = componentProvider;
100         this.formView = view;
101     }
102 
103     /**
104      * @deprecated since 5.4.2 - use {@link #FormDialogPresenterImpl(DialogDefinitionRegistry, ComponentProvider, DialogActionExecutor, FormView, I18nizer, SimpleTranslator, AvailabilityChecker, ContentConnector, FormPresenter)} instead.
105      */
106     @Deprecated
107     public FormDialogPresenterImpl(final DialogDefinitionRegistry dialogDefinitionRegistry, FormBuilder formBuilder, ComponentProvider componentProvider, DialogActionExecutor executor, FormView view, I18nizer i18nizer, SimpleTranslator i18n, AvailabilityChecker checker, ContentConnector contentConnector) {
108         this(dialogDefinitionRegistry, componentProvider, executor, view, i18nizer, i18n, checker, contentConnector, componentProvider.getComponent(FormPresenter.class));
109     }
110 
111     @Override
112     public DialogView start(Item item, FormDialogDefinition dialogDefinition, UiContext uiContext, EditorCallback callback, ContentConnector contentConnector) {
113         this.contentConnector = contentConnector;
114         return start(item, dialogDefinition, uiContext, callback);
115     }
116 
117     @Override
118     public DialogView start(Item item, String dialogId, final UiContext uiContext, EditorCallback callback) {
119         FormDialogDefinitionolia/ui/dialog/definition/FormDialogDefinition.html#FormDialogDefinition">FormDialogDefinition dialogDefinition = (FormDialogDefinition) dialogDefinitionRegistry.getProvider(dialogId).get();
120         return start(item, dialogDefinition, uiContext, callback);
121     }
122 
123     /**
124      * Returns a {@link DialogView} containing {@link FormView} as content.
125      * <ul>
126      * <li>Sets the created {@link FormView} as content of the created {@link DialogView}.</li>
127      * </ul>
128      *
129      * @param item
130      * @param dialogDefinition
131      * @param uiContext
132      */
133     @Override
134     public DialogView start(Item item, FormDialogDefinition dialogDefinition, final UiContext uiContext, EditorCallback callback) {
135         Locale initialLocale = asSubAppContext(uiContext).map(SubAppContext::getAuthoringLocale).orElse(null);
136 
137         this.callback = new EditorCallback() {
138             @Override
139             public void onCancel() {
140                 asSubAppContext(uiContext).ifPresent(subAppContext -> subAppContext.setAuthoringLocale(initialLocale));
141                 callback.onCancel();
142             }
143 
144             @Override
145             public void onSuccess(String actionName) {
146                 callback.onSuccess(actionName);
147             }
148         };
149 
150         this.item = item;
151 
152         super.start(dialogDefinition, uiContext);
153         getExecutor().setDialogDefinition(getDefinition());
154         buildView(getDefinition());
155 
156         final OverlayCloser overlayCloser = uiContext.openOverlay(getView(), getView().getModalityLevel());
157         getView().addDialogCloseHandler(dialogView -> overlayCloser.close());
158         getView().setClosable(true);
159         return getView();
160     }
161 
162     private void buildView(FormDialogDefinition dialogDefinition) {
163         final Dialogialog.html#Dialog">Dialog dialog = new Dialog(dialogDefinition);
164         formPresenter.presentView(formView, dialogDefinition.getForm(), item, dialog);
165 
166         final String description = dialogDefinition.getDescription();
167         final String label = dialogDefinition.getLabel();
168 
169         if (StringUtils.isNotBlank(description) && !isMessageKey(description)) {
170             getView().setDescription(description);
171         }
172 
173         if (StringUtils.isNotBlank(dialogDefinition.getDialogLabelItemProperty()) && item != null) {
174             Property labelProperty = item.getItemProperty(dialogDefinition.getDialogLabelItemProperty());
175             if (labelProperty != null && labelProperty.getValue() != null) {
176                 getView().setCaption(String.valueOf(labelProperty.getValue()));
177             }
178         } else if (StringUtils.isNotBlank(label) && !isMessageKey(label)) {
179             getView().setCaption(label);
180         }
181     }
182 
183     @Override
184     public FormView getView() {
185         return formView;
186     }
187 
188     @Override
189     public void showValidation(boolean visible) {
190         getView().showValidation(visible);
191     }
192 
193     @Override
194     public boolean isValid() {
195         return formPresenter.isValid();
196     }
197 
198     @Override
199     protected DialogActionExecutor getExecutor() {
200         return (DialogActionExecutor) super.getExecutor();
201     }
202 
203     @Override
204     protected Iterable<ActionDefinition> filterActions() {
205         List<ActionDefinition> actions = new ArrayList<ActionDefinition>(getDefinition().getActions().values());
206         Iterator<ActionDefinition> it = actions.iterator();
207         Object itemId = contentConnector.getItemId(item);
208         if (itemId != null) {
209             while (it.hasNext()) {
210                 ActionDefinition action = it.next();
211                 if (!checker.isAvailable(action.getAvailability(), Arrays.asList(itemId))) {
212                     it.remove();
213                 }
214             }
215         } else {
216             log.info("Could not resolve itemId for item {}:{}, will not restrict availability of dialog actions.", item.getClass().getName(), item);
217         }
218         return actions;
219     }
220 
221     @Override
222     protected Object[] getActionParameters(String actionName) {
223         return new Object[] { this, item, callback };
224     }
225 
226     @Override
227     protected FormDialogDefinition getDefinition() {
228         return (FormDialogDefinition) super.getDefinition();
229     }
230 
231     /**
232      * @deprecated is a hack and should not be used. See MGNLUI-2207.
233      */
234     private boolean isMessageKey(final String text) {
235         return !text.contains(" ") && text.contains(".") && !text.endsWith(".");
236     }
237 
238     @Override
239     protected void onCancel () {
240         if (callback != null) {
241             callback.onCancel();
242         }
243     }
244 
245     @Override
246     public Item getRelatedItem() {
247         return item;
248     }
249 
250     private Optional<SubAppContext> asSubAppContext(UiContext uiContext) {
251         if (uiContext instanceof SubAppContext) {
252             return Optional.of((SubAppContext) uiContext);
253         }
254         return Optional.empty();
255     }
256 }