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.file.types.CmsResourceTypeImage; 39 import org.opencms.jsp.CmsJspActionElement; 40 import org.opencms.main.CmsException; 41 import org.opencms.security.CmsPermissionSet; 42 import org.opencms.util.CmsStringUtil; 43 import org.opencms.util.PrintfFormat; 44 import org.opencms.workplace.CmsDialog; 45 import org.opencms.workplace.CmsWorkplaceSettings; 46 47 import java.util.ArrayList ; 48 import java.util.Iterator ; 49 import java.util.List ; 50 51 import javax.servlet.http.HttpServletRequest ; 52 import javax.servlet.http.HttpServletResponse ; 53 import javax.servlet.jsp.JspException ; 54 import javax.servlet.jsp.PageContext ; 55 56 71 public class CmsRenameImages extends CmsDialog { 72 73 74 public static final int ACTION_RENAMEIMAGES = 100; 75 76 77 public static final String DIALOG_TYPE = "renameimages"; 78 79 80 public static final String OPTION_DECIMALPLACES_1 = "1 (1, 2, ..., 9)"; 81 82 83 public static final String OPTION_DECIMALPLACES_2 = "2 (01, 02, ..., 99)"; 84 85 86 public static final String OPTION_DECIMALPLACES_3 = "3 (001, 002, ..., 999)"; 87 88 89 public static final String OPTION_DECIMALPLACES_4 = "4 (0001, 0002, ..., 9999)"; 90 91 92 public static final String PARAM_PLACES = "places"; 93 94 95 public static final String PARAM_PREFIX = "prefix"; 96 97 98 public static final String PARAM_REMOVETITLE = "removetitle"; 99 100 101 public static final String PARAM_STARTCOUNT = "startcount"; 102 103 private String m_paramPlaces; 104 private String m_paramPrefix; 105 private String m_paramRemovetitle; 106 private String m_paramStartcount; 107 108 113 public CmsRenameImages(CmsJspActionElement jsp) { 114 115 super(jsp); 116 } 117 118 125 public CmsRenameImages(PageContext context, HttpServletRequest req, HttpServletResponse res) { 126 127 this(new CmsJspActionElement(context, req, res)); 128 } 129 130 135 public void actionRenameImages() throws JspException { 136 137 getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this); 139 try { 140 if (performDialogOperation()) { 141 actionCloseDialog(); 143 } else { 144 getJsp().include(FILE_DIALOG_SCREEN_WAIT); 146 } 147 } catch (Throwable e) { 148 includeErrorpage(this, e); 150 } 151 } 152 153 158 public String buildImageInformation() { 159 160 int count = 0; 162 try { 163 CmsResourceFilter filter = CmsResourceFilter.IGNORE_EXPIRATION.addRequireType(CmsResourceTypeImage.getStaticTypeId()); 164 List images = getCms().readResources(getParamResource(), filter, false); 165 count = images.size(); 166 } catch (CmsException e) { 167 } 169 170 Object [] args = new Object [] {getParamResource(), new Integer (count)}; 171 return key(Messages.GUI_RENAMEIMAGES_INFO_IMAGECOUNT_2, args); 172 } 173 174 180 public String buildSelectPlaces(String htmlAttributes) { 181 182 List options = new ArrayList (4); 183 options.add(OPTION_DECIMALPLACES_1); 184 options.add(OPTION_DECIMALPLACES_2); 185 options.add(OPTION_DECIMALPLACES_3); 186 options.add(OPTION_DECIMALPLACES_4); 187 List values = new ArrayList (4); 188 values.add("1"); 189 values.add("2"); 190 values.add("3"); 191 values.add("4"); 192 int selectedIndex = 2; 193 if (getAction() != ACTION_DEFAULT) { 194 selectedIndex = values.indexOf(getParamPlaces()); 195 } 196 return buildSelect(htmlAttributes, options, values, selectedIndex); 197 } 198 199 204 public String getDefaultPrefix() { 205 206 return key(Messages.GUI_RENAMEIMAGES_DEFAULT_PREFIX_0); 207 } 208 209 214 public String getDefaultStartcount() { 215 216 return "1"; 217 } 218 219 224 public String getParamPlaces() { 225 226 return m_paramPlaces; 227 } 228 229 234 public String getParamPrefix() { 235 236 return m_paramPrefix; 237 } 238 239 244 public String getParamRemovetitle() { 245 246 return m_paramRemovetitle; 247 } 248 249 254 public String getParamStartcount() { 255 256 return m_paramStartcount; 257 } 258 259 264 public void setParamPlaces(String paramPlaces) { 265 266 m_paramPlaces = paramPlaces; 267 } 268 269 274 public void setParamPrefix(String paramPrefix) { 275 276 m_paramPrefix = paramPrefix; 277 } 278 279 284 public void setParamRemovetitle(String paramRemovetitle) { 285 286 m_paramRemovetitle = paramRemovetitle; 287 } 288 289 294 public void setParamStartcount(String paramStartcount) { 295 296 m_paramStartcount = paramStartcount; 297 } 298 299 302 protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) { 303 304 fillParamValues(request); 306 307 if (!checkResourcePermissions(CmsPermissionSet.ACCESS_WRITE, false)) { 309 setParamAction(DIALOG_CANCEL); 311 } 312 313 setParamDialogtype(DIALOG_TYPE); 315 if (DIALOG_TYPE.equals(getParamAction())) { 317 setAction(ACTION_RENAMEIMAGES); 318 } else if (DIALOG_WAIT.equals(getParamAction())) { 319 setAction(ACTION_WAIT); 320 } else if (DIALOG_CANCEL.equals(getParamAction())) { 321 setAction(ACTION_CANCEL); 322 } else { 323 setAction(ACTION_DEFAULT); 324 Object [] args = new Object [] {getParamResource()}; 326 setParamTitle(key(Messages.GUI_RENAMEIMAGES_TITLE_1, args)); 327 } 328 } 329 330 336 protected boolean performDialogOperation() throws CmsException { 337 338 if (!DIALOG_WAIT.equals(getParamAction())) { 340 return false; 342 } 343 344 checkLock(getParamResource()); 346 347 CmsResourceFilter filter = CmsResourceFilter.IGNORE_EXPIRATION.addRequireType(CmsResourceTypeImage.getStaticTypeId()); 349 List images = getCms().readResources(getParamResource(), filter, false); 350 351 int count = 1; 353 try { 354 count = Integer.parseInt(getParamStartcount()); 355 } catch (Exception e) { 356 } 358 359 PrintfFormat numberFormat = new PrintfFormat("%0." + getParamPlaces() + "d"); 361 362 String folder = getParamResource(); 364 if (!folder.endsWith("/")) { 365 folder += "/"; 366 } 367 368 Iterator i = images.iterator(); 369 while (i.hasNext()) { 371 CmsResource res = (CmsResource)i.next(); 372 String oldName = CmsResource.getName(res.getRootPath()); 373 CmsProperty titleProperty = getCms().readPropertyObject(res, CmsPropertyDefinition.PROPERTY_TITLE, false); 374 String oldTitle = titleProperty.getValue(); 375 376 int lastDot = oldName.lastIndexOf('.'); 378 String suffix = ""; 379 String oldNameWithoutSuffix = oldName; 380 if (lastDot > -1) { 381 suffix = oldName.substring(lastDot); 382 oldNameWithoutSuffix = oldName.substring(0, lastDot); 383 } 384 385 String newName = ""; 387 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getParamPrefix()) && !"null".equals(getParamPrefix())) { 388 newName += getParamPrefix(); 389 } 390 String imageNumber = numberFormat.sprintf(count); 392 newName += imageNumber + suffix; 393 394 if (!newName.equals(oldName)) { 395 if (getCms().existsResource(folder + newName, CmsResourceFilter.ALL)) { 397 throw new CmsException(Messages.get().container( 399 Messages.ERR_MOVE_FAILED_TARGET_EXISTS_2, 400 getCms().getSitePath(res), 401 folder + newName)); 402 } 403 404 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(oldTitle)) { 406 if (oldTitle.equals(oldNameWithoutSuffix)) { 407 if (Boolean.valueOf(getParamRemovetitle()).booleanValue()) { 408 if (oldTitle.equals(titleProperty.getStructureValue())) { 410 titleProperty.setStructureValue(CmsProperty.DELETE_VALUE); 411 } 412 if (oldTitle.equals(titleProperty.getResourceValue())) { 413 titleProperty.setResourceValue(CmsProperty.DELETE_VALUE); 414 } 415 } else { 416 if (oldTitle.equals(titleProperty.getStructureValue())) { 418 titleProperty.setStructureValue(getParamPrefix() + imageNumber); 419 } else if (oldTitle.equals(titleProperty.getResourceValue())) { 420 titleProperty.setResourceValue(getParamPrefix() + imageNumber); 421 } 422 } 423 getCms().writePropertyObject(getCms().getSitePath(res), titleProperty); 425 } 426 } 427 428 getCms().renameResource(folder + oldName, folder + newName); 430 } 431 432 count++; 434 } 435 436 return true; 437 } 438 439 } 440 | Popular Tags |