1 13 package info.magnolia.module.admininterface; 14 15 import info.magnolia.cms.beans.config.ObservedManager; 16 import info.magnolia.cms.beans.config.Paragraph; 17 import info.magnolia.cms.beans.config.ParagraphManager; 18 import info.magnolia.cms.core.Content; 19 import info.magnolia.cms.core.ItemType; 20 import info.magnolia.cms.util.ClassUtil; 21 import info.magnolia.cms.util.ContentUtil; 22 import info.magnolia.cms.util.FactoryUtil; 23 import info.magnolia.cms.util.NodeDataUtil; 24 import info.magnolia.module.admininterface.dialogs.ParagraphEditDialog; 25 26 import java.lang.reflect.Constructor ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.Map.Entry; 32 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 36 import org.apache.commons.lang.StringUtils; 37 import org.slf4j.Logger; 38 import org.slf4j.LoggerFactory; 39 40 41 46 public class DialogHandlerManager extends ObservedManager { 47 48 private static final String PARAGRAPH_EDIT_DIALOG = "info.magnolia.module.admininterface.dialogs.ParagraphEditDialog"; 49 50 private static final String CLASS = "class"; 51 52 private static final String ND_NAME = "name"; 53 54 57 private Logger log = LoggerFactory.getLogger(DialogHandlerManager.class); 58 59 62 private final Map dialogHandlers = new HashMap (); 63 64 68 protected void onRegister(Content node) { 69 List dialogs = ContentUtil.collectAllChildren(node, ItemType.CONTENT); 70 for (Iterator iter = dialogs.iterator(); iter.hasNext();) { 71 Content dialog = (Content) iter.next(); 72 registerAsParagraphDialog(node.getHandle(), dialog); 74 75 String name = dialog.getNodeData(ND_NAME).getString(); 76 if (StringUtils.isEmpty(name)) { 77 name = dialog.getName(); 78 } 79 String className = NodeDataUtil.getString(dialog, CLASS, PARAGRAPH_EDIT_DIALOG); 80 try { 81 registerDialogHandler(name, ClassUtil.classForName(className), dialog); 83 } 84 catch (ClassNotFoundException e) { 85 log.warn("can't find dialog handler class " + className, e); } 87 } 88 } 89 90 94 private void registerAsParagraphDialog(String basePath, Content dialog) { 95 96 String dialogPath = StringUtils.removeStart(dialog.getHandle(), basePath + "/"); 97 String dialogName = dialog.getNodeData(ND_NAME).getString(); 98 if (StringUtils.isEmpty(dialogName)) { 99 dialogName = dialog.getName(); 100 } 101 102 Map paragraphs = ParagraphManager.getInstance().getParagraphs(); 103 for (Iterator iter = paragraphs.entrySet().iterator(); iter.hasNext();) { 104 Paragraph paragraph = (Paragraph) ((Entry) iter.next()).getValue(); 105 String paragraphDialogPath = paragraph.getDialogPath(); 106 String paragraphDialogName = paragraph.getDialog(); 107 108 if (StringUtils.equals(paragraphDialogPath, dialogPath) 109 || StringUtils.equals(paragraphDialogName, dialogName)) { 110 Class handler = ParagraphEditDialog.class; 111 112 String className = dialog.getNodeData(CLASS).getString(); 113 if (StringUtils.isNotEmpty(className)) { 114 try { 115 handler = ClassUtil.classForName(className); 116 } 117 catch (ClassNotFoundException e) { 118 log.error("Registering paragraph: class [" + className + "] not found", e); } 120 } 121 122 registerDialogHandler(paragraph.getName(), handler, dialog); 123 } 124 } 125 126 } 127 128 protected void onClear() { 129 this.dialogHandlers.clear(); 130 } 131 132 protected void registerDialogHandler(String name, Class dialogHandler, Content configNode) { 133 if (log.isDebugEnabled()) { 134 log.debug("Registering dialog handler [{}]", name); } 136 dialogHandlers.put(name, new Object []{dialogHandler, configNode}); 138 } 139 140 public DialogMVCHandler getDialogHandler(String name, HttpServletRequest request, HttpServletResponse response) { 141 142 Object [] handlerConfig = (Object []) dialogHandlers.get(name); 143 144 if (handlerConfig == null) { 145 throw new InvalidDialogHandlerException(name); 146 } 147 148 return instantiateHandler(name, request, response, handlerConfig); 149 150 } 151 152 protected DialogMVCHandler instantiateHandler(String name, HttpServletRequest request, 153 HttpServletResponse response, Object [] handlerConfig) { 154 155 try { 156 157 Class dialogHandlerClass = (Class ) handlerConfig[0]; 158 Content configNode = (Content) handlerConfig[1]; 159 if (configNode != null) { 160 try { 161 Constructor constructor = dialogHandlerClass.getConstructor(new Class []{ 162 String .class, 163 HttpServletRequest .class, 164 HttpServletResponse .class, 165 Content.class}); 166 return (DialogMVCHandler) constructor 167 .newInstance(new Object []{name, request, response, configNode}); 168 } 169 catch (NoSuchMethodException e) { 170 Constructor constructor = dialogHandlerClass.getConstructor(new Class []{ 171 String .class, 172 HttpServletRequest .class, 173 HttpServletResponse .class}); 174 return (DialogMVCHandler) constructor.newInstance(new Object []{name, request, response}); 175 } 176 } 177 178 Constructor constructor = dialogHandlerClass.getConstructor(new Class []{ 179 String .class, 180 HttpServletRequest .class, 181 HttpServletResponse .class}); 182 return (DialogMVCHandler) constructor.newInstance(new Object []{name, request, response}); 183 } 184 catch (Exception e) { 185 throw new InvalidDialogHandlerException(name, e); 186 } 187 } 188 189 192 public static DialogHandlerManager getInstance() { 193 return (DialogHandlerManager) FactoryUtil.getSingleton(DialogHandlerManager.class); 194 } 195 196 } 197 | Popular Tags |