1 31 32 package org.opencms.workplace.commons; 33 34 import org.opencms.file.CmsProperty; 35 import org.opencms.file.CmsPropertyDefinition; 36 import org.opencms.file.CmsResource; 37 import org.opencms.file.CmsResourceFilter; 38 import org.opencms.i18n.CmsEncoder; 39 import org.opencms.jsp.CmsJspActionElement; 40 import org.opencms.main.CmsException; 41 import org.opencms.main.CmsLog; 42 import org.opencms.main.OpenCms; 43 import org.opencms.util.CmsStringUtil; 44 import org.opencms.workplace.CmsWorkplace; 45 import org.opencms.workplace.CmsWorkplaceSettings; 46 import org.opencms.workplace.explorer.CmsExplorerTypeSettings; 47 48 import java.util.HashMap ; 49 import java.util.Iterator ; 50 import java.util.Map ; 51 52 import javax.servlet.http.HttpServletRequest ; 53 import javax.servlet.http.HttpServletResponse ; 54 import javax.servlet.jsp.JspException ; 55 import javax.servlet.jsp.PageContext ; 56 57 import org.apache.commons.logging.Log; 58 59 77 public class CmsPropertyCustom extends CmsPropertyAdvanced { 78 79 80 public static final int ACTION_EDIT = 500; 81 82 83 private static final Log LOG = CmsLog.getLog(CmsPropertyCustom.class); 84 85 86 private Map m_activeProperties; 87 88 89 private CmsExplorerTypeSettings m_explorerTypeSettings; 90 91 92 private boolean m_showNavigation; 93 94 99 public CmsPropertyCustom(CmsJspActionElement jsp) { 100 101 super(jsp); 102 } 103 104 111 public CmsPropertyCustom(PageContext context, HttpServletRequest req, HttpServletResponse res) { 112 113 this(new CmsJspActionElement(context, req, res)); 114 } 115 116 122 public void actionEdit(HttpServletRequest request) throws JspException { 123 124 getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this); 126 try { 127 if (isEditable()) { 129 performEditOperation(request); 130 } 131 } catch (Throwable e) { 132 includeErrorpage(this, e); 134 } 135 } 136 137 142 public String buildEditForm() { 143 144 StringBuffer result = new StringBuffer (2048); 145 146 boolean editable = isEditable(); 148 149 result.append("<table border=\"0\">\n"); 151 result.append("<tr>\n"); 152 result.append("\t<td class=\"textbold\">"); 153 result.append(key(Messages.GUI_PROPERTY_0)); 154 result.append("</td>\n"); 155 result.append("\t<td class=\"textbold\">"); 156 result.append(key(Messages.GUI_PROPERTY_VALUE_0)); 157 result.append("</td>\n"); 158 result.append("\t<td class=\"textbold\" style=\"white-space: nowrap;\">"); 159 result.append(key(Messages.GUI_PROPERTY_USED_0)); 160 result.append("</td>\n"); 161 result.append("</tr>\n"); 162 result.append("<tr><td><span style=\"height: 6px;\"></span></td></tr>\n"); 163 164 result.append(buildTextInput(editable)); 166 167 if (showNavigation()) { 169 result.append(buildNavigationProperties(editable)); 170 } 171 result.append("</table>"); 172 173 return result.toString(); 174 } 175 176 188 public String buildSetFormValues() { 189 190 StringBuffer result = new StringBuffer (1024); 191 Iterator i = getExplorerTypeSettings().getProperties().iterator(); 192 while (i.hasNext()) { 194 String curProperty = (String )i.next(); 195 if (getActiveProperties().containsKey(curProperty)) { 196 CmsProperty property = (CmsProperty)getActiveProperties().get(curProperty); 197 String propValue = property.getValue(); 198 if (propValue != null) { 199 propValue = propValue.trim(); 200 propValue = CmsStringUtil.escapeJavaScript(propValue); 201 result.append("\tdocument.getElementById(\""); 203 result.append(PREFIX_VALUE); 204 result.append(curProperty); 205 result.append("\").value = \""); 206 result.append(propValue); 207 result.append("\";\n"); 208 } 209 } 210 } 211 if (showNavigation() && getActiveProperties().containsKey(CmsPropertyDefinition.PROPERTY_NAVTEXT)) { 213 CmsProperty property = (CmsProperty)getActiveProperties().get(CmsPropertyDefinition.PROPERTY_NAVTEXT); 214 String propValue = property.getValue(); 215 if (propValue != null) { 216 propValue = propValue.trim(); 217 propValue = CmsStringUtil.escapeJavaScript(propValue); 218 result.append("\tdocument.getElementById(\""); 220 result.append(PREFIX_VALUE); 221 result.append(CmsPropertyDefinition.PROPERTY_NAVTEXT); 222 result.append("\").value = \""); 223 result.append(propValue); 224 result.append("\";\n"); 225 } 226 } 227 return result.toString(); 228 } 229 230 238 public String dialogButtonsOkCancelAdvanced(String okAttributes, String cancelAttributes, String advancedAttributes) { 239 240 if (isEditable()) { 241 int okButton = BUTTON_OK; 242 if (getParamDialogmode() != null && getParamDialogmode().startsWith(MODE_WIZARD)) { 243 okButton = BUTTON_FINISH; 245 } 246 return dialogButtons(new int[] {okButton, BUTTON_CANCEL, BUTTON_ADVANCED}, new String [] { 247 okAttributes, 248 cancelAttributes, 249 advancedAttributes}); 250 } else { 251 return dialogButtons(new int[] {BUTTON_CLOSE, BUTTON_ADVANCED}, new String [] { 252 cancelAttributes, 253 advancedAttributes}); 254 } 255 } 256 257 262 public CmsExplorerTypeSettings getExplorerTypeSettings() { 263 264 return m_explorerTypeSettings; 265 } 266 267 272 public void setExplorerTypeSettings(CmsExplorerTypeSettings typeSettings) { 273 274 m_explorerTypeSettings = typeSettings; 275 } 276 277 282 public void setShowNavigation(boolean showNav) { 283 284 m_showNavigation = showNav; 285 } 286 287 292 public boolean showNavigation() { 293 294 return m_showNavigation; 295 } 296 297 303 protected StringBuffer buildNavigationProperties(boolean editable) { 304 305 StringBuffer result = new StringBuffer (1024); 306 307 String disabled = ""; 309 if (!editable) { 310 disabled = " disabled=\"disabled\""; 311 } 312 313 result.append(buildTableRowStart(key(Messages.GUI_PROPERTY_ADD_TO_NAV_0))); 315 result.append("<input type=\"checkbox\" name=\"enablenav\" id=\"enablenav\" value=\"true\" onClick=\"toggleNav();\""); 316 if (getActiveProperties().containsKey(CmsPropertyDefinition.PROPERTY_NAVTEXT) 317 || getActiveProperties().containsKey(CmsPropertyDefinition.PROPERTY_NAVPOS)) { 318 result.append(" checked=\"checked\""); 319 } 320 result.append(disabled); 321 result.append(">"); 322 result.append("</td>\n"); 323 result.append("\t<td class=\"textcenter\">"); 324 result.append(" "); 325 result.append(buildTableRowEnd()); 326 327 result.append(buildPropertyEntry(CmsPropertyDefinition.PROPERTY_NAVTEXT, key(Messages.GUI_LABEL_NAVTEXT_0), editable)); 329 330 result.append(buildTableRowStart(key(Messages.GUI_CHNAV_INSERT_AFTER_0))); 332 result.append(CmsChnav.buildNavPosSelector(getCms(), getParamResource(), disabled 333 + " id=\"navpos\" class=\"maxwidth noborder\"", getMessages())); 334 String navPos = null; 336 try { 337 navPos = getCms().readPropertyObject(getParamResource(), CmsPropertyDefinition.PROPERTY_NAVPOS, false).getValue(); 338 } catch (CmsException e) { 339 if (LOG.isInfoEnabled()) { 341 LOG.info(e.getLocalizedMessage()); 342 } 343 } 344 if (navPos == null) { 345 navPos = ""; 346 } 347 result.append("<input type=\"hidden\" name=\""); 348 result.append(PREFIX_HIDDEN); 349 result.append(CmsPropertyDefinition.PROPERTY_NAVPOS); 350 result.append("\" value=\""); 351 result.append(navPos); 352 result.append("\">"); 353 result.append("</td>\n"); 354 result.append("\t<td class=\"textcenter\">"); 355 result.append(" "); 356 result.append(buildTableRowEnd()); 357 358 return result; 359 } 360 361 372 protected StringBuffer buildPropertyEntry(String propertyName, String propertyTitle, boolean editable) { 373 374 StringBuffer result = new StringBuffer (256); 375 String disabled = ""; 377 if (!editable) { 378 disabled = " disabled=\"disabled\""; 379 } 380 result.append(buildTableRowStart(propertyTitle)); 381 if (getActiveProperties().containsKey(propertyName)) { 382 CmsProperty currentProperty = (CmsProperty)getActiveProperties().get(propertyName); 384 String propValue = currentProperty.getValue(); 385 if (propValue != null) { 386 propValue = propValue.trim(); 387 } 388 propValue = CmsEncoder.escapeXml(propValue); 389 390 result.append("<input type=\"text\" class=\"maxwidth\""); 392 result.append(" name=\""); 393 result.append(PREFIX_VALUE); 394 result.append(propertyName); 395 result.append("\" id=\""); 396 result.append(PREFIX_VALUE); 397 result.append(propertyName); 398 result.append("\""); 399 if (editable) { 400 result.append(" onKeyup=\"checkValue('"); 401 result.append(propertyName); 402 result.append("');\""); 403 } 404 result.append(disabled); 405 result.append(">"); 406 407 result.append("<input type=\"hidden\" name=\""); 409 result.append(PREFIX_HIDDEN); 410 result.append(propertyName); 411 result.append("\" id=\""); 412 result.append(PREFIX_HIDDEN); 413 result.append(propertyName); 414 result.append("\" value=\""); 415 result.append(propValue); 416 result.append("\">"); 417 result.append("</td>\n"); 418 result.append("\t<td class=\"textcenter\">"); 419 420 result.append("<input type=\"checkbox\" name=\""); 422 result.append(PREFIX_USEPROPERTY); 423 result.append(propertyName); 424 result.append("\" id=\""); 425 result.append(PREFIX_USEPROPERTY); 426 result.append(propertyName); 427 result.append("\" value=\"true\""); 428 result.append(" checked=\"checked\""); 429 if (editable) { 430 result.append(" onClick=\"toggleDelete('"); 431 result.append(propertyName); 432 result.append("');\""); 433 } 434 result.append(disabled + ">"); 435 } else { 436 result.append("<input type=\"text\" class=\"maxwidth\" "); 438 result.append("name=\""); 439 result.append(PREFIX_VALUE); 440 result.append(propertyName); 441 result.append("\" id=\""); 442 result.append(PREFIX_VALUE); 443 result.append(propertyName); 444 result.append("\""); 445 result.append(disabled); 446 result.append("></td>\n"); 447 result.append("\t<td class=\"textcenter\"> "); 448 } 449 result.append(buildTableRowEnd()); 450 return result; 451 } 452 453 458 protected String buildTableRowEnd() { 459 460 return "</td>\n</tr>\n"; 461 } 462 463 469 protected StringBuffer buildTableRowStart(String propertyName) { 470 471 StringBuffer result = new StringBuffer (96); 472 result.append("<tr>\n"); 473 result.append("\t<td style=\"white-space: nowrap;\" unselectable=\"on\">"); 474 result.append(propertyName); 475 result.append("</td>\n"); 476 result.append("\t<td class=\"maxwidth\">"); 477 return result; 478 } 479 480 486 protected StringBuffer buildTextInput(boolean editable) { 487 488 StringBuffer result = new StringBuffer (256); 489 Iterator i = getExplorerTypeSettings().getProperties().iterator(); 490 while (i.hasNext()) { 492 String curProperty = (String )i.next(); 493 result.append(buildPropertyEntry(curProperty, curProperty, editable)); 494 } 495 return result; 496 } 497 498 503 protected Map getActiveProperties() { 504 505 if (m_activeProperties == null) { 507 try { 508 m_activeProperties = CmsPropertyAdvanced.getPropertyMap(getCms().readPropertyObjects( 509 getParamResource(), 510 false)); 511 } catch (CmsException e) { 512 if (LOG.isInfoEnabled()) { 514 LOG.info(e.getLocalizedMessage()); 515 } 516 m_activeProperties = new HashMap (); 517 } 518 } 519 return m_activeProperties; 520 } 521 522 525 protected void initExplorerTypeSettings() { 526 527 try { 528 CmsResource res = getCms().readResource(getParamResource(), CmsResourceFilter.ALL); 529 String resTypeName = OpenCms.getResourceManager().getResourceType(res.getTypeId()).getTypeName(); 530 setExplorerTypeSettings(getSettingsForType(resTypeName)); 532 setShowNavigation(getExplorerTypeSettings().isShowNavigation()); 533 } catch (Throwable e) { 534 try { 536 includeErrorpage(this, e); 537 } catch (JspException exc) { 538 LOG.error(Messages.get().getBundle().key(Messages.LOG_ERROR_INCLUDE_FAILED_1, FILE_DIALOG_SCREEN_ERRORPAGE)); 539 } 540 } 541 } 542 543 546 protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) { 547 548 fillParamValues(request); 550 initExplorerTypeSettings(); 552 setParamDialogtype(DIALOG_TYPE); 554 boolean isPopup = Boolean.valueOf(getParamIsPopup()).booleanValue(); 555 if (DIALOG_SHOW_DEFAULT.equals(getParamAction())) { 557 setAction(ACTION_DEFAULT); 559 try { 560 actionEdit(request); 561 sendForward(CmsPropertyAdvanced.URI_PROPERTY_DIALOG, paramsAsParameterMap()); 562 } catch (Exception e) { 563 if (LOG.isInfoEnabled()) { 565 LOG.info(e.getLocalizedMessage()); 566 } 567 } 568 } else if (DIALOG_SAVE_EDIT.equals(getParamAction())) { 569 if (isPopup) { 571 setAction(ACTION_CLOSEPOPUP_SAVE); 572 } else { 573 setAction(ACTION_SAVE_EDIT); 574 } 575 } else if (DIALOG_CANCEL.equals(getParamAction())) { 576 if (isPopup) { 578 setAction(ACTION_CLOSEPOPUP); 579 } else { 580 setAction(ACTION_CANCEL); 581 } 582 } else { 583 setAction(ACTION_EDIT); 584 String resName = CmsResource.getName(getParamResource()); 585 if (resName.startsWith(CmsWorkplace.TEMP_FILE_PREFIX)) { 586 resName = resName.substring(1); 587 } 588 setParamTitle(key(Messages.GUI_PROPERTIES_1, new Object [] {resName})); 589 } 590 } 591 592 599 protected boolean performEditOperation(HttpServletRequest request) throws CmsException { 600 601 boolean useTempfileProject = Boolean.valueOf(getParamUsetempfileproject()).booleanValue(); 602 try { 603 if (useTempfileProject) { 604 switchToTempProject(); 605 } 606 Iterator i = getExplorerTypeSettings().getProperties().iterator(); 608 while (i.hasNext()) { 610 String curProperty = (String )i.next(); 611 String paramValue = request.getParameter(PREFIX_VALUE + curProperty); 612 String oldValue = request.getParameter(PREFIX_HIDDEN + curProperty); 613 writeProperty(curProperty, paramValue, oldValue); 614 } 615 616 if (showNavigation()) { 618 String paramValue = request.getParameter("enablenav"); 620 String oldValue = null; 621 if (Boolean.valueOf(paramValue).booleanValue()) { 622 paramValue = request.getParameter("navpos"); 624 if (!"-1".equals(paramValue) && !String.valueOf(Float.MAX_VALUE).equals(paramValue)) { 625 oldValue = request.getParameter(PREFIX_HIDDEN + CmsPropertyDefinition.PROPERTY_NAVPOS); 627 writeProperty(CmsPropertyDefinition.PROPERTY_NAVPOS, paramValue, oldValue); 628 } 629 paramValue = request.getParameter(PREFIX_VALUE + CmsPropertyDefinition.PROPERTY_NAVTEXT); 630 oldValue = request.getParameter(PREFIX_HIDDEN + CmsPropertyDefinition.PROPERTY_NAVTEXT); 631 writeProperty(CmsPropertyDefinition.PROPERTY_NAVTEXT, paramValue, oldValue); 632 } else { 633 writeProperty(CmsPropertyDefinition.PROPERTY_NAVPOS, null, null); 635 writeProperty(CmsPropertyDefinition.PROPERTY_NAVTEXT, null, null); 636 } 637 } 638 } finally { 639 if (useTempfileProject) { 640 switchToCurrentProject(); 641 } 642 } 643 return true; 644 } 645 646 657 protected void writeProperty(String propName, String propValue, String oldValue) throws CmsException { 658 659 CmsProperty currentProperty = (CmsProperty)getActiveProperties().get(propName); 661 if (currentProperty == null) { 662 currentProperty = new CmsProperty(); 664 currentProperty.setName(propName); 665 } else { 666 currentProperty = currentProperty.cloneAsProperty(); 668 } 669 670 boolean emptyParam = true; 672 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(propValue)) { 673 emptyParam = false; 674 } 675 676 if (emptyParam) { 677 if (getActiveProperties().containsKey(propName)) { 679 checkLock(getParamResource()); 681 if (currentProperty.getStructureValue() != null) { 683 currentProperty.setStructureValue(CmsProperty.DELETE_VALUE); 684 currentProperty.setResourceValue(null); 685 } else { 686 currentProperty.setResourceValue(CmsProperty.DELETE_VALUE); 687 currentProperty.setStructureValue(null); 688 } 689 getCms().writePropertyObject(getParamResource(), currentProperty); 691 } 692 } else { 693 if (!propValue.equals(oldValue)) { 695 checkLock(getParamResource()); 697 if (currentProperty.getStructureValue() == null && currentProperty.getResourceValue() == null) { 698 if (OpenCms.getWorkplaceManager().isDefaultPropertiesOnStructure()) { 700 currentProperty.setStructureValue(propValue); 701 currentProperty.setResourceValue(null); 702 } else { 703 currentProperty.setResourceValue(propValue); 704 currentProperty.setStructureValue(null); 705 } 706 } else if (currentProperty.getStructureValue() != null) { 707 currentProperty.setStructureValue(propValue); 709 currentProperty.setResourceValue(null); 710 } else { 711 currentProperty.setResourceValue(propValue); 713 currentProperty.setStructureValue(null); 714 } 715 currentProperty.setAutoCreatePropertyDefinition(true); 717 getCms().writePropertyObject(getParamResource(), currentProperty); 719 } 720 } 721 } 722 723 } 724 | Popular Tags |