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