KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > webman > form > db > queries > FormExists


1 package de.webman.form.db.queries;
2
3 import java.sql.Connection JavaDoc;
4 import java.sql.SQLException JavaDoc;
5 import java.util.Enumeration JavaDoc;
6 import com.teamkonzept.db.TKPrepQuery;
7 import com.teamkonzept.lib.TKHashtable;
8 import de.webman.form.db.FieldConstants;
9 import de.webman.form.db.FormConstants;
10
11 /**
12  * Searches form definitions by their attributes in the database.
13  * This query is generic, i.e. the SQL statement is constructed at
14  * runtime depending on the parameters set. There has to be at least
15  * one parameter in order to generate a valid query.
16  * <TABLE>
17  * <TR>
18  * <TD><B>Order</B></TD>
19  * <TD><B>Name</B></TD>
20  * <TD><B>Type</B></TD>
21  * </TR>
22  * <TR>
23  * <TD COLSPAN="3"><I>Parameters</I></TD>
24  * </TR>
25  * <TR>
26  * <TD><TT>1</TT></TD>
27  * <TD><TT>de.webman.form.db.FormConstants.FORM_ID</TT></TD>
28  * <TD><TT>java.lang.Integer</TT></TD>
29  * </TR>
30  * <TR>
31  * <TD><TT>2</TT></TD>
32  * <TD><TT>de.webman.form.db.FormConstants.FORM_TYPE</TT></TD>
33  * <TD><TT>java.lang.Integer</TT></TD>
34  * </TR>
35  * <TR>
36  * <TD><TT>3</TT></TD>
37  * <TD><TT>de.webman.form.db.FieldConstants.FIELD_NAME</TT> or
38  * <TT>de.webman.form.db.FormConstants.FORM_NAME</TT></TD>
39  * <TD><TT>java.lang.String</TT></TD>
40  * </TR>
41  * <TR>
42  * <TD><TT>4</TT></TD>
43  * <TD><TT>de.webman.form.db.FieldConstants.FIELD_SHOW_NAME</TT> or
44  * <TT>de.webman.form.db.FormConstants.FORM_DESCRIPTION</TT></TD>
45  * <TD><TT>java.lang.String</TT></TD>
46  * </TR>
47  * <TR>
48  * <TD COLSPAN="3"><I>Results</I></TD>
49  * </TR>
50  * <TR>
51  * <TD><TT>1</TT></TD>
52  * <TD><TT>de.webman.form.db.FormConstants.FORM_ID</TT></TD>
53  * <TD><TT>java.lang.Integer</TT></TD>
54  * </TR>
55  * </TABLE>
56  *
57  * @author $Author: uli $
58  * @version $Revision: 1.3 $
59  */

60 public class FormExists
61     extends TKPrepQuery
62 {
63
64     // Constants.
65

66     /**
67      * The preparation state.
68      */

69     private final static boolean IS_PREPARED = true;
70
71     /**
72      * The relevance state.
73      */

74     private final static boolean[] SET_RELEVANTS =
75     {
76         true
77     };
78
79
80     // Implementation of 'com.teamkonzept.db.TKQuery'
81

82     /**
83      * Initializes the query with the given connection.
84      *
85      * @param connection the connection.
86      */

87     public final void initQuery (Connection JavaDoc connection)
88     {
89         super.initQuery(connection,
90                         IS_PREPARED,
91                         null,
92                         null,
93                         SET_RELEVANTS,
94                         null);
95     }
96
97     /**
98      * Executes the query.
99      *
100      * @return <CODE>true</CODE>, if the query has been executed
101      * successfully, otherwise <CODE>false</CODE>.
102      * @throws SQLException if any error occurred during query execution.
103      */

104     public final boolean execute ()
105         throws SQLException JavaDoc
106     {
107         // Initialize parameter order array.
108
String JavaDoc[] order = new String JavaDoc[super.queryParams.size()];
109
110         // Initialize parameter types hash.
111
TKHashtable types = new TKHashtable();
112
113         // Initialize SQL buffer.
114
StringBuffer JavaDoc sql = new StringBuffer JavaDoc("SELECT FO.FORM_ID FROM FORM FO, FIELD FI WHERE ");
115
116         // Initialize parameter order index.
117
int index = 0;
118
119         // Get parameter names.
120
Enumeration JavaDoc keys = super.queryParams.keys();
121
122         while (keys.hasMoreElements())
123         {
124             // Build SQL.
125
if (index > 0)
126             {
127                 sql.append("AND ");
128             }
129
130             // Get parameter name.
131
String JavaDoc parameter = keys.nextElement().toString();
132
133             // Build parameter order and increment index.
134
order[index++] = parameter;
135
136             if (FormConstants.COLUMN_NAMES[FormConstants.FORM_ID].equals(parameter))
137             {
138                 // Build parameter type.
139
types.put(parameter, FormConstants.COLUMN_TYPES[FormConstants.FORM_ID]);
140
141                 // Build SQL.
142
sql.append("FO.FORM_ID != ? ");
143
144                 // Proceed.
145
continue;
146             }
147
148             if (FormConstants.COLUMN_NAMES[FormConstants.FORM_TYPE].equals(parameter))
149             {
150                 // Build parameter type.
151
types.put(parameter, FormConstants.COLUMN_TYPES[FormConstants.FORM_TYPE]);
152
153                 // Build SQL.
154
sql.append("FO.FORM_TYPE = ? ");
155
156                 // Proceed.
157
continue;
158             }
159
160             if (FieldConstants.COLUMN_NAMES[FieldConstants.FIELD_NAME].equals(parameter) ||
161                 FormConstants.COLUMN_NAMES[FormConstants.FORM_NAME].equals(parameter))
162             {
163                 // Build parameter type.
164
types.put(parameter, FieldConstants.COLUMN_TYPES[FieldConstants.FIELD_NAME]);
165
166                 // Build SQL.
167
sql.append("FI.FIELD_NAME = ? ");
168
169                 // Proceed.
170
continue;
171             }
172
173             if (FieldConstants.COLUMN_NAMES[FieldConstants.FIELD_SHOW_NAME].equals(parameter) ||
174                 FormConstants.COLUMN_NAMES[FormConstants.FORM_DESCRIPTION].equals(parameter))
175             {
176                 // Build parameter type.
177
types.put(parameter, FieldConstants.COLUMN_TYPES[FieldConstants.FIELD_SHOW_NAME]);
178
179                 // Build SQL.
180
sql.append("FI.FIELD_SHOW_NAME = ? ");
181
182                 // Proceed.
183
continue;
184             }
185         }
186
187         // Build SQL.
188
sql.append("AND FO.FORM_TYPE != 42 AND FO.FORM_ID = FI.FORM_ID AND FI.FIELD_ID = 0");
189
190         // Set parameter order.
191
super.paramOrder = order;
192
193         // Set parameter types.
194
super.paramTypes = types;
195
196         // Set SQL string.
197
super.sqlString = sql.toString();
198
199         // Set JDBC statement.
200
super.stmt = super.conn.prepareStatement(super.sqlString);
201
202         return super.execute();
203     }
204
205 }
206
Popular Tags