KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > event > datalist > InsertEvent


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/event/datalist/InsertEvent.java,v 1.26 2004/10/20 10:51:32 hkollmann Exp $
3  * $Revision: 1.26 $
4  * $Date: 2004/10/20 10:51:32 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23 package org.dbforms.event.datalist;
24 import org.dbforms.config.DbEventInterceptor;
25 import org.dbforms.config.DbEventInterceptorData;
26 import org.dbforms.config.DbFormsConfig;
27 import org.dbforms.config.Field;
28 import org.dbforms.config.FieldValues;
29 import org.dbforms.config.GrantedPrivileges;
30
31 import org.dbforms.event.ValidationEvent;
32 import org.dbforms.event.datalist.dao.DataSourceFactory;
33 import org.dbforms.event.datalist.dao.DataSourceSessionList;
34
35 import org.dbforms.util.MessageResourcesInternal;
36 import org.dbforms.util.StringUtil;
37
38 import java.sql.Connection JavaDoc;
39 import java.sql.SQLException JavaDoc;
40
41 import java.util.Vector JavaDoc;
42
43 import javax.servlet.http.HttpServletRequest JavaDoc;
44
45
46
47 /**
48  * This event prepares and performs a SQL-Insert operation. <br>
49  * Works with new factory classes
50  *
51  * @author Henner Kollmann
52  */

53 public class InsertEvent extends ValidationEvent {
54    /**
55     * Creates a new InsertEvent object.
56     *
57     * @param tableId the table identifier
58     * @param keyId the key
59     * @param request the request object
60     * @param config the configuration object
61     */

62    public InsertEvent(Integer JavaDoc tableId, String JavaDoc keyId,
63       HttpServletRequest JavaDoc request, DbFormsConfig config) {
64       super(tableId.intValue(), keyId, request, config);
65    }
66
67
68    /**
69     * Constructor. <br>
70     * Insert actionbutton-strings is as follows: ac_insert_12_root_3 which is
71     * equivalent to: ac_insert : insert action event 12 : table id
72     * root : key 3 : button count used to identify individual
73     * insert buttons
74     *
75     * @param action the action string
76     * @param request the request object
77     * @param config the config object
78     */

79    public InsertEvent(String JavaDoc action, HttpServletRequest JavaDoc request,
80       DbFormsConfig config) {
81       super(StringUtil.getEmbeddedStringAsInteger(action, 2, '_'),
82          StringUtil.getEmbeddedString(action, 3, '_'), request, config);
83    }
84
85    /**
86     * Get the FieldValues object representing the collection of FieldValue
87     * objects builded from the request parameters
88     *
89     * @return the FieldValues object representing the collection of FieldValue
90     * objects builded from the request parameters
91     */

92
93    // must be public because protected will break cactus testing!
94
public FieldValues getFieldValues() {
95       return getFieldValues(true);
96    }
97
98
99    /**
100     * Process this event.
101     *
102     * @param con the jdbc connection object
103     *
104     * @throws SQLException if any data access error occurs
105     * @throws MultipleValidationException if any validation error occurs
106     */

107    public void processEvent(Connection JavaDoc con) throws SQLException JavaDoc {
108       // Applying given security contraints (as defined in dbforms-config xml file)
109
// part 1: check if requested privilge is granted for role
110
if (!hasUserPrivileg(GrantedPrivileges.PRIVILEG_INSERT)) {
111          String JavaDoc s = MessageResourcesInternal.getMessage("dbforms.events.insert.nogrant",
112                getRequest().getLocale(), new String JavaDoc[] {
113                   getTable().getName()
114                });
115          throw new SQLException JavaDoc(s);
116       }
117
118       FieldValues fieldValues = getFieldValues();
119
120       if (fieldValues.size() == 0) {
121          throw new SQLException JavaDoc("no parameters");
122       }
123
124       DbEventInterceptorData interceptorData = new DbEventInterceptorData(getRequest(),
125             getConfig(), con, getTable());
126       interceptorData.setAttribute(DbEventInterceptorData.FIELDVALUES,
127          fieldValues);
128
129       // process the interceptors associated to this table
130
int operation = getTable().processInterceptors(DbEventInterceptor.PRE_INSERT,
131             interceptorData);
132
133       if ((operation == DbEventInterceptor.GRANT_OPERATION)
134                && (fieldValues.size() > 0)) {
135          // End of interceptor processing
136
if (!checkSufficentValues(fieldValues)) {
137             throw new SQLException JavaDoc("unsufficent parameters");
138          }
139
140          // INSERT operation;
141
DataSourceSessionList ds = DataSourceSessionList.getInstance(getRequest());
142          DataSourceFactory qry = ds.get(getTable(), getRequest());
143
144          boolean own = false;
145
146          if (qry == null) {
147             qry = new DataSourceFactory((String JavaDoc) interceptorData
148                   .getAttribute(DbEventInterceptorData.CONNECTIONNAME),
149                   interceptorData.getConnection(), getTable());
150             own = true;
151          }
152
153          qry.doInsert(interceptorData, fieldValues);
154
155          if (own) {
156             qry.close();
157          } else {
158             ds.remove(getTable(), getRequest());
159          }
160
161          // Show the last record inserted
162
String JavaDoc firstPosition = getTable().getPositionString(fieldValues);
163          getRequest().setAttribute("firstpos_" + getTable().getId(),
164             firstPosition);
165
166          // finally, we process interceptor again (post-insert)
167
// process the interceptors associated to this table
168
getTable().processInterceptors(DbEventInterceptor.POST_INSERT,
169             interceptorData);
170       }
171    }
172
173
174    /**
175     * Check a list of conditions on the the input FieldValues object:
176     *
177     * <ul>
178     * <li>
179     * it must contains the same number of fields as the current main Table
180     * </li>
181     * <li>
182     * any autoInc field must NOT have a related FieldValue object (generated by
183     * a request parameter) because that field value must be calculated by the
184     * underlying RDBMS
185     * </li>
186     * </ul>
187     *
188     *
189     * @param fieldValues the FieldValues object containing the Field elements
190     * to check
191     *
192     * @return true if the all the above conditions are true, false otherwise
193     *
194     * @throws SQLException if any check condition fails
195     */

196    private boolean checkSufficentValues(FieldValues fieldValues)
197       throws SQLException JavaDoc {
198       Vector JavaDoc fields = getTable().getFields();
199
200       for (int i = 0; i < fields.size(); i++) {
201          Field field = (Field) fields.elementAt(i);
202
203          // if a field is a key and if it is NOT automatically generated,
204
// then it should be provided by the user
205
if (!field.hasAutoIncSet() && field.hasIsKeySet()) {
206             if (fieldValues.get(field.getName()) == null) {
207                throw new SQLException JavaDoc("Field " + field.getName()
208                   + " is missing");
209             }
210          }
211          // in opposite, if a field is automatically generated by the RDBMS, we need to
212
else if (field.hasAutoIncSet()) {
213             if (fieldValues.get(field.getName()) != null) {
214                throw new SQLException JavaDoc("Field " + field.getName()
215                   + " should be calculated by RDBMS, remove it from the form");
216             }
217          }
218
219          // in future we could do some other checks like NOT-NULL conditions,etc.
220
}
221
222       return true;
223    }
224 }
225
Popular Tags