KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/content/CmsPropertyDelete.java,v $
3  * Date : $Date: 2006/03/31 13:59:16 $
4  * Version: $Revision: 1.16 $
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.CmsProperty;
35 import org.opencms.file.CmsPropertyDefinition;
36 import org.opencms.file.CmsResource;
37 import org.opencms.file.CmsVfsException;
38 import org.opencms.i18n.CmsMessages;
39 import org.opencms.jsp.CmsJspActionElement;
40 import org.opencms.lock.CmsLock;
41 import org.opencms.main.CmsException;
42 import org.opencms.main.OpenCms;
43 import org.opencms.workplace.CmsDialog;
44 import org.opencms.workplace.CmsWorkplaceSettings;
45
46 import java.util.ArrayList JavaDoc;
47 import java.util.Collections 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 delete property definition dialog.<p>
58  *
59  * @author Andreas Zahner
60  * @author Armen Markarian
61  *
62  * @version $Revision: 1.16 $
63  *
64  * @since 6.0.0
65  */

66 public class CmsPropertyDelete extends CmsDialog {
67
68     /** Value for the action: delete cascade. */
69     public static final int ACTION_DELETE_CASCADE = 100;
70
71     /** Request parameter value for the action: delete cascade. */
72     public static final String JavaDoc DIALOG_DELETE_CASCADE = "deletecascade";
73
74     /** The dialog type. */
75     public static final String JavaDoc DIALOG_TYPE = "propertydelete";
76
77     /** Request parameter name for the property name. */
78     public static final String JavaDoc PARAM_PROPERTYNAME = "propertyname";
79
80     private String JavaDoc m_paramPropertyName;
81
82     /**
83      * Public constructor with JSP action element.<p>
84      *
85      * @param jsp an initialized JSP action element
86      */

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

99     public CmsPropertyDelete(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
100
101         this(new CmsJspActionElement(context, req, res));
102     }
103
104     /**
105      * Deletes the property definition.<p>
106      *
107      * @throws JspException if problems including sub-elements occur
108      */

109     public void actionDelete() throws JspException JavaDoc {
110
111         // save initialized instance of this class in request attribute for included sub-elements
112
getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this);
113         try {
114             getCms().deletePropertyDefinition(getParamPropertyName());
115             // close the dialog
116
actionCloseDialog();
117         } catch (Throwable JavaDoc e) {
118             // error while deleting property definition, show error dialog
119
includeErrorpage(this, e);
120         }
121     }
122
123     /**
124      * Deletes the property definition by cascading the properties on resources.<p>
125      *
126      * @throws JspException if problems including sub-elements occur
127      */

128     public void actionDeleteCascade() throws JspException JavaDoc {
129         
130
131         // save initialized instance of this class in request attribute for included sub-elements
132
getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this);
133         try {
134             // list of all resources containing this propertydefinition
135
List JavaDoc resourcesWithProperty = getCms().readResourcesWithProperty(getParamPropertyName());
136             // list of all resources locked by another user, containing this propertydefinition
137
List JavaDoc resourcesLockedByOtherUser = getResourcesLockedByOtherUser(resourcesWithProperty);
138             // do the following operations only if all of the resources are not locked by another user
139
if (resourcesLockedByOtherUser.isEmpty()) {
140                 // save the site root
141
getCms().getRequestContext().saveSiteRoot();
142                 // change to the root site
143
getCms().getRequestContext().setSiteRoot("/");
144                 try {
145                     Iterator JavaDoc i = resourcesWithProperty.iterator();
146                     while (i.hasNext()) {
147                         CmsResource resource = (CmsResource)i.next();
148                         // read the property object
149
CmsProperty property = getCms().readPropertyObject(
150                             resource.getRootPath(),
151                             getParamPropertyName(),
152                             false);
153                         // try to delete the property if it is not the NULL PROPERTY
154
// if the property is the NULL PROPERTY, it only had a shared
155
// value which was deleted at a sibling which was already processed
156
if (!property.isNullProperty()) {
157                             CmsLock lock = getCms().getLock(resource);
158                             if (lock.getType() == CmsLock.TYPE_UNLOCKED) {
159                                 // lock the resource for the current (Admin) user
160
getCms().lockResource(resource.getRootPath());
161                             }
162                             property.setStructureValue(CmsProperty.DELETE_VALUE);
163                             property.setResourceValue(CmsProperty.DELETE_VALUE);
164                             // write the property with the null value to the resource and cascade it from the definition
165
getCms().writePropertyObject(resource.getRootPath(), property);
166                             // unlock the resource
167
getCms().unlockResource(resource.getRootPath());
168                         }
169                     }
170                     // delete the property definition at last
171
getCms().deletePropertyDefinition(getParamPropertyName());
172                 } finally {
173                     // restore the siteroot
174
getCms().getRequestContext().restoreSiteRoot();
175                     // close the dialog
176
actionCloseDialog();
177                 }
178             } else {
179
180                 StringBuffer JavaDoc reason = new StringBuffer JavaDoc();
181                 reason.append(dialogWhiteBoxStart());
182                 reason.append(buildResourceList(resourcesLockedByOtherUser, true));
183                 reason.append(dialogWhiteBoxEnd());
184                 throw new CmsVfsException(Messages.get().container(
185                     Messages.ERR_DEL_PROP_RESOURCES_LOCKED_1,
186                     reason.toString()));
187             }
188         } catch (Throwable JavaDoc e) {
189             // error while deleting property definition, show error dialog
190
includeErrorpage(this, e);
191         }
192     }
193
194     /**
195      * Builds a HTML list of Resources that use the specified property.<p>
196      *
197      * @throws CmsException if operation was not successful
198      *
199      * @return the HTML String for the Resource list
200      */

201     public String JavaDoc buildResourceList() throws CmsException {
202
203         List JavaDoc resourcesWithProperty = getCms().readResourcesWithProperty(getParamPropertyName());
204
205         return buildResourceList(resourcesWithProperty, false);
206     }
207
208     /**
209      * Builds a HTML list of Resources.<p>
210      *
211      * Columns: Type, Name, Uri, Value of the property, locked by(optional).<p>
212      *
213      * @param resourceList a list of resources
214      * @param lockInfo a boolean to decide if the locked info should be shown or not
215      * @throws CmsException if operation was not successful
216      *
217      * @return the HTML String for the Resource list
218      */

219     public String JavaDoc buildResourceList(List JavaDoc resourceList, boolean lockInfo) throws CmsException {
220
221         // reverse the resource list
222
Collections.reverse(resourceList);
223         CmsMessages messages = Messages.get().getBundle(getLocale());
224         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
225         result.append("<table border=\"0\" width=\"100%\" cellpadding=\"1\" cellspacing=\"1\">\n");
226         result.append("<tr>\n");
227         // Type
228
result.append("\t<td style=\"width:5%;\" class=\"textbold\">");
229         result.append(messages.key(Messages.GUI_INPUT_TYPE_0));
230         result.append("</td>\n");
231         // Uri
232
result.append("\t<td style=\"width:40%;\" class=\"textbold\">");
233         result.append(messages.key(Messages.GUI_INPUT_ADRESS_0));
234         result.append("</td>\n");
235         // Name
236
result.append("\t<td style=\"width:25%;\" class=\"textbold\">");
237         result.append(messages.key(Messages.GUI_INPUT_TITLE_0));
238         result.append("</td>\n");
239         if (!lockInfo) {
240             // Property value
241
result.append("\t<td style=\"width:30%;\" class=\"textbold\">");
242             result.append(messages.key(Messages.GUI_INPUT_PROPERTYVALUE_0));
243             result.append("</td>\n");
244         }
245         if (lockInfo) {
246             // Property value
247
result.append("\t<td style=\"width:30%;\" class=\"textbold\">");
248             result.append(messages.key(Messages.GUI_EXPLORER_LOCKEDBY_0));
249             result.append("</td>\n");
250             result.append("</tr>\n");
251         }
252         result.append("</tr>\n");
253         result.append("<tr><td colspan=\"4\"><span style=\"height: 6px;\">&nbsp;</span></td></tr>\n");
254
255         getCms().getRequestContext().saveSiteRoot();
256         getCms().getRequestContext().setSiteRoot("/");
257         try {
258             Iterator JavaDoc i = resourceList.iterator();
259             while (i.hasNext()) {
260                 CmsResource resource = (CmsResource)i.next();
261                 String JavaDoc filetype = OpenCms.getResourceManager().getResourceType(resource.getTypeId()).getTypeName();
262                 result.append("<tr>\n");
263                 // file type
264
result.append("\t<td>");
265                 result.append("<img SRC=\"");
266                 result.append(getSkinUri());
267                 result.append("filetypes/");
268                 result.append(filetype);
269                 result.append(".gif\">");
270                 result.append("</td>\n");
271                 // file address
272
result.append("\t<td>");
273                 result.append(resource.getRootPath());
274                 result.append("</td>\n");
275                 // title
276
result.append("\t<td>");
277                 result.append(getJsp().property(CmsPropertyDefinition.PROPERTY_TITLE, resource.getRootPath(), ""));
278                 result.append("</td>\n");
279                 // current value of the property
280
if (!lockInfo) {
281                     result.append("\t<td>");
282                     result.append(getJsp().property(getParamPropertyName(), resource.getRootPath()));
283                     result.append("</td>\n");
284                 }
285                 // locked by user
286
if (lockInfo) {
287                     CmsLock lock = getCms().getLock(resource);
288                     result.append("\t<td>");
289                     result.append(getCms().readUser(lock.getUserId()).getName());
290                     result.append("</td>\n");
291                 }
292                 result.append("</tr>\n");
293             }
294             result.append("</table>\n");
295         } finally {
296             getCms().getRequestContext().restoreSiteRoot();
297         }
298
299         return result.toString();
300     }
301
302     /**
303      * Builds the html for the property definition select box.<p>
304      *
305      * @param attributes optional attributes for the &lt;select&gt; tag
306      * @return the html for the property definition select box
307      */

308     public String JavaDoc buildSelectProperty(String JavaDoc attributes) {
309
310         return CmsPropertyChange.buildSelectProperty(getCms(), Messages.get().getBundle(getLocale()).key(
311             Messages.GUI_PLEASE_SELECT_0), attributes, "");
312     }
313
314     /**
315      * Returns the value of the propertyname parameter.<p>
316      *
317      * @return the value of the propertyname parameter
318      */

319     public String JavaDoc getParamPropertyName() {
320
321         return m_paramPropertyName;
322     }
323
324     /**
325      * Sets the value of the propertyname parameter.<p>
326      *
327      * @param paramPropertyName the value of the propertyname parameter
328      */

329     public void setParamPropertyName(String JavaDoc paramPropertyName) {
330
331         m_paramPropertyName = paramPropertyName;
332     }
333
334     /**
335      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
336      */

337     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
338
339         // fill the parameter values in the get/set methods
340
fillParamValues(request);
341         // set the dialog type
342
setParamDialogtype(DIALOG_TYPE);
343         // set the action for the JSP switch
344
if (DIALOG_OK.equals(getParamAction())) {
345             setAction(ACTION_OK);
346             setParamTitle(Messages.get().getBundle(getLocale()).key(Messages.GUI_TITLE_PROPERTYDELETE_0) + ": " + getParamPropertyName());
347         } else if (DIALOG_CANCEL.equals(getParamAction())) {
348             setAction(ACTION_CANCEL);
349         } else if (DIALOG_DELETE_CASCADE.equals(getParamAction())) {
350             setAction(ACTION_DELETE_CASCADE);
351         } else {
352             setAction(ACTION_DEFAULT);
353             // build title for change property value dialog
354
setParamTitle(Messages.get().getBundle(getLocale()).key(Messages.GUI_TITLE_PROPERTYDELETE_0));
355         }
356     }
357
358     /**
359      * Returns a list of resources that are locked by another user as the current user.<p>
360      *
361      * @param resourceList the list of all (mixed) resources
362      *
363      * @return a list of resources that are locked by another user as the current user
364      * @throws CmsException if the getLock operation fails
365      */

366     private List JavaDoc getResourcesLockedByOtherUser(List JavaDoc resourceList) throws CmsException {
367
368         List JavaDoc lockedResourcesByOtherUser = new ArrayList JavaDoc();
369         Iterator JavaDoc i = resourceList.iterator();
370         while (i.hasNext()) {
371             CmsResource resource = (CmsResource)i.next();
372             // get the lock state for the resource
373
CmsLock lock = getCms().getLock(resource);
374             // add this resource to the list if this is locked by another user
375
if (lock.getType() != CmsLock.TYPE_UNLOCKED
376                 && !lock.getUserId().equals(getCms().getRequestContext().currentUser().getId())) {
377                 lockedResourcesByOtherUser.add(resource);
378             }
379         }
380
381         return lockedResourcesByOtherUser;
382     }
383 }
384
Popular Tags