View Javadoc

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