Source code: com/RuntimeCollective/webapps/form/HtmlInputPostForm.java
1 /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/form/HtmlInputPostForm.java,v 1.5 2003/09/30 15:13:14 joe Exp $
2 * $Revision: 1.5 $
3 * $Date: 2003/09/30 15:13:14 $
4 *
5 * ====================================================================
6 *
7 * Josephine : http://www.runtime-collective.com/josephine/index.html
8 *
9 * Copyright (C) 2003 Runtime Collective
10 *
11 * This product includes software developed by the
12 * Apache Software Foundation (http://www.apache.org/).
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
29
30 package com.RuntimeCollective.webapps.form;
31
32 import com.RuntimeCollective.webapps.bean.EntityBean;
33 import com.RuntimeCollective.webapps.RuntimeParameters;
34 import javax.servlet.http.HttpServletRequest;
35 import org.apache.struts.action.ActionError;
36 import org.apache.struts.action.ActionErrors;
37 import org.apache.struts.action.ActionForm;
38 import org.apache.struts.action.ActionMapping;
39
40 import java.lang.reflect.Method;
41 import java.lang.reflect.Constructor;
42 import java.lang.reflect.InvocationTargetException;
43
44 /**
45 * An ActionForm for using the results of an HtmlInputPostTag
46 *
47 * @author Joe Holmberg
48 * @version $Id: HtmlInputPostForm.java,v 1.5 2003/09/30 15:13:14 joe Exp $
49 */
50 public class HtmlInputPostForm extends ActionForm {
51
52 // == Properties ===================================================
53
54 /** The sessionId the applet was created with */
55 protected String sessionId = "";
56 /** Get the sessionId the applet was created with */
57 public String getSessionId() { return this.sessionId; }
58 /** Set the sessionId the applet was created with */
59 public void setSessionId(String sessionId) { this.sessionId = sessionId; }
60
61 /** The class name of the EntityBean to update. */
62 protected String entityName = "";
63 /** Get the class name of the EntityBean to update */
64 public String getEntityName() { return this.entityName; }
65 /** Set the class name of the EntityBean to update */
66 public void setEntityName(String entityName) { this.entityName = entityName; }
67
68 /** The unique ID of the EntityBean to update */
69 protected int entityId = -1;
70 /** Get the unique ID of the EntityBean to update */
71 public int getEntityId() { return this.entityId; }
72 /** Set the unique ID of the EntityBean to update */
73 public void setEntityId(int entityId) { this.entityId = entityId; }
74
75 /** The property of the EntityBean to update (e.g. "gateway") */
76 protected String property = "";
77 /** Get the property of the EntityBean to update (e.g. "gateway") */
78 public String getProperty() { return this.property; }
79 /** Set the property of the EntityBean to update (e.g. "gateway") */
80 public void setProperty(String property) { this.property = property; }
81
82 /** The HTML from the editor to set the EntityBean's "property" to */
83 protected String editorHtml = "";
84 /** Get the HTML from the editor to set the EntityBean's "property" to */
85 public String getEditorHtml() { return this.editorHtml; }
86 /** Set the HTML from the editor to set the EntityBean's "property" to */
87 public void setEditorHtml(String editorHtml) { this.editorHtml = editorHtml; }
88
89 /** Have all the necessary properties been set? */
90 protected boolean isValid = false;
91 /** Get valid */
92 public boolean getIsValid() { return this.isValid; }
93 /** Set valid */
94 public void setIsValid(boolean isValid) { this.isValid = isValid; }
95
96
97
98 /** Reset all properties to default values.
99 * @param mapping The mapping used to select this instance
100 * @param request The servlet request we are processing
101 */
102 public void reset(ActionMapping mapping, HttpServletRequest request) {
103 setSessionId("");
104 setEntityName("");
105 setEntityId(-1);
106 setProperty("");
107 setEditorHtml("");
108 setIsValid(false);
109 }
110
111 /** Sets the property of the EntityBean specified by <code>this.getProperty()</code>, to the value of <code>this.getGateway()</code>
112 */
113 public EntityBean populateBean( EntityBean bean ) {
114
115 RuntimeParameters.logDebug(this, "Populating bean in HtmlInputPostForm");
116
117 // e.g. setGateway
118 String setName = "set"+getProperty().substring(0,1).toUpperCase()+getProperty().substring(1).toLowerCase();
119
120 RuntimeParameters.logDebug(this, "setName: "+setName);
121 RuntimeParameters.logDebug(this, "bean.getClass(): "+bean.getClass());
122
123 Method setPropertyMethod;
124 try {
125 setPropertyMethod = bean.getClass().getMethod( setName, new Class[] { Class.forName("java.lang.String") } );
126
127 RuntimeParameters.logDebug(this, "Method is of class: "+setPropertyMethod.getDeclaringClass()+", and is: "+setPropertyMethod.getName());
128
129 setPropertyMethod.invoke( bean, new Object[] { getEditorHtml() } );
130 } catch (Exception e) {
131 RuntimeParameters.logError( this, "Error setting the property for HtmlInputPostForm: "+e);
132 // We return a mapping, but will never actually see it
133 return null;
134 }
135
136 RuntimeParameters.logDebug(this, "Finished populating bean!");
137
138 return bean;
139 }
140
141
142
143 // == Other Methods ===================================================
144
145 /** Check if the session is valid; if not, just don't update.
146 * The errors will not be seen by the user, as the post request is silent.
147 */
148 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
149
150 ActionErrors errors = new ActionErrors();
151
152 RuntimeParameters.logDebug(this, "Validating HtmlInputPostForm - checking session id is valid! (well, will do, anyway!)");
153
154 RuntimeParameters.logDebug(this, "What do we have?");
155 RuntimeParameters.logDebug(this, "sessionId = "+getSessionId());
156 RuntimeParameters.logDebug(this, "entityName = "+getEntityName());
157 RuntimeParameters.logDebug(this, "entityId = "+getEntityId());
158 RuntimeParameters.logDebug(this, "property = "+getProperty());
159 RuntimeParameters.logDebug(this, "editorHtml = "+getEditorHtml());
160
161
162 // As these are all compulsory, make an error if any of them are missing
163 if (getSessionId().equals("")
164 || getEntityName().equals("")
165 || getEntityId() == -1
166 || getProperty().equals("")
167 || getEditorHtml().equals("")
168 ) {
169 //errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.editor.missingValues"));
170 setIsValid(false);
171 System.err.println("Some blank parameters passed into HtmlInputPostForm");
172 } else {
173 setIsValid(true);
174 }
175
176
177 // TO DO! Ticket http://intranet.runtime-collective.com/acs/ticket/issue-view?msg_id=2154
178 if (false) {
179 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.permission.invalidSession"));
180 }
181
182 return errors;
183 }
184
185
186 }