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.property;
022:
023: import java.util.List;
024:
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027:
028: import org.apache.struts.action.Action;
029: import org.apache.struts.action.ActionForm;
030: import org.apache.struts.action.ActionForward;
031: import org.apache.struts.action.ActionMapping;
032: import org.apache.struts.action.DynaActionForm;
033: import com.methodhead.sitecontext.SiteContext;
034: import com.methodhead.auth.AuthAction;
035: import com.methodhead.auth.AuthUser;
036: import com.methodhead.auth.AuthUtil;
037: import com.methodhead.util.OperationContext;
038: import com.methodhead.util.StrutsUtil;
039:
040: /**
041: <p>
042: Use <tt>PropertyAction</tt> to build a web interface to manage
043: properties. This action will respond to the mappings
044: <tt>/listProperties</tt>, <tt>/setPropertyForm</tt>, and
045: <tt>/setProperty</tt>, calling <tt>doListProperties()</tt>,
046: <tt>doSetPropertyForm()</tt>, and <tt>doSetProperty()</tt>
047: respectively.
048: </p>
049: <p>
050: The following are example mappings:
051: </p>
052: <xmp>
053: <action
054: path = "/listProperties"
055: type = "com.methodhead.property.PropertyAction"
056: scope = "request"
057: name = "propertyForm"
058: input = "/properties.jsp"
059: validate = "false">
060: <forward name="list" path="/properties.jsp"/>
061: </action>
062:
063: <action
064: path = "/setPropertyForm"
065: type = "com.methodhead.property.PropertyAction"
066: scope = "request"
067: name = "propertyForm"
068: input = "/property.jsp"
069: validate = "false">
070: </action>
071:
072: <action
073: path = "/setProperty"
074: type = "com.methodhead.property.PropertyAction"
075: scope = "request"
076: name = "propertyForm"
077: input = "/property.jsp"
078: validate = "false">
079: <forward name="saved" path="/listProperties.do"/>
080: </action>
081: </xmp>
082: <p>
083: The action expects a <tt>DynaActionForm</tt> with at least the
084: following configuration:
085: </p>
086: <xmp>
087: <form-bean
088: name = "propertyForm"
089: dynamic = "true"
090: type = "org.apache.struts.action.DynaActionForm">
091:
092: <form-property name="name" type="java.lang.String"/>
093: <form-property name="value" type="java.lang.String"/>
094: <form-property name="description" type="java.lang.String"/>
095: <form-property name="system" type="java.lang.String"/>
096: <form-property name="submit" type="java.lang.String"/>
097: <form-property name="properties" type="java.util.List" />
098:
099: </form-bean>
100: </xmp>
101: */
102: public class PropertyAction extends AuthAction {
103:
104: // constructors /////////////////////////////////////////////////////////////
105:
106: // constants ////////////////////////////////////////////////////////////////
107:
108: // classes //////////////////////////////////////////////////////////////////
109:
110: // methods //////////////////////////////////////////////////////////////////
111:
112: /**
113: * Loads all properties and puts them in
114: * <tt>properties</tt>.
115: */
116: protected ActionForward doListProperties(OperationContext op,
117: PropertyPolicy policy) throws Exception {
118:
119: //
120: // authorized?
121: //
122: String msg = policy.isListPropertiesAuthorized(op);
123: if (msg != null) {
124: StrutsUtil.addMessage(op.request, msg, null, null, null);
125: return op.mapping.findForward("accessDenied");
126: }
127:
128: op.form.set("properties", Property.loadAll(SiteContext
129: .getContext(op.request)));
130:
131: return op.mapping.findForward("list");
132: }
133:
134: /**
135: * Loads the property named <tt>name</tt> and sets <tt>value</tt>,
136: * <tt>description</tt>, and <tt>system</tt>.
137: */
138: protected ActionForward doSetPropertyForm(OperationContext op,
139: PropertyPolicy policy) throws Exception {
140:
141: //
142: // authorized?
143: //
144: String msg = policy.isSetPropertyFormAuthorized(op);
145: if (msg != null) {
146: StrutsUtil.addMessage(op.request, msg, null, null, null);
147: return op.mapping.findForward("accessDenied");
148: }
149:
150: Property p = new Property();
151: p.setSiteContext(SiteContext.getContext(op.request));
152: p.loadForName((String) op.form.get("name"));
153:
154: op.form.set("value", p.getString("value"));
155: op.form.set("description", p.getString("description"));
156: op.form.set("system", p.getBoolean("system_property") ? "on"
157: : "");
158:
159: return new ActionForward(op.mapping.getInput());
160: }
161:
162: /**
163: * Loads the property named <tt>name</tt> and sets its value to
164: * <tt>value</tt>.
165: */
166: protected ActionForward doSetProperty(OperationContext op,
167: PropertyPolicy policy) throws Exception {
168:
169: //
170: // authorized?
171: //
172: String msg = policy.isSetPropertyAuthorized(op);
173: if (msg != null) {
174: StrutsUtil.addMessage(op.request, msg, null, null, null);
175: return op.mapping.findForward("accessDenied");
176: }
177:
178: Property.setProperty(SiteContext.getContext(op.request),
179: (String) op.form.get("name"), (String) op.form
180: .get("value"));
181:
182: Property p = new Property();
183: p.setSiteContext(SiteContext.getContext(op.request));
184: p.loadForName((String) op.form.get("name"));
185:
186: op.form.set("value", p.getString("value"));
187: op.form.set("description", p.getString("description"));
188: op.form.set("system", p.getBoolean("system_property") ? "on"
189: : "");
190:
191: return op.mapping.findForward("saved");
192: }
193:
194: public ActionForward doExecute(ActionMapping mapping,
195: ActionForm form, HttpServletRequest request,
196: HttpServletResponse response) throws Exception {
197:
198: //
199: // get some things we'll need
200: //
201: DynaActionForm dynaForm = (DynaActionForm) form;
202: PropertyPolicy policy = (PropertyPolicy) StrutsUtil
203: .getPolicy(mapping);
204: AuthUser user = AuthUtil.getUser(request);
205:
206: OperationContext op = new OperationContext(mapping, dynaForm,
207: request, response, user);
208:
209: if (mapping.getPath().equals("/listProperties")) {
210: return doListProperties(op, policy);
211: }
212:
213: if (mapping.getPath().equals("/setPropertyForm")) {
214: return doSetPropertyForm(op, policy);
215: }
216:
217: if (mapping.getPath().equals("/setProperty")) {
218: return doSetProperty(op, policy);
219: }
220:
221: throw new Exception("Unexpected mapping path \""
222: + mapping.getPath() + "\"");
223: }
224:
225: // properties ///////////////////////////////////////////////////////////////
226:
227: // attributes ///////////////////////////////////////////////////////////////
228: }
|