1 16 package org.roller.presentation.planet; 17 18 import java.io.IOException ; 19 import java.util.ArrayList ; 20 import java.util.List ; 21 import java.util.Set ; 22 23 import javax.servlet.ServletException ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 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.PlanetConfigData; 41 import org.roller.pojos.PlanetGroupData; 42 import org.roller.pojos.PlanetSubscriptionData; 43 import org.roller.presentation.BasePageModel; 44 import org.roller.presentation.RollerRequest; 45 import org.roller.util.Technorati; 46 47 48 59 public final class PlanetSubscriptionsAction extends DispatchAction 60 { 61 private static Log logger = LogFactory.getFactory().getInstance( 62 PlanetSubscriptionsAction.class); 63 64 65 public ActionForward getSubscriptions(ActionMapping mapping, 66 ActionForm actionForm, HttpServletRequest request, 67 HttpServletResponse response) throws IOException , ServletException 68 { 69 ActionForward forward = mapping.findForward("planetSubscriptions.page"); 70 try 71 { 72 RollerRequest rreq = RollerRequest.getRollerRequest(request); 73 if (rreq.isUserAuthorizedToEdit()) 74 { 75 Roller roller = rreq.getRoller(); 76 PlanetManager planet = roller.getPlanetManager(); 77 PlanetSubscriptionFormEx form = (PlanetSubscriptionFormEx)actionForm; 78 if (request.getParameter("feedUrl") != null) 79 { 80 String feedUrl = request.getParameter("feedUrl"); 81 PlanetSubscriptionData sub = 82 planet.getSubscription(feedUrl); 83 form.copyFrom(sub, request.getLocale()); 84 } 85 else 86 { 87 form.doReset(mapping, request); 88 } 89 90 String groupHandle = request.getParameter("groupHandle"); 91 groupHandle = (groupHandle == null) ? form.getGroupHandle() : groupHandle; 92 groupHandle = (groupHandle == null) ? "external" : groupHandle; 93 94 PlanetGroupData targetGroup = planet.getGroup(groupHandle); 95 form.setGroupHandle(groupHandle); 96 request.setAttribute("model", 97 new SubscriptionsPageModel( 98 targetGroup, request, response, mapping)); 99 } 100 else 101 { 102 forward = mapping.findForward("access-denied"); 103 } 104 } 105 catch (Exception e) 106 { 107 request.getSession().getServletContext().log("ERROR", e); 108 throw new ServletException (e); 109 } 110 return forward; 111 } 112 113 114 public ActionForward cancelEditing(ActionMapping mapping, 115 ActionForm actionForm, HttpServletRequest request, 116 HttpServletResponse response) throws IOException , ServletException 117 { 118 ActionForward forward = mapping.findForward("planetSubscriptions.page"); 119 try 120 { 121 RollerRequest rreq = RollerRequest.getRollerRequest(request); 122 if (rreq.isUserAuthorizedToEdit()) 123 { 124 Roller roller = rreq.getRoller(); 125 PlanetManager planet = roller.getPlanetManager(); 126 PlanetSubscriptionFormEx form = (PlanetSubscriptionFormEx)actionForm; 127 128 form.doReset(mapping, request); 129 130 String groupHandle = request.getParameter("groupHandle"); 131 groupHandle = (groupHandle == null) ? form.getGroupHandle() : groupHandle; 132 groupHandle = (groupHandle == null) ? "external" : groupHandle; 133 134 PlanetGroupData targetGroup = planet.getGroup(groupHandle); 135 form.setGroupHandle(groupHandle); 136 request.setAttribute("model", 137 new SubscriptionsPageModel( 138 targetGroup, request, response, mapping)); 139 } 140 else 141 { 142 forward = mapping.findForward("access-denied"); 143 } 144 } 145 catch (Exception e) 146 { 147 request.getSession().getServletContext().log("ERROR", e); 148 throw new ServletException (e); 149 } 150 return forward; 151 } 152 153 154 public ActionForward deleteSubscription(ActionMapping mapping, 155 ActionForm actionForm, HttpServletRequest request, 156 HttpServletResponse response) throws IOException , ServletException 157 { 158 ActionForward forward = mapping.findForward("planetSubscriptions.page"); 159 try 160 { 161 RollerRequest rreq = RollerRequest.getRollerRequest(request); 162 if (rreq.isUserAuthorizedToEdit()) 163 { 164 Roller roller = rreq.getRoller(); 165 PlanetManager planet = roller.getPlanetManager(); 166 PlanetSubscriptionFormEx form = (PlanetSubscriptionFormEx)actionForm; 167 if (form.getId() != null) 168 { 169 PlanetSubscriptionData sub = 170 planet.getSubscriptionById(form.getId()); 171 172 String groupHandle = request.getParameter("groupHandle"); 173 groupHandle = (groupHandle == null) ? form.getGroupHandle() : groupHandle; 174 groupHandle = (groupHandle == null) ? "external" : groupHandle; 175 176 PlanetGroupData targetGroup = planet.getGroup(groupHandle); 177 178 targetGroup.removeSubscription(sub); 179 planet.deleteSubscription(sub); 180 roller.commit(); 181 roller.release(); 182 183 roller.begin(); 184 form.doReset(mapping, request); 185 186 form.setGroupHandle(groupHandle); 187 request.setAttribute("model", 188 new SubscriptionsPageModel( 189 targetGroup, request, response, mapping)); 190 191 ActionMessages messages = new ActionMessages(); 192 messages.add(null, 193 new ActionMessage("planetSubscription.success.deleted")); 194 saveMessages(request, messages); 195 } 196 } 197 else 198 { 199 forward = mapping.findForward("access-denied"); 200 } 201 } 202 catch (Exception e) 203 { 204 ActionErrors errors = new ActionErrors(); 205 errors.add(null, new ActionError("planetSubscription.error.deleting")); 206 saveErrors(request, errors); 207 } 208 return forward; 209 } 210 211 212 public ActionForward saveSubscription(ActionMapping mapping, 213 ActionForm actionForm, HttpServletRequest request, 214 HttpServletResponse response) throws IOException , ServletException 215 { 216 ActionForward forward = mapping.findForward("planetSubscriptions.page"); 217 try 218 { 219 RollerRequest rreq = RollerRequest.getRollerRequest(request); 220 Roller roller = rreq.getRoller(); 221 PlanetManager planet = roller.getPlanetManager(); 222 PlanetSubscriptionFormEx form = (PlanetSubscriptionFormEx)actionForm; 223 224 String groupHandle = request.getParameter("groupHandle"); 225 groupHandle = (groupHandle == null) ? form.getGroupHandle() : groupHandle; 226 groupHandle = (groupHandle == null) ? "external" : groupHandle; 227 228 PlanetGroupData targetGroup = planet.getGroup(groupHandle); 229 230 if (rreq.isUserAuthorizedToEdit()) 231 { 232 233 PlanetSubscriptionData sub = null; 234 ActionErrors errors = validate(planet, form); 235 if (errors.isEmpty()) 236 { 237 if (form.getId() == null || form.getId().trim().length() == 0) 238 { 239 sub = new PlanetSubscriptionData(); 240 targetGroup.addSubscription(sub); 241 } 242 else 243 { 244 sub = planet.getSubscriptionById(form.getId()); 245 } 246 form.copyTo(sub, request.getLocale()); 247 form.setGroupHandle(groupHandle); 248 planet.saveSubscription(sub); 249 planet.saveGroup(targetGroup); 250 roller.commit(); 251 252 ActionMessages messages = new ActionMessages(); 253 messages.add(null, 254 new ActionMessage("planetSubscription.success.saved")); 255 saveMessages(request, messages); 256 form.doReset(mapping, request); 257 } 258 else 259 { 260 saveErrors(request, errors); 261 } 262 } 263 else 264 { 265 forward = mapping.findForward("access-denied"); 266 } 267 request.setAttribute("model", 268 new SubscriptionsPageModel( 269 targetGroup, request, response, mapping)); 270 } 271 catch (RollerException e) 272 { 273 ActionErrors errors = new ActionErrors(); 274 errors.add(null, new ActionError( 275 "planetSubscriptions.error.duringSave",e.getRootCauseMessage())); 276 saveErrors(request, errors); 277 } 278 return forward; 279 } 280 281 282 private ActionErrors validate( 283 PlanetManager planet, PlanetSubscriptionFormEx form) 284 { 285 String technoratiTitle = null; 286 String technoratiFeedUrl = null; 287 int inboundlinks = -1; 288 int inboundblogs = -1; 289 if (form.getSiteUrl()!=null && form.getSiteUrl().trim().length() > 0) 290 { 291 try 292 { 293 PlanetConfigData config = planet.getConfiguration(); 294 Technorati technorati = null; 295 if (config.getProxyHost()!=null && config.getProxyPort() > 0) 296 { 297 technorati = new Technorati( 298 config.getProxyHost(), config.getProxyPort()); 299 } 300 else 301 { 302 technorati = new Technorati(); 303 } 304 Technorati.Result result = 305 technorati.getBloginfo(form.getSiteUrl()); 306 technoratiTitle = result.getWeblog().getName(); 307 technoratiFeedUrl = result.getWeblog().getRssurl(); 308 form.setInboundlinks(result.getWeblog().getInboundlinks()); 309 form.setInboundblogs(result.getWeblog().getInboundblogs()); 310 } 311 catch (Exception e) 312 { 313 logger.debug("Unable to contact Technorati", e); 314 } 315 } 316 317 ActionErrors errors = new ActionErrors(); 318 if (form.getTitle()==null || form.getTitle().trim().length()==0) 319 { 320 if (technoratiTitle!=null && technoratiTitle.trim().length()>0) 321 { 322 form.setTitle(technoratiTitle); 323 } 324 else 325 { 326 errors.add(null, 327 new ActionError("planetSubscription.error.title")); 328 } 329 } 330 if (form.getFeedUrl()==null || form.getFeedUrl().trim().length()==0) 331 { 332 if (technoratiFeedUrl!=null && technoratiFeedUrl.trim().length()>0) 333 { 334 form.setFeedUrl(technoratiFeedUrl); 335 } 336 else 337 { 338 errors.add(null, 339 new ActionError("planetSubscription.error.feedUrl")); 340 } 341 } 342 if (form.getSiteUrl()==null || form.getSiteUrl().trim().length()==0) 343 { 344 errors.add(null, 345 new ActionError("planetSubscription.error.siteUrl")); 346 } 347 return errors; 348 } 349 350 351 public class SubscriptionsPageModel extends BasePageModel 352 { 353 private List subscriptions = null; 354 private boolean unconfigured = false; 355 public SubscriptionsPageModel( 356 PlanetGroupData group, 357 HttpServletRequest request, 358 HttpServletResponse response, 359 ActionMapping mapping) throws RollerException 360 { 361 super(request, response, mapping); 362 RollerRequest rreq = RollerRequest.getRollerRequest(request); 363 Roller roller = rreq.getRoller(); 364 PlanetManager planet = roller.getPlanetManager(); 365 if (group != null) 366 { 367 Set subsSet = group.getSubscriptions(); 368 subscriptions = new ArrayList (subsSet); 369 } 370 else 371 { 372 unconfigured = true; 373 } 374 } 375 public List getSubscriptions() 376 { 377 return subscriptions; 378 } 379 public boolean isUnconfigured() 380 { 381 return unconfigured; 382 } 383 } 384 } 385 386 | Popular Tags |