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.sitecontext;
022:
023: import org.apache.struts.action.Action;
024: import org.apache.struts.action.ActionMapping;
025: import org.apache.struts.action.ActionForm;
026: import org.apache.struts.action.DynaActionForm;
027: import org.apache.struts.validator.DynaValidatorForm;
028: import org.apache.struts.action.ActionForward;
029: import com.methodhead.auth.AuthAction;
030:
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033:
034: import com.methodhead.auth.AuthUtil;
035: import com.methodhead.auth.AuthUser;
036: import com.methodhead.util.OperationContext;
037: import com.methodhead.MhfPolicy;
038: import com.methodhead.util.StrutsUtil;
039: import java.util.Collections;
040: import java.util.List;
041: import java.util.Iterator;
042: import org.apache.commons.lang.StringUtils;
043: import com.methodhead.aikp.AikpAction;
044: import com.methodhead.aikp.IntKey;
045: import com.methodhead.aikp.AutoIntKeyPersistable;
046: import com.methodhead.event.Event;
047:
048: public class SiteContextAction extends AikpAction {
049:
050: // constructors /////////////////////////////////////////////////////////////
051:
052: // constants ////////////////////////////////////////////////////////////////
053:
054: // classes //////////////////////////////////////////////////////////////////
055:
056: // methods //////////////////////////////////////////////////////////////////
057:
058: /**
059: * NOT UNIT TESTED Returns a new <tt>SiteContext</tt>.
060: */
061: protected AutoIntKeyPersistable createPersistable(
062: OperationContext op) {
063: return new SiteContext();
064: }
065:
066: /**
067: * Updates site context's domains from <tt>form.domains</tt>.
068: */
069: protected void populatePersistable(
070: AutoIntKeyPersistable persistable, DynaActionForm form) {
071:
072: super .populatePersistable(persistable, form);
073:
074: SiteContext siteContext = (SiteContext) persistable;
075:
076: siteContext.getDomains().clear();
077: siteContext.getDomains().addAll((List) form.get("domains"));
078: }
079:
080: /**
081: * Joins the site context's domain names to a newline-separated string and
082: * sets <tt>form.domainsText</tt> to it.
083: */
084: protected void populateForm(DynaActionForm form,
085: AutoIntKeyPersistable persistable) {
086:
087: super .populateForm(form, persistable);
088:
089: SiteContext siteContext = (SiteContext) persistable;
090:
091: form.set("domainsText", StringUtils.join(siteContext
092: .getDomains().iterator(), "\n"));
093: }
094:
095: /**
096: * Fully loads each site context loaded by the default behavior and sorts the
097: * resulting list.
098: */
099: protected ActionForward doList(OperationContext op, Object policy) {
100:
101: String msg = ((SiteContextPolicy) policy)
102: .isSiteContextListAuthorized(op);
103: if (msg != null) {
104: StrutsUtil.addMessage(op.request, msg, null, null, null);
105: return op.mapping.findForward("accessDenied");
106: }
107:
108: ActionForward forward = super .doList(op, policy);
109:
110: List list = (List) op.form.get("list");
111: for (Iterator iter = list.iterator(); iter.hasNext();) {
112: SiteContext siteContext = (SiteContext) iter.next();
113: siteContext.load(new IntKey(siteContext.get("id")));
114: }
115:
116: Collections.sort(list);
117:
118: return forward;
119: }
120:
121: /**
122: * Performs policy check and event logging.
123: */
124: protected ActionForward doDelete(OperationContext op, Object policy) {
125:
126: String msg = ((SiteContextPolicy) policy)
127: .isSiteContextDeleteAuthorized(op);
128: if (msg != null) {
129: StrutsUtil.addMessage(op.request, msg, null, null, null);
130: return op.mapping.findForward("accessDenied");
131: }
132:
133: //
134: // get the site context so we can get the name to log
135: //
136: SiteContext siteContext = (SiteContext) createPersistable(op);
137: siteContext.load(new IntKey(op.form.get("id")));
138:
139: ActionForward forward = super .doDelete(op, policy);
140:
141: Event.log(SiteContext.getContext(op.request), op.user
142: .getLogin(), "shim", "Deleted site context \""
143: + siteContext.getDomains().get(0) + "\".");
144:
145: return forward;
146: }
147:
148: /**
149: * Performs policy check and event logging.
150: */
151: protected ActionForward doSaveNew(OperationContext op, Object policy) {
152:
153: String msg = ((SiteContextPolicy) policy)
154: .isSiteContextSaveNewAuthorized(op);
155: if (msg != null) {
156: StrutsUtil.addMessage(op.request, msg, null, null, null);
157: return op.mapping.findForward("accessDenied");
158: }
159:
160: ActionForward forward = super .doSaveNew(op, policy);
161:
162: SiteContext siteContext = (SiteContext) createPersistable(op);
163: siteContext.load(new IntKey(op.form.get("id")));
164:
165: Event.log(SiteContext.getContext(op.request), op.user
166: .getLogin(), "shim", "Created site context \""
167: + siteContext.getDomains().get(0) + "\".");
168:
169: return forward;
170: }
171:
172: /**
173: * Performs policy check and event logging.
174: */
175: protected ActionForward doEdit(OperationContext op, Object policy) {
176:
177: String msg = ((SiteContextPolicy) policy)
178: .isSiteContextEditAuthorized(op);
179: if (msg != null) {
180: StrutsUtil.addMessage(op.request, msg, null, null, null);
181: return op.mapping.findForward("accessDenied");
182: }
183:
184: return super .doEdit(op, policy);
185: }
186:
187: /**
188: * Performs policy check and event logging.
189: */
190: protected ActionForward doNew(OperationContext op, Object policy) {
191:
192: String msg = ((SiteContextPolicy) policy)
193: .isSiteContextNewAuthorized(op);
194: if (msg != null) {
195: StrutsUtil.addMessage(op.request, msg, null, null, null);
196: return op.mapping.findForward("accessDenied");
197: }
198:
199: return super .doNew(op, policy);
200: }
201:
202: /**
203: * Performs policy check and event logging.
204: */
205: protected ActionForward doSave(OperationContext op, Object policy) {
206:
207: String msg = ((SiteContextPolicy) policy)
208: .isSiteContextSaveAuthorized(op);
209: if (msg != null) {
210: StrutsUtil.addMessage(op.request, msg, null, null, null);
211: return op.mapping.findForward("accessDenied");
212: }
213:
214: ActionForward forward = super .doSave(op, policy);
215:
216: SiteContext siteContext = (SiteContext) createPersistable(op);
217: siteContext.load(new IntKey(op.form.get("id")));
218:
219: Event.log(SiteContext.getContext(op.request), op.user
220: .getLogin(), "shim", "Updated site context \""
221: + siteContext.getDomains().get(0) + "\".");
222:
223: return forward;
224: }
225:
226: /**
227: * Returns the <tt>list</tt> forward.
228: */
229: protected ActionForward doCancelSave(OperationContext op,
230: Object policy) {
231:
232: return new ActionForward("/siteContext.do?action=list");
233: }
234:
235: // properties ///////////////////////////////////////////////////////////////
236:
237: // attributes ///////////////////////////////////////////////////////////////
238: }
|