KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > interceptors > BookstoreInsertDataInterceptor


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/examples/bookstore/WEB-INF/src/interceptors/BookstoreInsertDataInterceptor.java,v 1.7 2004/10/26 19:51:07 hkollmann Exp $
3  * $Revision: 1.7 $
4  * $Date: 2004/10/26 19:51:07 $
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 TemplePlace, Suite 330, Boston, MA 02111-1307 USA
22  */

23
24 package interceptors;
25
26 import org.dbforms.config.DbFormsConfig;
27 import org.dbforms.config.FieldValue;
28 import org.dbforms.config.FieldValues;
29 import org.dbforms.config.Table;
30 import org.dbforms.config.ValidationException;
31
32 import org.dbforms.event.DbEventInterceptorSupport;
33
34 import java.sql.Connection JavaDoc;
35 import java.sql.ResultSet JavaDoc;
36 import java.sql.SQLException JavaDoc;
37 import java.sql.Statement JavaDoc;
38
39 import javax.servlet.http.HttpServletRequest JavaDoc;
40
41
42
43 /**
44  * @author hkk
45  *
46  * Folgendes auswählen, um die Schablone für den erstellten Typenkommentar zu
47  * ändern: Fenster&gt;Benutzervorgaben&gt;Java&gt;Codegenerierung&gt;Code und
48  * Kommentare
49  */

50 public class BookstoreInsertDataInterceptor extends DbEventInterceptorSupport {
51    /**
52     * DOCUMENT ME!
53     *
54     * @param request DOCUMENT ME!
55     * @param table DOCUMENT ME!
56     * @param fieldValues DOCUMENT ME!
57     * @param config DOCUMENT ME!
58     * @param con DOCUMENT ME!
59     *
60     * @return DOCUMENT ME!
61     *
62     * @throws ValidationException DOCUMENT ME!
63     */

64    public int preInsert(HttpServletRequest JavaDoc request,
65                         Table table,
66                         FieldValues fieldValues,
67                         DbFormsConfig config,
68                         Connection JavaDoc con) throws ValidationException {
69       long new_id = 0;
70       String JavaDoc fieldName = table.getName() + "_ID";
71       FieldValue fv = fieldValues.get(fieldName);
72       if (fv != null) {
73          new_id = ((Integer JavaDoc) fv.getFieldValueAsObject()).intValue();
74       }
75
76       if (new_id == 0) {
77          String JavaDoc qry = "select max(" + fieldName + ") from " + table.getName();
78
79          try {
80             Statement JavaDoc stmt = con.createStatement();
81             ResultSet JavaDoc rs = stmt.executeQuery(qry);
82             rs.next();
83             new_id = rs.getLong(1);
84             stmt.close();
85          } catch (SQLException JavaDoc e) {
86             e.printStackTrace();
87          }
88
89          new_id++;
90          setValue(table, fieldValues, fieldName, String.valueOf(new_id));
91       }
92
93       return GRANT_OPERATION;
94    }
95 }
96
Popular Tags