1 16 package org.roller.presentation.planet; 17 18 import java.io.IOException ; 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 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.PlanetGroupData; 41 import org.roller.presentation.BasePageModel; 42 import org.roller.presentation.RollerRequest; 43 import org.roller.presentation.forms.PlanetGroupForm; 44 45 46 56 public final class PlanetGroupsAction extends DispatchAction 57 { 58 private static Log logger = LogFactory.getFactory().getInstance( 59 PlanetGroupsAction.class); 60 61 62 public ActionForward getGroups(ActionMapping mapping, 63 ActionForm actionForm, HttpServletRequest request, 64 HttpServletResponse response) throws IOException , ServletException 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 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 e) 94 { 95 request.getSession().getServletContext().log("ERROR", e); 96 throw new ServletException (e); 97 } 98 return forward; 99 } 100 101 102 public ActionForward cancelEditing(ActionMapping mapping, 103 ActionForm actionForm, HttpServletRequest request, 104 HttpServletResponse response) throws IOException , ServletException 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 e) 127 { 128 request.getSession().getServletContext().log("ERROR", e); 129 throw new ServletException (e); 130 } 131 return forward; 132 } 133 134 135 public ActionForward deleteGroup(ActionMapping mapping, 136 ActionForm actionForm, HttpServletRequest request, 137 HttpServletResponse response) throws IOException , ServletException 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 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 182 public ActionForward saveGroup(ActionMapping mapping, 183 ActionForm actionForm, HttpServletRequest request, 184 HttpServletResponse response) throws IOException , ServletException 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 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 262 public class GroupsPageModel extends BasePageModel 263 { 264 private List groups = new ArrayList (); 265 private boolean unconfigured = false; 266 public GroupsPageModel( 267 HttpServletRequest request, 268 HttpServletResponse 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 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 getGroups() 295 { 296 return groups; 297 } 298 public boolean isUnconfigured() 299 { 300 return unconfigured; 301 } 302 } 303 } 304 | Popular Tags |