KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > testimonials > DeleteTestimonialAction


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.testimonials;
17
18 import com.blandware.atleap.common.TestimonialModuleConstants;
19 import com.blandware.atleap.model.testimonials.Testimonial;
20 import com.blandware.atleap.search.SearchManager;
21 import com.blandware.atleap.service.testimonials.TestimonialManager;
22 import com.blandware.atleap.webapp.action.core.BaseAction;
23 import com.blandware.atleap.webapp.form.TestimonialForm;
24 import com.blandware.atleap.webapp.util.core.CacheUtil;
25 import com.blandware.atleap.webapp.util.core.WebappConstants;
26 import com.blandware.atleap.webapp.util.core.WebappUtil;
27 import org.apache.struts.action.ActionForm;
28 import org.apache.struts.action.ActionForward;
29 import org.apache.struts.action.ActionMapping;
30 import org.apache.struts.action.ActionMessage;
31 import org.apache.struts.action.ActionMessages;
32
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35
36 /**
37  * <p>Deletes testimonial
38  * </p>
39  * <p><a HREF="DeleteTestimonialAction.java.htm"><i>View Source</i></a></p>
40  * <p/>
41  *
42  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
43  * @version $Revision: 1.9 $ $Date: 2006/03/22 11:22:08 $
44  * @struts.action path="/testimonials/deleteTestimonial"
45  * name="testimonialForm"
46  * scope="request"
47  * validate="false"
48  * roles="testimonials-item-delete"
49  * @struts.action-forward name="listTestimonials"
50  * path="/testimonials/list.do"
51  * redirect="true"
52  * @struts.action-forward name="viewLinkedObjects"
53  * path="/core/linkedObjects/view.do"
54  */

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

65     public ActionForward execute(ActionMapping mapping, ActionForm form,
66                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
67
68         TestimonialForm testimonialForm = (TestimonialForm) form;
69         Long JavaDoc testimonialId = null;
70         if ( testimonialForm.getId() != null ) {
71             testimonialId = Long.valueOf(testimonialForm.getId());
72         } else {
73             if ( log.isWarnEnabled() ) {
74                 log.warn("Missing testimonial item ID. Returning to list...");
75             }
76             return mapping.findForward("listTestimonials");
77         }
78
79         TestimonialManager testimonialManager = (TestimonialManager) getBean(TestimonialModuleConstants.TESTIMONIAL_MANAGER_BEAN);
80         Testimonial testimonial = testimonialManager.retrieveTestimonial(testimonialId);
81
82         if ( testimonial == null ) {
83             // testimonial not found. it might be deleted by someone else
84
ActionMessages errors = new ActionMessages();
85             errors.add("testimonialNotFound", new ActionMessage("testimonials.errors.notFound"));
86             saveErrors(request, errors);
87             return mapping.findForward("listTestimonials");
88         }
89
90         if ( !"true".equalsIgnoreCase(request.getParameter("force")) && testimonial.isInUse() ) {
91             // trying to delete used testimonial - display warning
92
String JavaDoc action = mapping.findForward("listTestimonials").getPath();
93             String JavaDoc redirectUrl = WebappUtil.getActionMappingURL(action, null, request, WebappConstants.URL_TYPE_DOMAIN_RELATIVE);
94             request.getSession().setAttribute(WebappConstants.LINKED_OBJECTS_REDIRECT_URL_KEY, redirectUrl);
95             ActionMessages messages = new ActionMessages();
96             messages.add("deleteWarning", new ActionMessage("testimonials.messages.deleteWarning"));
97             saveMessages(request, messages);
98             request.getSession().removeAttribute(WebappConstants.LINKED_OBJECTS_UPDATE_ACTION_KEY);
99             request.getSession().setAttribute(WebappConstants.LINKED_OBJECTS_DELETE_ACTION_KEY, "testimonials/deleteTestimonial");
100             request.setAttribute(WebappConstants.LINKED_OBJECT_ID_KEY, testimonial.getId());
101             request.setAttribute(WebappConstants.LINKED_OBJECT_TYPE_KEY, "page");
102             return mapping.findForward("viewLinkedObjects");
103         }
104
105         String JavaDoc uri = testimonial.getUri();
106         testimonialManager.deleteTestimonial(testimonialId);
107
108         //unindex action page
109
SearchManager searchManager = SearchManager.getInstance(request.getSession().getServletContext());
110         searchManager.unIndexPage(uri, request);
111
112         CacheUtil cacheUtil = CacheUtil.getInstance(request);
113         cacheUtil.flushLocalizableFieldValueCache(testimonialId);
114
115         return mapping.findForward("listTestimonials");
116     }
117 }
Popular Tags