001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.transfer;
022:
023: import org.apache.struts.action.ActionForward;
024: import org.apache.struts.action.DynaActionForm;
025: import org.apache.struts.action.ActionForm;
026: import org.apache.struts.action.ActionMapping;
027: import org.apache.commons.lang.StringUtils;
028: import java.util.Iterator;
029: import java.util.List;
030: import java.util.ArrayList;
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033: import com.methodhead.sitecontext.SiteContextPolicy;
034: import com.methodhead.sitecontext.SiteContext;
035: import com.methodhead.persistable.Persistable;
036: import com.methodhead.util.StrutsUtil;
037: import com.methodhead.util.OperationContext;
038: import com.methodhead.reg.User;
039: import com.methodhead.reg.RegPolicy;
040: import com.methodhead.aikp.IntKey;
041: import com.methodhead.aikp.AutoIntKeyPersistable;
042: import com.methodhead.auth.AuthUser;
043: import com.methodhead.auth.AuthUtil;
044: import com.methodhead.event.Event;
045:
046: /**
047: * Subclasses SiteContextAction to disassociate a user from a site context when
048: * it is deleted.
049: */
050: public class SiteContextAction extends
051: com.methodhead.shim.SiteContextAction {
052:
053: // constructors /////////////////////////////////////////////////////////////
054:
055: // constants ////////////////////////////////////////////////////////////////
056:
057: // classes //////////////////////////////////////////////////////////////////
058:
059: // methods //////////////////////////////////////////////////////////////////
060:
061: /**
062: * Extends the default behavior to disassociate any users from the site being
063: * deleted.
064: */
065: public ActionForward doDelete(OperationContext op, Object policy) {
066:
067: //
068: // authorized?
069: //
070: String msg = ((SiteContextPolicy) policy)
071: .isSiteContextDeleteAuthorized(op);
072: if (msg != null) {
073: StrutsUtil.addMessage(op.request, msg, null, null, null);
074: return op.mapping.findForward("accessDenied");
075: }
076:
077: //
078: // disassociate any users from this context
079: //
080: SiteContext siteContext = (SiteContext) createPersistable(op);
081: siteContext.load(new IntKey(op.form.get("id")));
082:
083: User user = new User();
084: List users = user.loadAllForSiteContext(siteContext);
085:
086: for (Iterator iter = users.iterator(); iter.hasNext();) {
087: User u = (User) iter.next();
088:
089: throw new RuntimeException("FIX THIS");
090: /*
091: for ( Iterator iter2 = u.getSiteContexts().iterator(); iter2.hasNext(); ) {
092: SiteContext sc = ( SiteContext )iter2.next();
093:
094: if ( sc.equals( siteContext ) ) {
095: iter2.remove();
096: }
097: }
098:
099: u.save();
100: */
101: }
102:
103: //
104: // invoke default behavior
105: //
106: ActionForward forward = super .doDelete(op, policy);
107:
108: return forward;
109: }
110:
111: /**
112: * Initializes the extension <tt>form.classname</tt> for site context with
113: * the id <tt>form.id</tt> and returns a forward to edit the site context.
114: */
115: public ActionForward doInitExtension(OperationContext op,
116: TransferPolicy policy) {
117:
118: //
119: // authorized?
120: //
121: String msg = policy.isInitExtensionAuthorized(op);
122: if (msg != null) {
123: StrutsUtil.addMessage(op.request, msg, null, null, null);
124: return op.mapping.findForward("accessDenied");
125: }
126:
127: //
128: // load the site context
129: //
130: SiteContext siteContext = new SiteContext();
131: siteContext.load(new IntKey(op.form.get("id")));
132:
133: //
134: // instantiate the extension
135: //
136: SiteExtension siteExtension = policy.newSiteExtension();
137: Extension extension = siteExtension
138: .instantiateExtension((String) op.form.get("classname"));
139:
140: //
141: // call it's init methodhead
142: //
143: extension.init(siteContext);
144:
145: //
146: // save the site extension
147: //
148: siteExtension.setString("class_name", (String) op.form
149: .get("classname"));
150: siteExtension
151: .setInt("sitecontext_id", siteContext.getInt("id"));
152: siteExtension.setBoolean("enabled", true);
153: siteExtension.saveNew();
154:
155: //
156: // log the event
157: //
158: Event.log(SiteContext.getDefaultContext(), op.user.getLogin(),
159: "sitecontext", "Initialized extension \""
160: + extension.getName() + "\" for site \""
161: + siteContext + "\".");
162:
163: //
164: // return a forward to edit
165: //
166: return new ActionForward("/siteContext.do?action=edit&id="
167: + siteContext.getInt("id"));
168: }
169:
170: /**
171: * Destroys the extension <tt>form.classname</tt> for site context with
172: * the id <tt>form.id</tt> and returns a forward to edit the site context.
173: */
174: public ActionForward doDestroyExtension(OperationContext op,
175: TransferPolicy policy) {
176:
177: //
178: // authorized?
179: //
180: String msg = policy.isDestroyExtensionAuthorized(op);
181: if (msg != null) {
182: StrutsUtil.addMessage(op.request, msg, null, null, null);
183: return op.mapping.findForward("accessDenied");
184: }
185:
186: //
187: // cancelled?
188: //
189: if (StringUtils.isNotBlank((String) op.form.get("cancel"))) {
190: return new ActionForward("/siteContext.do?action=edit&id="
191: + op.form.get("id"));
192: }
193:
194: //
195: // instantiate the extension
196: //
197: SiteExtension siteExtension = policy.newSiteExtension();
198: Extension extension = siteExtension
199: .instantiateExtension((String) op.form.get("classname"));
200:
201: //
202: // confirmed?
203: //
204: if (StringUtils.isBlank((String) op.form.get("confirm"))) {
205: StrutsUtil.addMessage(op.request,
206: "transfer.confirmDestroyExtension", extension
207: .getName(), null, null);
208:
209: return op.mapping.findForward("confirm");
210: }
211:
212: //
213: // load the site context
214: //
215: SiteContext siteContext = new SiteContext();
216: siteContext.load(new IntKey(op.form.get("id")));
217:
218: //
219: // call it's destroy methodhead
220: //
221: extension.destroy(siteContext);
222:
223: //
224: // delete the extension
225: //
226: siteExtension.deleteAll("sitecontext_id="
227: + siteContext.getInt("id")
228: + " AND class_name="
229: + Persistable.getSqlLiteral((String) op.form
230: .get("classname")));
231:
232: //
233: // log the event
234: //
235: Event.log(SiteContext.getDefaultContext(), op.user.getLogin(),
236: "sitecontext", "Destroyed extension \""
237: + extension.getName() + "\" for site \""
238: + siteContext + "\".");
239:
240: //
241: // return a forward to edit
242: //
243: return new ActionForward("/siteContext.do?action=edit&id="
244: + siteContext.getInt("id"));
245: }
246:
247: /**
248: * Disables the extension <tt>form.classname</tt> for site context with
249: * the id <tt>form.id</tt> and returns a forward to edit the site context.
250: */
251: public ActionForward doDisableExtension(OperationContext op,
252: TransferPolicy policy) {
253:
254: //
255: // authorized?
256: //
257: String msg = policy.isDisableExtensionAuthorized(op);
258: if (msg != null) {
259: StrutsUtil.addMessage(op.request, msg, null, null, null);
260: return op.mapping.findForward("accessDenied");
261: }
262:
263: //
264: // load the site context
265: //
266: SiteContext siteContext = new SiteContext();
267: siteContext.load(new IntKey(op.form.get("id")));
268:
269: //
270: // disable the site extension
271: //
272: SiteExtension siteExtension = policy.newSiteExtension();
273: siteExtension.load(siteContext, (String) op.form
274: .get("classname"));
275: siteExtension.setBoolean("enabled", false);
276: siteExtension.save();
277:
278: //
279: // log the event
280: //
281: Extension extension = siteExtension
282: .instantiateExtension(siteExtension
283: .getString("class_name"));
284:
285: Event.log(SiteContext.getDefaultContext(), op.user.getLogin(),
286: "sitecontext", "Disabled extension \""
287: + extension.getName() + "\" for site \""
288: + siteContext + "\".");
289:
290: //
291: // return a forward to edit
292: //
293: return new ActionForward("/siteContext.do?action=edit&id="
294: + siteContext.getInt("id"));
295: }
296:
297: /**
298: * Enables the extension <tt>form.classname</tt> for site context with
299: * the id <tt>form.id</tt> and returns a forward to edit the site context.
300: */
301: public ActionForward doEnableExtension(OperationContext op,
302: TransferPolicy policy) {
303:
304: //
305: // authorized?
306: //
307: String msg = policy.isEnableExtensionAuthorized(op);
308: if (msg != null) {
309: StrutsUtil.addMessage(op.request, msg, null, null, null);
310: return op.mapping.findForward("accessDenied");
311: }
312:
313: //
314: // load the site context
315: //
316: SiteContext siteContext = new SiteContext();
317: siteContext.load(new IntKey(op.form.get("id")));
318:
319: //
320: // enable the site extension
321: //
322: SiteExtension siteExtension = policy.newSiteExtension();
323: siteExtension.load(siteContext, (String) op.form
324: .get("classname"));
325: siteExtension.setBoolean("enabled", true);
326: siteExtension.save();
327:
328: //
329: // log the event
330: //
331: Extension extension = siteExtension
332: .instantiateExtension(siteExtension
333: .getString("class_name"));
334:
335: Event.log(SiteContext.getDefaultContext(), op.user.getLogin(),
336: "sitecontext", "Enabled extension \""
337: + extension.getName() + "\" for site \""
338: + siteContext + "\".");
339:
340: //
341: // return a forward to edit
342: //
343: return new ActionForward("/siteContext.do?action=edit&id="
344: + siteContext.getInt("id"));
345: }
346:
347: /**
348: * Extends default behaviour to handle extension-related actions.
349: */
350: public ActionForward doExecute(ActionMapping mapping,
351: ActionForm form, HttpServletRequest request,
352: HttpServletResponse response) {
353:
354: //
355: // get some things we'll need
356: //
357: DynaActionForm dynaForm = (DynaActionForm) form;
358: TransferPolicy policy = (TransferPolicy) StrutsUtil
359: .getPolicy(mapping);
360: AuthUser user = AuthUtil.getUser(request);
361:
362: OperationContext op = new OperationContext(mapping, dynaForm,
363: request, response, user);
364:
365: //
366: // execute the appopriate method
367: //
368: if (mapping.getPath().equals("/initExtension")) {
369: return doInitExtension(op, policy);
370: }
371:
372: if (mapping.getPath().equals("/destroyExtension")) {
373: return doDestroyExtension(op, policy);
374: }
375:
376: if (mapping.getPath().equals("/disableExtension")) {
377: return doDisableExtension(op, policy);
378: }
379:
380: if (mapping.getPath().equals("/enableExtension")) {
381: return doEnableExtension(op, policy);
382: }
383:
384: return super .doExecute(mapping, form, request, response);
385: }
386:
387: // properties ///////////////////////////////////////////////////////////////
388:
389: // attributes ///////////////////////////////////////////////////////////////
390: }
|