KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > tools > content > CmsPropertyChange


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/content/CmsPropertyChange.java,v $
3  * Date : $Date: 2006/09/25 08:27:38 $
4  * Version: $Revision: 1.15 $
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.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 JavaDoc;
50 import java.util.List JavaDoc;
51 import java.util.regex.Pattern JavaDoc;
52 import java.util.regex.PatternSyntaxException JavaDoc;
53
54 import javax.servlet.http.HttpServletRequest JavaDoc;
55 import javax.servlet.http.HttpServletResponse JavaDoc;
56 import javax.servlet.jsp.JspException JavaDoc;
57 import javax.servlet.jsp.PageContext JavaDoc;
58
59 import org.apache.commons.logging.Log;
60
61 /**
62  * Provides methods for the change property values dialog.<p>
63  *
64  * @author Andreas Zahner
65  *
66  * @version $Revision: 1.15 $
67  *
68  * @since 6.0.0
69  */

70 public class CmsPropertyChange extends CmsDialog {
71
72     /** Value for the action: show result. */
73     public static final int ACTION_SHOWRESULT = 100;
74
75     /** Request parameter value for the action: show result. */
76     public static final String JavaDoc DIALOG_SHOWRESULT = "showresult";
77
78     /** The dialog type. */
79     public static final String JavaDoc DIALOG_TYPE = "propertychange";
80     /** Request parameter name for the property name. */
81     public static final String JavaDoc PARAM_NEWVALUE = "newvalue";
82     /** Request parameter name for the property name. */
83     public static final String JavaDoc PARAM_OLDVALUE = "oldvalue";
84     /** Request parameter name for the property name. */
85     public static final String JavaDoc PARAM_PROPERTYNAME = "propertyname";
86     /** Request parameter name for the property name. */
87     public static final String JavaDoc PARAM_RECURSIVE = "recursive";
88
89     /** The log object for this class. */
90     private static final Log LOG = CmsLog.getLog(CmsPropertyChange.class);
91
92     private List JavaDoc m_changedResources;
93
94     /** The error message. */
95     private String JavaDoc m_errorMessage;
96
97     private String JavaDoc m_paramNewValue;
98     private String JavaDoc m_paramOldValue;
99     private String JavaDoc m_paramPropertyName;
100     private String JavaDoc m_paramRecursive;
101
102     private boolean m_validationErrors;
103
104     /**
105      * Public constructor with JSP action element.<p>
106      *
107      * @param jsp an initialized JSP action element
108      */

109     public CmsPropertyChange(CmsJspActionElement jsp) {
110
111         super(jsp);
112     }
113
114     /**
115      * Public constructor with JSP variables.<p>
116      *
117      * @param context the JSP page context
118      * @param req the JSP request
119      * @param res the JSP response
120      */

121     public CmsPropertyChange(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
122
123         this(new CmsJspActionElement(context, req, res));
124     }
125
126     /**
127      * Builds the html for the property definition select box.<p>
128      *
129      * @param cms the CmsObject
130      * @param selectValue the localized value for the "Please select" option
131      * @param attributes optional attributes for the &lt;select&gt; tag
132      * @param selectedValue the value that is currently selected
133      * @return the html for the property definition select box
134      */

135     public static String JavaDoc buildSelectProperty(CmsObject cms, String JavaDoc selectValue, String JavaDoc attributes, String JavaDoc selectedValue) {
136
137         List JavaDoc propertyDef = new ArrayList JavaDoc();
138         try {
139             // get all property definitions
140
propertyDef = cms.readAllPropertyDefinitions();
141         } catch (CmsException e) {
142             // should usually never happen
143
if (LOG.isInfoEnabled()) {
144                 LOG.info(e);
145             }
146         }
147
148         int propertyCount = propertyDef.size();
149         List JavaDoc options = new ArrayList JavaDoc(propertyCount + 1);
150         List JavaDoc values = new ArrayList JavaDoc(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             // loop property definitions and get definition name
158
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     /**
172      * Changes the property values on the specified resources.<p>
173      *
174      * @throws JspException if problems including sub-elements occur
175      */

176     public void actionChange() throws JspException JavaDoc {
177
178         // save initialized instance of this class in request attribute for included sub-elements
179
getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this);
180         try {
181             boolean recursive = Boolean.valueOf(getParamRecursive()).booleanValue();
182             if (performChangeOperation(recursive)) {
183                 // if no exception is caused and "true" is returned change property operation was successful
184
setAction(ACTION_SHOWRESULT);
185             } else {
186                 // "false" returned, display "please wait" screen
187
getJsp().include(FILE_DIALOG_SCREEN_WAIT);
188             }
189         } catch (Throwable JavaDoc e) {
190             // error while changing property values, show error dialog
191
includeErrorpage(this, e);
192
193         }
194     }
195
196     /**
197      * Builds the html for the result list of resources where the property was changed.<p>
198      *
199      * @return the html for the result list
200      */

201     public String JavaDoc buildResultList() {
202
203         StringBuffer JavaDoc result = new StringBuffer JavaDoc(16);
204         if (getChangedResources() != null && getChangedResources().size() > 0) {
205             // at least one resource property value has been changed, show list
206
for (int i = 0; i < getChangedResources().size(); i++) {
207                 CmsResource res = (CmsResource)getChangedResources().get(i);
208                 String JavaDoc resName = getCms().getSitePath(res);
209                 result.append(resName);
210                 result.append("<br>\n");
211             }
212         } else {
213             // nothing was changed, show message
214
result.append(Messages.get().getBundle(getLocale()).key(Messages.GUI_INPUT_PROPERTYCHANGE_RESULT_NONE_0));
215         }
216         return result.toString();
217     }
218
219     /**
220      * Builds the html for the property definition select box.<p>
221      *
222      * @param attributes optional attributes for the &lt;select&gt; tag
223      * @return the html for the property definition select box
224      */

225     public String JavaDoc buildSelectProperty(String JavaDoc 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     /**
235      * Returns the error message.<p>
236      *
237      * @return the error message
238      */

239     public String JavaDoc getErrorMessage() {
240
241         if (CmsStringUtil.isEmpty(m_errorMessage)) {
242             return "";
243         }
244
245         return m_errorMessage;
246     }
247
248     /**
249      * Returns the value of the newvalue parameter.<p>
250      *
251      * @return the value of the newvalue parameter
252      */

253     public String JavaDoc getParamNewValue() {
254
255         if (m_paramNewValue != null) {
256             return m_paramNewValue;
257         } else {
258             return CmsProperty.DELETE_VALUE;
259         }
260     }
261
262     /**
263      * Returns the value of the oldvalue parametere.<p>
264      *
265      * @return the value of the oldvalue parameter
266      */

267     public String JavaDoc getParamOldValue() {
268
269         return m_paramOldValue;
270     }
271
272     /**
273      * Returns the value of the propertyname parameter.<p>
274      *
275      * @return the value of the propertyname parameter
276      */

277     public String JavaDoc getParamPropertyName() {
278
279         return m_paramPropertyName;
280     }
281
282     /**
283      * Returns the value of the recursive parameter.<p>
284      *
285      * @return the value of the recursive parameter
286      */

287     public String JavaDoc getParamRecursive() {
288
289         return m_paramRecursive;
290     }
291
292     /**
293      * Returns the height for the result list of changed resources.<p>
294      *
295      * @return the height for the result list of changed resources
296      */

297     public String JavaDoc 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     /**
311      * Returns if validation errors were found.<p>
312      *
313      * @return true if validation errors were found, otherwise false
314      */

315     public boolean hasValidationErrors() {
316
317         return m_validationErrors;
318     }
319
320     /**
321      * Sets the value of the newvalue parameter.<p>
322      *
323      * @param paramNewValue the value of the newvalue parameter
324      */

325     public void setParamNewValue(String JavaDoc paramNewValue) {
326
327         m_paramNewValue = paramNewValue;
328     }
329
330     /**
331      * Sets the value of the oldvalue parameter.<p>
332      *
333      * @param paramOldValue the value of the oldvalue parameter
334      */

335     public void setParamOldValue(String JavaDoc paramOldValue) {
336
337         m_paramOldValue = paramOldValue;
338     }
339
340     /**
341      * Sets the value of the propertyname parameter.<p>
342      *
343      * @param paramPropertyName the value of the propertyname parameter
344      */

345     public void setParamPropertyName(String JavaDoc paramPropertyName) {
346
347         m_paramPropertyName = paramPropertyName;
348     }
349
350     /**
351      * Sets the value of the recursive parameter.<p>
352      *
353      * @param paramRecursive the value of the recursive parameter
354      */

355     public void setParamRecursive(String JavaDoc paramRecursive) {
356
357         m_paramRecursive = paramRecursive;
358     }
359
360     /**
361      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
362      */

363     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
364
365         // fill the parameter values in the get/set methods
366
fillParamValues(request);
367         // set the dialog type
368
setParamDialogtype(DIALOG_TYPE);
369         // set the action for the JSP switch
370
if (DIALOG_OK.equals(getParamAction())) {
371             if (validateParameters()) {
372                 // all parameters are valid, proceed
373
setAction(ACTION_OK);
374             } else {
375                 // validation error(s), redisplay form
376
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             // build title for change property value dialog
385
setParamTitle(Messages.get().getBundle(getLocale()).key(Messages.GUI_TITLE_PROPERTYCHANGE_0));
386         }
387     }
388
389     /**
390      * Sets the error message.<p>
391      *
392      * @param errorMessage the error message to set
393      */

394     protected void setErrorMessage(String JavaDoc errorMessage) {
395
396         m_errorMessage = errorMessage;
397     }
398
399     /**
400      * Sets the validation error flag.<p>
401      *
402      * @param validationErrors the validation error flag, true if validation errors were found
403      */

404     protected void setValidationErrors(boolean validationErrors) {
405
406         m_validationErrors = validationErrors;
407     }
408
409     /**
410      * Returns the changed resources that were affected by the property change action.<p>
411      *
412      * @return the changed resources that were affected by the property change action
413      */

414     private List JavaDoc getChangedResources() {
415
416         return m_changedResources;
417     }
418
419     /**
420      * Performs the main property change value operation on the resource property.<p>
421      *
422      * @param recursive true, if the property value has to be changed recursively, otherwise false
423      * @return true, if the property values are changed successfully, otherwise false
424      * @throws CmsException if changing is not successful
425      */

426     private boolean performChangeOperation(boolean recursive) throws CmsException {
427
428         // on recursive property changes display "please wait" screen
429
if (recursive && !DIALOG_WAIT.equals(getParamAction())) {
430             // return false, this will trigger the "please wait" screen
431
return false;
432         }
433
434         // lock the selected resource
435
checkLock(getParamResource());
436         // change the property values
437
List JavaDoc 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     /**
457      * Sets the given property with the given value to the given resource
458      * (potentially recursiv) if it has not been set before.<p>
459      *
460      * Returns a list with all sub resources that have been modified this way.<p>
461      *
462      * @param context the current request context
463      * @param resourceRootPath the resource on which property definition values are changed
464      * @param propertyDefinition the name of the propertydefinition to change the value
465      * @param newValue the new value of the propertydefinition
466      * @param recursive if true, change recursively all property values on sub-resources (only for folders)
467      *
468      * @return a list with the <code>{@link CmsResource}</code>'s where the property value has been changed
469      *
470      * @throws CmsVfsException for now only when the search for the oldvalue failed.
471      * @throws CmsException if operation was not successful
472      */

473     private List JavaDoc setPropertyInFolder(
474         String JavaDoc resourceRootPath,
475         String JavaDoc propertyDefinition,
476         String JavaDoc newValue,
477         boolean recursive) throws CmsException, CmsVfsException {
478
479         CmsObject cms = this.getCms();
480
481         // collect the resources to look up
482
List JavaDoc resources = new ArrayList JavaDoc();
483         if (recursive) {
484             resources = cms.readResources(resourceRootPath, CmsResourceFilter.IGNORE_EXPIRATION);
485         } else {
486             resources.add(resourceRootPath);
487         }
488
489         List JavaDoc changedResources = new ArrayList JavaDoc(resources.size());
490         CmsProperty newProperty = new CmsProperty(propertyDefinition, null, null);
491         // create permission set and filter to check each resource
492
for (int i = 0; i < resources.size(); i++) {
493             // loop through found resources and check property values
494
CmsResource res = (CmsResource)resources.get(i);
495             CmsProperty property = cms.readPropertyObject(res, propertyDefinition, false);
496             if (property.isNullProperty()) {
497                 // change structure value
498
newProperty.setStructureValue(newValue);
499                 newProperty.setName(propertyDefinition);
500                 cms.writePropertyObject(cms.getRequestContext().removeSiteRoot(res.getRootPath()), newProperty);
501                 changedResources.add(res);
502             } else {
503                 // nop
504
}
505         }
506         return changedResources;
507     }
508     
509     /**
510      * Sets the changed resources that were affected by the property change action.<p>
511      *
512      * @param changedResources the changed resources that were affected by the property change action
513      */

514     private void setChangedResources(List JavaDoc changedResources) {
515
516         m_changedResources = changedResources;
517     }
518
519     /**
520      * Validates the submitted form parameters.<p>
521      *
522      * If parameters are missing, a localized error message String is created.<p>
523      *
524      * @return true if all parameters are correct, otherwise false
525      *
526      */

527     private boolean validateParameters() {
528
529         boolean allOk = true;
530
531         StringBuffer JavaDoc validationErrors = new StringBuffer JavaDoc(32);
532         CmsMessages messages = Messages.get().getBundle(getLocale());
533
534         // check resource parameter presence
535
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         // check selected property name
541
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         // check old property value to look up
547
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                 // compile regular expression pattern
553
Pattern.compile(getParamOldValue());
554             } catch (PatternSyntaxException JavaDoc 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         // check new property value
562
if (CmsStringUtil.isEmptyOrWhitespaceOnly(getParamNewValue())) {
563             // if no new value was given, set it to the delete value
564
setParamNewValue(CmsProperty.DELETE_VALUE);
565         }
566
567         setErrorMessage(validationErrors.toString());
568         setValidationErrors(!allOk);
569         return allOk;
570     }
571 }
Popular Tags