1 31 32 package org.opencms.workplace.tools.content; 33 34 import org.opencms.file.CmsObject; 35 import org.opencms.file.CmsProperty; 36 import org.opencms.file.CmsPropertyDefinition; 37 import org.opencms.file.CmsResource; 38 import org.opencms.file.CmsResourceFilter; 39 import org.opencms.file.CmsVfsException; 40 import org.opencms.i18n.CmsEncoder; 41 import org.opencms.i18n.CmsMessages; 42 import org.opencms.jsp.CmsJspActionElement; 43 import org.opencms.main.CmsException; 44 import org.opencms.main.CmsLog; 45 import org.opencms.util.CmsStringUtil; 46 import org.opencms.workplace.CmsDialog; 47 import org.opencms.workplace.CmsWorkplaceSettings; 48 49 import java.util.ArrayList ; 50 import java.util.List ; 51 import java.util.regex.Pattern ; 52 import java.util.regex.PatternSyntaxException ; 53 54 import javax.servlet.http.HttpServletRequest ; 55 import javax.servlet.http.HttpServletResponse ; 56 import javax.servlet.jsp.JspException ; 57 import javax.servlet.jsp.PageContext ; 58 59 import org.apache.commons.logging.Log; 60 61 70 public class CmsPropertyChange extends CmsDialog { 71 72 73 public static final int ACTION_SHOWRESULT = 100; 74 75 76 public static final String DIALOG_SHOWRESULT = "showresult"; 77 78 79 public static final String DIALOG_TYPE = "propertychange"; 80 81 public static final String PARAM_NEWVALUE = "newvalue"; 82 83 public static final String PARAM_OLDVALUE = "oldvalue"; 84 85 public static final String PARAM_PROPERTYNAME = "propertyname"; 86 87 public static final String PARAM_RECURSIVE = "recursive"; 88 89 90 private static final Log LOG = CmsLog.getLog(CmsPropertyChange.class); 91 92 private List m_changedResources; 93 94 95 private String m_errorMessage; 96 97 private String m_paramNewValue; 98 private String m_paramOldValue; 99 private String m_paramPropertyName; 100 private String m_paramRecursive; 101 102 private boolean m_validationErrors; 103 104 109 public CmsPropertyChange(CmsJspActionElement jsp) { 110 111 super(jsp); 112 } 113 114 121 public CmsPropertyChange(PageContext context, HttpServletRequest req, HttpServletResponse res) { 122 123 this(new CmsJspActionElement(context, req, res)); 124 } 125 126 135 public static String buildSelectProperty(CmsObject cms, String selectValue, String attributes, String selectedValue) { 136 137 List propertyDef = new ArrayList (); 138 try { 139 propertyDef = cms.readAllPropertyDefinitions(); 141 } catch (CmsException e) { 142 if (LOG.isInfoEnabled()) { 144 LOG.info(e); 145 } 146 } 147 148 int propertyCount = propertyDef.size(); 149 List options = new ArrayList (propertyCount + 1); 150 List values = new ArrayList (propertyCount + 1); 151 options.add(CmsEncoder.escapeXml(selectValue)); 152 values.add(""); 153 int selectedIndex = 0; 154 int count = 1; 155 156 for (int i = 0; i < propertyCount; i++) { 157 CmsPropertyDefinition currDef = (CmsPropertyDefinition)propertyDef.get(i); 159 if (currDef.getName().equals(selectedValue)) { 160 selectedIndex = count; 161 } 162 options.add(CmsEncoder.escapeXml(currDef.getName())); 163 values.add(CmsEncoder.escapeXml(currDef.getName())); 164 count += 1; 165 } 166 167 CmsDialog wp = new CmsDialog(null); 168 return wp.buildSelect(attributes, options, values, selectedIndex); 169 } 170 171 176 public void actionChange() throws JspException { 177 178 getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this); 180 try { 181 boolean recursive = Boolean.valueOf(getParamRecursive()).booleanValue(); 182 if (performChangeOperation(recursive)) { 183 setAction(ACTION_SHOWRESULT); 185 } else { 186 getJsp().include(FILE_DIALOG_SCREEN_WAIT); 188 } 189 } catch (Throwable e) { 190 includeErrorpage(this, e); 192 193 } 194 } 195 196 201 public String buildResultList() { 202 203 StringBuffer result = new StringBuffer (16); 204 if (getChangedResources() != null && getChangedResources().size() > 0) { 205 for (int i = 0; i < getChangedResources().size(); i++) { 207 CmsResource res = (CmsResource)getChangedResources().get(i); 208 String resName = getCms().getSitePath(res); 209 result.append(resName); 210 result.append("<br>\n"); 211 } 212 } else { 213 result.append(Messages.get().getBundle(getLocale()).key(Messages.GUI_INPUT_PROPERTYCHANGE_RESULT_NONE_0)); 215 } 216 return result.toString(); 217 } 218 219 225 public String buildSelectProperty(String attributes) { 226 227 return buildSelectProperty( 228 getCms(), 229 Messages.get().getBundle(getLocale()).key(Messages.GUI_PLEASE_SELECT_0), 230 attributes, 231 getParamPropertyName()); 232 } 233 234 239 public String getErrorMessage() { 240 241 if (CmsStringUtil.isEmpty(m_errorMessage)) { 242 return ""; 243 } 244 245 return m_errorMessage; 246 } 247 248 253 public String getParamNewValue() { 254 255 if (m_paramNewValue != null) { 256 return m_paramNewValue; 257 } else { 258 return CmsProperty.DELETE_VALUE; 259 } 260 } 261 262 267 public String getParamOldValue() { 268 269 return m_paramOldValue; 270 } 271 272 277 public String getParamPropertyName() { 278 279 return m_paramPropertyName; 280 } 281 282 287 public String getParamRecursive() { 288 289 return m_paramRecursive; 290 } 291 292 297 public String getResultListHeight() { 298 299 if (getChangedResources() != null && getChangedResources().size() > 0) { 300 int height = getChangedResources().size() * 14; 301 if (height > 300) { 302 height = 300; 303 } 304 return "" + height; 305 } else { 306 return "14"; 307 } 308 } 309 310 315 public boolean hasValidationErrors() { 316 317 return m_validationErrors; 318 } 319 320 325 public void setParamNewValue(String paramNewValue) { 326 327 m_paramNewValue = paramNewValue; 328 } 329 330 335 public void setParamOldValue(String paramOldValue) { 336 337 m_paramOldValue = paramOldValue; 338 } 339 340 345 public void setParamPropertyName(String paramPropertyName) { 346 347 m_paramPropertyName = paramPropertyName; 348 } 349 350 355 public void setParamRecursive(String paramRecursive) { 356 357 m_paramRecursive = paramRecursive; 358 } 359 360 363 protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) { 364 365 fillParamValues(request); 367 setParamDialogtype(DIALOG_TYPE); 369 if (DIALOG_OK.equals(getParamAction())) { 371 if (validateParameters()) { 372 setAction(ACTION_OK); 374 } else { 375 setAction(ACTION_DEFAULT); 377 } 378 } else if (DIALOG_WAIT.equals(getParamAction())) { 379 setAction(ACTION_WAIT); 380 } else if (DIALOG_CANCEL.equals(getParamAction())) { 381 setAction(ACTION_CANCEL); 382 } else { 383 setAction(ACTION_DEFAULT); 384 setParamTitle(Messages.get().getBundle(getLocale()).key(Messages.GUI_TITLE_PROPERTYCHANGE_0)); 386 } 387 } 388 389 394 protected void setErrorMessage(String errorMessage) { 395 396 m_errorMessage = errorMessage; 397 } 398 399 404 protected void setValidationErrors(boolean validationErrors) { 405 406 m_validationErrors = validationErrors; 407 } 408 409 414 private List getChangedResources() { 415 416 return m_changedResources; 417 } 418 419 426 private boolean performChangeOperation(boolean recursive) throws CmsException { 427 428 if (recursive && !DIALOG_WAIT.equals(getParamAction())) { 430 return false; 432 } 433 434 checkLock(getParamResource()); 436 List changedResources = null; 438 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getParamOldValue())) { 439 changedResources = getCms().changeResourcesInFolderWithProperty( 440 getParamResource(), 441 getParamPropertyName(), 442 getParamOldValue(), 443 getParamNewValue(), 444 recursive); 445 } else { 446 changedResources = setPropertyInFolder( 447 getParamResource(), 448 getParamPropertyName(), 449 getParamNewValue(), 450 recursive); 451 } 452 setChangedResources(changedResources); 453 return true; 454 } 455 456 473 private List setPropertyInFolder( 474 String resourceRootPath, 475 String propertyDefinition, 476 String newValue, 477 boolean recursive) throws CmsException, CmsVfsException { 478 479 CmsObject cms = this.getCms(); 480 481 List resources = new ArrayList (); 483 if (recursive) { 484 resources = cms.readResources(resourceRootPath, CmsResourceFilter.IGNORE_EXPIRATION); 485 } else { 486 resources.add(resourceRootPath); 487 } 488 489 List changedResources = new ArrayList (resources.size()); 490 CmsProperty newProperty = new CmsProperty(propertyDefinition, null, null); 491 for (int i = 0; i < resources.size(); i++) { 493 CmsResource res = (CmsResource)resources.get(i); 495 CmsProperty property = cms.readPropertyObject(res, propertyDefinition, false); 496 if (property.isNullProperty()) { 497 newProperty.setStructureValue(newValue); 499 newProperty.setName(propertyDefinition); 500 cms.writePropertyObject(cms.getRequestContext().removeSiteRoot(res.getRootPath()), newProperty); 501 changedResources.add(res); 502 } else { 503 } 505 } 506 return changedResources; 507 } 508 509 514 private void setChangedResources(List changedResources) { 515 516 m_changedResources = changedResources; 517 } 518 519 527 private boolean validateParameters() { 528 529 boolean allOk = true; 530 531 StringBuffer validationErrors = new StringBuffer (32); 532 CmsMessages messages = Messages.get().getBundle(getLocale()); 533 534 if (CmsStringUtil.isEmptyOrWhitespaceOnly(getParamResource()) || !getCms().existsResource(getParamResource())) { 536 allOk = false; 537 validationErrors.append(messages.key(Messages.GUI_PROP_CHANGE_VALIDATE_VFS_RESOURCE_0)).append("<br>"); 538 } 539 540 if (CmsStringUtil.isEmptyOrWhitespaceOnly(getParamPropertyName())) { 542 allOk = false; 543 validationErrors.append(messages.key(Messages.GUI_PROP_CHANGE_VALIDATE_SELECT_PROPERTY_0)).append("<br>"); 544 } 545 546 if (CmsStringUtil.isEmptyOrWhitespaceOnly(getParamOldValue())) { 548 allOk = false; 549 validationErrors.append(messages.key(Messages.GUI_PROP_CHANGE_VALIDATE_OLD_PROP_VALUE_0)).append("<br>"); 550 } else { 551 try { 552 Pattern.compile(getParamOldValue()); 554 } catch (PatternSyntaxException e) { 555 allOk = false; 556 validationErrors.append(messages.key(Messages.GUI_PROP_CHANGE_VALIDATE_OLD_PROP_PATTERN_0)).append( 557 "<br>"); 558 } 559 } 560 561 if (CmsStringUtil.isEmptyOrWhitespaceOnly(getParamNewValue())) { 563 setParamNewValue(CmsProperty.DELETE_VALUE); 565 } 566 567 setErrorMessage(validationErrors.toString()); 568 setValidationErrors(!allOk); 569 return allOk; 570 } 571 } | Popular Tags |