KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > core > contentResource > DeleteContentResourceAction


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.action.core.contentResource;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.model.core.ContentDocument;
20 import com.blandware.atleap.model.core.ContentResource;
21 import com.blandware.atleap.persistence.exception.DeleteException;
22 import com.blandware.atleap.search.SearchManager;
23 import com.blandware.atleap.service.core.ContentResourceManager;
24 import com.blandware.atleap.webapp.action.core.BaseAction;
25 import com.blandware.atleap.webapp.form.ContentResourceForm;
26 import com.blandware.atleap.webapp.util.core.CacheUtil;
27 import com.blandware.atleap.webapp.util.core.WebappConstants;
28 import com.blandware.atleap.webapp.util.core.WebappUtil;
29 import org.apache.commons.validator.GenericValidator;
30 import org.apache.struts.action.ActionForm;
31 import org.apache.struts.action.ActionForward;
32 import org.apache.struts.action.ActionMapping;
33 import org.apache.struts.action.ActionMessage;
34 import org.apache.struts.action.ActionMessages;
35
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpServletResponse JavaDoc;
38
39 /**
40  * <p>Deletes content resource
41  * </p>
42  * <p><a HREF="DeleteLayoutAction.java.htm"><i>View Source</i></a></p>
43  * <p/>
44  *
45  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
46  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
47  * @version $Revision: 1.24 $ $Date: 2006/03/22 11:22:02 $
48  * @struts.action path="/core/contentResource/delete"
49  * name="contentResourceForm"
50  * scope="request"
51  * validate="false"
52  * roles="core-contentResource-delete"
53  * @struts.action-forward name="listContentResources"
54  * path="/core/contentResource/list.do"
55  * redirect="true"
56  * @struts.action-forward name="viewLinkedObjects"
57  * path="/core/linkedObjects/view.do"
58  * @struts.action-forward name="unsatisfiable"
59  * path="/core/contentResource/list.do"
60  */

61 public final class DeleteContentResourceAction extends BaseAction {
62     /**
63      * @param mapping The ActionMapping used to select this instance
64      * @param form The optional ActionForm bean for this request (if any)
65      * @param request The HTTP request we are proceeding
66      * @param response The HTTP response we are creating
67      * @return an ActionForward instance describing where and how
68      * control should be forwarded, or null if response
69      * has already been completed
70      */

71     public ActionForward execute(ActionMapping mapping, ActionForm form,
72                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
73
74         ContentResourceForm contentResourceForm = (ContentResourceForm) form;
75         Long JavaDoc contentResourceId = null;
76         if ( !GenericValidator.isBlankOrNull(contentResourceForm.getId()) ) {
77             contentResourceId = Long.valueOf(contentResourceForm.getId());
78         } else {
79             if ( log.isWarnEnabled() ) {
80                 log.warn("Missing content resource ID. Returning to list...");
81             }
82             return mapping.findForward("listContentResources");
83         }
84
85         ContentResourceManager contentResourceManager = (ContentResourceManager) getBean(Constants.CONTENT_RESOURCE_MANAGER_BEAN);
86
87         ContentResource contentResource = contentResourceManager.retrieveContentResource(contentResourceId);
88
89         if ( contentResource == null ) {
90             // content resource not found. it might be deleted by someone else
91
ActionMessages errors = new ActionMessages();
92             errors.add("contentResourceNotFound", new ActionMessage("core.contentResource.errors.notFound"));
93             saveErrors(request, errors);
94             return mapping.findForward("listContentResources");
95         }
96
97         if ( !"true".equalsIgnoreCase(request.getParameter("force")) && contentResource.isInUse() ) {
98             // trying to delete used resource - display warning
99
String JavaDoc action = mapping.findForward("listContentResources").getPath();
100             String JavaDoc redirectUrl = WebappUtil.getActionMappingURL(action, null, request, WebappConstants.URL_TYPE_DOMAIN_RELATIVE);
101             request.getSession().setAttribute(WebappConstants.LINKED_OBJECTS_REDIRECT_URL_KEY, redirectUrl);
102             ActionMessages messages = new ActionMessages();
103             messages.add("deleteWarning", new ActionMessage("core.contentResource.messages.deleteWarning"));
104             saveMessages(request, messages);
105             request.getSession().removeAttribute(WebappConstants.LINKED_OBJECTS_UPDATE_ACTION_KEY);
106             request.getSession().setAttribute(WebappConstants.LINKED_OBJECTS_DELETE_ACTION_KEY, "core/contentResource/delete");
107             request.setAttribute(WebappConstants.LINKED_OBJECT_ID_KEY, contentResource.getId());
108             request.setAttribute(WebappConstants.LINKED_OBJECT_TYPE_KEY, "resource");
109             saveToken(request);
110             return mapping.findForward("viewLinkedObjects");
111         }
112
113         String JavaDoc uri = contentResource.getUri();
114
115         try {
116             contentResourceManager.deleteContentResource(contentResourceId);
117
118             //flush cache
119
CacheUtil cacheUtil = CacheUtil.getInstance(request);
120             cacheUtil.flushResourceCache(uri);
121
122             //index
123
if ( contentResource instanceof ContentDocument ) {
124                 SearchManager searchManager = SearchManager.getInstance(servlet.getServletContext());
125                 searchManager.unIndexDocument(uri, request);
126             }
127
128         } catch ( DeleteException e ) {
129             saveToken(request);
130             ActionMessages errors = new ActionMessages();
131             errors.add("contentResourceCannotDelete", new ActionMessage("errors.contentResourceCannotDelete"));
132             saveErrors(request, errors);
133             return mapping.findForward("listContentResources");
134         }
135         return mapping.findForward("listContentResources");
136     }
137 }
Popular Tags