View Javadoc

1   /**
2    * This file Copyright (c) 2012-2013 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.contentapp.detail;
35  
36  import info.magnolia.event.EventBus;
37  import info.magnolia.i18nsystem.I18nizer;
38  import info.magnolia.i18nsystem.SimpleTranslator;
39  import info.magnolia.objectfactory.ComponentProvider;
40  import info.magnolia.ui.api.action.ActionDefinition;
41  import info.magnolia.ui.api.action.ActionExecutionException;
42  import info.magnolia.ui.api.action.ActionExecutor;
43  import info.magnolia.ui.api.app.SubAppContext;
44  import info.magnolia.ui.api.event.AdmincentralEventBus;
45  import info.magnolia.ui.api.event.ContentChangedEvent;
46  import info.magnolia.ui.api.message.Message;
47  import info.magnolia.ui.api.message.MessageType;
48  import info.magnolia.ui.contentapp.definition.EditorDefinition;
49  import info.magnolia.ui.dialog.DialogView;
50  import info.magnolia.ui.dialog.actionarea.ActionListener;
51  import info.magnolia.ui.dialog.actionarea.EditorActionAreaPresenter;
52  import info.magnolia.ui.dialog.actionarea.definition.FormActionItemDefinition;
53  import info.magnolia.ui.dialog.actionarea.view.EditorActionAreaView;
54  import info.magnolia.ui.dialog.formdialog.FormBuilder;
55  import info.magnolia.ui.dialog.formdialog.FormView;
56  import info.magnolia.ui.form.EditorCallback;
57  import info.magnolia.ui.form.EditorValidator;
58  import info.magnolia.ui.form.definition.FormDefinition;
59  import info.magnolia.ui.form.definition.TabDefinition;
60  import info.magnolia.ui.form.field.definition.ConfiguredFieldDefinition;
61  import info.magnolia.ui.form.field.definition.FieldDefinition;
62  import info.magnolia.ui.framework.app.SubAppActionExecutor;
63  import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
64  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
65  
66  import java.util.LinkedList;
67  import java.util.List;
68  import java.util.Map;
69  
70  import javax.inject.Inject;
71  import javax.inject.Named;
72  
73  import org.slf4j.Logger;
74  import org.slf4j.LoggerFactory;
75  
76  import com.rits.cloning.Cloner;
77  
78  /**
79   * Presenter for the item displayed in the {@link info.magnolia.ui.contentapp.detail.DetailEditorPresenter}. Takes care
80   * of building and switching between the right {@link DetailView.ViewType}.
81   */
82  public class DetailPresenter implements EditorCallback, EditorValidator, ActionListener {
83  
84      private Logger log = LoggerFactory.getLogger(getClass());
85  
86      private SubAppContext subAppContext;
87  
88      private final EventBus eventBus;
89  
90      private final DetailView view;
91  
92      private FormBuilder formBuilder;
93  
94      private ComponentProvider componentProvider;
95  
96      private ActionExecutor executor;
97  
98      private EditorDefinition editorDefinition;
99  
100     private JcrNodeAdapter item;
101 
102     private DialogView dialogView;
103 
104     private final I18nizer i18nizer;
105 
106     private final SimpleTranslator i18n;
107 
108     @Inject
109     public DetailPresenter(SubAppContext subAppContext, final @Named(AdmincentralEventBus.NAME) EventBus eventBus, DetailView view,
110             FormBuilder formBuilder, ComponentProvider componentProvider, SubAppActionExecutor executor, I18nizer i18nizer, SimpleTranslator i18n) {
111         this.subAppContext = subAppContext;
112         this.eventBus = eventBus;
113         this.view = view;
114         this.formBuilder = formBuilder;
115         this.componentProvider = componentProvider;
116         this.executor = executor;
117         this.i18nizer = i18nizer;
118         this.i18n = i18n;
119     }
120 
121     public DetailView start(EditorDefinition editorDefinition, final JcrNodeAdapter item, DetailView.ViewType viewType) {
122         this.editorDefinition = editorDefinition;
123         this.item = item;
124         setItemView(viewType);
125         return view;
126     }
127 
128     private void setItemView(DetailView.ViewType viewType) {
129         FormView formView = componentProvider.getComponent(FormView.class);
130         this.dialogView = formView;
131 
132         FormDefinition formDefinition;
133 
134         switch (viewType) {
135         case VIEW:
136             formDefinition = cloneFormDefinitionReadOnly(editorDefinition.getForm());
137             break;
138         case EDIT:
139         default:
140             formDefinition = editorDefinition.getForm();
141             break;
142         }
143 
144         initActions();
145         formBuilder.buildForm(formView, formDefinition, item, null);
146         view.setItemView(dialogView.asVaadinComponent(), viewType);
147     }
148 
149     private void initActions() {
150         EditorActionAreaPresenter editorActionAreaPresenter = componentProvider.newInstance(editorDefinition.getActionArea().getPresenterClass());
151         EditorActionAreaView editorActionAreaView = editorActionAreaPresenter.start(filterSubAppActions(), editorDefinition.getActionArea(), this, subAppContext);
152         dialogView.setActionAreaView(editorActionAreaView);
153     }
154 
155     private Iterable<ActionDefinition> filterSubAppActions() {
156         Map<String, ActionDefinition> subAppActions = subAppContext.getSubAppDescriptor().getActions();
157         List<ActionDefinition> filteredActions = new LinkedList<ActionDefinition>();
158         List<FormActionItemDefinition> editorActions = editorDefinition.getActions();
159         boolean isJcrItemAdapter = (item instanceof JcrItemAdapter);
160         for (FormActionItemDefinition editorAction : editorActions) {
161             ActionDefinition def = subAppActions.get(editorAction.getName());
162             if (def != null && (!isJcrItemAdapter || (isJcrItemAdapter && executor.isAvailable(editorAction.getName(), ((JcrItemAdapter) item).getJcrItem())))) {
163                 filteredActions.add(def);
164             } else {
165                 log.debug("Action is configured for an editor but not configured for sub-app: " + editorAction.getName());
166             }
167         }
168         return filteredActions;
169     }
170 
171     /**
172      * Return clone of a form definition with all fields definitions set to read only.
173      *
174      * @see ConfiguredFieldDefinition#setReadOnly(boolean)
175      */
176     private FormDefinition cloneFormDefinitionReadOnly(FormDefinition formDefinition) {
177         Cloner cloner = new Cloner();
178         FormDefinition formDefinitionClone = cloner.deepClone(formDefinition);
179 
180         for (TabDefinition tab : formDefinitionClone.getTabs()) {
181             for (FieldDefinition field : tab.getFields()) {
182                 ((ConfiguredFieldDefinition) field).setReadOnly(true);
183             }
184         }
185 
186         return formDefinitionClone;
187     }
188 
189     @Override
190     public void onCancel() {
191         // setItemView(ItemView.ViewType.VIEW);
192         subAppContext.close();
193     }
194 
195     @Override
196     public void onSuccess(String actionName) {
197         eventBus.fireEvent(new ContentChangedEvent(item.getWorkspace(), item.getItemId()));
198         // setItemView(ItemView.ViewType.VIEW);
199         subAppContext.close();
200     }
201 
202     @Override
203     public void showValidation(boolean visible) {
204         if (dialogView instanceof FormView) {
205             ((FormView) dialogView).showValidation(visible);
206         }
207     }
208 
209     @Override
210     public boolean isValid() {
211         return dialogView instanceof FormView ? ((FormView) dialogView).isValid() : true;
212     }
213 
214     @Override
215     public void onActionFired(String actionName, Object... actionContextParams) {
216         Object[] providedParameters = new Object[] { this, item };
217         Object[] combinedParameters = new Object[providedParameters.length + actionContextParams.length];
218         System.arraycopy(providedParameters, 0, combinedParameters, 0, providedParameters.length);
219         System.arraycopy(actionContextParams, 0, combinedParameters, providedParameters.length, actionContextParams.length);
220         try {
221             executor.execute(actionName, combinedParameters);
222         } catch (ActionExecutionException e) {
223             log.error("An error occurred while executing an action.", e);
224             Message error = new Message(MessageType.ERROR, i18n.translate("ui-contentapp.error.action.execution"), e.getMessage());
225             subAppContext.getAppContext().broadcastMessage(error);
226         }
227     }
228 }