1. Project Clover database Fri Nov 20 2015 15:10:00 CET
  2. Package info.magnolia.module.blossom.dialog

File BlossomDialogDefinitionProvider.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
41% of files have more coverage

Code metrics

6
31
2
1
140
80
6
0.19
15.5
2
3
17% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
BlossomDialogDefinitionProvider 59 31 17% 6 8
0.794871879.5%
 

Contributing tests

This file is covered by 7 tests. .

Source view

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  12 toggle public BlossomDialogDefinitionProvider(BlossomDialogDescription dialogDescription) {
66  12 this.dialogDescription = dialogDescription;
67    }
68   
 
69    toggle @Override
70    public DefinitionMetadata getMetadata() {
71    return new ExplicitIdDefinitionMetadataBuilder(dialogDescription.getId())
72    .type(DefinitionTypes.DIALOG)
73    .build();
74    }
75   
 
76    toggle @Override
77    public DefinitionRawView getRaw() {
78    return DefinitionRawView.EMPTY;
79    }
80   
 
81    toggle @Override
82    public boolean isValid() {
83    return true;
84    }
85   
 
86    toggle @Override
87    public List<String> getErrorMessages() {
88    return Collections.emptyList();
89    }
90   
 
91  7 toggle @Override
92    public DialogDefinition get() throws Registry.InvalidDefinitionException {
93   
94  7 DialogCreationContext context = DialogCreationContextHolder.get();
95  7 if (context == null) {
96   
97  1 ConfiguredFormDialogDefinition dialog = new ConfiguredFormDialogDefinition();
98  1 dialog.setId(dialogDescription.getId());
99  1 dialog.setLabel(dialogDescription.getFactoryMetaData().getLabel());
100  1 dialog.setI18nBasename(dialogDescription.getFactoryMetaData().getI18nBasename());
101  1 dialog.setPresenterClass(BlossomFormDialogPresenter.class);
102  1 dialog.setForm(new ConfiguredFormDefinition());
103   
104  1 return dialog;
105    }
106   
107  6 context.setId(dialogDescription.getId());
108   
109  6 DialogCreator dialogCreator = dialogDescription.getDialogCreator();
110  6 DialogFactoryMetaData factoryMetaData = dialogDescription.getFactoryMetaData();
111  6 try {
112  6 dialogCreator.createDialog(factoryMetaData, context);
113    } catch (Exception e) {
114  0 if (factoryMetaData.getFactoryMethod() != null) {
115  0 logger.error("Failed to create dialog for method [" + factoryMetaData.getFactoryMethod() + "]", e);
116  0 throw new Registry.InvalidDefinitionException(dialogDescription.getId());
117    } else {
118  0 logger.error("Failed to create dialog for class [" + factoryMetaData.getFactoryObject().getClass() + "]", e);
119  0 throw new Registry.InvalidDefinitionException(dialogDescription.getId());
120    }
121    }
122   
123  6 ConfiguredFormDialogDefinition dialog = context.getDialog();
124   
125  6 if (dialog.getActions().isEmpty()) {
126   
127  6 SaveDialogActionDefinition callbackAction = new SaveDialogActionDefinition();
128  6 callbackAction.setName("commit");
129  6 callbackAction.setLabel("save");
130  6 dialog.getActions().put(callbackAction.getName(), callbackAction);
131   
132  6 CancelDialogActionDefinition cancelAction = new CancelDialogActionDefinition();
133  6 cancelAction.setName("cancel");
134  6 cancelAction.setLabel("cancel");
135  6 dialog.getActions().put(cancelAction.getName(), cancelAction);
136    }
137   
138  6 return dialog;
139    }
140    }