KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > authoring > struts > actions > CategoryDeleteAction


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * 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. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

18 /*
19  * Created on Oct 21, 2003
20  */

21 package org.apache.roller.ui.authoring.struts.actions;
22
23 import java.util.Iterator JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29
30 import org.apache.struts.action.Action;
31 import org.apache.struts.action.ActionForm;
32 import org.apache.struts.action.ActionForward;
33 import org.apache.struts.action.ActionMapping;
34 import org.apache.roller.model.RollerFactory;
35 import org.apache.roller.model.WeblogManager;
36 import org.apache.roller.pojos.WeblogCategoryData;
37 import org.apache.roller.ui.core.RollerRequest;
38 import org.apache.roller.ui.core.RollerSession;
39 import org.apache.roller.util.cache.CacheManager;
40 import org.apache.roller.ui.authoring.struts.formbeans.CategoryDeleteForm;
41 import org.apache.roller.ui.core.RequestConstants;
42
43 /**
44  * @struts.action path="/roller-ui/authoring/categoryDelete" name="categoryDeleteForm"
45  * @struts.action-forward name="CategoryDeleteOK" path=".CategoryDeleteOK"
46  *
47  * @author Dave Johnson
48  */

49 public class CategoryDeleteAction extends Action
50 {
51     public ActionForward execute(
52         ActionMapping mapping,
53         ActionForm actionForm,
54         HttpServletRequest JavaDoc request,
55         HttpServletResponse JavaDoc response)
56         throws Exception JavaDoc
57     {
58         ActionForward forward = mapping.findForward("categories");
59         CategoryDeleteForm form = (CategoryDeleteForm)actionForm;
60         WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager();
61
62         String JavaDoc catid = request.getParameter(RequestConstants.WEBLOGCATEGORY_ID);
63         WeblogCategoryData catToDelete =
64                 wmgr.getWeblogCategory(catid);
65         RollerSession rses = RollerSession.getRollerSession(request);
66         if (rses.isUserAuthorizedToAuthor(catToDelete.getWebsite()))
67         {
68             String JavaDoc returnId = null;
69             if (catToDelete.getParent() != null)
70             {
71                 returnId = catToDelete.getParent().getId();
72             }
73             if (form.isDelete() == null)
74             {
75                 // Present CategoryDeleteOK? page to user
76
RollerRequest rreq = RollerRequest.getRollerRequest(request);
77                 WeblogCategoryData theCat = wmgr.getWeblogCategory(catid);
78                 Iterator JavaDoc allCats =
79                     wmgr.getWeblogCategories(theCat.getWebsite()).iterator();
80                 List JavaDoc destCats = new LinkedList JavaDoc();
81                 while (allCats.hasNext())
82                 {
83                     WeblogCategoryData cat = (WeblogCategoryData)allCats.next();
84                     // Allow entries to be moved to any other category except
85
// root and the sub-cats of the category being deleted.
86
if (!cat.getId().equals(catid)
87                         && cat.getParent()!=null
88                         && !cat.descendentOf(catToDelete))
89                     {
90                         destCats.add(cat);
91                     }
92                 }
93                 if (destCats.size() > 0)
94                 {
95                     form.setName(theCat.getName());
96                     form.setCategoryId(catid);
97                     form.setCats(destCats);
98                     form.setInUse(Boolean.valueOf(catToDelete.isInUse()));
99                     forward = mapping.findForward("CategoryDeleteOK");
100                 }
101                 else
102                 {
103                     // Can't delete last category, send 'em back!
104
if (null != returnId)
105                     {
106                         request.setAttribute(
107                                 RequestConstants.WEBLOGCATEGORY_ID, returnId);
108                     }
109                 }
110             }
111             else if (form.isDelete().booleanValue()) {
112                 
113                 // User clicked YES to delete
114
// remove cat to delete
115
if (form.getMoveToWeblogCategoryId() != null)
116                 {
117                     WeblogCategoryData destCat =
118                         wmgr.getWeblogCategory(form.getMoveToWeblogCategoryId());
119                     if (rses.isUserAuthorizedToAuthor(destCat.getWebsite()))
120                     {
121                         wmgr.moveWeblogCategoryContents(catToDelete, destCat);
122                         RollerFactory.getRoller().flush();
123                     }
124                     else
125                     {
126                         return mapping.findForward("access-denied");
127                     }
128                 }
129                                 
130                 catToDelete = wmgr.getWeblogCategory(catToDelete.getId());
131                 wmgr.removeWeblogCategory(catToDelete);
132                 RollerFactory.getRoller().flush();
133
134                 // notify caches of invalidated object
135
CacheManager.invalidate(catToDelete);
136
137                 if (null != returnId)
138                 {
139                     request.setAttribute(RequestConstants.WEBLOGCATEGORY_ID, returnId);
140                 }
141             }
142             else
143             {
144                 // User clicked NO to delete, do back to categories form
145
if (null != returnId)
146                 {
147                     request.setAttribute(
148                        RequestConstants.WEBLOGCATEGORY_ID, returnId);
149                 }
150             }
151         }
152         else
153         {
154             forward = mapping.findForward("access-denied");
155         }
156         return forward;
157     }
158
159 }
160
Popular Tags