View Javadoc

1   /**
2    * This file Copyright (c) 2003-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.cms.gui.dialog;
35  
36  import info.magnolia.cms.core.Content;
37  import info.magnolia.objectfactory.ClassFactory;
38  import info.magnolia.objectfactory.Classes;
39  
40  import javax.jcr.RepositoryException;
41  import javax.servlet.http.HttpServletRequest;
42  import javax.servlet.http.HttpServletResponse;
43  import java.util.HashMap;
44  import java.util.Map;
45  
46  
47  /**
48   * Factory for dialogs. This class handles the registration of native dialogs mantaing a map of (control name | dialog
49   * class).
50   * @author Fabrizio Giustina
51   * @version $Revision$ ($Author$)
52   */
53  public final class DialogFactory {
54      private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DialogFactory.class);
55  
56  
57      /**
58       * Registered controls.
59       */
60      private static Map<String, Class<DialogControl>> controls = new HashMap<String, Class<DialogControl>>();
61  
62      /**
63       * Utility class, don't instantiate.
64       */
65      private DialogFactory() {
66          // unused
67      }
68  
69      /**
70       * Register a new dialog.
71       * @param name dialog name (e.g. "richEdit")
72       * @param dialogClass implementing class. Must implements <code>info.magnolia.cms.gui.dialog.DialogControl</code>
73       * @see info.magnolia.cms.gui.dialog.DialogControl
74       */
75      public static void registerDialog(String name, Class<DialogControl> dialogClass) {
76          // @todo synchronize
77  
78          log.debug("Registering control [{}]", name); //$NON-NLS-1$
79  
80          controls.put(name, dialogClass);
81      }
82  
83      /**
84       * Load and initialize a dialog.
85       * @param storageNode current website node
86       * @param configNode configuration node for the dialog. The type of the dialog is read from the "controlType"
87       * nodeData
88       * @throws RepositoryException for errors during initialization of dialog with repository data
89       */
90      public static DialogControl loadDialog(HttpServletRequest request, HttpServletResponse response,
91                                             Content storageNode, Content configNode) throws RepositoryException {
92          String controlType = configNode.getNodeData("controlType").getString(); //$NON-NLS-1$
93  
94          return getDialogControlInstanceByName(request, response, storageNode, configNode, controlType);
95      }
96  
97      public static Dialog getDialogInstance(HttpServletRequest request, HttpServletResponse response,
98                                             Content storageNode, Content configNode) throws RepositoryException {
99          Dialog dialog = new Dialog();
100         dialog.init(request, response, storageNode, configNode);
101         return dialog;
102     }
103 
104     public static DialogStatic getDialogStaticInstance(HttpServletRequest request, HttpServletResponse response,
105                                                        Content storageNode, Content configNode) throws RepositoryException {
106         DialogStatic dialog = new DialogStatic();
107         dialog.init(request, response, storageNode, configNode);
108         return dialog;
109     }
110 
111     public static DialogHidden getDialogHiddenInstance(HttpServletRequest request, HttpServletResponse response,
112                                                        Content storageNode, Content configNode) throws RepositoryException {
113         DialogHidden dialog = new DialogHidden();
114         dialog.init(request, response, storageNode, configNode);
115         return dialog;
116     }
117 
118     public static DialogEdit getDialogEditInstance(HttpServletRequest request, HttpServletResponse response,
119                                                    Content storageNode, Content configNode) throws RepositoryException {
120         DialogEdit dialog = new DialogEdit();
121         dialog.init(request, response, storageNode, configNode);
122         return dialog;
123     }
124 
125     public static DialogButton getDialogButtonInstance(HttpServletRequest request, HttpServletResponse response,
126                                                        Content storageNode, Content configNode) throws RepositoryException {
127         DialogButton dialog = new DialogButton();
128         dialog.init(request, response, storageNode, configNode);
129         return dialog;
130     }
131 
132     public static DialogPassword getDialogPasswordInstance(HttpServletRequest request, HttpServletResponse response,
133                                                            Content storageNode, Content configNode) throws RepositoryException {
134         DialogPassword dialog = new DialogPassword();
135         dialog.init(request, response, storageNode, configNode);
136         return dialog;
137     }
138 
139     public static DialogButtonSet getDialogButtonSetInstance(HttpServletRequest request, HttpServletResponse response,
140                                                              Content storageNode, Content configNode) throws RepositoryException {
141         DialogButtonSet dialog = new DialogButtonSet();
142         dialog.init(request, response, storageNode, configNode);
143         return dialog;
144     }
145 
146     public static DialogInclude getDialogIncludeInstance(HttpServletRequest request, HttpServletResponse response,
147                                                          Content storageNode, Content configNode) throws RepositoryException {
148         DialogInclude dialog = new DialogInclude();
149         dialog.init(request, response, storageNode, configNode);
150         return dialog;
151     }
152 
153     public static DialogSelect getDialogSelectInstance(HttpServletRequest request, HttpServletResponse response,
154                                                        Content storageNode, Content configNode) throws RepositoryException {
155         DialogSelect dialog = new DialogSelect();
156         dialog.init(request, response, storageNode, configNode);
157         return dialog;
158     }
159 
160     /**
161      * Get a instance by the control type name. Those name class mappings are configured in the admin interface
162      * configuration.
163      * @param request
164      * @param response
165      * @param storageNode the node holding the data (can be null)
166      * @param configNode the node holding the configuration (can be null)
167      * @param controlType the name of the control
168      * @return the conrol
169      * @throws RepositoryException
170      */
171     public static DialogControl getDialogControlInstanceByName(HttpServletRequest request, HttpServletResponse response,
172                                                                Content storageNode, Content configNode, String controlType) throws RepositoryException {
173 
174         final ClassFactory classFactory = Classes.getClassFactory();
175         Class<DialogControl> dialogClass = controls.get(controlType);
176 
177         if (dialogClass == null) {
178             try {
179                 dialogClass = classFactory.forName(controlType);
180             }
181             catch (ClassNotFoundException e) {
182                 throw new IllegalArgumentException("Unknown control type: \"" + controlType + "\""); //$NON-NLS-1$ //$NON-NLS-2$
183             }
184         }
185 
186         // classFactory.newInstance wraps instantation exception in MgnlInstantiationException, which is a runtime, so no need to catch/wrap here again.
187         final DialogControl control = classFactory.newInstance(dialogClass);
188         control.init(request, response, storageNode, configNode);
189         return control;
190     }
191 }