1 13 package info.magnolia.cms.gui.dialog; 14 15 import info.magnolia.cms.core.Content; 16 import info.magnolia.cms.core.ContentHandler; 17 import info.magnolia.cms.core.ItemType; 18 import info.magnolia.cms.core.NodeData; 19 20 import java.io.IOException ; 21 import java.io.Writer ; 22 import java.util.ArrayList ; 23 import java.util.Date ; 24 import java.util.Hashtable ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 29 import javax.jcr.RepositoryException; 30 import javax.servlet.http.HttpServletRequest ; 31 import javax.servlet.http.HttpServletResponse ; 32 import javax.servlet.http.HttpSession ; 33 34 import org.apache.commons.lang.BooleanUtils; 35 import org.apache.commons.lang.StringUtils; 36 import org.apache.log4j.Logger; 37 38 39 43 public abstract class DialogSuper implements DialogInterface { 44 45 public static final String SESSION_ATTRIBUTENAME_DIALOGOBJECT = "mgnlSessionAttribute"; 47 public static final String SESSION_ATTRIBUTENAME_DIALOGOBJECT_REMOVE = "mgnlSessionAttributeRemove"; 49 52 private static Logger log = Logger.getLogger(DialogSuper.class); 53 54 57 private HttpServletRequest request; 58 59 62 private HttpServletResponse response; 63 64 67 private Content websiteNode; 68 69 72 private Map config = new Hashtable (); 73 74 77 private List subs = new ArrayList (); 78 79 82 private List options = new ArrayList (); 83 84 private String id = "mgnlControl"; 86 private String value; 87 88 91 private List values = new ArrayList (); 92 93 private DialogSuper parent; 94 95 private DialogSuper topParent; 96 97 99 public void init(HttpServletRequest request, HttpServletResponse response, Content websiteNode, Content configNode) 100 throws RepositoryException { 101 102 if (log.isDebugEnabled()) { 103 log.debug("Init " + getClass().getName()); } 105 106 this.websiteNode = websiteNode; 107 this.request = request; 108 this.response = response; 109 110 this.initializeConfig(configNode); 111 } 112 113 116 public void drawHtml(Writer out) throws IOException { 117 this.drawHtmlPreSubs(out); 118 this.drawSubs(out); 119 this.drawHtmlPostSubs(out); 120 } 121 122 public void addSub(Object o) { 123 this.getSubs().add(o); 124 } 125 126 public void setConfig(String key, String value) { 127 if (value != null) { 128 this.config.put(key, value); 129 } 130 } 131 132 public void setConfig(String key, boolean value) { 133 this.config.put(key, BooleanUtils.toBooleanObject(value).toString()); 134 } 135 136 public void setConfig(String key, int value) { 137 this.config.put(key, Integer.toString(value)); 138 } 139 140 public String getConfigValue(String key, String nullValue) { 141 if (this.config.containsKey(key)) { 142 return (String ) this.config.get(key); 143 } 144 145 return nullValue; 146 } 147 148 public String getConfigValue(String key) { 149 return this.getConfigValue(key, StringUtils.EMPTY); 150 } 151 152 public void setValue(String s) { 153 this.value = s; 154 } 155 156 public String getValue() { 157 if (this.value != null) { 158 return this.value; 159 } 160 else if (this.getWebsiteNode() != null) { 161 return this.getWebsiteNode().getNodeData(this.getName()).getString(); 162 } 163 else { 164 return StringUtils.EMPTY; 165 } 166 } 167 168 public void setSaveInfo(boolean b) { 169 this.setConfig("saveInfo", b); } 171 172 public void setName(String s) { 173 this.setConfig("name", s); } 175 176 public String getName() { 177 return this.getConfigValue("name"); } 179 180 public void addOption(Object o) { 181 this.getOptions().add(o); 182 } 183 184 public Content getWebsiteNode() { 185 return this.websiteNode; 186 } 187 188 public void setLabel(String s) { 189 this.config.put("label", s); } 191 192 public void setDescription(String s) { 193 this.config.put("description", s); } 195 196 public void removeSessionAttribute() { 197 String name = this.getConfigValue(SESSION_ATTRIBUTENAME_DIALOGOBJECT); 198 HttpServletRequest request = this.getRequest(); 199 if (request == null) { 200 request = this.getTopParent().getRequest(); 201 } 202 try { 203 HttpSession session = request.getSession(); 204 session.removeAttribute(name); 205 } 206 catch (Exception e) { 207 log.debug("removeSessionAttribute() for " + name + " failed because this.request is null"); } 209 } 210 211 public HttpServletRequest getRequest() { 212 return this.request; 213 } 214 215 public void setOptions(List options) { 216 this.options = options; 217 } 218 219 protected void drawHtmlPreSubs(Writer out) throws IOException { 220 } 222 223 protected void drawSubs(Writer out) throws IOException { 224 Iterator it = this.getSubs().iterator(); 225 int i = 0; 226 while (it.hasNext()) { 227 String dsId = this.getId() + "_" + i; 230 DialogSuper ds = (DialogSuper) it.next(); 231 ds.setId(dsId); 232 ds.setParent(this); 233 if (this.getParent() == null) { 234 this.setTopParent(this); 235 } 236 ds.setTopParent(this.getTopParent()); 237 ds.drawHtml(out); 238 i++; 239 } 240 } 241 242 protected void drawHtmlPostSubs(Writer out) throws IOException { 243 } 245 246 protected DialogSuper getParent() { 247 return this.parent; 248 } 249 250 protected void setTopParent(DialogSuper top) { 251 this.topParent = top; 252 } 253 254 protected DialogSuper getTopParent() { 255 return this.topParent; 256 } 257 258 protected List getSubs() { 259 return this.subs; 260 } 261 262 protected HttpServletResponse getResponse() { 263 return this.response; 264 } 265 266 269 protected void clearWebsiteNode() { 270 this.websiteNode = null; 271 } 272 273 protected String getId() { 274 return this.id; 275 } 276 277 protected String getLabel() { 278 return this.getConfigValue("label", StringUtils.EMPTY); } 280 281 protected String getDescription() { 282 return this.getConfigValue("description", StringUtils.EMPTY); } 284 285 protected List getOptions() { 286 return this.options; 287 } 288 289 protected List getValues() { 290 if (this.getWebsiteNode() == null) { 291 return this.values; 292 } 293 294 try { 295 Iterator it = this.getWebsiteNode().getContent(this.getName()).getNodeDataCollection().iterator(); 296 List l = new ArrayList (); 297 while (it.hasNext()) { 298 NodeData data = (NodeData) it.next(); 299 l.add(data.getString()); 300 } 301 return l; 302 } 303 catch (RepositoryException re) { 304 return this.values; 305 } 306 307 } 308 309 protected void setSessionAttribute() { 310 String name = SESSION_ATTRIBUTENAME_DIALOGOBJECT + "_" + this.getName() + "_" + new Date ().getTime(); this.setConfig(SESSION_ATTRIBUTENAME_DIALOGOBJECT, name); 312 HttpServletRequest request = this.getRequest(); 313 if (request == null) { 314 request = this.getTopParent().getRequest(); 315 } 316 try { 317 HttpSession session = request.getSession(); 318 session.setAttribute(name, this); 319 } 320 catch (Exception e) { 321 log.error("setSessionAttribute() for " + name + " failed because this.request is null"); } 323 } 324 325 private void setId(String id) { 326 this.id = id; 327 } 328 329 private void initializeConfig(Content configNodeParent) throws RepositoryException { 330 Map config = new Hashtable (); 332 333 if (configNodeParent == null) { 334 return; 336 } 337 338 Iterator itProps = configNodeParent.getNodeDataCollection().iterator(); 340 while (itProps.hasNext()) { 341 NodeData data = (NodeData) itProps.next(); 342 String name = data.getName(); 343 String value = data.getString(); 344 config.put(name, value); 345 } 346 this.config = config; 347 348 Iterator it = configNodeParent.getChildren(ItemType.CONTENTNODE, ContentHandler.SORT_BY_SEQUENCE).iterator(); 349 while (it.hasNext()) { 350 Content configNode = (Content) it.next(); 351 String controlType = configNode.getNodeData("controlType").getString(); 353 if (StringUtils.isEmpty(controlType)) { 354 String name = configNode.getName(); 355 if (!name.startsWith("options")) { log.warn("Missing control type for configNode " + name); } 358 return; 359 } 360 361 if (log.isDebugEnabled()) { 362 log.debug("Loading control \"" + controlType + "\" for " + configNode.getHandle()); } 364 DialogInterface dialogControl = DialogFactory.loadDialog( 365 request, 366 response, 367 this.getWebsiteNode(), 368 configNode); 369 this.addSub(dialogControl); 370 } 371 } 372 373 private void setParent(DialogSuper parent) { 374 this.parent = parent; 375 } 376 377 } 378 | Popular Tags |