KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > planet > PlanetGroupsAction


1 /*
2  * Copyright 2005 Sun Microsystems, Inc.
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 org.roller.presentation.planet;
17
18 import java.io.IOException JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.struts.action.ActionError;
30 import org.apache.struts.action.ActionErrors;
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.struts.action.ActionMessage;
35 import org.apache.struts.action.ActionMessages;
36 import org.apache.struts.actions.DispatchAction;
37 import org.roller.RollerException;
38 import org.roller.model.PlanetManager;
39 import org.roller.model.Roller;
40 import org.roller.pojos.PlanetGroupData;
41 import org.roller.presentation.BasePageModel;
42 import org.roller.presentation.RollerRequest;
43 import org.roller.presentation.forms.PlanetGroupForm;
44
45
46 /////////////////////////////////////////////////////////////////////////////
47
/**
48  * Add, remove, and view user defined groups.
49  *
50  * @struts.action name="planetGroupForm" path="/admin/planetGroups"
51  * scope="request" parameter="method"
52  *
53  * @struts.action-forward name="planetGroups.page"
54  * path="/planet/PlanetGroups.jsp"
55  */

56 public final class PlanetGroupsAction extends DispatchAction
57 {
58     private static Log logger = LogFactory.getFactory().getInstance(
59             PlanetGroupsAction.class);
60
61     /** Populate page model and forward to subscription page */
62     public ActionForward getGroups(ActionMapping mapping,
63             ActionForm actionForm, HttpServletRequest JavaDoc request,
64             HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
65     {
66         ActionForward forward = mapping.findForward("planetGroups.page");
67         try
68         {
69             RollerRequest rreq = RollerRequest.getRollerRequest(request);
70             if (rreq.isUserAuthorizedToEdit())
71             {
72                 Roller roller = rreq.getRoller();
73                 PlanetManager planet = roller.getPlanetManager();
74                 PlanetGroupForm form = (PlanetGroupForm)actionForm;
75                 if (request.getParameter("groupHandle") != null)
76                 {
77                     String JavaDoc feedUrl = request.getParameter("groupHandle");
78                     PlanetGroupData group = planet.getGroup(feedUrl);
79                     form.copyFrom(group, request.getLocale());
80                 }
81                 else
82                 {
83                     form.doReset(mapping, request);
84                 }
85                 request.setAttribute("model",
86                     new GroupsPageModel(request, response, mapping));
87             }
88             else
89             {
90                 forward = mapping.findForward("access-denied");
91             }
92         }
93         catch (Exception JavaDoc e)
94         {
95             request.getSession().getServletContext().log("ERROR", e);
96             throw new ServletException JavaDoc(e);
97         }
98         return forward;
99     }
100
101     /** Cancel editing, reset form */
102     public ActionForward cancelEditing(ActionMapping mapping,
103             ActionForm actionForm, HttpServletRequest JavaDoc request,
104             HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
105     {
106         ActionForward forward = mapping.findForward("planetGroups.page");
107         try
108         {
109             RollerRequest rreq = RollerRequest.getRollerRequest(request);
110             if (rreq.isUserAuthorizedToEdit())
111             {
112                 Roller roller = rreq.getRoller();
113                 PlanetManager planet = roller.getPlanetManager();
114                 PlanetGroupForm form = (PlanetGroupForm)actionForm;
115                 
116                 form.doReset(mapping, request);
117                 
118                 request.setAttribute("model",
119                     new GroupsPageModel(request, response, mapping));
120             }
121             else
122             {
123                 forward = mapping.findForward("access-denied");
124             }
125         }
126         catch (Exception JavaDoc e)
127         {
128             request.getSession().getServletContext().log("ERROR", e);
129             throw new ServletException JavaDoc(e);
130         }
131         return forward;
132     }
133     
134     /** Delete subscription, reset form */
135     public ActionForward deleteGroup(ActionMapping mapping,
136             ActionForm actionForm, HttpServletRequest JavaDoc request,
137             HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
138     {
139         ActionForward forward = mapping.findForward("planetGroups.page");
140         try
141         {
142             RollerRequest rreq = RollerRequest.getRollerRequest(request);
143             if (rreq.isUserAuthorizedToEdit())
144             {
145                 Roller roller = rreq.getRoller();
146                 PlanetManager planet = roller.getPlanetManager();
147                 PlanetGroupForm form = (PlanetGroupForm)actionForm;
148                 if (form.getHandle() != null)
149                 {
150                     PlanetGroupData group = planet.getGroup(form.getHandle());
151                     planet.deleteGroup(group);
152                     roller.commit();
153                     roller.release();
154                     
155                     roller.begin();
156                     form.doReset(mapping, request);
157                     
158                     request.setAttribute("model",
159                         new GroupsPageModel(request, response, mapping));
160                     
161                     ActionMessages messages = new ActionMessages();
162                     messages.add(null,
163                         new ActionMessage("planetSubscription.success.deleted"));
164                     saveMessages(request, messages);
165                 }
166             }
167             else
168             {
169                 forward = mapping.findForward("access-denied");
170             }
171         }
172         catch (Exception JavaDoc e)
173         {
174             ActionErrors errors = new ActionErrors();
175             errors.add(null, new ActionError("planetGroup.error.deleting"));
176             saveErrors(request, errors);
177         }
178         return forward;
179     }
180
181     /** Save subscription, add to "external" group */
182     public ActionForward saveGroup(ActionMapping mapping,
183             ActionForm actionForm, HttpServletRequest JavaDoc request,
184             HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
185     {
186         ActionForward forward = mapping.findForward("planetGroups.page");
187         try
188         {
189             RollerRequest rreq = RollerRequest.getRollerRequest(request);
190             if (rreq.isUserAuthorizedToEdit())
191             {
192                 PlanetGroupForm form = (PlanetGroupForm)actionForm;
193                 Roller roller = rreq.getRoller();
194                 PlanetManager planet = roller.getPlanetManager();
195                 ActionErrors errors = validate(planet, form);
196                 if (errors.isEmpty())
197                 {
198                     PlanetGroupData group = null;
199                     if (form.getId() == null || form.getId().trim().length() == 0)
200                     {
201                         group = new PlanetGroupData();
202                     }
203                     else
204                     {
205                         group = planet.getGroupById(form.getId());
206                     }
207                     form.copyTo(group, request.getLocale());
208                     planet.saveGroup(group);
209                     roller.commit();
210
211                     ActionMessages messages = new ActionMessages();
212                     messages.add(null,
213                             new ActionMessage("planetGroups.success.saved"));
214                     saveMessages(request, messages);
215                     form.doReset(mapping, request);
216
217                     request.setAttribute("model",
218                             new GroupsPageModel(request, response, mapping));
219                 }
220                 else
221                 {
222                     saveErrors(request, errors);
223                 }
224             }
225             else
226             {
227                 forward = mapping.findForward("access-denied");
228             }
229         }
230         catch (RollerException e)
231         {
232             ActionErrors errors = new ActionErrors();
233             errors.add(null, new ActionError(
234               "planetSubscriptions.error.duringSave",e.getRootCauseMessage()));
235             saveErrors(request, errors);
236         }
237         return forward;
238     }
239     
240     /** Validate posted group */
241     private ActionErrors validate(
242             PlanetManager planet, PlanetGroupForm form)
243     {
244         ActionErrors errors = new ActionErrors();
245         if (form.getTitle()==null || form.getTitle().trim().length()==0)
246         {
247             errors.add(null, new ActionError("planetGroups.error.title"));
248         }
249         if (form.getHandle()==null || form.getHandle().trim().length()==0)
250         {
251             errors.add(null, new ActionError("planetGroups.error.handle"));
252         }
253         if (form.getHandle() != null &&
254         (form.getHandle().equals("all") || form.getHandle().equals("external")))
255         {
256            errors.add(null, new ActionError("planetGroups.error.nameReserved"));
257         }
258         return errors;
259     }
260
261     /** Page model */
262     public class GroupsPageModel extends BasePageModel
263     {
264         private List JavaDoc groups = new ArrayList JavaDoc();
265         private boolean unconfigured = false;
266         public GroupsPageModel(
267             HttpServletRequest JavaDoc request,
268             HttpServletResponse JavaDoc response,
269             ActionMapping mapping) throws RollerException
270         {
271             super(request, response, mapping);
272             RollerRequest rreq = RollerRequest.getRollerRequest(request);
273             Roller roller = rreq.getRoller();
274             PlanetManager planet = roller.getPlanetManager();
275             PlanetGroupData externalGroup = planet.getGroup("external");
276             if (externalGroup != null)
277             {
278                 Iterator JavaDoc allgroups = planet.getGroups().iterator();
279                 while (allgroups.hasNext())
280                 {
281                     PlanetGroupData agroup = (PlanetGroupData)allgroups.next();
282                     if ( !agroup.getHandle().equals("external")
283                          && !agroup.getHandle().equals("all"))
284                       {
285                           groups.add(agroup);
286                       }
287                 }
288             }
289             else
290             {
291                 unconfigured = true;
292             }
293         }
294         public List JavaDoc getGroups()
295         {
296             return groups;
297         }
298         public boolean isUnconfigured()
299         {
300             return unconfigured;
301         }
302     }
303 }
304
Popular Tags