KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > foo > bar > BugInterceptor


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/examples/bugtracker/WEB-INF/src/com/foo/bar/BugInterceptor.java,v 1.6 2004/08/18 12:25:54 hkollmann Exp $
3  * $Revision: 1.6 $
4  * $Date: 2004/08/18 12:25:54 $
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
24 package com.foo.bar;
25
26 import org.dbforms.config.DbFormsConfig;
27 import org.dbforms.config.FieldValues;
28 import org.dbforms.config.Table;
29 import org.dbforms.config.ValidationException;
30
31 import org.dbforms.event.DbEventInterceptorSupport;
32
33 import java.sql.Connection JavaDoc;
34
35 import java.util.Calendar JavaDoc;
36 import java.util.GregorianCalendar JavaDoc;
37
38 import javax.servlet.http.HttpServletRequest JavaDoc;
39
40
41
42 /**
43  *
44  * Litte example of a database event listener/interceptor
45  * -----------------------------------------------------------
46  *
47  * We could do virtually _anything_ with this class, as we have - a database
48  * connection (-> we could "trigger" other functionality like
49  * datebase-logging,...) - a config object (-> config.getServletContext() gives
50  * access to other J2EE-resources) - a request object (-> gives access to many
51  * kinds of parameter info) - and in the preXxx()-methods there is a hashtable
52  * containg the actual row fieds. - in preInsert() and preUpdate() changes to
53  * this hashtable are automatically reflected in the database!
54  *
55  * In this concrete example we decide to just override 2 methods (preInsert and
56  * preUpdate) to do some basic validation checking. But keep in mind: this class
57  * has _much_ more potential for use in your apps
58  *
59  */

60 public class BugInterceptor extends DbEventInterceptorSupport {
61    /*
62     * private int checkCustomer(Hashtable fieldValues) throws
63     * ValidationException { // PHP/Perl programmers: the hashtabe works just
64     * like an "associative array" in PHP/Perl
65     *
66     * String lastName = (String) fieldValues.get("lastname"); String pCode =
67     * (String) fieldValues.get("pcode"); String city = (String)
68     * fieldValues.get("city");
69     *
70     * if( lastName == null || lastName.trim().length()==0 || pCode == null ||
71     * pCode.trim().length()==0 || city == null || city.trim().length()==0) {
72     *
73     * throw new ValidationException("Please fill out the form completly!"); }
74     * else return GRANT_OPERATION; }
75     */

76    public int preInsert(HttpServletRequest JavaDoc request,
77                         Table table,
78                         FieldValues fieldValues,
79                         DbFormsConfig config,
80                         Connection JavaDoc con) throws ValidationException {
81       Calendar JavaDoc calendar = new GregorianCalendar JavaDoc();
82       java.util.Date JavaDoc date = new java.util.Date JavaDoc();
83       calendar.setTime(date);
84
85       int year = calendar.get(Calendar.YEAR);
86       int month = calendar.get(Calendar.MONTH) + 1;
87       int day = calendar.get(Calendar.DAY_OF_MONTH) + 1;
88
89       StringBuffer JavaDoc dateBuf = new StringBuffer JavaDoc();
90       dateBuf.append(year);
91       dateBuf.append("-");
92       dateBuf.append(month);
93       dateBuf.append("-");
94       dateBuf.append(day);
95
96       setValue(table, fieldValues, "indate", dateBuf.toString());
97       setValue(table, fieldValues, "bugstate", "0");
98
99       return GRANT_OPERATION;
100    }
101
102
103    /**
104     * DOCUMENT ME!
105     *
106     * @param request DOCUMENT ME!
107     * @param table DOCUMENT ME!
108     * @param fieldValues DOCUMENT ME!
109     * @param config DOCUMENT ME!
110     * @param con DOCUMENT ME!
111     *
112     * @return DOCUMENT ME!
113     *
114     * @throws ValidationException DOCUMENT ME!
115     */

116    public int preUpdate(HttpServletRequest JavaDoc request,
117                         Table table,
118                         FieldValues fieldValues,
119                         DbFormsConfig config,
120                         Connection JavaDoc con) throws ValidationException {
121       int newState = Integer.parseInt(fieldValues.get("bugstate").getFieldValue());
122
123       if (newState == 2) {
124          Calendar JavaDoc calendar = new GregorianCalendar JavaDoc();
125          java.util.Date JavaDoc date = new java.util.Date JavaDoc();
126          calendar.setTime(date);
127
128          int year = calendar.get(Calendar.YEAR);
129          int month = calendar.get(Calendar.MONTH) + 1;
130          int day = calendar.get(Calendar.DAY_OF_MONTH) + 1;
131
132          StringBuffer JavaDoc dateBuf = new StringBuffer JavaDoc();
133          dateBuf.append(year);
134          dateBuf.append("-");
135          dateBuf.append(month);
136          dateBuf.append("-");
137          dateBuf.append(day);
138
139          setValue(table, fieldValues, "outdate", dateBuf.toString());
140       }
141
142       return GRANT_OPERATION;
143    }
144 }
145
Popular Tags