0001: /*
0002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
0003: *
0004: * This file is part of TransferCM.
0005: *
0006: * TransferCM is free software; you can redistribute it and/or modify it under the
0007: * terms of the GNU General Public License as published by the Free Software
0008: * Foundation; either version 2 of the License, or (at your option) any later
0009: * version.
0010: *
0011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
0012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
0013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
0014: * details.
0015: *
0016: * You should have received a copy of the GNU General Public License along with
0017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
0018: * Fifth Floor, Boston, MA 02110-1301 USA
0019: */
0020:
0021: package com.methodhead.shim;
0022:
0023: import org.apache.struts.action.Action;
0024: import org.apache.struts.action.ActionMapping;
0025: import org.apache.struts.action.ActionForm;
0026: import org.apache.struts.action.DynaActionForm;
0027: import org.apache.struts.action.ActionForward;
0028:
0029: import com.methodhead.auth.AuthAction;
0030:
0031: import javax.servlet.RequestDispatcher;
0032: import javax.servlet.http.HttpServletRequest;
0033: import javax.servlet.http.HttpServletResponse;
0034: import com.methodhead.auth.AuthUser;
0035: import com.methodhead.auth.AuthUtil;
0036: import com.methodhead.sitecontext.SiteContext;
0037: import com.methodhead.util.OperationContext;
0038: import java.util.Iterator;
0039: import com.methodhead.aikp.IntKey;
0040: import com.methodhead.util.StrutsUtil;
0041: import org.apache.struts.action.ActionMessages;
0042: import org.apache.commons.lang.StringUtils;
0043: import com.methodhead.tree.FoldingTreeNode;
0044: import com.methodhead.tree.Tree;
0045: import java.util.List;
0046: import java.util.ArrayList;
0047: import java.util.Enumeration;
0048: import com.methodhead.event.Event;
0049: import com.methodhead.persistable.PersistableException;
0050: import org.apache.log4j.Logger;
0051:
0052: public class PageAction extends AuthAction {
0053:
0054: // constructors /////////////////////////////////////////////////////////////
0055:
0056: // constants ////////////////////////////////////////////////////////////////
0057:
0058: // classes //////////////////////////////////////////////////////////////////
0059:
0060: // methods //////////////////////////////////////////////////////////////////
0061:
0062: /**
0063: * Destroys any modules associated with <tt>page</tt> and then deletes it.
0064: * <tt>page</tt> should be fully loaded with {@link Page#loadFull loadFull()}
0065: * before calling this method.
0066: */
0067: private void deletePage(Page page) {
0068:
0069: //
0070: // destroy any modules in this page
0071: //
0072: for (Iterator iter = page.getPanels().values().iterator(); iter
0073: .hasNext();) {
0074:
0075: Panel panel = (Panel) iter.next();
0076: panel.getModule().destroy();
0077: }
0078:
0079: //
0080: // delete the page
0081: //
0082: page.delete();
0083: }
0084:
0085: /**
0086: * Instantiates the module for <tt>className</tt>.
0087: */
0088: protected static Module instantiateModule(String className) {
0089:
0090: Module module = null;
0091:
0092: try {
0093: module = (Module) Class.forName(className).newInstance();
0094: } catch (Exception e) {
0095: throw new ShimException(
0096: "Unexepected exception instantiating module \""
0097: + className + "\":" + e.getMessage());
0098: }
0099:
0100: return module;
0101: }
0102:
0103: /**
0104: * Updates <tt>page</tt> so that it works with <tt>template</tt>. If panels
0105: * defined in <tt>template</tt> do not exist in <tt>page</tt>, modules are
0106: * created for those panels. If there are panels in <tt>page</tt> that do
0107: * not exist in <tt>template</tt>, modules in those panels are destroyed.
0108: * Any other panels are left as is. NOTE: Make sure <tt>page.loadFull()</tt>
0109: * has been called before calling this method.
0110: */
0111: protected static void applyTemplate(Page page, Template template) {
0112:
0113: for (Iterator iter = template.getPanelConfigs().values()
0114: .iterator(); iter.hasNext();) {
0115:
0116: PanelConfig config = (PanelConfig) iter.next();
0117: Panel panel = (Panel) page.getPanels()
0118: .get(config.getName());
0119:
0120: //
0121: // no such panel in this page?
0122: //
0123: if (panel == null) {
0124:
0125: //
0126: // create the panel
0127: //
0128: panel = new Panel();
0129: panel.setName(config.getName());
0130: panel.setModuleClass(config.getDefaultModule());
0131:
0132: //
0133: // add the panel to the page
0134: //
0135: page.addPanel(panel);
0136:
0137: //
0138: // create the module
0139: //
0140: Module module = instantiateModule(config
0141: .getDefaultModule());
0142: module.init(page, config.getName());
0143: module.create();
0144:
0145: //
0146: // set the panel's module
0147: //
0148: panel.setModule(module);
0149: }
0150:
0151: //
0152: // otherwise, just call the create method
0153: //
0154: else {
0155: Module module = instantiateModule(panel
0156: .getModuleClass());
0157: module.init(page, config.getName());
0158: module.create();
0159: }
0160: }
0161:
0162: for (Iterator iter = page.getPanels().values().iterator(); iter
0163: .hasNext();) {
0164:
0165: Panel panel = (Panel) iter.next();
0166: PanelConfig config = (PanelConfig) template
0167: .getPanelConfigs().get(panel.getName());
0168:
0169: //
0170: // no such panel in this template?
0171: //
0172: if (config == null) {
0173:
0174: //
0175: // destroy the module
0176: //
0177: panel.getModule().destroy();
0178:
0179: //
0180: // remove the panel from the page; NOTE: probably bad encapsulation
0181: // here
0182: //
0183: //page.getPanels().remove( panel.getName() );
0184: iter.remove();
0185: }
0186: }
0187: }
0188:
0189: /**
0190: * Populates <tt>page</tt> with values from <tt>form</tt>.
0191: */
0192: protected void populatePage(Page page, DynaActionForm form) {
0193:
0194: page.set("title", (String) form.get("title"));
0195: page.set("alttitle", (String) form.get("alttitle"));
0196:
0197: if (StringUtils.isBlank((String) form.get("alias")))
0198: page.setDefaultAliasName();
0199: else
0200: page.set("aliasname", (String) form.get("alias"));
0201:
0202: page.set("template", (String) form.get("template"));
0203: page.set("metadescription", (String) form
0204: .get("metadescription"));
0205: page.set("metakeywords", (String) form.get("metakeywords"));
0206:
0207: page.setBoolean("hidden", !StringUtils.isBlank((String) form
0208: .get("hidden")));
0209: }
0210:
0211: /**
0212: * Returns the <tt>form</tt> forward.
0213: */
0214: protected ActionForward doNewPageForm(OperationContext op,
0215: ShimPolicy policy) throws Exception {
0216:
0217: String msg = policy.isNewPageFormAuthorized(op);
0218: if (msg != null) {
0219: StrutsUtil.addMessage(op.request, msg, null, null, null);
0220: return op.mapping.findForward("accessDenied");
0221: }
0222:
0223: return op.mapping.findForward("form");
0224: }
0225:
0226: /**
0227: * Creates a new page with panels defined in the specified template. Default
0228: * modules for those panels are created. Returns the <tt>editPage</tt>
0229: * forward.
0230: */
0231: protected ActionForward doConfigurePage(OperationContext op,
0232: ShimPolicy policy) throws Exception {
0233:
0234: if (!StringUtils.isBlank((String) op.form.get("cancel"))) {
0235: if ("".equals(op.form.get("id")))
0236: return new ActionForward("/home.do");
0237: else
0238: return new ActionForward("/editPage.do?id="
0239: + op.form.get("id"));
0240: }
0241:
0242: String msg = policy.isConfigurePageAuthorized(op);
0243: if (msg != null) {
0244: StrutsUtil.addMessage(op.request, msg, null, null, null);
0245: return op.mapping.findForward("accessDenied");
0246: }
0247:
0248: //
0249: // get the site context
0250: //
0251: SiteContext siteContext = SiteContext.getContext(op.request);
0252:
0253: Page page = policy.newPage();
0254: page.setSiteContext(siteContext);
0255:
0256: //
0257: // get the site map
0258: //
0259: SiteMap siteMap = ShimUtils.getSiteMap(op.request);
0260: SiteMapTree tree = ShimUtils.getSiteMapTree(op.request);
0261:
0262: Link link = null;
0263: FoldingTreeNode node = null;
0264:
0265: //
0266: // id provided?
0267: //
0268: if ("".equals(op.form.get("id"))) {
0269:
0270: //
0271: // create from copy?
0272: //
0273: if (!StringUtils.isBlank((String) op.form.get("copyfrom"))) {
0274: page.loadFull(new IntKey(op.form.get("copyfrom")));
0275: Page copy = page.copy();
0276:
0277: //
0278: // populate from form fields
0279: //
0280: populatePage(copy, op.form);
0281:
0282: //
0283: // override form fields with properties from source page
0284: //
0285: copy.setString("template", page.getString("template"));
0286: copy.setString("metadescription", page
0287: .getString("metadescription"));
0288: copy.setString("metakeywords", page
0289: .getString("metakeywords"));
0290:
0291: copy.save();
0292:
0293: page = copy;
0294: }
0295:
0296: //
0297: // create a new page normally
0298: //
0299: else {
0300: //
0301: // load the template (this forces any jsp exception to surface before
0302: // the page is created)
0303: //
0304: Template template = policy.newTemplate();
0305: template.setSiteContext(siteContext);
0306: template.load(op.request, (String) op.form
0307: .get("template"));
0308:
0309: //
0310: // create the new page
0311: //
0312: populatePage(page, op.form);
0313: page.saveNew();
0314:
0315: //
0316: // apply the template
0317: //
0318: applyTemplate(page, template);
0319: page.save();
0320: }
0321:
0322: //
0323: // create link and corresponding node
0324: //
0325: link = new Link();
0326: link.setTitle(page.getString("title"));
0327: link.setAlias(page.getString("aliasname"));
0328: link.setHidden(page.getBoolean("hidden"));
0329: link.setPageId(page.getInt("id"));
0330:
0331: node = SiteMapTree.buildNode(link);
0332:
0333: //
0334: // destination provided?
0335: //
0336: if (!StringUtils.isBlank((String) op.form.get("destid"))) {
0337: int destid = Integer.parseInt((String) op.form
0338: .get("destid"));
0339:
0340: if ("under".equals(op.form.get("position"))) {
0341: siteMap.insertUnder(siteMap.find(destid), link);
0342: tree.insertUnder(tree.find(destid), node);
0343: } else if ("before".equals(op.form.get("position"))) {
0344: siteMap.insertBefore(siteMap.find(destid), link);
0345: tree.insertBefore(tree.find(destid), node);
0346: } else if ("after".equals(op.form.get("position"))) {
0347: siteMap.insertAfter(siteMap.find(destid), link);
0348: tree.insertAfter(tree.find(destid), node);
0349: } else {
0350: throw new ShimException("Unexpected position \""
0351: + op.form.get("position") + "\".");
0352: }
0353: } else {
0354:
0355: //
0356: // insert as the root if there is no root
0357: //
0358: if (siteMap.getRoot() == null) {
0359: siteMap.setRoot(link);
0360: tree.setRoot(node);
0361: }
0362:
0363: //
0364: // otherwise add as a child of the root
0365: //
0366: else {
0367: siteMap.insertUnder(siteMap.getRoot(), link);
0368: tree.insertUnder(tree.getRoot(), node);
0369: }
0370: }
0371:
0372: siteMap.save();
0373:
0374: Event.log(SiteContext.getContext(op.request), op.user
0375: .getLogin(), "shim", "Created page \""
0376: + page.getString("title") + "\".");
0377: }
0378:
0379: else {
0380:
0381: //
0382: // existing page
0383: //
0384: page.loadFull(new IntKey(op.form.get("id")));
0385: populatePage(page, op.form);
0386:
0387: //
0388: // apply the template
0389: //
0390: Template template = policy.newTemplate();
0391: template.setSiteContext(siteContext);
0392: template.load(op.request, page.getString("template"));
0393:
0394: applyTemplate(page, template);
0395: page.save();
0396:
0397: //
0398: // update the sitemap (don't need to save; titles aren't stored in the
0399: // sitemap database)
0400: //
0401: link = siteMap.find(page.getInt("id"));
0402: link.setTitle(page.getString("title"));
0403: link.setAlias(page.getString("aliasname"));
0404: link.setHidden(page.getBoolean("hidden"));
0405:
0406: node = tree.find(page.getInt("id"));
0407: node.setLabel(page.getString("title"));
0408:
0409: Event.log(SiteContext.getContext(op.request), op.user
0410: .getLogin(), "shim", "Configured page \""
0411: + page.getString("title") + "\".");
0412: }
0413:
0414: //
0415: // set up the form for the forward
0416: //
0417: op.form.set("id", "" + page.getInt("id"));
0418:
0419: return new ActionForward("/editPage.do?id=" + page.getInt("id"));
0420: }
0421:
0422: /**
0423: * Displays the configure page form for <tt>form.id</tt>.
0424: */
0425: protected ActionForward doConfigurePageForm(OperationContext op,
0426: ShimPolicy policy) throws Exception {
0427:
0428: String msg = policy.isConfigurePageFormAuthorized(op);
0429: if (msg != null) {
0430: StrutsUtil.addMessage(op.request, msg, null, null, null);
0431: return op.mapping.findForward("accessDenied");
0432: }
0433:
0434: Page p = policy.newPage();
0435: p.setSiteContext(SiteContext.getContext(op.request));
0436: p.load(new IntKey(op.form.get("id")));
0437:
0438: op.form.set("title", p.getString("title"));
0439: op.form.set("alttitle", p.getString("alttitle"));
0440: op.form.set("alias", p.getString("aliasname"));
0441: op.form.set("template", p.getString("template"));
0442: op.form.set("metadescription", p.getString("metadescription"));
0443: op.form.set("metakeywords", p.getString("metakeywords"));
0444:
0445: if (p.getBoolean("hidden"))
0446: op.form.set("hidden", "yes");
0447:
0448: return op.mapping.findForward("form");
0449: }
0450:
0451: /**
0452: * Puts the session in edit mode and displays the page. Forwards to
0453: * <code>pageNotFound</code> if loading by alias and the page cannot be
0454: * found.
0455: */
0456: protected ActionForward doEditPage(OperationContext op,
0457: ShimPolicy policy) throws Exception {
0458:
0459: String msg = policy.isEditPageAuthorized(op);
0460: if (msg != null) {
0461: StrutsUtil.addMessage(op.request, msg, null, null, null);
0462: return op.mapping.findForward("accessDenied");
0463: }
0464:
0465: //
0466: // load the page
0467: //
0468: Page page = policy.newPage();
0469: page.setSiteContext(SiteContext.getContext(op.request));
0470:
0471: if (!StringUtils.isBlank((String) op.form.get("id"))) {
0472: page.loadFull(new IntKey(op.form.get("id")));
0473: } else {
0474: try {
0475: page.loadFullForAlias((String) op.form.get("alias"));
0476: } catch (PersistableException e) {
0477: //
0478: // does the pagenotfound page exist?
0479: //
0480: try {
0481: page.loadFullForAlias("pagenotfound");
0482:
0483: if (logger_.isDebugEnabled()) {
0484: logger_
0485: .debug("Loaded page for alias \"pagenotfound\"");
0486: }
0487: } catch (PersistableException ee) {
0488:
0489: if (logger_.isDebugEnabled()) {
0490: logger_
0491: .debug("Couldn't load page for alias \""
0492: + op.form.get("alias")
0493: + "\" or \"pagenotfound\"; forwarding to /pageNotFound.do");
0494: }
0495:
0496: return op.mapping.findForward("pageNotFound");
0497: }
0498: }
0499: }
0500:
0501: //
0502: // build the menu
0503: //
0504: policy.setUpEditorMenu(op, page);
0505:
0506: //
0507: // put the page into the request
0508: //
0509: op.request.setAttribute(ShimGlobals.PAGE_KEY, page);
0510:
0511: //
0512: // put the session into edit mode
0513: //
0514: op.request.getSession().setAttribute(ShimGlobals.MODE_KEY,
0515: ShimGlobals.MODE_EDIT);
0516:
0517: //
0518: // panels may have been added or removed from the template; apply the
0519: // template to the page
0520: //
0521: Template template = policy.newTemplate();
0522: template.setSiteContext(SiteContext.getContext(op.request));
0523: template.load(op.request, page.getString("template"));
0524: applyTemplate(page, template);
0525: page.save();
0526:
0527: //
0528: // include the template
0529: //
0530: template.include(op.request, op.response, page
0531: .getString("template"));
0532:
0533: return null;
0534: }
0535:
0536: /**
0537: * Loads the page for <tt>form.id</tt> and calls {@link
0538: * com.methodhead.shim.Module#configure configure()} on the module in
0539: * <tt>form.panel</tt>.
0540: */
0541: protected ActionForward doConfigureModule(OperationContext op,
0542: ShimPolicy policy) throws Exception {
0543:
0544: String msg = policy.isConfigureModuleAuthorized(op);
0545: if (msg != null) {
0546: StrutsUtil.addMessage(op.request, msg, null, null, null);
0547: return op.mapping.findForward("accessDenied");
0548: }
0549:
0550: //
0551: // load the page
0552: //
0553: Page page = policy.newPage();
0554: page.setSiteContext(SiteContext.getContext(op.request));
0555: page.loadFull(new IntKey(op.form.get("pageid")));
0556:
0557: Panel panel = (Panel) page.getPanels()
0558: .get(op.form.get("panel"));
0559:
0560: return panel.getModule().configure(op.mapping, op.form,
0561: op.request, op.response);
0562: }
0563:
0564: /**
0565: * Forwards to <tt>editor</tt>, a frameset that includes the toolbar and edit
0566: * window.
0567: */
0568: protected ActionForward doEditPanel(OperationContext op,
0569: ShimPolicy policy) throws Exception {
0570:
0571: String msg = policy.isEditPanelAuthorized(op);
0572: if (msg != null) {
0573: StrutsUtil.addMessage(op.request, msg, null, null, null);
0574: return op.mapping.findForward("accessDenied");
0575: }
0576:
0577: //
0578: // toolbar?
0579: //
0580: if ("yes".equals(op.form.get("toolbar")))
0581: return op.mapping.findForward("toolbar");
0582:
0583: //
0584: // editWin?
0585: //
0586: if ("yes".equals(op.form.get("editWin"))) {
0587: Page page = policy.newPage();
0588: page.setSiteContext(SiteContext.getContext(op.request));
0589: page.loadFull(new IntKey(op.form.get("pageid")));
0590:
0591: op.request.setAttribute(ShimGlobals.PAGE_KEY, page);
0592: op.request.setAttribute(ShimGlobals.MODE_KEY,
0593: ShimGlobals.MODE_RICHEDIT);
0594: op.request.setAttribute(ShimGlobals.EDITPANEL_KEY, op.form
0595: .get("panel"));
0596:
0597: Template template = policy.newTemplate();
0598: template.setSiteContext(SiteContext.getContext(op.request));
0599: template.include(op.request, op.response, page
0600: .getString("template"));
0601:
0602: return null;
0603: }
0604:
0605: //
0606: // frameset
0607: //
0608: Page page = policy.newPage();
0609: page.setSiteContext(SiteContext.getContext(op.request));
0610: page.load(new IntKey(op.form.get("pageid")));
0611: op.form.set("title", page.getString("title"));
0612:
0613: return op.mapping.findForward("editor");
0614: }
0615:
0616: /**
0617: * Updates the module for page <tt>pageid</tt> and <tt>panel</tt> with text
0618: * from the rich text editor.
0619: */
0620: protected ActionForward doSavePanel(OperationContext op,
0621: ShimPolicy policy) throws Exception {
0622:
0623: String msg = policy.isSavePanelAuthorized(op);
0624: if (msg != null) {
0625: StrutsUtil.addMessage(op.request, msg, null, null, null);
0626: return op.mapping.findForward("accessDenied");
0627: }
0628:
0629: //
0630: // load the page
0631: //
0632: Page page = policy.newPage();
0633: page.setSiteContext(SiteContext.getContext(op.request));
0634: page.loadFull(new IntKey(op.form.get("pageid")));
0635:
0636: Panel panel = (Panel) page.getPanels()
0637: .get(op.form.get("panel"));
0638:
0639: panel.getModule().update((String) op.form.get("text"));
0640:
0641: Event.log(SiteContext.getContext(op.request), op.user
0642: .getLogin(), "shim", "Updated panel \""
0643: + op.form.get("panel") + "\" on page \""
0644: + page.getString("title") + "\".");
0645:
0646: return new ActionForward("/editPage.do?id="
0647: + op.form.get("pageid"));
0648: }
0649:
0650: /**
0651: * Sets <tt>form.delOption</tt> to <tt>promote</tt> and forwards to
0652: * "form".
0653: */
0654: protected ActionForward doDeletePageForm(OperationContext op,
0655: ShimPolicy policy) throws Exception {
0656:
0657: String msg = policy.isDeletePageFormAuthorized(op);
0658: if (msg != null) {
0659: StrutsUtil.addMessage(op.request, msg, null, null, null);
0660: return op.mapping.findForward("accessDenied");
0661: }
0662:
0663: Page page = policy.newPage();
0664: page.setSiteContext(SiteContext.getContext(op.request));
0665: page.load(new IntKey(op.form.get("id")));
0666:
0667: op.form.set("delOption", "promote");
0668: op.form.set("title", page.getString("title"));
0669:
0670: return op.mapping.findForward("form");
0671: }
0672:
0673: /**
0674: * Deletes the page with id <tt>form.id</tt>.
0675: */
0676: protected ActionForward doDeletePage(
0677: OperationContext op,
0678: ShimPolicy policy )
0679: throws
0680: Exception {
0681:
0682: String msg = policy.isDeletePageAuthorized( op );
0683: if ( msg != null ) {
0684: StrutsUtil.addMessage( op.request, msg, null, null, null );
0685: return op.mapping.findForward( "accessDenied" );
0686: }
0687:
0688: //
0689: // cancelled?
0690: //
0691: if ( StringUtils.isNotBlank( ( String )op.form.get( "cancel" ) ) )
0692: return new ActionForward( "/editPage.do?id=" + op.form.get( "id" ) );
0693:
0694: //
0695: // get some things we'll need
0696: //
0697: Page page = policy.newPage();
0698: page.setSiteContext( SiteContext.getContext( op.request ) );
0699: page.loadFull( new IntKey( op.form.get( "id" ) ) );
0700:
0701: SiteMap siteMap = ShimUtils.getSiteMap( op.request );
0702: Link link = siteMap.find( page.getInt( "id" ) );
0703:
0704: SiteMapTree tree = ShimUtils.getSiteMapTree( op.request );
0705:
0706: //
0707: // do we know what to do with child nodes if we have them?
0708: //
0709: if ( !link.isLeaf() &&
0710: StringUtils.isBlank( ( String )op.form.get( "delOption" ) ) ) {
0711: return new ActionForward(
0712: "/deletePageForm.do?id=" + op.form.get( "id" ) );
0713: }
0714:
0715: //
0716: // confirm?
0717: //
0718: if ( StringUtils.isBlank( ( String )op.form.get( "confirm" ) ) ) {
0719:
0720: List pageNames = new ArrayList();
0721: pageNames.add( link.getTitle() );
0722:
0723: //
0724: // get the names of child pages if we need them
0725: //
0726: if ( !link.isLeaf() && "delete".equals( op.form.get( "delOption" ) ) ) {
0727: for (
0728: Enumeration enum = link.depthFirstEnumeration();
0729: enum.hasMoreElements(); ) {
0730:
0731: Link l = ( Link )enum.nextElement();
0732: pageNames.add( l.getTitle( ) );
0733: }
0734: }
0735:
0736: StrutsUtil.addMessage(
0737: op.request,
0738: "shim.deletePage",
0739: StringUtils.join( pageNames.iterator(), "," ),
0740: null,
0741: null );
0742:
0743: return op.mapping.findForward( "confirm" );
0744: }
0745:
0746: //
0747: // figure out where we're going to forward to
0748: //
0749: ActionForward forward = null;
0750: if ( link.isRoot() )
0751: forward = op.mapping.findForward( "home" );
0752: else
0753: forward =
0754: new ActionForward(
0755: "/editPage.do?id=" + ( ( Link )link.getParent() ).getPageId() );
0756:
0757: if ( link.isLeaf() ) {
0758: siteMap.remove( link );
0759: siteMap.save();
0760:
0761: tree.remove( tree.find( link.getPageId() ) );
0762:
0763: deletePage( page );
0764: }
0765: else {
0766: if ( "delete".equals( op.form.get( "delOption" ) ) ) {
0767: //
0768: // make a list of page ids to delete; database constraints force us to
0769: // update the site map first
0770: //
0771: List pageIds = new ArrayList();
0772:
0773: for (
0774: Enumeration enum = link.depthFirstEnumeration();
0775: enum.hasMoreElements(); ) {
0776:
0777: Link l = ( Link )enum.nextElement();
0778: pageIds.add( new Integer( l.getPageId() ) );
0779: }
0780:
0781: siteMap.remove( link );
0782: siteMap.save();
0783:
0784: tree.remove( tree.find( link.getPageId() ) );
0785:
0786: Page p = policy.newPage();
0787: p.setSiteContext( SiteContext.getContext( op.request ) );
0788: for ( Iterator iter = pageIds.iterator(); iter.hasNext(); ) {
0789: p.loadFull( new IntKey( iter.next() ) );
0790: deletePage( p );
0791: }
0792: }
0793: else {
0794: siteMap.removeAndPromoteChildren( link );
0795: siteMap.save();
0796:
0797: tree.removeAndPromoteChildren( tree.find( link.getPageId() ) );
0798:
0799: deletePage( page );
0800: }
0801: }
0802:
0803: Event.log(
0804: SiteContext.getContext( op.request ),
0805: op.user.getLogin(),
0806: "shim",
0807: "Deleted page \"" + page.getString( "title" ) + "\"." );
0808:
0809: return forward;
0810: }
0811:
0812: /**
0813: * Sets <tt>form.position</tt> to <tt>before</tt> and forwards to
0814: * "form".
0815: */
0816: protected ActionForward doMovePageForm(OperationContext op,
0817: ShimPolicy policy) throws Exception {
0818:
0819: String msg = policy.isMovePageFormAuthorized(op);
0820: if (msg != null) {
0821: StrutsUtil.addMessage(op.request, msg, null, null, null);
0822: return op.mapping.findForward("accessDenied");
0823: }
0824:
0825: op.form.set("position", "before");
0826:
0827: return op.mapping.findForward("form");
0828: }
0829:
0830: /**
0831: * Moves the page with <tt>form.id</tt> near the page <tt>form.destid</tt>
0832: * according to <tt>form.position</tt> (legal values are "under", "before",
0833: * "after"), returning an edit page forward for what page is parent to the
0834: * page being moved.
0835: */
0836: protected ActionForward doMovePage(OperationContext op,
0837: ShimPolicy policy) throws Exception {
0838:
0839: String msg = policy.isMovePageAuthorized(op);
0840: if (msg != null) {
0841: StrutsUtil.addMessage(op.request, msg, null, null, null);
0842: return op.mapping.findForward("accessDenied");
0843: }
0844:
0845: //
0846: // cancelled?
0847: //
0848: if (StringUtils.isNotBlank((String) op.form.get("cancel")))
0849: return new ActionForward("/editPage.do?id="
0850: + op.form.get("id"));
0851:
0852: SiteMap siteMap = ShimUtils.getSiteMap(op.request);
0853: SiteMapTree tree = ShimUtils.getSiteMapTree(op.request);
0854:
0855: Link link = siteMap.find(Integer.parseInt((String) op.form
0856: .get("id")));
0857: Link dest = siteMap.find(Integer.parseInt((String) op.form
0858: .get("destid")));
0859:
0860: FoldingTreeNode node = tree.find(link.getPageId());
0861: FoldingTreeNode destNode = tree.find(dest.getPageId());
0862:
0863: if ("under".equals(op.form.get("position"))) {
0864: siteMap.moveUnder(dest, link);
0865: tree.moveUnder(destNode, node);
0866: } else if ("before".equals(op.form.get("position"))) {
0867: siteMap.moveBefore(dest, link);
0868: tree.moveBefore(destNode, node);
0869: } else if ("after".equals(op.form.get("position"))) {
0870: siteMap.moveAfter(dest, link);
0871: tree.moveAfter(destNode, node);
0872: } else
0873: throw new ShimException("Unexpected position \""
0874: + op.form.get("position") + "\".");
0875:
0876: siteMap.save();
0877:
0878: Event.log(SiteContext.getContext(op.request), op.user
0879: .getLogin(), "shim", "Moved page \"" + link.getTitle()
0880: + "\".");
0881:
0882: return new ActionForward("/editPage.do?id="
0883: + ((Link) link.getParent()).getPageId());
0884: }
0885:
0886: /**
0887: * Set <tt>form.module</tt> to whatever module is specified for
0888: * <tt>form.pageid</tt> and <tt>panel</tt>.
0889: */
0890: protected ActionForward doConfigurePanelForm(OperationContext op,
0891: ShimPolicy policy) throws Exception {
0892:
0893: String msg = policy.isConfigurePanelFormAuthorized(op);
0894: if (msg != null) {
0895: StrutsUtil.addMessage(op.request, msg, null, null, null);
0896: return op.mapping.findForward("accessDenied");
0897: }
0898:
0899: Page page = policy.newPage();
0900: page.setSiteContext(SiteContext.getContext(op.request));
0901: page.loadFull(new IntKey(op.form.get("pageid")));
0902:
0903: //
0904: // panels may have been added or removed from the template; apply the
0905: // template to the page
0906: //
0907: Template template = policy.newTemplate();
0908: template.setSiteContext(SiteContext.getContext(op.request));
0909: template.load(op.request, page.getString("template"));
0910: applyTemplate(page, template);
0911: page.save();
0912:
0913: Panel panel = (Panel) page.getPanels()
0914: .get(op.form.get("panel"));
0915:
0916: op.form.set("module", panel.getModuleClass());
0917:
0918: return op.mapping.findForward("form");
0919: }
0920:
0921: /**
0922: * Set <tt>form.module</tt> to whatever module is specified for
0923: * <tt>form.pageid</tt> and <tt>panel</tt>.
0924: */
0925: protected ActionForward doConfigurePanel(OperationContext op,
0926: ShimPolicy policy) throws Exception {
0927:
0928: String msg = policy.isConfigurePanelAuthorized(op);
0929: if (msg != null) {
0930: StrutsUtil.addMessage(op.request, msg, null, null, null);
0931: return op.mapping.findForward("accessDenied");
0932: }
0933:
0934: //
0935: // cancelled?
0936: //
0937: if (StringUtils.isNotBlank((String) op.form.get("cancel")))
0938: return new ActionForward("/editPage.do?id="
0939: + op.form.get("pageid"));
0940:
0941: //
0942: // load the page
0943: //
0944: Page page = policy.newPage();
0945: page.setSiteContext(SiteContext.getContext(op.request));
0946: page.loadFull(new IntKey(op.form.get("pageid")));
0947:
0948: //
0949: // was a different module selected?
0950: //
0951: Panel panel = (Panel) page.getPanels().get(
0952: (String) op.form.get("panel"));
0953:
0954: if (!panel.getModuleClass().equals(op.form.get("module"))) {
0955:
0956: //
0957: // destroy the existing module
0958: //
0959: Module module = panel.getModule();
0960: module.destroy();
0961:
0962: //
0963: // create new module
0964: //
0965: module = instantiateModule((String) op.form.get("module"));
0966: module.init(page, (String) op.form.get("panel"));
0967: module.create();
0968:
0969: //
0970: // update the panel and save the page
0971: //
0972: panel.setModuleClass((String) op.form.get("module"));
0973: page.save();
0974: }
0975:
0976: Event.log(SiteContext.getContext(op.request), op.user
0977: .getLogin(), "shim", "Configured panel \""
0978: + op.form.get("panel") + "\" on page \""
0979: + page.getString("title") + "\".");
0980:
0981: return new ActionForward("/editPage.do?id="
0982: + op.form.get("pageid"));
0983: }
0984:
0985: public ActionForward doExecute(ActionMapping mapping,
0986: ActionForm form, HttpServletRequest request,
0987: HttpServletResponse response) throws Exception {
0988:
0989: //
0990: // get some things we'll need
0991: //
0992: DynaActionForm dynaForm = (DynaActionForm) form;
0993: ShimPolicy policy = (ShimPolicy) StrutsUtil.getPolicy(mapping);
0994: AuthUser user = AuthUtil.getUser(request);
0995:
0996: //
0997: // mapping authorized?
0998: //
0999: if (!policy.isMappingAuthorized(user, mapping.getPath()))
1000: return mapping.findForward("accessDenied");
1001:
1002: OperationContext op = new OperationContext(mapping, dynaForm,
1003: request, response, user);
1004:
1005: //
1006: // execute the appopriate method
1007: //
1008: if (mapping.getPath().equals("/newPageForm")) {
1009: return doNewPageForm(op, policy);
1010: }
1011: if (mapping.getPath().equals("/configurePageForm")) {
1012: return doConfigurePageForm(op, policy);
1013: }
1014: if (mapping.getPath().equals("/configurePage")) {
1015: return doConfigurePage(op, policy);
1016: }
1017: if (mapping.getPath().equals("/editPage")) {
1018: return doEditPage(op, policy);
1019: }
1020: if (mapping.getPath().equals("/configureModule")) {
1021: return doConfigureModule(op, policy);
1022: }
1023: if (mapping.getPath().equals("/editPanel")) {
1024: return doEditPanel(op, policy);
1025: }
1026: if (mapping.getPath().equals("/savePanel")) {
1027: return doSavePanel(op, policy);
1028: }
1029: if (mapping.getPath().equals("/deletePageForm")) {
1030: return doDeletePageForm(op, policy);
1031: }
1032: if (mapping.getPath().equals("/deletePage")) {
1033: return doDeletePage(op, policy);
1034: }
1035: if (mapping.getPath().equals("/movePageForm")) {
1036: return doMovePageForm(op, policy);
1037: }
1038: if (mapping.getPath().equals("/movePage")) {
1039: return doMovePage(op, policy);
1040: }
1041: if (mapping.getPath().equals("/configurePanelForm")) {
1042: return doConfigurePanelForm(op, policy);
1043: }
1044: if (mapping.getPath().equals("/configurePanel")) {
1045: return doConfigurePanel(op, policy);
1046: }
1047:
1048: throw new Exception("Unexpected mapping path \""
1049: + mapping.getPath() + "\"");
1050: }
1051:
1052: // properties ///////////////////////////////////////////////////////////////
1053:
1054: // attributes ///////////////////////////////////////////////////////////////
1055:
1056: private Logger logger_ = Logger.getLogger(PageAction.class);
1057: }
|