KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > admin > struts > actions > PlanetSubscriptionsAction


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.apache.roller.ui.admin.struts.actions;
17
18 import java.io.IOException JavaDoc;
19 import java.text.MessageFormat JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import javax.servlet.ServletException JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.roller.ui.admin.struts.formbeans.PlanetSubscriptionFormEx;
31 import org.apache.struts.action.ActionError;
32 import org.apache.struts.action.ActionErrors;
33 import org.apache.struts.action.ActionForm;
34 import org.apache.struts.action.ActionForward;
35 import org.apache.struts.action.ActionMapping;
36 import org.apache.struts.action.ActionMessage;
37 import org.apache.struts.action.ActionMessages;
38 import org.apache.struts.actions.DispatchAction;
39 import org.apache.roller.RollerException;
40 import org.apache.roller.model.PlanetManager;
41 import org.apache.roller.model.Roller;
42 import org.apache.roller.model.RollerFactory;
43 import org.apache.roller.pojos.PlanetConfigData;
44 import org.apache.roller.pojos.PlanetGroupData;
45 import org.apache.roller.pojos.PlanetSubscriptionData;
46 import org.apache.roller.ui.core.BasePageModel;
47 import org.apache.roller.ui.core.RollerSession;
48 import org.apache.roller.util.Technorati;
49
50
51 /////////////////////////////////////////////////////////////////////////////
52
/**
53  * Add, remove, and view existing subscriptions in a group.
54  * If no group is specified via the groupHandle parameter, then uses "external".
55  *
56  * @struts.action name="planetSubscriptionFormEx" path="/roller-ui/admin/planetSubscriptions"
57  * scope="request" parameter="method"
58  *
59  * @struts.action-forward name="planetSubscriptions.page"
60  * path=".PlanetSubscriptions"
61  */

62 public final class PlanetSubscriptionsAction extends DispatchAction {
63     private static Log logger = LogFactory.getFactory().getInstance(
64             PlanetSubscriptionsAction.class);
65     
66     /** Populate page model and forward to subscription page */
67     public ActionForward getSubscriptions(ActionMapping mapping,
68             ActionForm actionForm, HttpServletRequest JavaDoc request,
69             HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc {
70         ActionForward forward = mapping.findForward("planetSubscriptions.page");
71         try {
72             if (RollerSession.getRollerSession(request).isGlobalAdminUser()) {
73                 Roller roller = RollerFactory.getRoller();
74                 PlanetManager planet = roller.getPlanetManager();
75                 PlanetSubscriptionFormEx form = (PlanetSubscriptionFormEx)actionForm;
76                 if (request.getParameter("feedUrl") != null) {
77                     String JavaDoc feedUrl = request.getParameter("feedUrl");
78                     PlanetSubscriptionData sub =
79                             planet.getSubscription(feedUrl);
80                     form.copyFrom(sub, request.getLocale());
81                 } else {
82                     form.doReset(mapping, request);
83                 }
84                 
85                 String JavaDoc groupHandle = request.getParameter("groupHandle");
86                 groupHandle = (groupHandle == null) ? form.getGroupHandle() : groupHandle;
87                 groupHandle = (groupHandle == null) ? "external" : groupHandle;
88                 
89                 PlanetGroupData targetGroup = planet.getGroup(groupHandle);
90                 form.setGroupHandle(groupHandle);
91                 request.setAttribute("model",
92                         new SubscriptionsPageModel(
93                         targetGroup, request, response, mapping, form));
94             } else {
95                 forward = mapping.findForward("access-denied");
96             }
97         } catch (Exception JavaDoc e) {
98             request.getSession().getServletContext().log("ERROR", e);
99             throw new ServletException JavaDoc(e);
100         }
101         return forward;
102     }
103     
104     /** Cancel editing, reset form */
105     public ActionForward cancelEditing(ActionMapping mapping,
106             ActionForm actionForm, HttpServletRequest JavaDoc request,
107             HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc {
108         ActionForward forward = mapping.findForward("planetSubscriptions.page");
109         try {
110             if (RollerSession.getRollerSession(request).isGlobalAdminUser()) {
111                 Roller roller = RollerFactory.getRoller();
112                 PlanetManager planet = roller.getPlanetManager();
113                 PlanetSubscriptionFormEx form = (PlanetSubscriptionFormEx)actionForm;
114                 
115                 form.doReset(mapping, request);
116                 
117                 String JavaDoc groupHandle = request.getParameter("groupHandle");
118                 groupHandle = (groupHandle == null) ? form.getGroupHandle() : groupHandle;
119                 groupHandle = (groupHandle == null) ? "external" : groupHandle;
120                 
121                 PlanetGroupData targetGroup = planet.getGroup(groupHandle);
122                 form.setGroupHandle(groupHandle);
123                 request.setAttribute("model",
124                         new SubscriptionsPageModel(
125                         targetGroup, request, response, mapping, form));
126             } else {
127                 forward = mapping.findForward("access-denied");
128             }
129         } catch (Exception JavaDoc e) {
130             request.getSession().getServletContext().log("ERROR", e);
131             throw new ServletException JavaDoc(e);
132         }
133         return forward;
134     }
135     
136     /** Delete subscription, reset form */
137     public ActionForward deleteSubscription(ActionMapping mapping,
138             ActionForm actionForm, HttpServletRequest JavaDoc request,
139             HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc {
140         ActionForward forward = mapping.findForward("planetSubscriptions.page");
141         try {
142             //RollerRequest rreq = RollerRequest.getRollerRequest(request);
143
if (RollerSession.getRollerSession(request).isGlobalAdminUser()) {
144                 Roller roller = RollerFactory.getRoller();
145                 PlanetManager planet = roller.getPlanetManager();
146                 PlanetSubscriptionFormEx form = (PlanetSubscriptionFormEx)actionForm;
147                 if (form.getId() != null) {
148                     PlanetSubscriptionData sub =
149                             planet.getSubscriptionById(form.getId());
150                     
151                     String JavaDoc groupHandle = request.getParameter("groupHandle");
152                     groupHandle = (groupHandle == null) ? form.getGroupHandle() : groupHandle;
153                     groupHandle = (groupHandle == null) ? "external" : groupHandle;
154                     
155                     PlanetGroupData targetGroup = planet.getGroup(groupHandle);
156                     
157                     targetGroup.removeSubscription(sub);
158                     planet.deleteSubscription(sub);
159                     roller.flush();
160                     // TODO: why release here?
161
roller.release();
162                     
163                     form.doReset(mapping, request);
164                     
165                     form.setGroupHandle(groupHandle);
166                     request.setAttribute("model",
167                             new SubscriptionsPageModel(
168                             targetGroup, request, response, mapping, form));
169                     
170                     ActionMessages messages = new ActionMessages();
171                     messages.add(null,
172                             new ActionMessage("planetSubscription.success.deleted"));
173                     saveMessages(request, messages);
174                 }
175             } else {
176                 forward = mapping.findForward("access-denied");
177             }
178         } catch (Exception JavaDoc e) {
179             ActionErrors errors = new ActionErrors();
180             errors.add(null, new ActionError("planetSubscription.error.deleting"));
181             saveErrors(request, errors);
182         }
183         return forward;
184     }
185     
186     /** Save subscription, add to current group */
187     public ActionForward saveSubscription(ActionMapping mapping,
188             ActionForm actionForm, HttpServletRequest JavaDoc request,
189             HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc {
190         ActionForward forward = mapping.findForward("planetSubscriptions.page");
191         try {
192             Roller roller = RollerFactory.getRoller();
193             PlanetManager planet = roller.getPlanetManager();
194             PlanetSubscriptionFormEx form = (PlanetSubscriptionFormEx)actionForm;
195             
196             String JavaDoc groupHandle = request.getParameter("groupHandle");
197             groupHandle = (groupHandle == null) ? form.getGroupHandle() : groupHandle;
198             groupHandle = (groupHandle == null) ? "external" : groupHandle;
199             
200             PlanetGroupData targetGroup = planet.getGroup(groupHandle);
201             
202             if (RollerSession.getRollerSession(request).isGlobalAdminUser()) {
203                 
204                 ActionMessages messages = new ActionMessages();
205                 PlanetSubscriptionData sub = null;
206                 ActionErrors errors = validate(planet, form);
207                 if (errors.isEmpty()) {
208                     if (form.getId() == null || form.getId().trim().length() == 0) {
209                         // Adding new subscription to group
210
// But, does subscription to that feed already exist?
211
if (form.getFeedURL() != null) {
212                             sub = planet.getSubscription(form.getFeedURL());
213                         }
214                         if (sub != null) {
215                             // Yes, we'll use it instead
216
messages.add(null, new ActionMessage(
217                                 "planetSubscription.foundExisting", sub.getTitle()));
218                         } else {
219                             // No, add new subscription
220
sub = new PlanetSubscriptionData();
221                             form.copyTo(sub, request.getLocale());
222                         }
223                         targetGroup.addSubscription(sub);
224                         
225                     } else {
226                         // User editing an existing subscription within a group
227
sub = planet.getSubscriptionById(form.getId());
228                         form.copyTo(sub, request.getLocale());
229                     }
230                     form.setGroupHandle(groupHandle);
231                     planet.saveSubscription(sub);
232                     planet.saveGroup(targetGroup);
233                     roller.flush();
234                     
235                     messages.add(null,
236                             new ActionMessage("planetSubscription.success.saved"));
237                     saveMessages(request, messages);
238                     form.doReset(mapping, request);
239                 } else {
240                     saveErrors(request, errors);
241                 }
242             } else {
243                 forward = mapping.findForward("access-denied");
244             }
245             request.setAttribute("model",
246                     new SubscriptionsPageModel(
247                     targetGroup, request, response, mapping, form));
248         } catch (RollerException e) {
249             ActionErrors errors = new ActionErrors();
250             errors.add(null, new ActionError(
251                     "planetSubscriptions.error.duringSave",e.getRootCauseMessage()));
252             saveErrors(request, errors);
253         }
254         return forward;
255     }
256     
257     /** Validate posted subscription, fill in blanks via Technorati */
258     private ActionErrors validate(
259             PlanetManager planet, PlanetSubscriptionFormEx form) {
260         String JavaDoc technoratiTitle = null;
261         String JavaDoc technoratiFeedUrl = null;
262         int inboundlinks = -1;
263         int inboundblogs = -1;
264         if (form.getSiteURL()!=null && form.getSiteURL().trim().length() > 0) {
265             try {
266                 PlanetConfigData config = planet.getConfiguration();
267                 Technorati technorati = null;
268                 if (config.getProxyHost()!=null && config.getProxyPort() > 0) {
269                     technorati = new Technorati(
270                             config.getProxyHost(), config.getProxyPort());
271                 } else {
272                     technorati = new Technorati();
273                 }
274                 Technorati.Result result =
275                         technorati.getBloginfo(form.getSiteURL());
276                 technoratiTitle = result.getWeblog().getName();
277                 technoratiFeedUrl = result.getWeblog().getRssurl();
278                 form.setInboundlinks(result.getWeblog().getInboundlinks());
279                 form.setInboundblogs(result.getWeblog().getInboundblogs());
280             } catch (Exception JavaDoc e) {
281                 logger.debug("Unable to contact Technorati", e);
282             }
283         }
284         
285         ActionErrors errors = new ActionErrors();
286         if (form.getTitle()==null || form.getTitle().trim().length()==0) {
287             if (technoratiTitle!=null && technoratiTitle.trim().length()>0) {
288                 form.setTitle(technoratiTitle);
289             } else {
290                 errors.add(null,
291                         new ActionError("planetSubscription.error.title"));
292             }
293         }
294         if (form.getFeedURL()==null || form.getFeedURL().trim().length()==0) {
295             if (technoratiFeedUrl!=null && technoratiFeedUrl.trim().length()>0) {
296                 form.setFeedURL(technoratiFeedUrl);
297             } else {
298                 errors.add(null,
299                         new ActionError("planetSubscription.error.feedUrl"));
300             }
301         }
302         if (form.getSiteURL()==null || form.getSiteURL().trim().length()==0) {
303             errors.add(null,
304                     new ActionError("planetSubscription.error.siteUrl"));
305         }
306         return errors;
307     }
308     
309     /** Page model, includes subscriptions in "external" group */
310     public class SubscriptionsPageModel extends BasePageModel {
311         private List JavaDoc subscriptions = null;
312         private boolean unconfigured = false;
313         private PlanetSubscriptionFormEx form = null;
314         
315         public SubscriptionsPageModel(
316                 PlanetGroupData group,
317                 HttpServletRequest JavaDoc request,
318                 HttpServletResponse JavaDoc response,
319                 ActionMapping mapping,
320                 PlanetSubscriptionFormEx form) throws RollerException {
321             super("dummy", request, response, mapping);
322             this.form = form;
323             if (group != null) {
324                 Set JavaDoc subsSet = group.getSubscriptions();
325                 subscriptions = new ArrayList JavaDoc(subsSet);
326             } else {
327                 unconfigured = true;
328             }
329         }
330         
331         public String JavaDoc getTitle() {
332             if (!form.getGroupHandle().equals("external")) {
333                 return MessageFormat.format(
334                         bundle.getString("planetSubscriptions.titleGroup"),
335                         new Object JavaDoc[] {form.getGroupHandle()});
336             } else {
337                 return bundle.getString("planetSubscriptions.title");
338             }
339         }
340         
341         public List JavaDoc getSubscriptions() {
342             return subscriptions;
343         }
344         
345         public boolean isUnconfigured() {
346             return unconfigured;
347         }
348     }
349 }
350
351
Popular Tags