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 java.io.Serializable;
024: import javax.servlet.http.HttpServletRequest;
025:
026: import org.apache.struts.action.ActionMapping;
027: import org.apache.struts.action.ActionErrors;
028: import org.apache.struts.action.ActionError;
029: import org.apache.struts.validator.DynaValidatorForm;
030: import java.util.List;
031: import java.util.ArrayList;
032: import java.util.Iterator;
033: import org.apache.struts.util.LabelValueBean;
034: import com.methodhead.sitecontext.SiteContext;
035: import com.methodhead.auth.AuthUtil;
036: import com.methodhead.tree.Tree;
037: import org.apache.commons.lang.StringUtils;
038:
039: public final class MovePageForm extends DynaValidatorForm implements
040: Serializable {
041:
042: /**
043: * Instantiates the policy for this action.
044: */
045: private ShimPolicy getPolicy(ActionMapping mapping) {
046:
047: try {
048: return (ShimPolicy) Class.forName(mapping.getParameter())
049: .newInstance();
050: } catch (Exception e) {
051: throw new ShimException(
052: "Unexpected exception while instantiating \""
053: + mapping.getParameter() + "\":"
054: + e.getMessage());
055: }
056: }
057:
058: public void reset(ActionMapping mapping, HttpServletRequest request) {
059:
060: //
061: // don't do anything if no one is logged in; action will forward to login
062: //
063: if (AuthUtil.getUser(request) == null)
064: return;
065:
066: ShimPolicy policy = getPolicy(mapping);
067:
068: //
069: // get page options
070: //
071: Page page = policy.newPage();
072: page.setSiteContext(SiteContext.getContext(request));
073: List pages = page.loadAll();
074:
075: List pageOptions = new ArrayList();
076: pageOptions.add(new LabelValueBean("Select...", ""));
077: for (Iterator iter = pages.iterator(); iter.hasNext();) {
078: Page p = (Page) iter.next();
079: pageOptions.add(new LabelValueBean(p.getString("title"), ""
080: + p.getInt("id")));
081: }
082:
083: set("pages", pageOptions);
084: }
085:
086: public ActionErrors validate(ActionMapping mapping,
087: HttpServletRequest request) {
088:
089: //
090: // don't do anything if we're cancelling
091: //
092: if (!StringUtils.isBlank((String) get("cancel")))
093: return new ActionErrors();
094:
095: ActionErrors errors = super .validate(mapping, request);
096:
097: if (errors.size() == 0) {
098: ShimPolicy policy = getPolicy(mapping);
099:
100: SiteMap siteMap = ShimUtils.getSiteMap(request);
101:
102: Link link = siteMap.find(Integer
103: .parseInt((String) get("id")));
104: Link dest = siteMap.find(Integer
105: .parseInt((String) get("destid")));
106:
107: String position = Tree.POSITION_UNDER;
108: if ("under".equals(get("position")))
109: position = Tree.POSITION_UNDER;
110: else if ("before".equals(get("position")))
111: position = Tree.POSITION_BEFORE;
112: else if ("after".equals(get("position")))
113: position = Tree.POSITION_AFTER;
114: else
115: throw new ShimException("Unexpected position \""
116: + get("position") + "\".");
117:
118: String result = siteMap.validateMove(link, dest, position);
119:
120: if (Tree.INVALIDMOVE_CANNOTMOVEROOT.equals(result))
121: errors.add("dest", new ActionError(
122: "shim.movePageForm.cannotMoveRoot"));
123: else if (Tree.INVALIDMOVE_CANNOTMOVEBEFOREROOT
124: .equals(result))
125: errors.add("dest", new ActionError(
126: "shim.movePageForm.cannotMoveBeforeRoot"));
127: else if (Tree.INVALIDMOVE_CANNOTMOVEAFTERROOT
128: .equals(result))
129: errors.add("dest", new ActionError(
130: "shim.movePageForm.cannotMoveAfterRoot"));
131: else if (Tree.INVALIDMOVE_CANNOTMOVENEARSELF.equals(result))
132: errors.add("dest", new ActionError(
133: "shim.movePageForm.cannotMoveNearSelf"));
134: else if (Tree.INVALIDMOVE_CANNOTMOVEUNDERSELF
135: .equals(result))
136: errors.add("dest", new ActionError(
137: "shim.movePageForm.cannotMoveUnderSelf"));
138: }
139:
140: return errors;
141: }
142: }
|