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.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import javax.servlet.ServletException;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpServletResponse;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.apache.struts.action.ActionError;
030: import org.apache.struts.action.ActionErrors;
031: import org.apache.struts.action.ActionForm;
032: import org.apache.struts.action.ActionForward;
033: import org.apache.struts.action.ActionMapping;
034: import org.apache.struts.action.ActionMessage;
035: import org.apache.struts.action.ActionMessages;
036: import org.apache.struts.actions.DispatchAction;
037: import org.apache.roller.RollerException;
038: import org.apache.roller.planet.business.PlanetManager;
039: import org.apache.roller.business.Roller;
040: import org.apache.roller.business.RollerFactory;
041: import org.apache.roller.planet.pojos.PlanetGroupData;
042: import org.apache.roller.planet.ui.authoring.struts.forms.PlanetGroupForm;
043: import org.apache.roller.ui.core.BasePageModel;
044: import org.apache.roller.ui.core.RollerRequest;
045: import org.apache.roller.ui.core.RollerSession;
046:
047: /////////////////////////////////////////////////////////////////////////////
048: /**
049: * Add, remove, and view user defined groups.
050: *
051: * @struts.action name="planetGroupForm" path="/roller-ui/admin/planetGroups"
052: * scope="request" parameter="method"
053: *
054: * @struts.action-forward name="planetGroups.page"
055: * path=".PlanetGroups"
056: */
057: public final class PlanetGroupsAction extends DispatchAction {
058: private static Log logger = LogFactory.getFactory().getInstance(
059: PlanetGroupsAction.class);
060:
061: /** Populate page model and forward to subscription page */
062: public ActionForward getGroups(ActionMapping mapping,
063: ActionForm actionForm, HttpServletRequest request,
064: HttpServletResponse response) throws IOException,
065: ServletException {
066: ActionForward forward = mapping
067: .findForward("planetGroups.page");
068: try {
069: RollerRequest rreq = RollerRequest
070: .getRollerRequest(request);
071: if (RollerSession.getRollerSession(request)
072: .isGlobalAdminUser()) {
073: Roller roller = RollerFactory.getRoller();
074: PlanetManager planet = roller.getPlanetManager();
075: PlanetGroupForm form = (PlanetGroupForm) actionForm;
076: if (request.getParameter("groupHandle") != null) {
077: String feedUrl = request
078: .getParameter("groupHandle");
079: PlanetGroupData group = planet.getGroup(feedUrl);
080: form.copyFrom(group, request.getLocale());
081: } else {
082: form.doReset(mapping, request);
083: }
084: request.setAttribute("model", new GroupsPageModel(
085: request, response, mapping));
086: } else {
087: forward = mapping.findForward("access-denied");
088: }
089: } catch (Exception e) {
090: request.getSession().getServletContext().log("ERROR", e);
091: throw new ServletException(e);
092: }
093: return forward;
094: }
095:
096: /** Cancel editing, reset form */
097: public ActionForward cancelEditing(ActionMapping mapping,
098: ActionForm actionForm, HttpServletRequest request,
099: HttpServletResponse response) throws IOException,
100: ServletException {
101: ActionForward forward = mapping
102: .findForward("planetGroups.page");
103: try {
104: if (RollerSession.getRollerSession(request)
105: .isGlobalAdminUser()) {
106: PlanetGroupForm form = (PlanetGroupForm) actionForm;
107: form.doReset(mapping, request);
108:
109: request.setAttribute("model", new GroupsPageModel(
110: request, response, mapping));
111: } else {
112: forward = mapping.findForward("access-denied");
113: }
114: } catch (Exception e) {
115: request.getSession().getServletContext().log("ERROR", e);
116: throw new ServletException(e);
117: }
118: return forward;
119: }
120:
121: /** Delete subscription, reset form */
122: public ActionForward deleteGroup(ActionMapping mapping,
123: ActionForm actionForm, HttpServletRequest request,
124: HttpServletResponse response) throws IOException,
125: ServletException {
126: ActionForward forward = mapping
127: .findForward("planetGroups.page");
128: try {
129: RollerRequest rreq = RollerRequest
130: .getRollerRequest(request);
131: if (RollerSession.getRollerSession(request)
132: .isGlobalAdminUser()) {
133: Roller roller = RollerFactory.getRoller();
134: PlanetManager planet = roller.getPlanetManager();
135: PlanetGroupForm form = (PlanetGroupForm) actionForm;
136: if (form.getHandle() != null) {
137: PlanetGroupData group = planet.getGroup(form
138: .getHandle());
139: planet.deleteGroup(group);
140: roller.flush();
141: // TODO: why release here?
142: roller.release();
143:
144: form.doReset(mapping, request);
145:
146: request.setAttribute("model", new GroupsPageModel(
147: request, response, mapping));
148:
149: ActionMessages messages = new ActionMessages();
150: messages.add(null, new ActionMessage(
151: "planetSubscription.success.deleted"));
152: saveMessages(request, messages);
153: }
154: } else {
155: forward = mapping.findForward("access-denied");
156: }
157: } catch (Exception e) {
158: ActionErrors errors = new ActionErrors();
159: errors.add(null, new ActionError(
160: "planetGroup.error.deleting"));
161: saveErrors(request, errors);
162: }
163: return forward;
164: }
165:
166: /** Save subscription, add to "external" group */
167: public ActionForward saveGroup(ActionMapping mapping,
168: ActionForm actionForm, HttpServletRequest request,
169: HttpServletResponse response) throws IOException,
170: ServletException {
171: ActionForward forward = mapping
172: .findForward("planetGroups.page");
173: try {
174: RollerRequest rreq = RollerRequest
175: .getRollerRequest(request);
176: if (RollerSession.getRollerSession(request)
177: .isGlobalAdminUser()) {
178: PlanetGroupForm form = (PlanetGroupForm) actionForm;
179: Roller roller = RollerFactory.getRoller();
180: PlanetManager planet = roller.getPlanetManager();
181: ActionErrors errors = validate(planet, form);
182: if (errors.isEmpty()) {
183: PlanetGroupData group = null;
184: if (form.getId() == null
185: || form.getId().trim().length() == 0) {
186: group = new PlanetGroupData();
187: } else {
188: group = planet.getGroupById(form.getId());
189: }
190: form.copyTo(group, request.getLocale());
191: planet.saveGroup(group);
192: roller.flush();
193:
194: ActionMessages messages = new ActionMessages();
195: messages.add(null, new ActionMessage(
196: "planetGroups.success.saved"));
197: saveMessages(request, messages);
198: form.doReset(mapping, request);
199:
200: request.setAttribute("model", new GroupsPageModel(
201: request, response, mapping));
202: } else {
203: saveErrors(request, errors);
204: }
205: } else {
206: forward = mapping.findForward("access-denied");
207: }
208: } catch (RollerException e) {
209: ActionErrors errors = new ActionErrors();
210: errors.add(null, new ActionError(
211: "planetSubscriptions.error.duringSave", e
212: .getRootCauseMessage()));
213: saveErrors(request, errors);
214: }
215: return forward;
216: }
217:
218: /** Validate posted group */
219: private ActionErrors validate(PlanetManager planet,
220: PlanetGroupForm form) {
221: ActionErrors errors = new ActionErrors();
222: if (form.getTitle() == null
223: || form.getTitle().trim().length() == 0) {
224: errors.add(null,
225: new ActionError("planetGroups.error.title"));
226: }
227: if (form.getHandle() == null
228: || form.getHandle().trim().length() == 0) {
229: errors.add(null, new ActionError(
230: "planetGroups.error.handle"));
231: }
232: if (form.getHandle() != null
233: && (form.getHandle().equals("all") || form.getHandle()
234: .equals("external"))) {
235: errors.add(null, new ActionError(
236: "planetGroups.error.nameReserved"));
237: }
238: return errors;
239: }
240:
241: /** Page model */
242: public class GroupsPageModel extends BasePageModel {
243: private List groups = new ArrayList();
244: private boolean unconfigured = false;
245:
246: public GroupsPageModel(HttpServletRequest request,
247: HttpServletResponse response, ActionMapping mapping)
248: throws RollerException {
249: super ("planetGroups.pagetitle", request, response, mapping);
250: RollerRequest rreq = RollerRequest
251: .getRollerRequest(request);
252: Roller roller = RollerFactory.getRoller();
253: PlanetManager planet = roller.getPlanetManager();
254: PlanetGroupData externalGroup = planet.getGroup("external");
255: if (externalGroup != null) {
256: Iterator allgroups = planet.getGroups().iterator();
257: while (allgroups.hasNext()) {
258: PlanetGroupData agroup = (PlanetGroupData) allgroups
259: .next();
260: if (!agroup.getHandle().equals("external")
261: && !agroup.getHandle().equals("all")) {
262: groups.add(agroup);
263: }
264: }
265: } else {
266: unconfigured = true;
267: }
268: }
269:
270: public List getGroups() {
271: return groups;
272: }
273:
274: public boolean isUnconfigured() {
275: return unconfigured;
276: }
277: }
278: }
|