KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > explorer > CmsNewResourceSibling


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/explorer/CmsNewResourceSibling.java,v $
3  * Date : $Date: 2006/03/27 14:52:30 $
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.explorer;
33
34 import org.opencms.file.CmsResource;
35 import org.opencms.jsp.CmsJspActionElement;
36 import org.opencms.lock.CmsLock;
37 import org.opencms.main.CmsException;
38 import org.opencms.main.CmsLog;
39 import org.opencms.site.CmsSiteManager;
40 import org.opencms.workplace.CmsWorkplaceSettings;
41 import org.opencms.workplace.commons.CmsPropertyAdvanced;
42
43 import java.io.IOException JavaDoc;
44 import java.util.HashMap JavaDoc;
45 import java.util.List JavaDoc;
46 import java.util.Map JavaDoc;
47
48 import javax.servlet.ServletException JavaDoc;
49 import javax.servlet.http.HttpServletRequest JavaDoc;
50 import javax.servlet.http.HttpServletResponse JavaDoc;
51 import javax.servlet.jsp.JspException JavaDoc;
52 import javax.servlet.jsp.PageContext JavaDoc;
53
54 import org.apache.commons.logging.Log;
55
56 /**
57  * The new resource sibling dialog handles the creation of a new sibling in the VFS.<p>
58  *
59  * The following files use this class:
60  * <ul>
61  * <li>/commons/newresource_sibling.html
62  * </ul>
63  * <p>
64  *
65  * @author Andreas Zahner
66  *
67  * @version $Revision: 1.15 $
68  *
69  * @since 6.0.0
70  */

71 public class CmsNewResourceSibling extends CmsNewResourcePointer {
72
73     /** Request parameter name for the keep properties flag. */
74     public static final String JavaDoc PARAM_KEEPPROPERTIES = "keepproperties";
75
76     /** The log object for this class. */
77     private static final Log LOG = CmsLog.getLog(CmsNewResourceSibling.class);
78
79     private String JavaDoc m_paramKeepProperties;
80
81     /**
82      * Public constructor with JSP action element.<p>
83      *
84      * @param jsp an initialized JSP action element
85      */

86     public CmsNewResourceSibling(CmsJspActionElement jsp) {
87
88         super(jsp);
89     }
90
91     /**
92      * Public constructor with JSP variables.<p>
93      *
94      * @param context the JSP page context
95      * @param req the JSP request
96      * @param res the JSP response
97      */

98     public CmsNewResourceSibling(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
99
100         this(new CmsJspActionElement(context, req, res));
101     }
102
103     /**
104      * Creates the new sibling of a resource.<p>
105      *
106      * @throws JspException if inclusion of error dialog fails
107      */

108     public void actionCreateResource() throws JspException JavaDoc {
109
110         try {
111             // create the full resource name
112
String JavaDoc fullResourceName = computeFullResourceName();
113             String JavaDoc newResourceParam = fullResourceName;
114             // the target
115
String JavaDoc targetName = getParamLinkTarget();
116             if (targetName == null) {
117                 targetName = "";
118             }
119
120             // create the sibling
121
boolean restoreSiteRoot = false;
122             try {
123                 if (CmsSiteManager.getSiteRoot(targetName) != null) {
124                     // add site root to new resource path
125
String JavaDoc siteRootFolder = getCms().getRequestContext().getSiteRoot();
126                     if (siteRootFolder.endsWith("/")) {
127                         siteRootFolder = siteRootFolder.substring(0, siteRootFolder.length() - 1);
128                     }
129                     fullResourceName = siteRootFolder + fullResourceName;
130                     getCms().getRequestContext().saveSiteRoot();
131                     getCms().getRequestContext().setSiteRoot("/");
132                     restoreSiteRoot = true;
133                 }
134
135                 // check if the link target is a file or a folder
136
boolean isFolder = false;
137                 CmsResource targetRes = getCms().readResource(targetName);
138                 isFolder = targetRes.isFolder();
139
140                 if (isFolder) {
141                     // link URL is a folder, so copy the folder with all sub resources as siblings
142
if (targetName.endsWith("/")) {
143                         targetName = targetName.substring(0, targetName.length() - 1);
144                     }
145                     // copy the folder
146
getCms().copyResource(targetName, fullResourceName, CmsResource.COPY_AS_SIBLING);
147                 } else {
148                     // link URL is a file, so create sibling of the link target
149
List JavaDoc targetProperties = null;
150                     boolean keepProperties = Boolean.valueOf(getParamKeepProperties()).booleanValue();
151                     if (keepProperties) {
152                         // keep the individual properties of the original file
153
try {
154                             targetProperties = getCms().readPropertyObjects(targetName, false);
155                         } catch (Exception JavaDoc e) {
156                             LOG.error(e);
157                         }
158                     }
159                     getCms().createSibling(targetName, fullResourceName, targetProperties);
160                 }
161
162             } finally {
163                 // restore the site root
164
if (restoreSiteRoot) {
165                     getCms().getRequestContext().restoreSiteRoot();
166                 }
167             }
168
169             // set resource parameter to new resource name for property dialog
170
setParamResource(newResourceParam);
171             setResourceCreated(true);
172         } catch (Throwable JavaDoc e) {
173             // error creating pointer, show error dialog
174
setParamMessage(Messages.get().getBundle(getLocale()).key(Messages.ERR_CREATE_LINK_0));
175             includeErrorpage(this, e);
176         }
177
178     }
179
180     /**
181      * Forwards to the property dialog if the resourceeditprops parameter is true.<p>
182      *
183      * If the parameter is not true, the dialog will be closed.<p>
184      * If the sibling of the new resource is locked, the paramter will be ignored as properties
185      * cannot be created in this case.<p>
186      *
187      * @throws IOException if forwarding to the property dialog fails
188      * @throws ServletException if forwarding to the property dialog fails
189      * @throws JspException if an inclusion fails
190      */

191     public void actionEditProperties() throws IOException JavaDoc, JspException JavaDoc, ServletException JavaDoc {
192
193         boolean editProps = Boolean.valueOf(getParamNewResourceEditProps()).booleanValue();
194         // get the sibling name
195
String JavaDoc newRes = getParamResource();
196         // check if this sibling is locked
197
try {
198             CmsLock lock = getCms().getLock(newRes);
199             // if the new resource has no exclusive lock, set the editProps flag to false
200
if (lock.getType() != CmsLock.TYPE_EXCLUSIVE) {
201                 editProps = false;
202             }
203         } catch (CmsException e) {
204             throw new JspException JavaDoc(e);
205         }
206         if (editProps) {
207             // edit properties checkbox checked, forward to property dialog
208
Map JavaDoc params = new HashMap JavaDoc();
209             params.put(PARAM_RESOURCE, getParamResource());
210             params.put(CmsPropertyAdvanced.PARAM_DIALOGMODE, CmsPropertyAdvanced.MODE_WIZARD);
211             sendForward(CmsPropertyAdvanced.URI_PROPERTY_DIALOG_HANDLER, params);
212         } else {
213             // edit properties not checked, close the dialog
214
actionCloseDialog();
215         }
216     }
217
218     /**
219      * Returns the current explorer path for use in Javascript of new sibling dialog.<p>
220      *
221      * @return the current explorer path
222      */

223     public String JavaDoc getCurrentPath() {
224
225         String JavaDoc path = getSettings().getExplorerResource();
226         if (path == null) {
227             path = "/";
228         }
229         return CmsResource.getFolderPath(path);
230     }
231
232     /**
233      * Returns the keep properties request parameter value.<p>
234      *
235      * @return the keep properties request parameter value
236      */

237     public String JavaDoc getParamKeepProperties() {
238
239         return m_paramKeepProperties;
240     }
241
242     /**
243      * Sets the keep properties request parameter value.<p>
244      *
245      * @param keepProperties the keep properties request parameter value
246      */

247     public void setParamKeepProperties(String JavaDoc keepProperties) {
248
249         m_paramKeepProperties = keepProperties;
250     }
251
252     /**
253      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
254      */

255     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
256
257         // fill the parameter values in the get/set methods
258
fillParamValues(request);
259         // set the dialog type
260
setParamDialogtype(DIALOG_TYPE);
261         // set the action for the JSP switch
262
if (DIALOG_OK.equals(getParamAction())) {
263             setAction(ACTION_OK);
264         } else if (DIALOG_CANCEL.equals(getParamAction())) {
265             setAction(ACTION_CANCEL);
266         } else {
267             setAction(ACTION_DEFAULT);
268             // build title for new resource dialog
269
setParamTitle(key(Messages.GUI_NEWRESOURCE_SIBLING_0));
270         }
271     }
272
273 }
Popular Tags