KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/commons/CmsMove.java,v $
3  * Date : $Date: 2006/03/27 14:52:18 $
4  * Version: $Revision: 1.20 $
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 move resources dialog.<p>
62  *
63  * The following files use this class:
64  * <ul>
65  * <li>/commons/move.jsp
66  * </ul>
67  * <p>
68  *
69  * @author Andreas Zahner
70  *
71  * @version $Revision: 1.20 $
72  *
73  * @since 6.0.0
74  */

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

97     public CmsMove(CmsJspActionElement jsp) {
98
99         super(jsp);
100     }
101
102     /**
103      * Public constructor with JSP variables.<p>
104      *
105      * @param context the JSP page context
106      * @param req the JSP request
107      * @param res the JSP response
108      */

109     public CmsMove(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
110
111         this(new CmsJspActionElement(context, req, res));
112     }
113
114     /**
115      * Performs the move action, will be called by the JSP page.<p>
116      *
117      * @throws JspException if problems including sub-elements occur
118      */

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

202     public String JavaDoc buildMoveOptions() {
203
204         StringBuffer JavaDoc retValue = new StringBuffer JavaDoc(256);
205         if (isMultiOperation()) {
206             // show overwrite option for multi resource moving
207
retValue.append("<input type=\"checkbox\" name=\"");
208             retValue.append(PARAM_OVERWRITE);
209             retValue.append("\" value=\"true\"> ");
210             retValue.append(key(Messages.GUI_COPY_MULTI_OVERWRITE_0));
211             retValue.append("<br>\n");
212             retValue.append(dialogSpacer());
213         }
214         return retValue.toString();
215     }
216
217     /**
218      * Returns the current name of the resource without path information.<p>
219      *
220      * This is used to preset the input text field with the current resource name for single resource operations.<p>
221      *
222      * @return the current name of the resource without path information
223      */

224     public String JavaDoc getCurrentResourceName() {
225
226         if (isMultiOperation()) {
227             return "";
228         }
229         String JavaDoc resourceName = CmsResource.getName(getParamResource());
230         if (resourceName.endsWith("/")) {
231             resourceName = resourceName.substring(0, resourceName.length() - 1);
232         }
233         return resourceName;
234     }
235     
236     /**
237      * Returns the value of the overwrite parameter.<p>
238      *
239      * @return the value of the overwrite parameter
240      */

241     public String JavaDoc getParamOverwrite() {
242
243         return m_paramOverwrite;
244     }
245
246     /**
247      * Returns the value of the target parameter,
248      * or null if this parameter was not provided.<p>
249      *
250      * The target parameter selects the target name
251      * of the operation.<p>
252      *
253      * @return the value of the target parameter
254      */

255     public String JavaDoc getParamTarget() {
256
257         return m_paramTarget;
258     }
259     
260     /**
261      * Sets the value of the overwrite parameter.<p>
262      *
263      * @param paramOverwrite the value of the overwrite parameter
264      */

265     public void setParamOverwrite(String JavaDoc paramOverwrite) {
266
267         m_paramOverwrite = paramOverwrite;
268     }
269
270     /**
271      * Sets the value of the target parameter.<p>
272      *
273      * @param value the value to set
274      */

275     public void setParamTarget(String JavaDoc value) {
276
277         m_paramTarget = value;
278     }
279
280     /**
281      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
282      */

283     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
284
285         // fill the parameter values in the get/set methods
286
fillParamValues(request);
287         
288         // check the required permissions to rename/move the resource
289
if (! checkResourcePermissions(CmsPermissionSet.ACCESS_WRITE, false)) {
290             // no write permissions for the resource, set cancel action to close dialog
291
setParamAction(DIALOG_CANCEL);
292         }
293         
294         // set the dialog type
295
setParamDialogtype(DIALOG_TYPE);
296         // set the action for the JSP switch
297
if (DIALOG_TYPE.equals(getParamAction())) {
298             setAction(ACTION_MOVE);
299         } else if (DIALOG_CONFIRMED.equals(getParamAction())) {
300             setAction(ACTION_CONFIRMED);
301         } else if (DIALOG_WAIT.equals(getParamAction())) {
302             setAction(ACTION_WAIT);
303         } else if (DIALOG_CANCEL.equals(getParamAction())) {
304             setAction(ACTION_CANCEL);
305         } else {
306             setAction(ACTION_DEFAULT);
307             // build title for move dialog
308
setDialogTitle(Messages.GUI_MOVE_RESOURCE_1, Messages.GUI_MOVE_MULTI_2);
309         }
310     }
311
312     /**
313      * Performs the resource moving.<p>
314      *
315      * @return true, if the resource was successfully moved, otherwise false
316      * @throws CmsException if moving is not successful
317      */

318     protected boolean performDialogOperation() throws CmsException {
319
320         // check if the current resource is a folder for single operation
321
boolean isFolder = isOperationOnFolder();
322         // on folder move operation display "please wait" screen, not for simple file move operation
323
if ((isMultiOperation() || isFolder) && !DIALOG_WAIT.equals(getParamAction())) {
324             // return false, this will trigger the "please wait" screen
325
return false;
326         }
327         
328         // check the overwrite options
329
boolean overwrite = Boolean.valueOf(getParamOverwrite()).booleanValue();
330         overwrite = ((isMultiOperation() && overwrite) || DIALOG_CONFIRMED.equals(getParamAction()));
331
332         // get the target name
333
String JavaDoc target = getParamTarget();
334         if (target == null) {
335             target = "";
336         }
337
338         boolean restoreSiteRoot = false;
339         try {
340             // check if a site root was added to the target name
341
String JavaDoc sitePrefix = "";
342             if (CmsSiteManager.getSiteRoot(target) != null) {
343                 String JavaDoc siteRootFolder = getCms().getRequestContext().getSiteRoot();
344                 if (siteRootFolder.endsWith("/")) {
345                     siteRootFolder = siteRootFolder.substring(0, siteRootFolder.length() - 1);
346                 }
347                 sitePrefix = siteRootFolder;
348                 getCms().getRequestContext().saveSiteRoot();
349                 getCms().getRequestContext().setSiteRoot("/");
350                 restoreSiteRoot = true;
351             }
352
353             Iterator JavaDoc i = getResourceList().iterator();
354             // iterate the resources to move
355
while (i.hasNext()) {
356                 String JavaDoc resName = (String JavaDoc)i.next();
357                 try {
358                     performSingleMoveOperation(resName, target, sitePrefix, overwrite);
359                 } catch (CmsException e) {
360                     if (isMultiOperation()) {
361                         // collect exceptions to create a detailed output
362
addMultiOperationException(e);
363                     } else {
364                         // for single operation, throw the exception immediately
365
throw e;
366                     }
367                 }
368             }
369             // check if exceptions occured
370
checkMultiOperationException(Messages.get(), Messages.ERR_MOVE_MULTI_0);
371         } finally {
372             if (restoreSiteRoot) {
373                 getCms().getRequestContext().restoreSiteRoot();
374             }
375         }
376         return true;
377     }
378     
379     /**
380      * Performs the move operation for a single VFS resource.<p>
381      *
382      * @param source the source VFS path
383      * @param target the target VFS path
384      * @param sitePrefix the site prefix
385      * @param overwrite the overwrite flag
386      * @throws CmsException if moving the resource fails
387      */

388     protected void performSingleMoveOperation(String JavaDoc source, String JavaDoc target, String JavaDoc sitePrefix, boolean overwrite)
389     throws CmsException {
390
391         // calculate the target name
392
target = CmsLinkManager.getAbsoluteUri(target, CmsResource.getParentFolder(source));
393
394         if (target.equals(source) || (isMultiOperation() && target.startsWith(source))) {
395             throw new CmsVfsException(Messages.get().container(Messages.ERR_MOVE_ONTO_ITSELF_1, target));
396         }
397
398         try {
399             CmsResource res = getCms().readResource(target, CmsResourceFilter.ALL);
400             if (res.isFolder()) {
401                 // target folder already exists, so we add the current folder name
402
if (! target.endsWith("/")) {
403                     target += "/";
404                 }
405                 target = target + CmsResource.getName(source);
406                 if (target.endsWith("/")) {
407                     target = target.substring(0, target.length() - 1);
408                 }
409             }
410         } catch (CmsVfsResourceNotFoundException e) {
411             // target folder does not already exist, so target name is o.k.
412
if (LOG.isInfoEnabled()) {
413                 LOG.info(e.getLocalizedMessage());
414             }
415         }
416
417         // set the target parameter value
418
setParamTarget(target);
419         
420         // set the target parameter value
421
setParamTarget(target);
422
423         // delete existing target resource if selected or confirmed by the user
424
if (getCms().existsResource(target, CmsResourceFilter.ALL)) {
425             if (overwrite) {
426                 checkLock(target);
427                 getCms().deleteResource(target, CmsResource.DELETE_PRESERVE_SIBLINGS);
428             } else {
429                 // throw exception to indicate that the target exists
430
throw new CmsVfsResourceAlreadyExistsException(Messages.get().container(
431                     Messages.ERR_MOVE_FAILED_TARGET_EXISTS_2,
432                     source,
433                     getJsp().getRequestContext().removeSiteRoot(target)));
434             }
435         }
436
437         // lock resource if autolock is enabled
438
checkLock(sitePrefix + source);
439         // move the resource
440
getCms().moveResource(sitePrefix + source, target);
441     }
442 }
443
Popular Tags