KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > commons > CmsRenameImages


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/commons/CmsRenameImages.java,v $
3  * Date : $Date: 2006/03/27 14:52:18 $
4  * Version: $Revision: 1.2 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

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 JavaDoc;
48 import java.util.Iterator JavaDoc;
49 import java.util.List JavaDoc;
50
51 import javax.servlet.http.HttpServletRequest JavaDoc;
52 import javax.servlet.http.HttpServletResponse JavaDoc;
53 import javax.servlet.jsp.JspException JavaDoc;
54 import javax.servlet.jsp.PageContext JavaDoc;
55
56 /**
57  * Provides methods for the rename images dialog.<p>
58  *
59  * The following files use this class:
60  * <ul>
61  * <li>/commons/renameimages.jsp
62  * </ul>
63  * <p>
64  *
65  * @author Andreas Zahner
66  *
67  * @version $Revision: 1.2 $
68  *
69  * @since 6.1.3
70  */

71 public class CmsRenameImages extends CmsDialog {
72
73     /** Value for the action: rename images. */
74     public static final int ACTION_RENAMEIMAGES = 100;
75
76     /** The dialog type. */
77     public static final String JavaDoc DIALOG_TYPE = "renameimages";
78
79     /** Selectbox option for decimal places selection: 1 place. */
80     public static final String JavaDoc OPTION_DECIMALPLACES_1 = "1 (1, 2, ..., 9)";
81
82     /** Selectbox option for decimal places selection: 2 places. */
83     public static final String JavaDoc OPTION_DECIMALPLACES_2 = "2 (01, 02, ..., 99)";
84
85     /** Selectbox option for decimal places selection: 3 places. */
86     public static final String JavaDoc OPTION_DECIMALPLACES_3 = "3 (001, 002, ..., 999)";
87
88     /** Selectbox option for decimal places selection: 4 places. */
89     public static final String JavaDoc OPTION_DECIMALPLACES_4 = "4 (0001, 0002, ..., 9999)";
90
91     /** Request parameter name for the counter places. */
92     public static final String JavaDoc PARAM_PLACES = "places";
93
94     /** Request parameter name for the image prefix. */
95     public static final String JavaDoc PARAM_PREFIX = "prefix";
96
97     /** Request parameter name for the remove title flag. */
98     public static final String JavaDoc PARAM_REMOVETITLE = "removetitle";
99
100     /** Request parameter name for the start count. */
101     public static final String JavaDoc PARAM_STARTCOUNT = "startcount";
102
103     private String JavaDoc m_paramPlaces;
104     private String JavaDoc m_paramPrefix;
105     private String JavaDoc m_paramRemovetitle;
106     private String JavaDoc m_paramStartcount;
107
108     /**
109      * Public constructor with JSP action element.<p>
110      *
111      * @param jsp an initialized JSP action element
112      */

113     public CmsRenameImages(CmsJspActionElement jsp) {
114
115         super(jsp);
116     }
117
118     /**
119      * Public constructor with JSP variables.<p>
120      *
121      * @param context the JSP page context
122      * @param req the JSP request
123      * @param res the JSP response
124      */

125     public CmsRenameImages(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
126
127         this(new CmsJspActionElement(context, req, res));
128     }
129
130     /**
131      * Performs the rename images action, will be called by the JSP page.<p>
132      *
133      * @throws JspException if problems including sub-elements occur
134      */

135     public void actionRenameImages() throws JspException JavaDoc {
136
137         // save initialized instance of this class in request attribute for included sub-elements
138
getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this);
139         try {
140             if (performDialogOperation()) {
141                 // if no exception is caused and "true" is returned rename operation was successful
142
actionCloseDialog();
143             } else {
144                 // "false" returned, display "please wait" screen
145
getJsp().include(FILE_DIALOG_SCREEN_WAIT);
146             }
147         } catch (Throwable JavaDoc e) {
148             // error during rename images, show error dialog
149
includeErrorpage(this, e);
150         }
151     }
152
153     /**
154      * Returns information about the image count of the selected gallery folder.<p>
155      *
156      * @return information about the image count of the selected gallery folder
157      */

158     public String JavaDoc buildImageInformation() {
159
160         // count all image resources of the gallery folder
161
int count = 0;
162         try {
163             CmsResourceFilter filter = CmsResourceFilter.IGNORE_EXPIRATION.addRequireType(CmsResourceTypeImage.getStaticTypeId());
164             List JavaDoc images = getCms().readResources(getParamResource(), filter, false);
165             count = images.size();
166         } catch (CmsException e) {
167             // ignore this exception
168
}
169
170         Object JavaDoc[] args = new Object JavaDoc[] {getParamResource(), new Integer JavaDoc(count)};
171         return key(Messages.GUI_RENAMEIMAGES_INFO_IMAGECOUNT_2, args);
172     }
173
174     /**
175      * Builds the html for the default copy folder mode select box.<p>
176      *
177      * @param htmlAttributes optional html attributes for the &lgt;select&gt; tag
178      * @return the html for the default copy folder mode select box
179      */

180     public String JavaDoc buildSelectPlaces(String JavaDoc htmlAttributes) {
181
182         List JavaDoc options = new ArrayList JavaDoc(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 JavaDoc values = new ArrayList JavaDoc(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     /**
200      * Returns the default prefix shown when opening the dialog.<p>
201      *
202      * @return the default prefix shown when opening the dialog
203      */

204     public String JavaDoc getDefaultPrefix() {
205
206         return key(Messages.GUI_RENAMEIMAGES_DEFAULT_PREFIX_0);
207     }
208
209     /**
210      * Returns the default start count shown when opening the dialog.<p>
211      *
212      * @return the default start count shown when opening the dialog
213      */

214     public String JavaDoc getDefaultStartcount() {
215
216         return "1";
217     }
218
219     /**
220      * Returns the value of the places parameter.<p>
221      *
222      * @return the value of the places parameter
223      */

224     public String JavaDoc getParamPlaces() {
225
226         return m_paramPlaces;
227     }
228
229     /**
230      * Returns the value of the prefix parameter.<p>
231      *
232      * @return the value of the prefix parameter
233      */

234     public String JavaDoc getParamPrefix() {
235
236         return m_paramPrefix;
237     }
238
239     /**
240      * Returns the value of the remove title parameter.<p>
241      *
242      * @return the value of the remove title parameter
243      */

244     public String JavaDoc getParamRemovetitle() {
245
246         return m_paramRemovetitle;
247     }
248
249     /**
250      * Returns the value of the startcount parameter.<p>
251      *
252      * @return the value of the startcount parameter
253      */

254     public String JavaDoc getParamStartcount() {
255
256         return m_paramStartcount;
257     }
258
259     /**
260      * Sets the value of the places parameter.<p>
261      *
262      * @param paramPlaces the value of the places parameter
263      */

264     public void setParamPlaces(String JavaDoc paramPlaces) {
265
266         m_paramPlaces = paramPlaces;
267     }
268
269     /**
270      * Sets the value of the prefix parameter.<p>
271      *
272      * @param paramPrefix the value of the prefix parameter
273      */

274     public void setParamPrefix(String JavaDoc paramPrefix) {
275
276         m_paramPrefix = paramPrefix;
277     }
278
279     /**
280      * Sets the value of the remove title parameter.<p>
281      *
282      * @param paramRemovetitle the value of the remove title parameter
283      */

284     public void setParamRemovetitle(String JavaDoc paramRemovetitle) {
285
286         m_paramRemovetitle = paramRemovetitle;
287     }
288
289     /**
290      * Sets the value of the startcount parameter.<p>
291      *
292      * @param paramStartcount the value of the startcount parameter
293      */

294     public void setParamStartcount(String JavaDoc paramStartcount) {
295
296         m_paramStartcount = paramStartcount;
297     }
298
299     /**
300      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
301      */

302     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
303
304         // fill the parameter values in the get/set methods
305
fillParamValues(request);
306
307         // check the required permissions to rename the resource
308
if (!checkResourcePermissions(CmsPermissionSet.ACCESS_WRITE, false)) {
309             // no write permissions for the resource, set cancel action to close dialog
310
setParamAction(DIALOG_CANCEL);
311         }
312
313         // set the dialog type
314
setParamDialogtype(DIALOG_TYPE);
315         // set the action for the JSP switch
316
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             // build title for rename images dialog
325
Object JavaDoc[] args = new Object JavaDoc[] {getParamResource()};
326             setParamTitle(key(Messages.GUI_RENAMEIMAGES_TITLE_1, args));
327         }
328     }
329
330     /**
331      * Performs the rename images operation.<p>
332      *
333      * @return true, if the resources were successfully renamed, otherwise false
334      * @throws CmsException if renaming is not successful
335      */

336     protected boolean performDialogOperation() throws CmsException {
337
338         // display "please wait" screen before renaming the images
339
if (!DIALOG_WAIT.equals(getParamAction())) {
340             // return false, this will trigger the "please wait" screen
341
return false;
342         }
343
344         // lock the image gallery folder
345
checkLock(getParamResource());
346
347         // get all image resources of the folder
348
CmsResourceFilter filter = CmsResourceFilter.IGNORE_EXPIRATION.addRequireType(CmsResourceTypeImage.getStaticTypeId());
349         List JavaDoc images = getCms().readResources(getParamResource(), filter, false);
350
351         // determine start count
352
int count = 1;
353         try {
354             count = Integer.parseInt(getParamStartcount());
355         } catch (Exception JavaDoc e) {
356             // ignore this exception
357
}
358
359         // create number printer instance
360
PrintfFormat numberFormat = new PrintfFormat("%0." + getParamPlaces() + "d");
361
362         // create image galler folder name
363
String JavaDoc folder = getParamResource();
364         if (!folder.endsWith("/")) {
365             folder += "/";
366         }
367
368         Iterator JavaDoc i = images.iterator();
369         // loop over all image resource to change
370
while (i.hasNext()) {
371             CmsResource res = (CmsResource)i.next();
372             String JavaDoc oldName = CmsResource.getName(res.getRootPath());
373             CmsProperty titleProperty = getCms().readPropertyObject(res, CmsPropertyDefinition.PROPERTY_TITLE, false);
374             String JavaDoc oldTitle = titleProperty.getValue();
375
376             // store image name suffix
377
int lastDot = oldName.lastIndexOf('.');
378             String JavaDoc suffix = "";
379             String JavaDoc oldNameWithoutSuffix = oldName;
380             if (lastDot > -1) {
381                 suffix = oldName.substring(lastDot);
382                 oldNameWithoutSuffix = oldName.substring(0, lastDot);
383             }
384
385             // determine new image name
386
String JavaDoc newName = "";
387             if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getParamPrefix()) && !"null".equals(getParamPrefix())) {
388                 newName += getParamPrefix();
389             }
390             // create image number
391
String JavaDoc imageNumber = numberFormat.sprintf(count);
392             newName += imageNumber + suffix;
393
394             if (!newName.equals(oldName)) {
395                 // only rename resources which have a new resource name
396
if (getCms().existsResource(folder + newName, CmsResourceFilter.ALL)) {
397                     // target resource exists, interrupt & show error
398
throw new CmsException(Messages.get().container(
399                         Messages.ERR_MOVE_FAILED_TARGET_EXISTS_2,
400                         getCms().getSitePath(res),
401                         folder + newName));
402                 }
403
404                 // determine the new title property value
405
if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(oldTitle)) {
406                     if (oldTitle.equals(oldNameWithoutSuffix)) {
407                         if (Boolean.valueOf(getParamRemovetitle()).booleanValue()) {
408                             // remove the title property value
409
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                             // set the title property to the new resource name
417
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                         // write changed title property
424
getCms().writePropertyObject(getCms().getSitePath(res), titleProperty);
425                     }
426                 }
427
428                 // now rename the resource
429
getCms().renameResource(folder + oldName, folder + newName);
430             }
431
432             // increase image counter
433
count++;
434         }
435
436         return true;
437     }
438
439 }
440
Popular Tags