001: /*
002: * Copyright 2005 Sun Microsystems, Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.roller.ui.admin.struts.actions;
017:
018: import java.io.IOException;
019: import java.text.MessageFormat;
020: import java.util.ArrayList;
021: import java.util.List;
022: import java.util.Set;
023:
024: import javax.servlet.ServletException;
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.roller.ui.admin.struts.formbeans.PlanetSubscriptionFormEx;
031: import org.apache.struts.action.ActionError;
032: import org.apache.struts.action.ActionErrors;
033: import org.apache.struts.action.ActionForm;
034: import org.apache.struts.action.ActionForward;
035: import org.apache.struts.action.ActionMapping;
036: import org.apache.struts.action.ActionMessage;
037: import org.apache.struts.action.ActionMessages;
038: import org.apache.struts.actions.DispatchAction;
039: import org.apache.roller.RollerException;
040: import org.apache.roller.planet.business.PlanetManager;
041: import org.apache.roller.business.Roller;
042: import org.apache.roller.business.RollerFactory;
043: import org.apache.roller.planet.pojos.PlanetConfigData;
044: import org.apache.roller.planet.pojos.PlanetGroupData;
045: import org.apache.roller.planet.pojos.PlanetSubscriptionData;
046: import org.apache.roller.ui.core.BasePageModel;
047: import org.apache.roller.ui.core.RollerSession;
048: import org.apache.roller.util.Technorati;
049:
050: /////////////////////////////////////////////////////////////////////////////
051: /**
052: * Add, remove, and view existing subscriptions in a group.
053: * If no group is specified via the groupHandle parameter, then uses "external".
054: *
055: * @struts.action name="planetSubscriptionFormEx" path="/roller-ui/admin/planetSubscriptions"
056: * scope="request" parameter="method"
057: *
058: * @struts.action-forward name="planetSubscriptions.page"
059: * path=".PlanetSubscriptions"
060: */
061: public final class PlanetSubscriptionsAction extends DispatchAction {
062: private static Log logger = LogFactory.getFactory().getInstance(
063: PlanetSubscriptionsAction.class);
064:
065: /** Populate page model and forward to subscription page */
066: public ActionForward getSubscriptions(ActionMapping mapping,
067: ActionForm actionForm, HttpServletRequest request,
068: HttpServletResponse response) throws IOException,
069: ServletException {
070: ActionForward forward = mapping
071: .findForward("planetSubscriptions.page");
072: try {
073: if (RollerSession.getRollerSession(request)
074: .isGlobalAdminUser()) {
075: Roller roller = RollerFactory.getRoller();
076: PlanetManager planet = roller.getPlanetManager();
077: PlanetSubscriptionFormEx form = (PlanetSubscriptionFormEx) actionForm;
078: if (request.getParameter("feedUrl") != null) {
079: String feedUrl = request.getParameter("feedUrl");
080: PlanetSubscriptionData sub = planet
081: .getSubscription(feedUrl);
082: form.copyFrom(sub, request.getLocale());
083: } else {
084: form.doReset(mapping, request);
085: }
086:
087: String groupHandle = request
088: .getParameter("groupHandle");
089: groupHandle = (groupHandle == null) ? form
090: .getGroupHandle() : groupHandle;
091: groupHandle = (groupHandle == null) ? "external"
092: : groupHandle;
093:
094: PlanetGroupData targetGroup = planet
095: .getGroup(groupHandle);
096: form.setGroupHandle(groupHandle);
097: request.setAttribute("model",
098: new SubscriptionsPageModel(targetGroup,
099: request, response, mapping, form));
100: } else {
101: forward = mapping.findForward("access-denied");
102: }
103: } catch (Exception e) {
104: request.getSession().getServletContext().log("ERROR", e);
105: throw new ServletException(e);
106: }
107: return forward;
108: }
109:
110: /** Cancel editing, reset form */
111: public ActionForward cancelEditing(ActionMapping mapping,
112: ActionForm actionForm, HttpServletRequest request,
113: HttpServletResponse response) throws IOException,
114: ServletException {
115: ActionForward forward = mapping
116: .findForward("planetSubscriptions.page");
117: try {
118: if (RollerSession.getRollerSession(request)
119: .isGlobalAdminUser()) {
120: Roller roller = RollerFactory.getRoller();
121: PlanetManager planet = roller.getPlanetManager();
122: PlanetSubscriptionFormEx form = (PlanetSubscriptionFormEx) actionForm;
123:
124: form.doReset(mapping, request);
125:
126: String groupHandle = request
127: .getParameter("groupHandle");
128: groupHandle = (groupHandle == null) ? form
129: .getGroupHandle() : groupHandle;
130: groupHandle = (groupHandle == null) ? "external"
131: : groupHandle;
132:
133: PlanetGroupData targetGroup = planet
134: .getGroup(groupHandle);
135: form.setGroupHandle(groupHandle);
136: request.setAttribute("model",
137: new SubscriptionsPageModel(targetGroup,
138: request, response, mapping, form));
139: } else {
140: forward = mapping.findForward("access-denied");
141: }
142: } catch (Exception e) {
143: request.getSession().getServletContext().log("ERROR", e);
144: throw new ServletException(e);
145: }
146: return forward;
147: }
148:
149: /** Delete subscription, reset form */
150: public ActionForward deleteSubscription(ActionMapping mapping,
151: ActionForm actionForm, HttpServletRequest request,
152: HttpServletResponse response) throws IOException,
153: ServletException {
154: ActionForward forward = mapping
155: .findForward("planetSubscriptions.page");
156: try {
157: //RollerRequest rreq = RollerRequest.getRollerRequest(request);
158: if (RollerSession.getRollerSession(request)
159: .isGlobalAdminUser()) {
160: Roller roller = RollerFactory.getRoller();
161: PlanetManager planet = roller.getPlanetManager();
162: PlanetSubscriptionFormEx form = (PlanetSubscriptionFormEx) actionForm;
163: if (form.getId() != null) {
164: PlanetSubscriptionData sub = planet
165: .getSubscriptionById(form.getId());
166:
167: String groupHandle = request
168: .getParameter("groupHandle");
169: groupHandle = (groupHandle == null) ? form
170: .getGroupHandle() : groupHandle;
171: groupHandle = (groupHandle == null) ? "external"
172: : groupHandle;
173:
174: PlanetGroupData targetGroup = planet
175: .getGroup(groupHandle);
176:
177: targetGroup.getSubscriptions().remove(sub);
178: planet.deleteSubscription(sub);
179: roller.flush();
180: // TODO: why release here?
181: roller.release();
182:
183: form.doReset(mapping, request);
184:
185: form.setGroupHandle(groupHandle);
186: request.setAttribute("model",
187: new SubscriptionsPageModel(targetGroup,
188: request, response, mapping, form));
189:
190: ActionMessages messages = new ActionMessages();
191: messages.add(null, new ActionMessage(
192: "planetSubscription.success.deleted"));
193: saveMessages(request, messages);
194: }
195: } else {
196: forward = mapping.findForward("access-denied");
197: }
198: } catch (Exception e) {
199: ActionErrors errors = new ActionErrors();
200: errors.add(null, new ActionError(
201: "planetSubscription.error.deleting"));
202: saveErrors(request, errors);
203: }
204: return forward;
205: }
206:
207: /** Save subscription, add to current group */
208: public ActionForward saveSubscription(ActionMapping mapping,
209: ActionForm actionForm, HttpServletRequest request,
210: HttpServletResponse response) throws IOException,
211: ServletException {
212: ActionForward forward = mapping
213: .findForward("planetSubscriptions.page");
214: try {
215: Roller roller = RollerFactory.getRoller();
216: PlanetManager planet = roller.getPlanetManager();
217: PlanetSubscriptionFormEx form = (PlanetSubscriptionFormEx) actionForm;
218:
219: String groupHandle = request.getParameter("groupHandle");
220: groupHandle = (groupHandle == null) ? form.getGroupHandle()
221: : groupHandle;
222: groupHandle = (groupHandle == null) ? "external"
223: : groupHandle;
224:
225: PlanetGroupData targetGroup = planet.getGroup(groupHandle);
226:
227: if (RollerSession.getRollerSession(request)
228: .isGlobalAdminUser()) {
229:
230: ActionMessages messages = new ActionMessages();
231: PlanetSubscriptionData sub = null;
232: ActionErrors errors = validate(planet, form);
233: if (errors.isEmpty()) {
234: if (form.getId() == null
235: || form.getId().trim().length() == 0) {
236: // Adding new subscription to group
237: // But, does subscription to that feed already exist?
238: if (form.getFeedURL() != null) {
239: sub = planet.getSubscription(form
240: .getFeedURL());
241: }
242: if (sub != null) {
243: // Yes, we'll use it instead
244: messages.add(null, new ActionMessage(
245: "planetSubscription.foundExisting",
246: sub.getTitle()));
247: } else {
248: // No, add new subscription
249: sub = new PlanetSubscriptionData();
250: form.copyTo(sub, request.getLocale());
251: planet.saveSubscription(sub);
252: }
253: targetGroup.getSubscriptions().add(sub);
254:
255: } else {
256: // User editing an existing subscription within a group
257: sub = planet.getSubscriptionById(form.getId());
258: form.copyTo(sub, request.getLocale());
259: }
260: form.setGroupHandle(groupHandle);
261: planet.saveGroup(targetGroup);
262: roller.flush();
263:
264: messages.add(null, new ActionMessage(
265: "planetSubscription.success.saved"));
266: saveMessages(request, messages);
267: form.doReset(mapping, request);
268: } else {
269: saveErrors(request, errors);
270: }
271: } else {
272: forward = mapping.findForward("access-denied");
273: }
274: request.setAttribute("model", new SubscriptionsPageModel(
275: targetGroup, request, response, mapping, form));
276: } catch (RollerException e) {
277: ActionErrors errors = new ActionErrors();
278: errors.add(null, new ActionError(
279: "planetSubscriptions.error.duringSave", e
280: .getRootCauseMessage()));
281: saveErrors(request, errors);
282: }
283: return forward;
284: }
285:
286: /** Validate posted subscription, fill in blanks via Technorati */
287: private ActionErrors validate(PlanetManager planet,
288: PlanetSubscriptionFormEx form) {
289: String technoratiTitle = null;
290: String technoratiFeedUrl = null;
291: int inboundlinks = -1;
292: int inboundblogs = -1;
293: if (form.getSiteURL() != null
294: && form.getSiteURL().trim().length() > 0) {
295: try {
296: PlanetConfigData config = planet.getConfiguration();
297: Technorati technorati = null;
298: if (config.getProxyHost() != null
299: && config.getProxyPort() > 0) {
300: technorati = new Technorati(config.getProxyHost(),
301: config.getProxyPort());
302: } else {
303: technorati = new Technorati();
304: }
305: Technorati.Result result = technorati.getBloginfo(form
306: .getSiteURL());
307: technoratiTitle = result.getWeblog().getName();
308: technoratiFeedUrl = result.getWeblog().getRssurl();
309: form.setInboundlinks(result.getWeblog()
310: .getInboundlinks());
311: form.setInboundblogs(result.getWeblog()
312: .getInboundblogs());
313: } catch (Exception e) {
314: logger.debug("Unable to contact Technorati", e);
315: }
316: }
317:
318: ActionErrors errors = new ActionErrors();
319: if (form.getTitle() == null
320: || form.getTitle().trim().length() == 0) {
321: if (technoratiTitle != null
322: && technoratiTitle.trim().length() > 0) {
323: form.setTitle(technoratiTitle);
324: } else {
325: errors.add(null, new ActionError(
326: "planetSubscription.error.title"));
327: }
328: }
329: if (form.getFeedURL() == null
330: || form.getFeedURL().trim().length() == 0) {
331: if (technoratiFeedUrl != null
332: && technoratiFeedUrl.trim().length() > 0) {
333: form.setFeedURL(technoratiFeedUrl);
334: } else {
335: errors.add(null, new ActionError(
336: "planetSubscription.error.feedUrl"));
337: }
338: }
339: if (form.getSiteURL() == null
340: || form.getSiteURL().trim().length() == 0) {
341: errors.add(null, new ActionError(
342: "planetSubscription.error.siteUrl"));
343: }
344: return errors;
345: }
346:
347: /** Page model, includes subscriptions in "external" group */
348: public class SubscriptionsPageModel extends BasePageModel {
349: private List subscriptions = null;
350: private boolean unconfigured = false;
351: private PlanetSubscriptionFormEx form = null;
352:
353: public SubscriptionsPageModel(PlanetGroupData group,
354: HttpServletRequest request,
355: HttpServletResponse response, ActionMapping mapping,
356: PlanetSubscriptionFormEx form) throws RollerException {
357: super ("dummy", request, response, mapping);
358: this .form = form;
359: if (group != null) {
360: Set subsSet = group.getSubscriptions();
361: subscriptions = new ArrayList(subsSet);
362: } else {
363: unconfigured = true;
364: }
365: }
366:
367: public String getTitle() {
368: if (!form.getGroupHandle().equals("external")) {
369: return MessageFormat.format(bundle
370: .getString("planetSubscriptions.titleGroup"),
371: new Object[] { form.getGroupHandle() });
372: } else {
373: return bundle.getString("planetSubscriptions.title");
374: }
375: }
376:
377: public List getSubscriptions() {
378: return subscriptions;
379: }
380:
381: public boolean isUnconfigured() {
382: return unconfigured;
383: }
384: }
385: }
|