KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > news > DeleteNewsItemAction


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.news;
17
18 import com.blandware.atleap.common.NewsModuleConstants;
19 import com.blandware.atleap.model.news.NewsItem;
20 import com.blandware.atleap.search.SearchManager;
21 import com.blandware.atleap.service.news.NewsManager;
22 import com.blandware.atleap.webapp.action.core.BaseAction;
23 import com.blandware.atleap.webapp.form.NewsItemForm;
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 news item
38  * </p>
39  * <p><a HREF="DeleteNewsItemAction.java.htm"><i>View Source</i></a></p>
40  * <p/>
41  *
42  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
43  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
44  * @version $Revision: 1.16 $ $Date: 2006/03/22 11:22:07 $
45  * @struts.action path="/news/deleteItem"
46  * name="newsItemForm"
47  * scope="request"
48  * validate="false"
49  * roles="news-item-delete"
50  * @struts.action-forward name="listNewsItems"
51  * path="/news/listItems.do"
52  * redirect="true"
53  * @struts.action-forward name="viewLinkedObjects"
54  * path="/core/linkedObjects/view.do"
55  */

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

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