001: /*
002: * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/content/CmsPropertyDelete.java,v $
003: * Date : $Date: 2008-02-27 12:05:37 $
004: * Version: $Revision: 1.19 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.workplace.tools.content;
033:
034: import org.opencms.file.CmsProperty;
035: import org.opencms.file.CmsPropertyDefinition;
036: import org.opencms.file.CmsResource;
037: import org.opencms.file.CmsVfsException;
038: import org.opencms.i18n.CmsMessages;
039: import org.opencms.jsp.CmsJspActionElement;
040: import org.opencms.lock.CmsLock;
041: import org.opencms.main.CmsException;
042: import org.opencms.main.OpenCms;
043: import org.opencms.workplace.CmsDialog;
044: import org.opencms.workplace.CmsWorkplaceSettings;
045:
046: import java.util.ArrayList;
047: import java.util.Collections;
048: import java.util.Iterator;
049: import java.util.List;
050:
051: import javax.servlet.http.HttpServletRequest;
052: import javax.servlet.http.HttpServletResponse;
053: import javax.servlet.jsp.JspException;
054: import javax.servlet.jsp.PageContext;
055:
056: /**
057: * Provides methods for the delete property definition dialog.<p>
058: *
059: * @author Andreas Zahner
060: * @author Armen Markarian
061: *
062: * @version $Revision: 1.19 $
063: *
064: * @since 6.0.0
065: */
066: public class CmsPropertyDelete extends CmsDialog {
067:
068: /** Value for the action: delete cascade. */
069: public static final int ACTION_DELETE_CASCADE = 100;
070:
071: /** Request parameter value for the action: delete cascade. */
072: public static final String DIALOG_DELETE_CASCADE = "deletecascade";
073:
074: /** The dialog type. */
075: public static final String DIALOG_TYPE = "propertydelete";
076:
077: /** Request parameter name for the property name. */
078: public static final String PARAM_PROPERTYNAME = "propertyname";
079:
080: private String m_paramPropertyName;
081:
082: /**
083: * Public constructor with JSP action element.<p>
084: *
085: * @param jsp an initialized JSP action element
086: */
087: public CmsPropertyDelete(CmsJspActionElement jsp) {
088:
089: super (jsp);
090: }
091:
092: /**
093: * Public constructor with JSP variables.<p>
094: *
095: * @param context the JSP page context
096: * @param req the JSP request
097: * @param res the JSP response
098: */
099: public CmsPropertyDelete(PageContext context,
100: HttpServletRequest req, HttpServletResponse res) {
101:
102: this (new CmsJspActionElement(context, req, res));
103: }
104:
105: /**
106: * Deletes the property definition.<p>
107: *
108: * @throws JspException if problems including sub-elements occur
109: */
110: public void actionDelete() throws JspException {
111:
112: // save initialized instance of this class in request attribute for included sub-elements
113: getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS,
114: this );
115: try {
116: getCms().deletePropertyDefinition(getParamPropertyName());
117: // close the dialog
118: actionCloseDialog();
119: } catch (Throwable e) {
120: // error while deleting property definition, show error dialog
121: includeErrorpage(this , e);
122: }
123: }
124:
125: /**
126: * Deletes the property definition by cascading the properties on resources.<p>
127: *
128: * @throws JspException if problems including sub-elements occur
129: */
130: public void actionDeleteCascade() throws JspException {
131:
132: // save initialized instance of this class in request attribute for included sub-elements
133: getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS,
134: this );
135: try {
136: // list of all resources containing this propertydefinition
137: List resourcesWithProperty = getCms()
138: .readResourcesWithProperty(getParamPropertyName());
139: // list of all resources locked by another user, containing this propertydefinition
140: List resourcesLockedByOtherUser = getResourcesLockedByOtherUser(resourcesWithProperty);
141: // do the following operations only if all of the resources are not locked by another user
142: if (resourcesLockedByOtherUser.isEmpty()) {
143: // save the site root
144: String storedSiteRoot = getCms().getRequestContext()
145: .getSiteRoot();
146: try {
147: // change to the root site
148: getCms().getRequestContext().setSiteRoot("/");
149:
150: Iterator i = resourcesWithProperty.iterator();
151: while (i.hasNext()) {
152: CmsResource resource = (CmsResource) i.next();
153: // read the property object
154: CmsProperty property = getCms()
155: .readPropertyObject(
156: resource.getRootPath(),
157: getParamPropertyName(), false);
158: // try to delete the property if it is not the NULL PROPERTY
159: // if the property is the NULL PROPERTY, it only had a shared
160: // value which was deleted at a sibling which was already processed
161: if (!property.isNullProperty()) {
162: CmsLock lock = getCms().getLock(resource);
163: if (lock.isUnlocked()) {
164: // lock the resource for the current (Admin) user
165: getCms().lockResource(
166: resource.getRootPath());
167: }
168: property
169: .setStructureValue(CmsProperty.DELETE_VALUE);
170: property
171: .setResourceValue(CmsProperty.DELETE_VALUE);
172: // write the property with the null value to the resource and cascade it from the definition
173: getCms().writePropertyObject(
174: resource.getRootPath(), property);
175: // unlock the resource
176: getCms().unlockResource(
177: resource.getRootPath());
178: }
179: }
180: // delete the property definition at last
181: getCms().deletePropertyDefinition(
182: getParamPropertyName());
183: } finally {
184: // restore the siteroot
185: getCms().getRequestContext().setSiteRoot(
186: storedSiteRoot);
187: // close the dialog
188: actionCloseDialog();
189: }
190: } else {
191:
192: StringBuffer reason = new StringBuffer();
193: reason.append(dialogWhiteBoxStart());
194: reason.append(buildResourceList(
195: resourcesLockedByOtherUser, true));
196: reason.append(dialogWhiteBoxEnd());
197: throw new CmsVfsException(Messages.get().container(
198: Messages.ERR_DEL_PROP_RESOURCES_LOCKED_1,
199: reason.toString()));
200: }
201: } catch (Throwable e) {
202: // error while deleting property definition, show error dialog
203: includeErrorpage(this , e);
204: }
205: }
206:
207: /**
208: * Builds a HTML list of Resources that use the specified property.<p>
209: *
210: * @throws CmsException if operation was not successful
211: *
212: * @return the HTML String for the Resource list
213: */
214: public String buildResourceList() throws CmsException {
215:
216: List resourcesWithProperty = getCms()
217: .readResourcesWithProperty(getParamPropertyName());
218:
219: return buildResourceList(resourcesWithProperty, false);
220: }
221:
222: /**
223: * Builds a HTML list of Resources.<p>
224: *
225: * Columns: Type, Name, Uri, Value of the property, locked by(optional).<p>
226: *
227: * @param resourceList a list of resources
228: * @param lockInfo a boolean to decide if the locked info should be shown or not
229: * @throws CmsException if operation was not successful
230: *
231: * @return the HTML String for the Resource list
232: */
233: public String buildResourceList(List resourceList, boolean lockInfo)
234: throws CmsException {
235:
236: // reverse the resource list
237: Collections.reverse(resourceList);
238: CmsMessages messages = Messages.get().getBundle(getLocale());
239: StringBuffer result = new StringBuffer();
240: result
241: .append("<table border=\"0\" width=\"100%\" cellpadding=\"1\" cellspacing=\"1\">\n");
242: result.append("<tr>\n");
243: // Type
244: result.append("\t<td style=\"width:5%;\" class=\"textbold\">");
245: result.append(messages.key(Messages.GUI_INPUT_TYPE_0));
246: result.append("</td>\n");
247: // Uri
248: result.append("\t<td style=\"width:40%;\" class=\"textbold\">");
249: result.append(messages.key(Messages.GUI_INPUT_ADRESS_0));
250: result.append("</td>\n");
251: // Name
252: result.append("\t<td style=\"width:25%;\" class=\"textbold\">");
253: result.append(messages.key(Messages.GUI_INPUT_TITLE_0));
254: result.append("</td>\n");
255: if (!lockInfo) {
256: // Property value
257: result
258: .append("\t<td style=\"width:30%;\" class=\"textbold\">");
259: result.append(messages
260: .key(Messages.GUI_INPUT_PROPERTYVALUE_0));
261: result.append("</td>\n");
262: }
263: if (lockInfo) {
264: // Property value
265: result
266: .append("\t<td style=\"width:30%;\" class=\"textbold\">");
267: result.append(messages
268: .key(Messages.GUI_EXPLORER_LOCKEDBY_0));
269: result.append("</td>\n");
270: result.append("</tr>\n");
271: }
272: result.append("</tr>\n");
273: result
274: .append("<tr><td colspan=\"4\"><span style=\"height: 6px;\"> </span></td></tr>\n");
275:
276: String storedSiteRoot = getCms().getRequestContext()
277: .getSiteRoot();
278: try {
279: getCms().getRequestContext().setSiteRoot("/");
280: Iterator i = resourceList.iterator();
281: while (i.hasNext()) {
282: CmsResource resource = (CmsResource) i.next();
283: String filetype = OpenCms.getResourceManager()
284: .getResourceType(resource.getTypeId())
285: .getTypeName();
286: result.append("<tr>\n");
287: // file type
288: result.append("\t<td>");
289: result.append("<img src=\"");
290: result.append(getSkinUri());
291: result.append("filetypes/");
292: result.append(filetype);
293: result.append(".gif\">");
294: result.append("</td>\n");
295: // file address
296: result.append("\t<td>");
297: result.append(resource.getRootPath());
298: result.append("</td>\n");
299: // title
300: result.append("\t<td>");
301: result.append(getJsp().property(
302: CmsPropertyDefinition.PROPERTY_TITLE,
303: resource.getRootPath(), ""));
304: result.append("</td>\n");
305: // current value of the property
306: if (!lockInfo) {
307: result.append("\t<td>");
308: result.append(getJsp().property(
309: getParamPropertyName(),
310: resource.getRootPath()));
311: result.append("</td>\n");
312: }
313: // locked by user
314: if (lockInfo) {
315: CmsLock lock = getCms().getLock(resource);
316: result.append("\t<td>");
317: result.append(getCms().readUser(lock.getUserId())
318: .getName());
319: result.append("</td>\n");
320: }
321: result.append("</tr>\n");
322: }
323: result.append("</table>\n");
324: } finally {
325: getCms().getRequestContext().setSiteRoot(storedSiteRoot);
326: }
327:
328: return result.toString();
329: }
330:
331: /**
332: * Builds the html for the property definition select box.<p>
333: *
334: * @param attributes optional attributes for the <select> tag
335: * @return the html for the property definition select box
336: */
337: public String buildSelectProperty(String attributes) {
338:
339: return CmsPropertyChange.buildSelectProperty(getCms(), Messages
340: .get().getBundle(getLocale()).key(
341: Messages.GUI_PLEASE_SELECT_0), attributes, "");
342: }
343:
344: /**
345: * Returns the value of the propertyname parameter.<p>
346: *
347: * @return the value of the propertyname parameter
348: */
349: public String getParamPropertyName() {
350:
351: return m_paramPropertyName;
352: }
353:
354: /**
355: * Sets the value of the propertyname parameter.<p>
356: *
357: * @param paramPropertyName the value of the propertyname parameter
358: */
359: public void setParamPropertyName(String paramPropertyName) {
360:
361: m_paramPropertyName = paramPropertyName;
362: }
363:
364: /**
365: * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
366: */
367: protected void initWorkplaceRequestValues(
368: CmsWorkplaceSettings settings, HttpServletRequest request) {
369:
370: // fill the parameter values in the get/set methods
371: fillParamValues(request);
372: // set the dialog type
373: setParamDialogtype(DIALOG_TYPE);
374: // set the action for the JSP switch
375: if (DIALOG_OK.equals(getParamAction())) {
376: setAction(ACTION_OK);
377: setParamTitle(Messages.get().getBundle(getLocale()).key(
378: Messages.GUI_TITLE_PROPERTYDELETE_0)
379: + ": " + getParamPropertyName());
380: } else if (DIALOG_CANCEL.equals(getParamAction())) {
381: setAction(ACTION_CANCEL);
382: } else if (DIALOG_DELETE_CASCADE.equals(getParamAction())) {
383: setAction(ACTION_DELETE_CASCADE);
384: } else {
385: setAction(ACTION_DEFAULT);
386: // build title for change property value dialog
387: setParamTitle(Messages.get().getBundle(getLocale()).key(
388: Messages.GUI_TITLE_PROPERTYDELETE_0));
389: }
390: }
391:
392: /**
393: * Returns a list of resources that are locked by another user as the current user.<p>
394: *
395: * @param resourceList the list of all (mixed) resources
396: *
397: * @return a list of resources that are locked by another user as the current user
398: * @throws CmsException if the getLock operation fails
399: */
400: private List getResourcesLockedByOtherUser(List resourceList)
401: throws CmsException {
402:
403: List lockedResourcesByOtherUser = new ArrayList();
404: Iterator i = resourceList.iterator();
405: while (i.hasNext()) {
406: CmsResource resource = (CmsResource) i.next();
407: // get the lock state for the resource
408: CmsLock lock = getCms().getLock(resource);
409: // add this resource to the list if this is locked by another user
410: if (!lock.isUnlocked()
411: && !lock.isOwnedBy(getCms().getRequestContext()
412: .currentUser())) {
413: lockedResourcesByOtherUser.add(resource);
414: }
415: }
416:
417: return lockedResourcesByOtherUser;
418: }
419: }
|