1 31 32 package org.opencms.workplace; 33 34 import org.opencms.file.CmsObject; 35 import org.opencms.main.CmsException; 36 import org.opencms.main.CmsIllegalArgumentException; 37 import org.opencms.main.CmsRuntimeException; 38 import org.opencms.util.CmsStringUtil; 39 import org.opencms.widgets.A_CmsWidget; 40 import org.opencms.widgets.CmsWidgetException; 41 import org.opencms.widgets.I_CmsWidget; 42 import org.opencms.widgets.I_CmsWidgetParameter; 43 import org.opencms.widgets.Messages; 44 45 import java.lang.reflect.InvocationTargetException ; 46 import java.util.ArrayList ; 47 import java.util.List ; 48 import java.util.SortedMap ; 49 50 import org.apache.commons.beanutils.ConvertUtilsBean; 51 import org.apache.commons.beanutils.PropertyUtilsBean; 52 53 63 public class CmsWidgetDialogParameter implements I_CmsWidgetParameter { 64 65 66 public static final String DEFAULT_DIALOG_PAGE = "default"; 67 68 69 public static final int MAX_OCCURENCES = 200; 70 71 72 protected Object m_baseCollection; 73 74 75 protected Object m_baseObject; 76 77 78 protected String m_baseObjectProperty; 79 80 81 protected String m_defaultValue; 82 83 84 protected String m_dialogPage; 85 86 87 protected Throwable m_error; 88 89 90 protected String m_id; 91 92 93 protected int m_index; 94 95 96 protected int m_maxOccurs; 97 98 99 protected int m_minOccurs; 100 101 102 protected String m_name; 103 104 105 protected String m_prefix; 106 107 108 protected String m_value; 109 110 111 protected I_CmsWidget m_widget; 112 113 119 public CmsWidgetDialogParameter(CmsWidgetDialogParameter base, int index) { 120 121 this( 122 null, 123 base.m_defaultValue, 124 base.getName(), 125 base.getWidget(), 126 base.getDialogPage(), 127 base.getMinOccurs(), 128 base.getMaxOccurs(), 129 index); 130 131 m_baseObject = base.m_baseObject; 132 m_baseObjectProperty = base.m_baseObjectProperty; 133 m_baseCollection = base.m_baseCollection; 134 m_prefix = base.m_prefix; 135 } 136 137 144 public CmsWidgetDialogParameter(CmsWidgetDialogParameter base, int index, int originalIndex) { 145 146 this( 147 null, 148 base.m_defaultValue, 149 base.getName(), 150 base.getWidget(), 151 base.getDialogPage(), 152 base.getMinOccurs(), 153 base.getMaxOccurs(), 154 index); 155 156 m_baseObject = base.m_baseObject; 157 m_baseObjectProperty = base.m_baseObjectProperty; 158 m_baseCollection = base.m_baseCollection; 159 160 if (m_baseCollection != null) { 161 if (m_baseCollection instanceof List ) { 162 List baseList = (List )m_baseCollection; 164 if (originalIndex < baseList.size()) { 165 Object o = baseList.get(originalIndex); 166 if (o != null) { 167 m_value = o.toString(); 168 } 169 } 170 } else if (m_baseCollection instanceof SortedMap ) { 171 SortedMap baseMap = (SortedMap )m_baseCollection; 173 List keyList = new ArrayList (baseMap.keySet()); 174 if (originalIndex < keyList.size()) { 175 Object key = keyList.get(originalIndex); 176 Object value = baseMap.get(key); 177 StringBuffer val = new StringBuffer (); 178 val.append(key != null ? key.toString() : ""); 179 val.append('='); 180 val.append(value != null ? value.toString() : ""); 181 m_value = val.toString(); 182 } 183 } 184 } 185 } 186 187 194 public CmsWidgetDialogParameter(Object base, String property, I_CmsWidget widget) { 195 196 this(base, property, DEFAULT_DIALOG_PAGE, widget); 197 } 198 199 207 public CmsWidgetDialogParameter(Object base, String property, String dialogPage, I_CmsWidget widget) { 208 209 this(base, property, null, dialogPage, widget, 1, 1); 210 } 211 212 222 public CmsWidgetDialogParameter(Object base, String property, String htmlName, String dialogPage, I_CmsWidget widget) { 223 224 this(base, property, htmlName, null, dialogPage, widget, 1, 1); 225 } 226 227 238 public CmsWidgetDialogParameter( 239 Object base, 240 String property, 241 String defaultValue, 242 String dialogPage, 243 I_CmsWidget widget, 244 int minOccurs, 245 int maxOccurs) { 246 247 this(base, property, property, defaultValue, dialogPage, widget, minOccurs, maxOccurs); 248 } 249 250 262 public CmsWidgetDialogParameter( 263 Object base, 264 String property, 265 String htmlName, 266 String defaultValue, 267 String dialogPage, 268 I_CmsWidget widget, 269 int minOccurs, 270 int maxOccurs) { 271 272 if (htmlName == null) { 273 htmlName = property; 274 } 275 276 if ((base instanceof List ) || (base instanceof SortedMap )) { 277 278 init(null, defaultValue, htmlName, widget, dialogPage, 0, MAX_OCCURENCES, 0); 280 281 m_baseObject = null; 282 m_baseObjectProperty = null; 283 m_baseCollection = base; 284 285 } else { 286 287 init(null, defaultValue, htmlName, widget, dialogPage, minOccurs, maxOccurs, 0); 289 290 m_baseObject = base; 291 m_baseObjectProperty = property; 292 m_baseCollection = null; 293 294 PropertyUtilsBean bean = new PropertyUtilsBean(); 295 296 if (!bean.isReadable(m_baseObject, m_baseObjectProperty) 298 || !bean.isWriteable(m_baseObject, m_baseObjectProperty)) { 299 300 throw new CmsIllegalArgumentException(Messages.get().container( 301 Messages.ERR_NO_PROPERTY_2, 302 base.getClass().getName(), 303 property)); 304 } 305 306 Object value; 307 try { 308 value = bean.getNestedProperty(m_baseObject, m_baseObjectProperty); 309 } catch (Exception e) { 310 throw new CmsRuntimeException(Messages.get().container( 311 Messages.ERR_PROPERTY_READ_2, 312 property, 313 base.getClass().getName()), e); 314 } 315 316 if (value != null) { 317 318 if ((value instanceof List ) || (value instanceof SortedMap )) { 319 m_baseCollection = value; 320 m_minOccurs = 0; 321 m_maxOccurs = MAX_OCCURENCES; 322 } else { 323 m_defaultValue = String.valueOf(value); 324 m_value = m_defaultValue; 325 if ((m_minOccurs == 0) && !m_value.equals(defaultValue)) { 326 m_minOccurs = 1; 328 } 329 } 330 } 331 } 332 } 333 334 340 public CmsWidgetDialogParameter(String name, I_CmsWidget widget) { 341 342 this(null, null, name, widget, DEFAULT_DIALOG_PAGE, 1, 1, 0); 343 } 344 345 353 public CmsWidgetDialogParameter(String name, I_CmsWidget widget, int minOccurs, int maxOccurs) { 354 355 this(null, null, name, widget, DEFAULT_DIALOG_PAGE, minOccurs, maxOccurs, 0); 356 } 357 358 370 public CmsWidgetDialogParameter( 371 String value, 372 String defaultValue, 373 String name, 374 I_CmsWidget widget, 375 String dialog, 376 int minOccurs, 377 int maxOccurs, 378 int index) { 379 380 super(); 381 init(value, defaultValue, name, widget, dialog, minOccurs, maxOccurs, index); 382 } 383 384 392 public static String createId(String name, int index) { 393 394 StringBuffer result = new StringBuffer (); 395 result.append(name); 396 result.append('.'); 397 result.append(index); 398 399 return result.toString(); 400 } 401 402 409 public void commitValue(CmsWidgetDialog dialog) throws CmsException { 410 411 if (m_baseCollection == null) { 412 413 PropertyUtilsBean bean = new PropertyUtilsBean(); 414 ConvertUtilsBean converter = new ConvertUtilsBean(); 415 Object value = null; 416 try { 417 Class type = bean.getPropertyType(m_baseObject, m_baseObjectProperty); 418 value = converter.convert(m_value, type); 419 bean.setNestedProperty(m_baseObject, m_baseObjectProperty, value); 420 setError(null); 421 } catch (InvocationTargetException e) { 422 setError(e.getTargetException()); 423 throw new CmsWidgetException(Messages.get().container( 424 Messages.ERR_PROPERTY_WRITE_3, 425 value, 426 dialog.keyDefault(A_CmsWidget.getLabelKey(this), getKey()), 427 m_baseObject.getClass().getName()), e.getTargetException(), this); 428 } catch (Exception e) { 429 setError(e); 430 throw new CmsWidgetException(Messages.get().container( 431 Messages.ERR_PROPERTY_WRITE_3, 432 value, 433 dialog.keyDefault(A_CmsWidget.getLabelKey(this), getKey()), 434 m_baseObject.getClass().getName()), e, this); 435 } 436 } else if (m_baseCollection instanceof SortedMap ) { 437 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_value)) { 438 int pos = m_value.indexOf('='); 439 if ((pos > 0) && (pos < (m_value.length() - 1))) { 440 String key = m_value.substring(0, pos); 441 String value = m_value.substring(pos + 1); 442 SortedMap map = (SortedMap )m_baseCollection; 443 if (map.containsKey(key)) { 444 Object val = map.get(key); 445 CmsWidgetException error = new CmsWidgetException(Messages.get().container( 446 Messages.ERR_MAP_DUPLICATE_KEY_3, 447 dialog.keyDefault(A_CmsWidget.getLabelKey(this), getKey()), 448 key, 449 val), this); 450 setError(error); 451 throw error; 452 } 453 map.put(key, value); 454 } else { 455 CmsWidgetException error = new CmsWidgetException(Messages.get().container( 456 Messages.ERR_MAP_PARAMETER_FORM_1, 457 dialog.keyDefault(A_CmsWidget.getLabelKey(this), getKey())), this); 458 setError(error); 459 throw error; 460 } 461 } 462 } else if (m_baseCollection instanceof List ) { 463 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_value)) { 464 List list = (List )m_baseCollection; 465 list.add(m_value); 466 } 467 } 468 } 469 470 473 public String getDefault(CmsObject cms) { 474 475 return m_defaultValue; 476 } 477 478 486 public String getDialogPage() { 487 488 return m_dialogPage; 489 } 490 491 497 public Throwable getError() { 498 499 return m_error; 500 } 501 502 505 public String getId() { 506 507 return m_id; 508 } 509 510 513 public int getIndex() { 514 515 return m_index; 516 } 517 518 521 public String getKey() { 522 523 StringBuffer result = new StringBuffer (128); 524 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_prefix)) { 525 result.append(m_prefix); 526 result.append('.'); 527 } 528 result.append(getName()); 529 return result.toString(); 530 } 531 532 535 public int getMaxOccurs() { 536 537 return m_maxOccurs; 538 } 539 540 543 public int getMinOccurs() { 544 545 return m_minOccurs; 546 } 547 548 551 public String getName() { 552 553 return m_name; 554 } 555 556 559 public String getStringValue(CmsObject cms) throws CmsRuntimeException { 560 561 return m_value; 562 } 563 564 569 public I_CmsWidget getWidget() { 570 571 return m_widget; 572 } 573 574 577 public boolean hasError() { 578 579 return m_error != null; 580 } 581 582 591 public boolean hasValue(int index) { 592 593 if (m_baseCollection instanceof List ) { 594 return index < ((List )m_baseCollection).size(); 595 } else if (m_baseCollection instanceof SortedMap ) { 596 return index < ((SortedMap )m_baseCollection).size(); 597 } 598 return false; 599 } 600 601 606 public boolean isCollectionBase() { 607 608 return (m_baseCollection != null) 609 && ((m_baseCollection instanceof List ) || (m_baseCollection instanceof SortedMap )); 610 } 611 612 618 public void prepareCommit() { 619 620 if (m_baseCollection instanceof List ) { 621 List list = (List )m_baseCollection; 622 list.clear(); 623 } else if (m_baseCollection instanceof SortedMap ) { 624 SortedMap map = (SortedMap )m_baseCollection; 625 map.clear(); 626 } 627 } 628 629 636 public void setError(Throwable error) { 637 638 m_error = error; 639 } 640 641 646 public void setindex(int index) { 647 648 m_index = index; 649 m_id = createId(m_name, m_index); 650 } 651 652 655 public void setKeyPrefix(String prefix) { 656 657 m_prefix = prefix; 658 } 659 660 663 public void setStringValue(CmsObject cms, String value) throws CmsIllegalArgumentException { 664 665 m_value = value; 666 } 667 668 680 protected void init( 681 String value, 682 String defaultValue, 683 String name, 684 I_CmsWidget widget, 685 String dialog, 686 int minOccurs, 687 int maxOccurs, 688 int index) { 689 690 if (defaultValue == null) { 691 m_defaultValue = ""; 692 } else { 693 m_defaultValue = defaultValue; 694 } 695 if (value == null) { 696 m_value = m_defaultValue; 697 } else { 698 m_value = value; 699 } 700 m_name = name; 701 m_widget = widget; 702 if (maxOccurs < MAX_OCCURENCES) { 703 m_maxOccurs = maxOccurs; 704 } else { 705 m_maxOccurs = MAX_OCCURENCES; 706 } 707 if (minOccurs >= 0) { 708 m_minOccurs = minOccurs; 709 } else { 710 m_minOccurs = 0; 711 } 712 if (m_minOccurs > m_maxOccurs) { 713 m_minOccurs = m_maxOccurs; 714 } 715 m_dialogPage = dialog; 716 m_error = null; 717 718 setindex(index); 719 } 720 }
| Popular Tags
|