KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > event > datalist > dao > DataSourceXML


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/event/datalist/dao/DataSourceXML.java,v 1.30 2005/02/19 21:26:30 hkollmann Exp $
3  * $Revision: 1.30 $
4  * $Date: 2005/02/19 21:26:30 $
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 org.dbforms.event.datalist.dao;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import org.dbforms.config.Constants;
30 import org.dbforms.config.DbFormsConfigRegistry;
31 import org.dbforms.config.Field;
32 import org.dbforms.config.FieldValue;
33 import org.dbforms.config.FieldValues;
34
35 import org.dbforms.dom.DOMFactory;
36
37 import org.dbforms.util.Util;
38
39 import org.w3c.dom.Document JavaDoc;
40 import org.w3c.dom.Element JavaDoc;
41
42 import java.net.URL JavaDoc;
43
44 import java.sql.Connection JavaDoc;
45 import java.sql.SQLException JavaDoc;
46
47 import java.util.Hashtable JavaDoc;
48 import java.util.Iterator JavaDoc;
49 import java.util.Vector JavaDoc;
50
51
52
53 /**
54  * Special implementation of DataSource. This class deals with xml data
55  *
56  * @author hkk
57  */

58 public class DataSourceXML extends DataSource {
59    private Hashtable JavaDoc keys;
60    private Log logCat = LogFactory.getLog(this.getClass().getName());
61    private String JavaDoc sqlFilter;
62    private XMLDataResult data;
63    private Object JavaDoc[][] dataObject = null;
64    private FieldValue[] filterConstraint;
65    private FieldValue[] orderConstraint;
66    private FieldValue[] sqlFilterParams;
67
68
69    /**
70     * Set the filterConstraint and orderConstraint used to build the SQL Select
71     * condition.
72     *
73     * @param filterConstraint FieldValue array used to build a cumulation of
74     * rules for filtering fields.
75     * @param orderConstraint FieldValue array used to build a cumulation of
76     * rules for ordering (sorting) and restricting fields.
77     * @param sqlFilter sql condition to add to where clause
78     * @param sqlFilterParams list of FieldValues to fill the sqlFilter with
79     */

80    public void setSelect(FieldValue[] filterConstraint,
81                          FieldValue[] orderConstraint,
82                          String JavaDoc sqlFilter,
83                          FieldValue[] sqlFilterParams) {
84       this.filterConstraint = filterConstraint;
85       this.orderConstraint = orderConstraint;
86       this.sqlFilter = sqlFilter;
87       this.sqlFilterParams = sqlFilterParams;
88    }
89
90
91    /**
92     * performs an update into the DataSource
93     *
94     * @param con DOCUMENT ME!
95     * @param fieldValues FieldValues to update
96     * @param keyValuesStr keyValueStr to the row to update<br>
97     * key format: FieldID ":" Length ":" Value<br>
98     * example: if key id = 121 and field id=2 then keyValueStr contains "2:3:121"<br>
99     * If the key consists of more than one fields, the key values are
100     * seperated through "-"<br>
101     * example: value of field 1=12, value of field 3=1992, then we'll
102     * get "1:2:12-3:4:1992"
103     *
104     * @throws SQLException if any error occurs
105     */

106    public void doUpdate(Connection JavaDoc con,
107                         FieldValues fieldValues,
108                         String JavaDoc keyValuesStr) throws SQLException JavaDoc {
109       Integer JavaDoc row = (Integer JavaDoc) keys.get(keyValuesStr);
110
111       if (row != null) {
112          int r = row.intValue();
113          Iterator JavaDoc iter = fieldValues.elements();
114
115          while (iter.hasNext()) {
116             FieldValue fv = (FieldValue) iter.next();
117             Field f = fv.getField();
118             data.setItemValue(r,
119                               Util.isNull(f.getExpression()) ? f.getName()
120                                                              : f.getExpression(),
121                               f.getType(), fv.getFieldValueAsObject());
122          }
123
124          dataObject[r] = null;
125       }
126    }
127
128
129    /**
130     * should retrieve the row at an special index as an Object[]
131     *
132     * @param currRow index of row to fetch
133     *
134     * @return Object[] of the fetched row
135     *
136     * @throws SQLException
137     */

138    protected final Object JavaDoc[] getRow(int currRow) throws SQLException JavaDoc {
139       if ((currRow < 0) || (currRow >= size())) {
140          return null;
141       }
142
143       if (dataObject[currRow] == null) {
144          Vector JavaDoc fields = getTable()
145                               .getFields();
146          Object JavaDoc[] objectRow = new Object JavaDoc[fields.size()];
147          String JavaDoc[] stringRow = new String JavaDoc[fields.size()];
148
149          for (int i = 0; i < fields.size(); i++) {
150             Field f = (Field) fields.elementAt(i);
151             objectRow[i] = data.getItemValue(currRow,
152                                              Util.isNull(f.getExpression())
153                                              ? f.getName()
154                                              : f.getExpression(), f.getType());
155             stringRow[i] = (objectRow[i] != null) ? objectRow[i].toString()
156                                                   : null;
157          }
158
159          String JavaDoc key = getTable()
160                          .getKeyPositionString(stringRow);
161          keys.put(key, new Integer JavaDoc(currRow));
162          dataObject[currRow] = objectRow;
163       }
164
165       return dataObject[currRow];
166    }
167
168
169    /**
170     * should close all open datasets
171     */

172    protected final void close() {
173       if ((data != null) && data.hasChanged()) {
174          try {
175             String JavaDoc url = getFilePath() + getQuery();
176             write(url, data.getRoot());
177          } catch (Exception JavaDoc e) {
178             logCat.error(e);
179          }
180       }
181
182       if (keys != null) {
183          keys.clear();
184       }
185
186       dataObject = null;
187    }
188
189
190    /**
191     * maps the startRow to the internal index
192     *
193     * @param startRow keyValueStr to the row<br>
194     * key format: FieldID ":" Length ":" Value<br>
195     * example: if key id = 121 and field id=2 then keyValueStr contains "2:3:121"<br>
196     * If the key consists of more than one fields, the key values are
197     * seperated through "-"<br>
198     * example: value of field 1=12, value of field 3=1992, then we'll
199     * get "1:2:12-3:4:1992"
200     *
201     * @return the index of the row, 0 as first row if not found
202     *
203     * @throws SQLException
204     */

205    protected final int findStartRow(String JavaDoc startRow) throws SQLException JavaDoc {
206       Integer JavaDoc res = null;
207
208       if (!Util.isNull(startRow)) {
209          res = (Integer JavaDoc) keys.get(startRow);
210       }
211
212       return (res != null) ? res.intValue()
213                            : 0;
214    }
215
216
217    /**
218     * Will be called to open all datasets
219     *
220     * @throws SQLException
221     */

222    protected final void open() throws SQLException JavaDoc {
223       if (dataObject == null) {
224          try {
225             String JavaDoc qry = getQuery();
226             String JavaDoc url = getFilePath() + qry;
227
228             try {
229                URL JavaDoc u = new URL JavaDoc(url);
230                qry = u.getQuery();
231             } catch (Exception JavaDoc e) {
232                logCat.info("open", e);
233             }
234
235             Document JavaDoc doc = read(url);
236
237             if (doc != null) {
238                Element JavaDoc elem = doc.getDocumentElement();
239                data = new XMLDataResult(elem, qry);
240             }
241          } catch (Exception JavaDoc e) {
242             logCat.error("open", e);
243             throw new SQLException JavaDoc(e.getMessage());
244          }
245
246          keys = new Hashtable JavaDoc();
247          dataObject = new Object JavaDoc[size()][];
248       }
249    }
250
251
252    /**
253     * Must return the size of the whole resultset with all data fetch
254     *
255     * @return size of whole resultset
256     *
257     * @throws SQLException
258     */

259    protected final int size() throws SQLException JavaDoc {
260       return (data != null) ? data.size()
261                             : 0;
262    }
263
264
265    /**
266     * return true if there are more records to fetch then the given record
267     * number
268     *
269     * @param i index of last fetched row.
270     *
271     * @return true if there are more records to fetch then the given record
272     * number
273     *
274     * @throws SQLException
275     */

276    protected boolean hasMore(int i) throws SQLException JavaDoc {
277       return (i < size());
278    }
279
280
281    /**
282     * gets the document from the remote system.
283     *
284     * @param url the uri to query
285     *
286     * @return NODE the result
287     *
288     * @throws Exception Exception during processing IO
289     */

290    protected Document JavaDoc read(String JavaDoc url) throws Exception JavaDoc {
291       return DOMFactory.instance()
292                        .read(url);
293    }
294
295
296    /**
297     * saves the document to the remote system.
298     *
299     * @param url DOCUMENT ME!
300     * @param root DOCUMENT ME!
301     *
302     * @throws Exception Exception during processing IO
303     */

304    protected void write(String JavaDoc url,
305                         Element JavaDoc root) throws Exception JavaDoc {
306       DOMFactory.instance()
307                 .write(url, root);
308    }
309
310
311    private String JavaDoc getFilePath() throws Exception JavaDoc {
312       return Util.replaceRealPath(getTable().getAlias(),
313                                   DbFormsConfigRegistry.instance().lookup().getRealPath());
314    }
315
316
317    private String JavaDoc getQuery() throws SQLException JavaDoc {
318       StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
319
320       String JavaDoc filter = getWhereClause();
321       String JavaDoc psqlFilter = getSQLFilter();
322
323       if (!Util.isNull(filter) || !Util.isNull(sqlFilter)) {
324          buf.append("[");
325          buf.append(filter);
326
327          if (!Util.isNull(psqlFilter)) {
328             if (!Util.isNull(filter)) {
329                buf.append(" and ");
330             }
331
332             buf.append(sqlFilter);
333          }
334
335          buf.append("]");
336       }
337
338       return buf.toString();
339    }
340
341
342    private String JavaDoc getSQLFilter() {
343       /** substitute ? with corresponding value in list */
344       int p1 = 0;
345       int p2 = sqlFilter.indexOf('?', p1);
346       StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
347       int cnt = 0;
348
349       while (p2 > -1) {
350          // add the string before the next ?
351
buf.append(sqlFilter.substring(p1, p2));
352
353          // if values are exausted, then abort
354
if (cnt >= sqlFilterParams.length) {
355             logCat.error("reference to a missing filterValue in " + sqlFilter);
356
357             return null;
358          }
359
360          // retrieve value
361
String JavaDoc value = sqlFilterParams[cnt].getFieldValue();
362
363          if (!Util.isNull(value)) {
364             // add value to string gbuffer
365
buf.append("\"");
366             buf.append(value);
367             buf.append("\"");
368          }
369
370          // restart search from next char after ?
371
p1 = p2 + 1;
372          p2 = sqlFilter.indexOf('?', p1);
373          cnt++;
374       }
375
376       // add remaining part of string
377
buf.append(sqlFilter.substring(p1));
378
379       return buf.toString();
380    }
381
382
383    private String JavaDoc getWhereClause() throws SQLException JavaDoc {
384       StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
385
386       if (!FieldValue.isNull(filterConstraint)) {
387          for (int i = 0; i < filterConstraint.length; i++) {
388             if (i != 0) {
389                if (filterConstraint[i].getLogicalOR()) {
390                   buf.append(" or ");
391                } else {
392                   buf.append(" and ");
393                }
394             }
395
396             Field f = filterConstraint[i].getField();
397             buf.append(Util.isNull(f.getExpression()) ? f.getName()
398                                                       : f.getExpression());
399
400             // Check what type of operator is required
401
switch (filterConstraint[i].getOperator()) {
402                case Constants.FILTER_EQUAL:
403                   buf.append("=");
404
405                   break;
406
407                case Constants.FILTER_NOT_EQUAL:
408                   buf.append("!=");
409
410                   break;
411
412                case Constants.FILTER_GREATER_THEN:
413                   buf.append("&gt;");
414
415                   break;
416
417                case Constants.FILTER_SMALLER_THEN:
418                   buf.append("&lt;");
419
420                   break;
421
422                case Constants.FILTER_GREATER_THEN_EQUAL:
423                   buf.append("&gt;=");
424
425                   break;
426
427                case Constants.FILTER_SMALLER_THEN_EQUAL:
428                   buf.append("&lt;=");
429
430                   break;
431             }
432
433             buf.append("\"");
434             buf.append(filterConstraint[i].getFieldValueAsObject().toString());
435             buf.append("\"");
436          }
437       }
438
439       return buf.toString();
440    }
441 }
442
Popular Tags