1 13 package info.magnolia.cms.gui.dialog; 14 15 import info.magnolia.cms.core.Content; 16 import info.magnolia.cms.core.ItemType; 17 import info.magnolia.cms.core.NodeData; 18 import info.magnolia.cms.i18n.Messages; 19 import info.magnolia.cms.i18n.MessagesUtil; 20 import info.magnolia.cms.i18n.TemplateMessagesUtil; 21 import info.magnolia.cms.util.AlertUtil; 22 import info.magnolia.cms.util.ContentUtil; 23 import info.magnolia.cms.util.RequestFormUtil; 24 25 import java.io.IOException ; 26 import java.io.Writer ; 27 import java.util.ArrayList ; 28 import java.util.Date ; 29 import java.util.Hashtable ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 import javax.jcr.PathNotFoundException; 35 import javax.jcr.RepositoryException; 36 import javax.servlet.http.HttpServletRequest ; 37 import javax.servlet.http.HttpServletResponse ; 38 import javax.servlet.http.HttpSession ; 39 40 import org.apache.commons.lang.BooleanUtils; 41 import org.apache.commons.lang.StringUtils; 42 import org.slf4j.Logger; 43 import org.slf4j.LoggerFactory; 44 45 46 50 public abstract class DialogControlImpl implements DialogControl { 51 52 private static final String I18N_BASENAME_PROPERTY = "i18nBasename"; 53 54 public static final String SESSION_ATTRIBUTENAME_DIALOGOBJECT = "mgnlSessionAttribute"; 56 public static final String SESSION_ATTRIBUTENAME_DIALOGOBJECT_REMOVE = "mgnlSessionAttributeRemove"; 58 61 private static Logger log = LoggerFactory.getLogger(DialogControlImpl.class); 62 63 66 private HttpServletRequest request; 67 68 71 private HttpServletResponse response; 72 73 76 private Content websiteNode; 77 78 81 private Map config = new Hashtable (); 82 83 86 private List subs = new ArrayList (); 87 88 91 private List options = new ArrayList (); 92 93 97 private String id = "mgnlControl"; 99 protected String value; 100 101 104 private List values; 105 106 private DialogControlImpl parent; 107 108 private DialogControlImpl topParent; 109 110 114 private Messages messages; 115 116 118 public void init(HttpServletRequest request, HttpServletResponse response, Content websiteNode, Content configNode) 119 throws RepositoryException { 120 121 if (log.isDebugEnabled()) { 122 log.debug("Init " + getClass().getName()); } 124 125 this.websiteNode = websiteNode; 126 this.request = request; 127 this.response = response; 128 129 this.initializeConfig(configNode); 130 } 131 132 135 public void drawHtml(Writer out) throws IOException { 136 this.drawHtmlPreSubs(out); 137 this.drawSubs(out); 138 this.drawHtmlPostSubs(out); 139 } 140 141 public void addSub(Object o) { 142 this.getSubs().add(o); 143 } 144 145 public void setConfig(String key, String value) { 146 if (value != null) { 147 this.config.put(key, value); 148 } 149 } 150 151 public void setConfig(String key, boolean value) { 152 this.config.put(key, BooleanUtils.toBooleanObject(value).toString()); 153 } 154 155 public void setConfig(String key, int value) { 156 this.config.put(key, Integer.toString(value)); 157 } 158 159 public String getConfigValue(String key, String nullValue) { 160 if (this.config.containsKey(key)) { 161 return (String ) this.config.get(key); 162 } 163 164 return nullValue; 165 } 166 167 public String getConfigValue(String key) { 168 return this.getConfigValue(key, StringUtils.EMPTY); 169 } 170 171 public void setValue(String s) { 172 this.value = s; 173 } 174 175 public String getValue() { 176 if (this.value == null) { 177 if (this.getWebsiteNode() != null) { 178 this.value = readValue(); 179 if(this instanceof UUIDDialogControl){ 180 String repository = ((UUIDDialogControl)this).getRepository(); 181 this.value = ContentUtil.uuid2path(repository, this.value); 182 } 183 } 184 RequestFormUtil params = new RequestFormUtil(request); 185 if (params.getParameter(this.getName()) != null) { 186 this.value = params.getParameter(this.getName()); 187 } 188 189 if (this.value == null && StringUtils.isNotEmpty(getConfigValue("defaultValue"))) { 190 return this.getMessage(this.getConfigValue("defaultValue")); 191 } 192 193 if (this.value == null) { 194 this.value = StringUtils.EMPTY; 195 } 196 } 197 return this.value; 198 } 199 200 protected String readValue() { 201 return this.getWebsiteNode().getNodeData(this.getName()).getString(); 202 } 203 204 public void setSaveInfo(boolean b) { 205 this.setConfig("saveInfo", b); } 207 208 213 public void setName(String s) { 214 this.setConfig("name", s); } 216 217 221 public String getName() { 222 return this.getConfigValue("name"); } 224 225 public void addOption(Object o) { 226 this.getOptions().add(o); 227 } 228 229 public Content getWebsiteNode() { 230 return this.websiteNode; 231 } 232 233 public void setLabel(String s) { 234 this.config.put("label", s); } 236 237 public void setDescription(String s) { 238 this.config.put("description", s); } 240 241 public void removeSessionAttribute() { 242 String name = this.getConfigValue(SESSION_ATTRIBUTENAME_DIALOGOBJECT); 243 HttpServletRequest request = this.getRequest(); 244 if (request == null) { 245 request = this.getTopParent().getRequest(); 246 } 247 try { 248 HttpSession httpsession = request.getSession(false); 249 if (httpsession != null) { 250 httpsession.removeAttribute(name); 251 } 252 } 253 catch (Exception e) { 254 if (log.isDebugEnabled()) { 255 log.debug("removeSessionAttribute() for " + name + " failed because this.request is null"); } 257 } 258 } 259 260 public HttpServletRequest getRequest() { 261 return this.request; 262 } 263 264 public void setOptions(List options) { 265 this.options = options; 266 } 267 268 protected void drawHtmlPreSubs(Writer out) throws IOException { 269 } 271 272 protected void drawSubs(Writer out) throws IOException { 273 Iterator it = this.getSubs().iterator(); 274 int i = 0; 275 while (it.hasNext()) { 276 String dsId = this.getId() + "_" + i; 279 DialogControlImpl ds = (DialogControlImpl) it.next(); 280 ds.setId(dsId); 281 ds.setParent(this); 282 if (this.getParent() == null) { 283 this.setTopParent(this); 284 } 285 ds.setTopParent(this.getTopParent()); 286 ds.drawHtml(out); 287 i++; 288 } 289 } 290 291 protected void drawHtmlPostSubs(Writer out) throws IOException { 292 } 294 295 public DialogControlImpl getParent() { 296 return this.parent; 297 } 298 299 protected void setTopParent(DialogControlImpl top) { 300 this.topParent = top; 301 } 302 303 public DialogControlImpl getTopParent() { 304 return this.topParent; 305 } 306 307 public List getSubs() { 308 return this.subs; 309 } 310 311 316 public DialogControlImpl getSub(String name) { 317 DialogControlImpl found; 318 for (Iterator iter = subs.iterator(); iter.hasNext();) { 319 Object control = iter.next(); 320 321 if (control instanceof DialogControlImpl) { 323 if (StringUtils.equals(((DialogControlImpl) control).getName(), name)) { 324 return (DialogControlImpl) control; 325 } 326 found = ((DialogControlImpl) control).getSub(name); 327 if (found != null) { 328 return found; 329 } 330 } 331 } 332 return null; 333 } 334 335 protected HttpServletResponse getResponse() { 336 return this.response; 337 } 338 339 342 protected void clearWebsiteNode() { 343 this.websiteNode = null; 344 } 345 346 public String getId() { 347 return this.id; 348 } 349 350 public String getLabel() { 351 return this.getConfigValue("label", StringUtils.EMPTY); } 353 354 public String getDescription() { 355 return this.getConfigValue("description", StringUtils.EMPTY); } 357 358 public List getOptions() { 359 return this.options; 360 } 361 362 public List getValues() { 363 if (this.values == null) { 364 this.values = readValues(); 365 366 if(this instanceof UUIDDialogControl){ 367 String repository = ((UUIDDialogControl)this).getRepository(); 368 List pathes = new ArrayList (); 369 for (Iterator iter = this.values.iterator(); iter.hasNext();) { 370 String uuid = (String ) iter.next(); 371 String path = ContentUtil.uuid2path(repository, uuid); 372 pathes.add(path); 373 } 374 this.values = pathes; 375 } 376 377 if (request != null) { 378 RequestFormUtil params = new RequestFormUtil(request); 379 String [] values = params.getParameterValues(this.getName()); 380 if (values != null && values.length > 0) { 381 this.values.clear(); 382 for (int i = 0; i < values.length; i++) { 383 String value = values[i]; 384 this.values.add(value); 385 } 386 } 387 } 388 } 389 390 return this.values; 391 } 392 393 protected List readValues() { 394 List values = new ArrayList (); 395 if (this.getWebsiteNode() != null) { 396 try { 397 Iterator it = this.getWebsiteNode().getContent(this.getName()).getNodeDataCollection().iterator(); 398 while (it.hasNext()) { 399 NodeData data = (NodeData) it.next(); 400 values.add(data.getString()); 401 } 402 } 403 catch (PathNotFoundException e) { 404 } 406 catch (RepositoryException re) { 407 log.error("can't set values", re); 408 } 409 } 410 return values; 411 } 412 413 416 public void setSessionAttribute() { 417 String name = SESSION_ATTRIBUTENAME_DIALOGOBJECT + "_" + this.getName() + "_" + new Date ().getTime(); this.setConfig(SESSION_ATTRIBUTENAME_DIALOGOBJECT, name); 419 HttpServletRequest request = this.getRequest(); 420 if (request == null) { 421 request = this.getTopParent().getRequest(); 422 } 423 try { 424 425 HttpSession httpsession = request.getSession(true); 427 httpsession.setAttribute(name, this); 428 } 429 catch (Exception e) { 430 log.error("setSessionAttribute() for " + name + " failed because this.request is null"); } 432 } 433 434 private void setId(String id) { 435 this.id = id; 436 } 437 438 private void initializeConfig(Content configNodeParent) throws RepositoryException { 439 Map config = new Hashtable (); 441 442 if (configNodeParent == null) { 443 return; 445 } 446 447 Iterator itProps = configNodeParent.getNodeDataCollection().iterator(); 449 while (itProps.hasNext()) { 450 NodeData data = (NodeData) itProps.next(); 451 String name = data.getName(); 452 String value = data.getString(); 453 config.put(name, value); 454 } 455 456 if (!config.containsKey("name")) { 458 config.put("name", configNodeParent.getName()); 459 } 460 461 this.config = config; 462 463 Iterator it = configNodeParent.getChildren(ItemType.CONTENTNODE).iterator(); 464 while (it.hasNext()) { 465 Content configNode = (Content) it.next(); 466 String controlType = configNode.getNodeData("controlType").getString(); 468 if (StringUtils.isEmpty(controlType)) { 469 String name = configNode.getName(); 470 if (!name.startsWith("options")) { log.warn("Missing control type for configNode " + name); } 473 return; 474 } 475 476 if (log.isDebugEnabled()) { 477 log.debug("Loading control \"" + controlType + "\" for " + configNode.getHandle()); } 479 DialogControl dialogControl = DialogFactory 480 .loadDialog(request, response, this.getWebsiteNode(), configNode); 481 this.addSub(dialogControl); 482 } 483 } 484 485 private void setParent(DialogControlImpl parent) { 486 this.parent = parent; 487 } 488 489 494 protected Messages getMessages() { 495 if (messages == null) { 496 if (this.getParent() == null) { 498 messages = TemplateMessagesUtil.getMessages(); 499 } 500 else { 501 messages = this.getParent().getMessages(); 503 } 504 String basename = this.getConfigValue(I18N_BASENAME_PROPERTY); 506 if (StringUtils.isNotEmpty(basename)) { 507 messages = MessagesUtil.chain(basename, messages); 509 } 510 } 511 return messages; 512 } 513 514 519 public String getMessage(String key) { 520 return this.getMessages().getWithDefault(key, key); 521 } 522 523 529 public String getMessage(String key, Object [] args) { 530 return this.getMessages().getWithDefault(key, args, key); 531 } 532 533 537 public boolean validate() { 538 if (this.isRequired()) { 539 if (StringUtils.isEmpty(this.getValue()) && this.getValues().size() == 0) { 540 String name = this.getMessage(this.getLabel()); 541 AlertUtil.setMessage(this.getMessage("dialogs.validation.required", new Object []{name})); 542 return false; 543 } 544 } 545 for (Iterator iter = this.getSubs().iterator(); iter.hasNext();) { 546 DialogControl sub = (DialogControl) iter.next(); 547 if (sub instanceof DialogControlImpl) { 548 if (!((DialogControlImpl) sub).validate()) { 549 return false; 550 } 551 } 552 553 } 554 return true; 555 } 556 557 561 public boolean isRequired() { 562 if (BooleanUtils.toBoolean(this.getConfigValue("required"))) { 563 return true; 564 } 565 return false; 566 } 567 568 public void setRequired(boolean required) { 569 this.setConfig("required", BooleanUtils.toStringTrueFalse(required)); 570 } 571 572 } 573 | Popular Tags |