KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/commons/CmsCopy.java,v $
3  * Date : $Date: 2006/11/16 13:40:28 $
4  * Version: $Revision: 1.21 $
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.CmsResource;
35 import org.opencms.file.CmsResourceFilter;
36 import org.opencms.file.CmsVfsException;
37 import org.opencms.file.CmsVfsResourceAlreadyExistsException;
38 import org.opencms.file.CmsVfsResourceNotFoundException;
39 import org.opencms.jsp.CmsJspActionElement;
40 import org.opencms.main.CmsException;
41 import org.opencms.main.CmsLog;
42 import org.opencms.security.CmsPermissionSet;
43 import org.opencms.site.CmsSiteManager;
44 import org.opencms.staticexport.CmsLinkManager;
45 import org.opencms.util.CmsStringUtil;
46 import org.opencms.workplace.CmsMultiDialog;
47 import org.opencms.workplace.CmsWorkplaceSettings;
48
49 import java.util.ArrayList JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.List JavaDoc;
52
53 import javax.servlet.http.HttpServletRequest JavaDoc;
54 import javax.servlet.http.HttpServletResponse JavaDoc;
55 import javax.servlet.jsp.JspException JavaDoc;
56 import javax.servlet.jsp.PageContext JavaDoc;
57
58 import org.apache.commons.logging.Log;
59
60 /**
61  * Provides methods for the copy resources dialog.<p>
62  *
63  * The following files use this class:
64  * <ul>
65  * <li>/commons/copy.jsp
66  * </ul>
67  * <p>
68  *
69  * @author Andreas Zahner
70  *
71  * @version $Revision: 1.21 $
72  *
73  * @since 6.0.0
74  */

75 public class CmsCopy extends CmsMultiDialog {
76
77     /** Value for the action: copy the resource. */
78     public static final int ACTION_COPY = 100;
79
80     /** The dialog type. */
81     public static final String JavaDoc DIALOG_TYPE = "copy";
82
83     /** Request parameter name for the keep rights flag. */
84     public static final String JavaDoc PARAM_KEEPRIGHTS = "keeprights";
85
86     /** Request parameter name for the overwrite flag. */
87     public static final String JavaDoc PARAM_OVERWRITE = "overwrite";
88
89     /** The log object for this class. */
90     private static final Log LOG = CmsLog.getLog(CmsCopy.class);
91
92     private String JavaDoc m_paramCopymode;
93     private String JavaDoc m_paramKeeprights;
94     private String JavaDoc m_paramOverwrite;
95     private String JavaDoc m_paramTarget;
96
97     /**
98      * Public constructor with JSP action element.<p>
99      *
100      * @param jsp an initialized JSP action element
101      */

102     public CmsCopy(CmsJspActionElement jsp) {
103
104         super(jsp);
105     }
106
107     /**
108      * Public constructor with JSP variables.<p>
109      *
110      * @param context the JSP page context
111      * @param req the JSP request
112      * @param res the JSP response
113      */

114     public CmsCopy(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
115
116         this(new CmsJspActionElement(context, req, res));
117     }
118
119     /**
120      * Performs the copy action, will be called by the JSP page.<p>
121      *
122      * @throws JspException if problems including sub-elements occur
123      */

124     public void actionCopy() throws JspException JavaDoc {
125
126         // save initialized instance of this class in request attribute for included sub-elements
127
getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this);
128         CmsResource resource = null;
129         try {
130             boolean isFolder = false;
131             String JavaDoc source = (String JavaDoc)getResourceList().get(0);
132             String JavaDoc target = CmsLinkManager.getAbsoluteUri(getParamTarget(), CmsResource.getParentFolder(source));
133             if (! isMultiOperation()) {
134                 resource = getCms().readResource(source, CmsResourceFilter.ALL);
135                 isFolder = resource.isFolder();
136             } else {
137                 String JavaDoc siteRootFolder = null;
138                 try {
139                     // check if a site root was added to the target name
140
if (CmsSiteManager.getSiteRoot(target) != null) {
141                         siteRootFolder = getCms().getRequestContext().getSiteRoot();
142                         if (siteRootFolder.endsWith("/")) {
143                             siteRootFolder = siteRootFolder.substring(0, siteRootFolder.length() - 1);
144                         }
145                         getCms().getRequestContext().setSiteRoot("/");
146                     }
147                     resource = getCms().readResource(target, CmsResourceFilter.ALL);
148                 } finally {
149                     if (siteRootFolder != null) {
150                         getCms().getRequestContext().setSiteRoot(siteRootFolder);
151                     }
152                 }
153                 if (! resource.isFolder()) {
154                     // no folder selected for multi operation, throw exception
155
throw new CmsVfsException(Messages.get().container(Messages.ERR_COPY_MULTI_TARGET_NOFOLDER_1, target));
156                 }
157             }
158             if (performDialogOperation()) {
159                 // if no exception is caused and "true" is returned copy operation was successful
160
if (isMultiOperation() || isFolder) {
161                     // set request attribute to reload the explorer tree view
162
List JavaDoc folderList = new ArrayList JavaDoc(1);
163                     String JavaDoc targetParent = CmsResource.getParentFolder(target);
164                     folderList.add(targetParent);
165                     getJsp().getRequest().setAttribute(REQUEST_ATTRIBUTE_RELOADTREE, folderList);
166                 }
167                 actionCloseDialog();
168             } else {
169                 // "false" returned, display "please wait" screen
170
getJsp().include(FILE_DIALOG_SCREEN_WAIT);
171             }
172         } catch (Throwable JavaDoc e) {
173             // check if this exception requires a confirmation or error screen for single resource operations
174
if (! isMultiOperation() && (e instanceof CmsVfsResourceAlreadyExistsException) && !(resource.isFolder())) {
175                 // file copy but file already exists, now check target file type
176
int targetType = -1;
177                 boolean restoreSiteRoot = false;
178                 try {
179                     if (CmsSiteManager.getSiteRoot(getParamTarget()) != null) {
180                         getCms().getRequestContext().saveSiteRoot();
181                         getCms().getRequestContext().setSiteRoot("/");
182                         restoreSiteRoot = true;
183                     }
184                     CmsResource targetRes = getCms().readResource(getParamTarget());
185                     targetType = targetRes.getTypeId();
186                 } catch (CmsException e2) {
187                     // can usually be ignored
188
if (LOG.isInfoEnabled()) {
189                         LOG.info(e2.getLocalizedMessage());
190                     }
191                 } finally {
192                     if (restoreSiteRoot) {
193                         getCms().getRequestContext().restoreSiteRoot();
194                     }
195                 }
196                 if (resource.getTypeId() == targetType) {
197                     // file type of target is the same as source, show confirmation dialog
198
setParamMessage(CmsStringUtil.escapeHtml(key(Messages.GUI_COPY_CONFIRM_OVERWRITE_2, new Object JavaDoc[] {
199                         getParamResource(),
200                         getParamTarget()})));
201                     getJsp().include(FILE_DIALOG_SCREEN_CONFIRM);
202                 } else {
203                     // file type is different, create error message
204
includeErrorpage(this, e);
205                 }
206             } else {
207                 // error during copy, show error dialog
208
includeErrorpage(this, e);
209             }
210         }
211     }
212
213     /**
214      * Builds the input radio buttons to select between preserving links or creating new resources when copying.<p>
215      *
216      * @return the HTML code for the radio buttons
217      */

218     public String JavaDoc buildRadioCopyMode() {
219
220         StringBuffer JavaDoc retValue = new StringBuffer JavaDoc(256);
221
222         // check if the current resource is a folder for single operation
223
boolean isFolder = isOperationOnFolder();
224         String JavaDoc checkedAttr = " checked=\"checked\"";
225
226         if (isMultiOperation() || isFolder) {
227             // for multi resource operations or folders, show an additional option "preserve links"
228
int defaultMode = getSettings().getUserSettings().getDialogCopyFolderMode();
229             retValue.append("<input type=\"radio\" name=\"copymode\" value=\"");
230             retValue.append(CmsResource.COPY_AS_SIBLING);
231             retValue.append("\"");
232             if (defaultMode == CmsResource.COPY_AS_SIBLING) {
233                 retValue.append(checkedAttr);
234             }
235             retValue.append("> ");
236             String JavaDoc msgKey;
237             if (isMultiOperation()) {
238                 msgKey = Messages.GUI_COPY_MULTI_CREATE_SIBLINGS_0;
239             } else {
240                 msgKey = Messages.GUI_COPY_CREATE_SIBLINGS_0;
241             }
242             retValue.append(key(msgKey));
243             retValue.append("<br>\n");
244             retValue.append("<input type=\"radio\" name=\"copymode\" value=\"");
245             retValue.append(CmsResource.COPY_PRESERVE_SIBLING);
246             retValue.append("\"");
247             if (defaultMode == CmsResource.COPY_PRESERVE_SIBLING) {
248                 retValue.append(checkedAttr);
249             }
250             retValue.append("> ");
251             retValue.append(key(Messages.GUI_COPY_ALL_NO_SIBLINGS_0));
252             retValue.append("<br>\n");
253             retValue.append("<input type=\"radio\" name=\"copymode\" value=\"");
254             retValue.append(CmsResource.COPY_AS_NEW);
255             retValue.append("\"");
256             if (defaultMode == CmsResource.COPY_AS_NEW) {
257                 retValue.append(checkedAttr);
258             }
259             retValue.append("> ");
260             retValue.append(key(Messages.GUI_COPY_ALL_0));
261             retValue.append("<br>\n");
262
263             if (isMultiOperation()) {
264                 // show overwrite option for multi resource copy
265
retValue.append(dialogSpacer());
266                 retValue.append("<input type=\"checkbox\" name=\"");
267                 retValue.append(PARAM_OVERWRITE);
268                 retValue.append("\" value=\"true\"> ");
269                 retValue.append(key(Messages.GUI_COPY_MULTI_OVERWRITE_0));
270                 retValue.append("<br>\n");
271             }
272         } else {
273             // for files, show copy option "copy as sibling" and "copy as new resource"
274
int defaultMode = getSettings().getUserSettings().getDialogCopyFileMode();
275             retValue.append("<input type=\"radio\" name=\"copymode\" value=\"");
276             retValue.append(CmsResource.COPY_AS_SIBLING);
277             retValue.append("\"");
278             if (defaultMode == CmsResource.COPY_AS_SIBLING) {
279                 retValue.append(checkedAttr);
280             }
281             retValue.append("> ");
282             retValue.append(key(Messages.GUI_CREATE_SIBLING_0));
283             retValue.append("<br>\n");
284             retValue.append("<input type=\"radio\" name=\"copymode\" value=\"");
285             retValue.append(CmsResource.COPY_AS_NEW);
286             retValue.append("\"");
287             if (defaultMode == CmsResource.COPY_AS_NEW) {
288                 retValue.append(checkedAttr);
289             }
290             retValue.append("> ");
291             retValue.append(key(Messages.GUI_COPY_AS_NEW_0));
292             retValue.append("<br>\n");
293         }
294
295         return retValue.toString();
296     }
297
298     /**
299      * Returns the value of the copymode parameter.<p>
300      *
301      * @return the value of the copymode parameter
302      */

303     public String JavaDoc getParamCopymode() {
304
305         return m_paramCopymode;
306     }
307
308     /**
309      * Returns the value of the keeprights parameter.<p>
310      *
311      * @return the value of the keeprights parameter
312      */

313     public String JavaDoc getParamKeeprights() {
314
315         return m_paramKeeprights;
316     }
317
318     /**
319      * Returns the value of the overwrite parameter.<p>
320      *
321      * @return the value of the overwrite parameter
322      */

323     public String JavaDoc getParamOverwrite() {
324
325         return m_paramOverwrite;
326     }
327
328     /**
329      * Returns the value of the target parameter,
330      * or null if this parameter was not provided.<p>
331      *
332      * The target parameter selects the target name
333      * of the operation.<p>
334      *
335      * @return the value of the target parameter
336      */

337     public String JavaDoc getParamTarget() {
338
339         return m_paramTarget;
340     }
341
342     /**
343      * Sets the value of the copymode parameter.<p>
344      *
345      * @param value the value of the copymode parameter
346      */

347     public void setParamCopymode(String JavaDoc value) {
348
349         m_paramCopymode = value;
350     }
351
352     /**
353      * Sets the value of the "keeprights" parameter.<p>
354      *
355      * @param value the value of the "keeprights" parameter
356      */

357     public void setParamKeeprights(String JavaDoc value) {
358
359         m_paramKeeprights = value;
360     }
361
362     /**
363      * Sets the value of the overwrite parameter.<p>
364      *
365      * @param paramOverwrite the value of the overwrite parameter
366      */

367     public void setParamOverwrite(String JavaDoc paramOverwrite) {
368
369         m_paramOverwrite = paramOverwrite;
370     }
371
372     /**
373      * Sets the value of the target parameter.<p>
374      *
375      * @param value the value to set
376      */

377     public void setParamTarget(String JavaDoc value) {
378
379         m_paramTarget = value;
380     }
381
382     /**
383      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
384      */

385     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
386
387         // fill the parameter values in the get/set methods
388
fillParamValues(request);
389
390         // check the required permissions to copy the resource
391
if (! checkResourcePermissions(CmsPermissionSet.ACCESS_WRITE, false)) {
392             // no write permissions for the resource, set cancel action to close dialog
393
setParamAction(DIALOG_CANCEL);
394         }
395
396         // set the dialog type
397
setParamDialogtype(DIALOG_TYPE);
398         // set the action for the JSP switch
399
if (DIALOG_TYPE.equals(getParamAction())) {
400             setAction(ACTION_COPY);
401         } else if (DIALOG_CONFIRMED.equals(getParamAction())) {
402             setAction(ACTION_CONFIRMED);
403         } else if (DIALOG_WAIT.equals(getParamAction())) {
404             setAction(ACTION_WAIT);
405         } else if (DIALOG_CANCEL.equals(getParamAction())) {
406             setAction(ACTION_CANCEL);
407         } else {
408             setAction(ACTION_DEFAULT);
409             // build title for copy dialog
410
setDialogTitle(Messages.GUI_COPY_RESOURCE_1, Messages.GUI_COPY_MULTI_2);
411         }
412     }
413
414     /**
415      * Performs the resource copying.<p>
416      *
417      * @return true, if the resource was copied, otherwise false
418      * @throws CmsException if copying is not successful
419      */

420     protected boolean performDialogOperation() throws CmsException {
421
422         // check if the current resource is a folder for single operation
423
boolean isFolder = isOperationOnFolder();
424
425         // on folder copy display "please wait" screen, not for simple file copy
426
if ((isMultiOperation() || isFolder) && !DIALOG_WAIT.equals(getParamAction())) {
427             // return false, this will trigger the "please wait" screen
428
return false;
429         }
430
431         // get the copy mode from request parameter value
432
int copyMode = CmsResource.COPY_PRESERVE_SIBLING;
433         try {
434             copyMode = Integer.parseInt(getParamCopymode());
435         } catch (Exception JavaDoc e) {
436             // can usually be ignored
437
if (LOG.isInfoEnabled()) {
438                 LOG.info(e.getLocalizedMessage());
439             }
440         }
441
442         // check the overwrite options
443
boolean overwrite = Boolean.valueOf(getParamOverwrite()).booleanValue();
444         overwrite = ((isMultiOperation() && overwrite) || DIALOG_CONFIRMED.equals(getParamAction()));
445
446         // calculate the target name
447
String JavaDoc target = getParamTarget();
448         if (target == null) {
449             target = "";
450         }
451
452         boolean restoreSiteRoot = false;
453         try {
454             // check if a site root was added to the target name
455
String JavaDoc sitePrefix = "";
456             if (CmsSiteManager.getSiteRoot(target) != null) {
457                 String JavaDoc siteRootFolder = getCms().getRequestContext().getSiteRoot();
458                 if (siteRootFolder.endsWith("/")) {
459                     siteRootFolder = siteRootFolder.substring(0, siteRootFolder.length() - 1);
460                 }
461                 sitePrefix = siteRootFolder;
462                 getCms().getRequestContext().saveSiteRoot();
463                 getCms().getRequestContext().setSiteRoot("/");
464                 restoreSiteRoot = true;
465             }
466
467             Iterator JavaDoc i = getResourceList().iterator();
468             // iterate the resources to copy
469
while (i.hasNext()) {
470                 String JavaDoc resName = (String JavaDoc)i.next();
471                 try {
472                     performSingleCopyOperation(resName, target, sitePrefix, copyMode, overwrite);
473                 } catch (CmsException e) {
474                     if (isMultiOperation()) {
475                         // collect exceptions to create a detailed output
476
addMultiOperationException(e);
477                     } else {
478                         // for single operation, throw the exception immediately
479
throw e;
480                     }
481                 }
482             }
483             // check if exceptions occured
484
checkMultiOperationException(Messages.get(), Messages.ERR_COPY_MULTI_0);
485         } finally {
486             // restore the site root
487
if (restoreSiteRoot) {
488                 getCms().getRequestContext().restoreSiteRoot();
489             }
490         }
491         return true;
492     }
493
494     /**
495      * Performs the copy operation for a single VFS resource.<p>
496      *
497      * @param source the source VFS path
498      * @param target the target VFS path
499      * @param sitePrefix the site prefix
500      * @param copyMode the copy mode for siblings
501      * @param overwrite the overwrite flag
502      * @throws CmsException if copying the resource fails
503      */

504     protected void performSingleCopyOperation(
505         String JavaDoc source,
506         String JavaDoc target,
507         String JavaDoc sitePrefix,
508         int copyMode,
509         boolean overwrite) throws CmsException {
510
511         // calculate the target name
512
target = CmsLinkManager.getAbsoluteUri(target, CmsResource.getParentFolder(source));
513
514         if (target.equals(source) || (isMultiOperation() && target.startsWith(source))) {
515             throw new CmsVfsException(Messages.get().container(Messages.ERR_COPY_ONTO_ITSELF_1, target));
516         }
517
518         try {
519             CmsResource res = getCms().readResource(target, CmsResourceFilter.ALL);
520             if (res.isFolder()) {
521                 // target folder already exists, so we add the current folder name
522
if (! target.endsWith("/")) {
523                     target += "/";
524                 }
525                 target = target + CmsResource.getName(source);
526             }
527         } catch (CmsVfsResourceNotFoundException e) {
528             // target folder does not already exist, so target name is o.k.
529
if (LOG.isInfoEnabled()) {
530                 LOG.info(e.getLocalizedMessage());
531             }
532         }
533
534         // set the target parameter value
535
setParamTarget(target);
536
537         // delete existing target resource if selected or confirmed by the user
538
if (overwrite && getCms().existsResource(target)) {
539             checkLock(target);
540             getCms().deleteResource(target, CmsResource.DELETE_PRESERVE_SIBLINGS);
541         }
542
543         // copy the resource
544
getCms().copyResource(sitePrefix + source, target, copyMode);
545     }
546 }
547
Popular Tags