View Javadoc
1   /**
2    * This file Copyright (c) 2013-2015 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.module.blossom.dialog;
35  
36  import info.magnolia.config.registry.DefinitionMetadata;
37  import info.magnolia.config.registry.DefinitionProvider;
38  import info.magnolia.config.registry.DefinitionRawView;
39  import info.magnolia.config.registry.Registry;
40  import info.magnolia.module.blossom.support.ExplicitIdDefinitionMetadataBuilder;
41  import info.magnolia.ui.admincentral.dialog.action.CancelDialogActionDefinition;
42  import info.magnolia.ui.admincentral.dialog.action.SaveDialogActionDefinition;
43  import info.magnolia.ui.dialog.definition.ConfiguredFormDialogDefinition;
44  import info.magnolia.ui.dialog.definition.DialogDefinition;
45  import info.magnolia.ui.dialog.registry.DefinitionTypes;
46  import info.magnolia.ui.form.definition.ConfiguredFormDefinition;
47  
48  import java.util.Collections;
49  import java.util.List;
50  
51  import org.slf4j.Logger;
52  import org.slf4j.LoggerFactory;
53  
54  /**
55   * Provides blossom dialogs by invoking methods on the factory object.
56   *
57   * @since 3.0
58   */
59  public class BlossomDialogDefinitionProvider implements DefinitionProvider<DialogDefinition> {
60  
61      private final Logger logger = LoggerFactory.getLogger(getClass());
62  
63      private BlossomDialogDescription dialogDescription;
64  
65      public BlossomDialogDefinitionProvider(BlossomDialogDescription dialogDescription) {
66          this.dialogDescription = dialogDescription;
67      }
68  
69      @Override
70      public DefinitionMetadata getMetadata() {
71          return new ExplicitIdDefinitionMetadataBuilder(dialogDescription.getId())
72                  .type(DefinitionTypes.DIALOG)
73                  .build();
74      }
75  
76      @Override
77      public DefinitionRawView getRaw() {
78          return DefinitionRawView.EMPTY;
79      }
80  
81      @Override
82      public boolean isValid() {
83          return true;
84      }
85  
86      @Override
87      public List<String> getErrorMessages() {
88          return Collections.emptyList();
89      }
90  
91      @Override
92      public DialogDefinition get() throws Registry.InvalidDefinitionException {
93  
94          DialogCreationContext context = DialogCreationContextHolder.get();
95          if (context == null) {
96  
97              ConfiguredFormDialogDefinition dialog = new ConfiguredFormDialogDefinition();
98              dialog.setId(dialogDescription.getId());
99              dialog.setLabel(dialogDescription.getFactoryMetaData().getLabel());
100             dialog.setI18nBasename(dialogDescription.getFactoryMetaData().getI18nBasename());
101             dialog.setPresenterClass(BlossomFormDialogPresenter.class);
102             dialog.setForm(new ConfiguredFormDefinition());
103 
104             return dialog;
105         }
106 
107         context.setId(dialogDescription.getId());
108 
109         DialogCreator dialogCreator = dialogDescription.getDialogCreator();
110         DialogFactoryMetaData factoryMetaData = dialogDescription.getFactoryMetaData();
111         try {
112             dialogCreator.createDialog(factoryMetaData, context);
113         } catch (Exception e) {
114             if (factoryMetaData.getFactoryMethod() != null) {
115                 logger.error("Failed to create dialog for method [" + factoryMetaData.getFactoryMethod() + "]", e);
116                 throw new Registry.InvalidDefinitionException(dialogDescription.getId());
117             } else {
118                 logger.error("Failed to create dialog for class [" + factoryMetaData.getFactoryObject().getClass() + "]", e);
119                 throw new Registry.InvalidDefinitionException(dialogDescription.getId());
120             }
121         }
122 
123         ConfiguredFormDialogDefinition dialog = context.getDialog();
124 
125         if (dialog.getActions().isEmpty()) {
126 
127             SaveDialogActionDefinition callbackAction = new SaveDialogActionDefinition();
128             callbackAction.setName("commit");
129             callbackAction.setLabel("save");
130             dialog.getActions().put(callbackAction.getName(), callbackAction);
131 
132             CancelDialogActionDefinition cancelAction = new CancelDialogActionDefinition();
133             cancelAction.setName("cancel");
134             cancelAction.setLabel("cancel");
135             dialog.getActions().put(cancelAction.getName(), cancelAction);
136         }
137 
138         return dialog;
139     }
140 }