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.ActionForward;
027: import org.apache.struts.action.DynaActionForm;
028: import org.apache.struts.action.ActionMessages;
029: import org.apache.struts.util.MessageResources;
030: import org.apache.struts.Globals;
031: import org.apache.struts.util.LabelValueBean;
032: import com.methodhead.event.Event;
033:
034: import javax.servlet.http.HttpServletRequest;
035: import javax.servlet.http.HttpServletResponse;
036: import com.methodhead.aikp.AutoIntKeyPersistable;
037: import com.methodhead.aikp.AikpAction;
038: import com.methodhead.aikp.IntKey;
039: import com.methodhead.util.OperationContext;
040: import com.methodhead.util.StrutsUtil;
041: import com.methodhead.sitecontext.SiteContext;
042: import java.util.List;
043: import java.util.ArrayList;
044: import java.util.Iterator;
045: import org.apache.commons.lang.StringUtils;
046:
047: public class HtmlFragmentAction extends AikpAction {
048:
049: // constructors /////////////////////////////////////////////////////////////
050:
051: // constants ////////////////////////////////////////////////////////////////
052:
053: // classes //////////////////////////////////////////////////////////////////
054:
055: // methods //////////////////////////////////////////////////////////////////
056:
057: protected AutoIntKeyPersistable createPersistable(
058: OperationContext op) {
059: HtmlFragment fragment = new HtmlFragment();
060: fragment.setSiteContext(SiteContext.getContext(op.request));
061: return fragment;
062: }
063:
064: protected void populateForm(DynaActionForm form,
065: AutoIntKeyPersistable persistable) {
066:
067: populateFormProperty("name", form, persistable);
068: populateFormProperty("value", form, persistable);
069: }
070:
071: protected void populatePersistable(
072: AutoIntKeyPersistable persistable, DynaActionForm form) {
073:
074: populatePersistableField("name", persistable, form);
075: populatePersistableField("value", persistable, form);
076: }
077:
078: /**
079: * Loads all fragments for the site, creates a list of options for them, and
080: * puts those options in <tt>form.list</tt>.
081: */
082: protected ActionForward doList(OperationContext op, Object policy) {
083:
084: String msg = ((ShimPolicy) policy)
085: .isHtmlFragmentListAuthorized(op);
086: if (msg != null) {
087: StrutsUtil.addMessage(op.request, msg, null, null, null);
088: return op.mapping.findForward("accessDenied");
089: }
090:
091: op.form.set("list", ShimUtils
092: .getHtmlFragmentOptions(op.request));
093:
094: //
095: // update the action
096: //
097: op.form.set("action", "edit");
098:
099: return op.mapping.findForward("list");
100: }
101:
102: /**
103: * Calls base class function and forwards to
104: * <tt>/htmlFragment.do?action=list</tt>.
105: */
106: protected ActionForward doSaveNew(OperationContext op, Object policy) {
107:
108: String msg = ((ShimPolicy) policy)
109: .isHtmlFragmentSaveNewAuthorized(op);
110: if (msg != null) {
111: StrutsUtil.addMessage(op.request, msg, null, null, null);
112: return op.mapping.findForward("accessDenied");
113: }
114:
115: super .doSaveNew(op, policy);
116:
117: Event.log(SiteContext.getContext(op.request), op.user
118: .getLogin(), "shim", "Created html fragment \""
119: + op.form.get("name") + "\".");
120:
121: return new ActionForward("/htmlFragment.do?action=list");
122: }
123:
124: protected ActionForward doNew(OperationContext op, Object policy) {
125:
126: String msg = ((ShimPolicy) policy)
127: .isHtmlFragmentNewAuthorized(op);
128: if (msg != null) {
129: StrutsUtil.addMessage(op.request, msg, null, null, null);
130: return op.mapping.findForward("accessDenied");
131: }
132:
133: return super .doNew(op, policy);
134: }
135:
136: protected ActionForward doEdit(OperationContext op, Object policy) {
137:
138: String msg = ((ShimPolicy) policy)
139: .isHtmlFragmentEditAuthorized(op);
140: if (msg != null) {
141: StrutsUtil.addMessage(op.request, msg, null, null, null);
142: return op.mapping.findForward("accessDenied");
143: }
144:
145: return super .doEdit(op, policy);
146: }
147:
148: /**
149: * Calls base class function and forwards to
150: * <tt>/htmlFragment.do?action=list</tt>.
151: */
152: protected ActionForward doSave(OperationContext op, Object policy) {
153:
154: String msg = ((ShimPolicy) policy)
155: .isHtmlFragmentSaveAuthorized(op);
156: if (msg != null) {
157: StrutsUtil.addMessage(op.request, msg, null, null, null);
158: return op.mapping.findForward("accessDenied");
159: }
160:
161: super .doSave(op, policy);
162:
163: Event.log(SiteContext.getContext(op.request), op.user
164: .getLogin(), "shim", "Saved html fragment \""
165: + op.form.get("name") + "\".");
166:
167: return new ActionForward("/htmlFragment.do?action=list");
168: }
169:
170: /**
171: * Returns a forward to <tt>list</tt>.
172: */
173: protected ActionForward doCancelSave(OperationContext op,
174: Object policy) {
175:
176: return new ActionForward("/htmlFragment.do?action=list");
177: }
178:
179: /**
180: * Forwards to <tt>/htmlFragment.do?action=list</tt>.
181: */
182: protected ActionForward doDelete(OperationContext op, Object policy) {
183:
184: String msg = ((ShimPolicy) policy)
185: .isHtmlFragmentDeleteAuthorized(op);
186: if (msg != null) {
187: StrutsUtil.addMessage(op.request, msg, null, null, null);
188: return op.mapping.findForward("accessDenied");
189: }
190:
191: super .doDelete(op, policy);
192:
193: Event.log(SiteContext.getContext(op.request), op.user
194: .getLogin(), "shim", "Deleted html fragment \""
195: + op.form.get("name") + "\".");
196:
197: return new ActionForward("/htmlFragment.do?action=list");
198: }
199:
200: /**
201: *
202: */
203: protected ActionForward doConfirm(OperationContext op, Object policy) {
204:
205: //
206: // load the fragment
207: //
208: HtmlFragment fragment = (HtmlFragment) createPersistable(op);
209: fragment.load(new IntKey(op.form.get("id")));
210:
211: //
212: // see if any text modules depend on it
213: //
214: ShimPolicy shimPolicy = (ShimPolicy) policy;
215: TextModule textModule = shimPolicy.newTextModule();
216: List pages = textModule.getDependentPages(fragment);
217:
218: if (pages.size() > 0) {
219:
220: //
221: // concatenate page names
222: //
223: StrutsUtil.addMessage(op.request,
224: "shim.htmlFragment.fragmentInUse", StringUtils
225: .join(pages.iterator(), ", "), null, null);
226:
227: return op.mapping.getInputForward();
228: }
229:
230: return super .doConfirm(op, policy);
231: }
232:
233: // properties ///////////////////////////////////////////////////////////////
234:
235: // attributes ///////////////////////////////////////////////////////////////
236: }
|