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.shim;
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.action.ActionForward;
028:
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031:
032: import com.methodhead.auth.AuthAction;
033: import com.methodhead.util.OperationContext;
034: import com.methodhead.sitecontext.SiteContext;
035: import com.methodhead.auth.AuthUser;
036: import com.methodhead.auth.AuthUtil;
037: import com.methodhead.aikp.IntKey;
038: import com.methodhead.util.StrutsUtil;
039: import com.methodhead.event.Event;
040: import org.apache.commons.lang.StringUtils;
041:
042: public class ModuleAction extends AuthAction {
043:
044: // constructors /////////////////////////////////////////////////////////////
045:
046: // constants ////////////////////////////////////////////////////////////////
047:
048: // classes //////////////////////////////////////////////////////////////////
049:
050: // methods //////////////////////////////////////////////////////////////////
051:
052: /**
053: * Populates the action form for the text module at <tt>form.pageid</tt> and
054: * <tt>form.panel</tt>.
055: */
056: protected ActionForward doConfigureTextModuleForm(
057: OperationContext op, ShimPolicy policy) throws Exception {
058:
059: String msg = policy.isConfigureTextModuleAuthorized(op);
060: if (msg != null) {
061: StrutsUtil.addMessage(op.request, msg, null, null, null);
062: return op.mapping.findForward("accessDenied");
063: }
064:
065: TextModule textModule = policy.newTextModule();
066: Page page = policy.newPage();
067: page.setSiteContext(SiteContext.getContext(op.request));
068:
069: page.load(new IntKey(op.form.get("pageid")));
070: textModule.init(page, (String) op.form.get("panel"));
071: textModule.load();
072:
073: op.form.set("text", textModule.getString("value"));
074:
075: if (textModule.getInt("htmlfragment_id") != 0) {
076: op.form.set("usefragment", "yes");
077: op.form.set("fragment", ""
078: + textModule.getInt("htmlfragment_id"));
079: }
080:
081: return op.mapping.findForward("form");
082: }
083:
084: /**
085: * Updates the text model at <tt>form.pageid</tt> and
086: * <tt>form.panel</tt>.
087: */
088: protected ActionForward doConfigureTextModule(OperationContext op,
089: ShimPolicy policy) throws Exception {
090:
091: String msg = policy.isConfigureTextModuleAuthorized(op);
092: if (msg != null) {
093: StrutsUtil.addMessage(op.request, msg, null, null, null);
094: return op.mapping.findForward("accessDenied");
095: }
096:
097: TextModule textModule = policy.newTextModule();
098: Page page = policy.newPage();
099: page.setSiteContext(SiteContext.getContext(op.request));
100:
101: page.load(new IntKey(op.form.get("pageid")));
102: textModule.init(page, (String) op.form.get("panel"));
103: textModule.load();
104:
105: textModule.setString("value", (String) op.form.get("text"));
106:
107: if (!StringUtils.isBlank((String) op.form.get("usefragment"))) {
108: textModule.setInt("htmlfragment_id", Integer
109: .parseInt((String) op.form.get("fragment")));
110: } else {
111: textModule.setInt("htmlfragment_id", 0);
112: }
113:
114: textModule.save();
115:
116: Event.log(SiteContext.getContext(op.request), op.user
117: .getLogin(), "shim",
118: "Configured text module for page \""
119: + page.getString("title") + "\" and panel \""
120: + op.form.get("panel") + "\".");
121:
122: return new ActionForward("/editPage.do?id=" + page.getInt("id"));
123: }
124:
125: /**
126: * Populates the action form for the text module at <tt>form.pageid</tt> and
127: * <tt>form.panel</tt>.
128: */
129: protected ActionForward doConfigureNavModuleForm(
130: OperationContext op, ShimPolicy policy) throws Exception {
131:
132: String msg = policy.isConfigureNavModuleAuthorized(op);
133: if (msg != null) {
134: StrutsUtil.addMessage(op.request, msg, null, null, null);
135: return op.mapping.findForward("accessDenied");
136: }
137:
138: NavModule navModule = policy.newNavModule();
139: Page page = policy.newPage();
140: page.setSiteContext(SiteContext.getContext(op.request));
141:
142: page.load(new IntKey(op.form.get("pageid")));
143: navModule.init(page, (String) op.form.get("panel"));
144: navModule.load();
145:
146: if (NavModule.TYPE_TEXTSEPARATED.equals(navModule
147: .getString("type")))
148: op.form.set("type", "textseparated");
149: else if (NavModule.TYPE_JSP.equals(navModule.getString("type")))
150: op.form.set("type", "jsp");
151: else if (NavModule.TYPE_FOLDING.equals(navModule
152: .getString("type")))
153: op.form.set("type", "folding");
154:
155: op.form.set("header", navModule.getString("header"));
156: op.form.set("link", navModule.getString("link"));
157: op.form.set("curlink", navModule.getString("curlink"));
158: op.form.set("footer", navModule.getString("footer"));
159:
160: op.form.set("separator", navModule.getString("separator"));
161: op.form.set("hideroot",
162: navModule.getBoolean("hideroot") ? "yes" : "");
163: op.form.set("jsp", navModule.getString("jsp"));
164:
165: op.form.set("startlev", "" + navModule.getInt("startlev"));
166: op.form.set("contextlev", "" + navModule.getInt("contextlev"));
167: op.form.set("depthlev", "" + navModule.getInt("depthlev"));
168: op.form.set("toplev", "" + navModule.getInt("toplev"));
169:
170: return op.mapping.findForward("form");
171: }
172:
173: /**
174: * Updates the text model at <tt>form.pageid</tt> and
175: * <tt>form.panel</tt>.
176: */
177: protected ActionForward doConfigureNavModule(OperationContext op,
178: ShimPolicy policy) throws Exception {
179:
180: String msg = policy.isConfigureNavModuleAuthorized(op);
181: if (msg != null) {
182: StrutsUtil.addMessage(op.request, msg, null, null, null);
183: return op.mapping.findForward("accessDenied");
184: }
185:
186: //
187: // load the page
188: //
189: Page page = policy.newPage();
190: page.setSiteContext(SiteContext.getContext(op.request));
191: page.load(new IntKey(op.form.get("pageid")));
192:
193: //
194: // load the nav module
195: //
196: NavModule navModule = policy.newNavModule();
197: navModule.init(page, (String) op.form.get("panel"));
198: navModule.load();
199:
200: //
201: // set common fields
202: //
203: navModule.setString("header", (String) op.form.get("header"));
204: navModule.setString("link", (String) op.form.get("link"));
205: navModule.setString("curlink", (String) op.form.get("curlink"));
206: navModule.setString("footer", (String) op.form.get("footer"));
207:
208: //
209: // set type-specific fields
210: //
211: if ("textseparated".equals(op.form.get("type"))) {
212:
213: //
214: // text-separated fields
215: //
216: navModule.setString("type", NavModule.TYPE_TEXTSEPARATED);
217: navModule.setString("separator", (String) op.form
218: .get("separator"));
219: navModule.setBoolean("hideroot", !StringUtils
220: .isBlank((String) op.form.get("hideroot")));
221: } else if ("jsp".equals(op.form.get("type"))) {
222:
223: //
224: // jsp fields
225: //
226: navModule.setString("type", NavModule.TYPE_JSP);
227: navModule.setString("jsp", (String) op.form.get("jsp"));
228: } else if ("folding".equals(op.form.get("type"))) {
229:
230: //
231: // folding fields
232: //
233: navModule.setString("type", NavModule.TYPE_FOLDING);
234: navModule.setInt("startlev", Integer
235: .parseInt((String) op.form.get("startlev")));
236: navModule.setInt("contextlev", Integer
237: .parseInt((String) op.form.get("contextlev")));
238: navModule.setInt("depthlev", Integer
239: .parseInt((String) op.form.get("depthlev")));
240: navModule.setInt("toplev", Integer
241: .parseInt((String) op.form.get("toplev")));
242: } else
243: throw new ShimException("Unexpected nav type \""
244: + op.form.get("type") + "\"");
245:
246: //
247: // save the module
248: //
249: navModule.save();
250:
251: //
252: // log the event
253: //
254: Event.log(SiteContext.getContext(op.request), op.user
255: .getLogin(), "shim",
256: "Configured nav module for page \""
257: + page.getString("title") + "\" and panel \""
258: + op.form.get("panel") + "\".");
259:
260: return new ActionForward("/editPage.do?id=" + page.getInt("id"));
261: }
262:
263: /**
264: * Populates the action form for the text module at <tt>form.pageid</tt> and
265: * <tt>form.panel</tt>.
266: */
267: protected ActionForward doConfigureIncludeModuleForm(
268: OperationContext op, ShimPolicy policy) throws Exception {
269:
270: String msg = policy.isConfigureIncludeModuleAuthorized(op);
271: if (msg != null) {
272: StrutsUtil.addMessage(op.request, msg, null, null, null);
273: return op.mapping.findForward("accessDenied");
274: }
275:
276: IncludeModule includeModule = policy.newIncludeModule();
277: Page page = policy.newPage();
278: page.setSiteContext(SiteContext.getContext(op.request));
279:
280: page.load(new IntKey(op.form.get("pageid")));
281: includeModule.init(page, (String) op.form.get("panel"));
282: includeModule.load();
283:
284: op.form.set("jsp", includeModule.getString("jsp"));
285:
286: return op.mapping.findForward("form");
287: }
288:
289: /**
290: * Updates the text model at <tt>form.pageid</tt> and
291: * <tt>form.panel</tt>.
292: */
293: protected ActionForward doConfigureIncludeModule(
294: OperationContext op, ShimPolicy policy) throws Exception {
295:
296: String msg = policy.isConfigureIncludeModuleAuthorized(op);
297: if (msg != null) {
298: StrutsUtil.addMessage(op.request, msg, null, null, null);
299: return op.mapping.findForward("accessDenied");
300: }
301:
302: IncludeModule includeModule = policy.newIncludeModule();
303: Page page = policy.newPage();
304: page.setSiteContext(SiteContext.getContext(op.request));
305:
306: page.load(new IntKey(op.form.get("pageid")));
307: includeModule.init(page, (String) op.form.get("panel"));
308: includeModule.load();
309:
310: includeModule.setString("jsp", (String) op.form.get("jsp"));
311: includeModule.save();
312:
313: Event.log(SiteContext.getContext(op.request), op.user
314: .getLogin(), "shim",
315: "Configured include module for page \""
316: + page.getString("title") + "\" and panel \""
317: + op.form.get("panel") + "\".");
318:
319: return new ActionForward("/editPage.do?id=" + page.getInt("id"));
320: }
321:
322: public ActionForward doExecute(ActionMapping mapping,
323: ActionForm form, HttpServletRequest request,
324: HttpServletResponse response) throws Exception {
325:
326: //
327: // set up the operation context
328: //
329: DynaActionForm dynaForm = (DynaActionForm) form;
330: ShimPolicy policy = (ShimPolicy) StrutsUtil.getPolicy(mapping);
331: AuthUser user = AuthUtil.getUser(request);
332:
333: OperationContext op = new OperationContext(mapping,
334: (DynaActionForm) form, request, response, AuthUtil
335: .getUser(request));
336:
337: //
338: // mapping authorized?
339: //
340: if (!policy.isMappingAuthorized(user, mapping.getPath()))
341: return mapping.findForward("accessDenied");
342:
343: //
344: // cancelling?
345: //
346: if (!StringUtils.isBlank((String) op.form.get("cancel")))
347: return new ActionForward("/editPage.do?id="
348: + op.form.get("pageid"));
349:
350: //
351: // execute the appopriate method
352: //
353: if (mapping.getPath().equals("/configureTextModuleForm")) {
354: return doConfigureTextModuleForm(op, policy);
355: }
356: if (mapping.getPath().equals("/configureTextModule")) {
357: return doConfigureTextModule(op, policy);
358: }
359: if (mapping.getPath().equals("/configureNavModuleForm")) {
360: return doConfigureNavModuleForm(op, policy);
361: }
362: if (mapping.getPath().equals("/configureNavModule")) {
363: return doConfigureNavModule(op, policy);
364: }
365: if (mapping.getPath().equals("/configureIncludeModuleForm")) {
366: return doConfigureIncludeModuleForm(op, policy);
367: }
368: if (mapping.getPath().equals("/configureIncludeModule")) {
369: return doConfigureIncludeModule(op, policy);
370: }
371:
372: throw new Exception("Unexpected mapping path \""
373: + mapping.getPath() + "\"");
374: }
375:
376: // properties ///////////////////////////////////////////////////////////////
377:
378: // attributes ///////////////////////////////////////////////////////////////
379: }
|