KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > dataobjects > jdbc > JoinedDigesterBean


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64 package com.jcorporate.expresso.core.dataobjects.jdbc;
65
66 import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
67 import org.apache.commons.digester.Digester;
68 import org.apache.log4j.Logger;
69 import org.xml.sax.SAXException JavaDoc;
70
71 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
72 import java.io.InputStream JavaDoc;
73 import java.net.URL JavaDoc;
74 import java.util.List JavaDoc;
75 import java.util.Map JavaDoc;
76 import java.util.StringTokenizer JavaDoc;
77
78
79 /**
80  * Not yet released - do not use in your own code yet.
81  * This class provides the Digester information to parse the join definitions
82  * so that we can define the join definition in an XML file.
83  *
84  * @author Michael Rimov
85  */

86
87 public class JoinedDigesterBean {
88
89     /**
90      * The log4j Logger
91      */

92     private static final Logger log = Logger.getLogger(JoinedDigesterBean.class);
93
94     /**
95      * A list of dataobjects
96      */

97     private List JavaDoc dataObjects = new java.util.ArrayList JavaDoc();
98
99     /**
100      * A list of relations.
101      */

102     private List JavaDoc relations = new java.util.ArrayList JavaDoc();
103
104
105     private Map JavaDoc permissions = new java.util.HashMap JavaDoc();
106
107     /**
108      * set whether the queries are distinct or not.
109      */

110     private boolean distinct = false;
111
112     /**
113      * Description of the join.
114      */

115     private String JavaDoc description = "";
116
117
118     public JoinedDigesterBean() {
119     }
120
121     public void addDataObject(String JavaDoc className, String JavaDoc definitionName, String JavaDoc alias, String JavaDoc fieldExpressionList) {
122         dataObjects.add(new DigesterJoinedDataObject(className, definitionName, alias, fieldExpressionList));
123     }
124
125     public void addDataObject(String JavaDoc className, String JavaDoc definitionName, String JavaDoc alias) {
126         addDataObject(className, definitionName, alias, null);
127     }
128
129     /**
130      * Adds a relation to the data we've collected from Digester
131      *
132      * @param localAlias the local 'short name' of the dbobject
133      * @param localKey the local 'key' of the dbobject [Pipe delimited for multiple keys]
134      * @param foreignAlias the foreign 'short name' of the dbobject
135      * @param foreignKey the foreign keys (Pipe Delimited for multiple keys)
136      * @param joinType The type of join. Either left|right|inner or blank
137      */

138     public void addRelation(String JavaDoc localAlias, String JavaDoc localKey,
139                             String JavaDoc foreignAlias, String JavaDoc foreignKey, String JavaDoc joinType) {
140         DigesterJoinRelations newRelation = new DigesterJoinRelations();
141
142         StringTokenizer JavaDoc stoklocal = new StringTokenizer JavaDoc(localKey, "|");
143         StringTokenizer JavaDoc stokForeign = new StringTokenizer JavaDoc(foreignKey, "|");
144         if (stoklocal.countTokens() != stokForeign.countTokens()) {
145             throw new IllegalArgumentException JavaDoc("Foreign key and local" +
146                     " key must have the same number of keys");
147         }
148         newRelation.setLocalAlias(localAlias);
149         newRelation.setLocalKey(localKey);
150         newRelation.setForeignAlias(foreignAlias);
151         newRelation.setForeignKey(foreignKey);
152         newRelation.setJoinType(joinType);
153         relations.add(newRelation);
154
155     }
156
157     /**
158      * Loads the join data by constructing the Digester object and then merging
159      * the given URL into the current data.
160      *
161      * @param location the location to load the data from.
162      */

163     public void loadJoinData(URL JavaDoc location) {
164         Digester d = buildDigester();
165         digest(d, location);
166     }
167
168     /**
169      * Set the permissions of the join
170      *
171      * @param create create permission
172      * @param read read permission
173      * @param update update permission
174      * @param delete delete permission
175      */

176     public void setPermissions(Boolean JavaDoc create, Boolean JavaDoc read, Boolean JavaDoc update, Boolean JavaDoc delete) {
177
178         permissions.put(SecuredDBObject.ADD, create);
179         permissions.put(SecuredDBObject.SEARCH, read);
180         permissions.put(SecuredDBObject.UPDATE, update);
181         permissions.put(SecuredDBObject.DELETE, delete);
182     }
183
184     /**
185      * Retrieve the permissions
186      *
187      * @return java.util.Map
188      */

189     public Map JavaDoc getPermissions() {
190         return permissions;
191     }
192
193     /**
194      * Builds the digester so it is ready to parse rules.
195      *
196      * @return a commons Digester instance with all rules built.
197      */

198     public Digester buildDigester() {
199         Digester digester = new Digester();
200         digester.setClassLoader(Thread.currentThread().getContextClassLoader());
201         setupResolvers(digester);
202
203         digester.push(this);
204         digester.addSetProperties("dataobject-join");
205         digester.addCallMethod("dataobject-join/dataobject", "addDataObject", 4);
206         digester.addCallParam("dataobject-join/dataobject", 0, "className");
207         digester.addCallParam("dataobject-join/dataobject", 1, "definitionName");
208         digester.addCallParam("dataobject-join/dataobject", 2, "alias");
209         digester.addCallParam("dataobject-join/dataobject", 3, "fieldExpressionList");
210
211         digester.addCallMethod("dataobject-join/distinct", "setDistinct", 1, new Class JavaDoc[]{java.lang.Boolean JavaDoc.class});
212         digester.addCallParam("dataobject-join/distinct", 0, "distinctJoin");
213
214         digester.addCallMethod("dataobject-join/permissions", "setPermissions", 4,
215                 new Class JavaDoc[]{java.lang.Boolean JavaDoc.class,
216                             java.lang.Boolean JavaDoc.class, java.lang.Boolean JavaDoc.class, java.lang.Boolean JavaDoc.class});
217         digester.addCallParam("dataobject-join/permissions", 0, "add");
218         digester.addCallParam("dataobject-join/permissions", 1, "read");
219         digester.addCallParam("dataobject-join/permissions", 2, "update");
220         digester.addCallParam("dataobject-join/permissions", 3, "delete");
221
222         digester.addCallMethod("dataobject-join/relations/foreign-key", "addRelation", 5);
223         digester.addCallParam("dataobject-join/relations/foreign-key", 0, "local-alias-ref");
224         digester.addCallParam("dataobject-join/relations/foreign-key", 1, "local-alias-key");
225         digester.addCallParam("dataobject-join/relations/foreign-key", 2, "foreign-alias-ref");
226         digester.addCallParam("dataobject-join/relations/foreign-key", 3, "foreign-alias-key");
227         digester.addCallParam("dataobject-join/relations/foreign-key", 4, "join-type");
228         return digester;
229     }
230
231     /**
232      * Sets up the appropriate locations to resolve the Doctypes
233      *
234      * @param digester the Apache digester to register the resolvers with.
235      */

236     private void setupResolvers(Digester digester) {
237         URL JavaDoc url = this.getClass().getResource("/com/jcorporate/expresso/core/dataobjects/jdbc/jdbc-join_5_1.dtd");
238         if (url != null) {
239             digester.register("-//Jcorporate Ltd//DTD Expresso DataObject Join 5.1//EN",
240                     url.toString());
241         } else {
242             throw new IllegalArgumentException JavaDoc("Unable to locate " +
243                     "jdbc-join_5_1.dtd in package");
244         }
245     }
246
247
248     /**
249      * Mergest the data from the given URL into the current object. If the load fails,
250      * nothing will be added, and a log will be noted
251      *
252      * @param parser the constructed Digester object
253      * @param location the URL location to load.
254      */

255     public void digest(Digester parser, URL JavaDoc location) {
256         try {
257             InputStream JavaDoc is = location.openStream();
258             parser.parse(is);
259         } catch (FactoryConfigurationError JavaDoc ex) {
260             log.error("Fatal error trying to find a suitable Digester compatible parser.", ex);
261         } catch (SAXException JavaDoc ex) {
262             log.error("Fatal error trying to digest expresso-services.xml file", ex);
263         } catch (java.io.IOException JavaDoc ex) {
264             log.error("Fatal IO error parsing input.", ex);
265         }
266
267     }
268
269     /**
270      * Retrieve a list of the dataobjects for this join.
271      *
272      * @return java.util.List of DigesterJoinedDataObject beans/
273      */

274     public List JavaDoc getDataObjects() {
275         return dataObjects;
276     }
277
278     /**
279      * Returns whether the join is distinct or not.
280      *
281      * @return true if the join needs to be distinct.
282      */

283     public boolean isDistinct() {
284         return distinct;
285     }
286
287     /**
288      * Sets whether the join is distinct or not. Should normally only be
289      * called by the digester
290      *
291      * @param distinct new distinct value.
292      */

293     public void setDistinct(boolean distinct) {
294         this.distinct = distinct;
295     }
296
297
298     /**
299      * Sets whether the join is distinct or not with a Boolean value. Expresso
300      * parses the value appropriately
301      *
302      * @param newValue the new String value
303      */

304     public void setDistinct(Boolean JavaDoc newValue) {
305         if (newValue != null) {
306             this.distinct = newValue.booleanValue();
307         }
308     }
309
310     /**
311      * Sets whether the join is distinct or not with a string value. Expresso
312      * parses the value appropriately
313      *
314      * @param newValue the new String value
315      */

316     public void setDistinct(String JavaDoc newValue) {
317         this.distinct = com.jcorporate.expresso.core.misc.StringUtil.toBoolean(newValue);
318     }
319
320
321     /**
322      * Retrieves a list of the relations with this bean.
323      *
324      * @return java.util.List of DigesterJoinRelations beans
325      */

326     public List JavaDoc getRelations() {
327         return relations;
328     }
329
330     public String JavaDoc getDescription() {
331         return description;
332     }
333
334     public void setDescription(String JavaDoc description) {
335         this.description = description;
336     }
337
338
339     /**
340      * Bean representing all the dataobject information. Instances of
341      * this bean are retrieved by getDataObjects();
342      * <p/>
343      * author Michael Rimov
344      */

345     public class DigesterJoinedDataObject {
346
347         private String JavaDoc definitionName;
348         private String JavaDoc alias;
349         private String JavaDoc className;
350         private String JavaDoc fieldExpressionList;
351
352         private DigesterJoinedDataObject() {
353         }
354
355         public DigesterJoinedDataObject(String JavaDoc name, String JavaDoc definition, String JavaDoc newAlias,
356                                         String JavaDoc fieldExpression) {
357             className = name;
358             definitionName = definition;
359             alias = newAlias;
360             fieldExpressionList = fieldExpression;
361         }
362
363         public String JavaDoc getClassName() {
364             return this.className;
365         }
366
367         public String JavaDoc getDefinitionName() {
368             return definitionName;
369         }
370
371         public void setDefinitionName(String JavaDoc definitionName) {
372             this.definitionName = definitionName;
373         }
374
375         public void setAlias(String JavaDoc alias) {
376             this.alias = alias;
377         }
378
379         public String JavaDoc getAlias() {
380             return alias;
381         }
382
383         public String JavaDoc getFieldExpressionList() {
384             return fieldExpressionList;
385         }
386
387     }
388
389     /**
390      * Bean representing all the joined relation information. Instances of
391      * this bean are retrieved by getRelations();
392      * <p/>
393      * author Michael Rimov
394      */

395     public class DigesterJoinRelations {
396
397         private String JavaDoc localAlias;
398         private String JavaDoc localKey;
399         private String JavaDoc foreignAlias;
400         private String JavaDoc foreignKey;
401         private String JavaDoc joinType;
402
403         public DigesterJoinRelations() {
404         }
405
406         public String JavaDoc getLocalAlias() {
407             return localAlias;
408         }
409
410         public void setLocalAlias(String JavaDoc localAlias) {
411             this.localAlias = localAlias;
412         }
413
414         public void setLocalKey(String JavaDoc localKey) {
415             this.localKey = localKey;
416         }
417
418         public String JavaDoc getLocalKey() {
419             return localKey;
420         }
421
422         public void setForeignAlias(String JavaDoc foreignAlias) {
423             this.foreignAlias = foreignAlias;
424         }
425
426         public String JavaDoc getForeignAlias() {
427             return foreignAlias;
428         }
429
430         public void setForeignKey(String JavaDoc foreignKey) {
431             this.foreignKey = foreignKey;
432         }
433
434         public String JavaDoc getForeignKey() {
435             return foreignKey;
436         }
437
438         public String JavaDoc getJoinType() {
439             return joinType;
440         }
441
442         public void setJoinType(String JavaDoc newValue) {
443             joinType = newValue;
444         }
445     }
446 }
447
Popular Tags