KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > odmg > OQLTest


1 package org.apache.ojb.odmg;
2
3 import java.io.Serializable JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Collection JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8
9 import org.apache.commons.lang.SerializationUtils;
10 import org.apache.ojb.junit.ODMGTestCase;
11 import org.apache.ojb.odmg.shared.Article;
12 import org.apache.ojb.odmg.shared.Person;
13 import org.apache.ojb.odmg.shared.PersonImpl;
14 import org.apache.ojb.odmg.shared.ProductGroup;
15 import org.odmg.OQLQuery;
16 import org.odmg.Transaction;
17
18 /**
19  * OQLQuery tests.
20  *
21  * @version $Id: OQLTest.java,v 1.16.2.3 2005/03/09 16:30:36 arminw Exp $
22  */

23 public class OQLTest extends ODMGTestCase
24 {
25     private int COUNT = 10;
26     private int id_filter = 2;
27
28     public static void main(String JavaDoc[] args)
29     {
30         String JavaDoc[] arr = {OQLTest.class.getName()};
31         junit.textui.TestRunner.main(arr);
32     }
33
34     public OQLTest(String JavaDoc name)
35     {
36         super(name);
37     }
38
39     private void createPersons()
40             throws Exception JavaDoc
41     {
42         Transaction tx = odmg.newTransaction();
43         tx.begin();
44         for (int i = 0; i < COUNT; i++)
45         {
46             Person aPerson = new PersonImpl();
47             aPerson.setFirstname("firstname" + i);
48             aPerson.setLastname("lastname" + i);
49             database.makePersistent(aPerson);
50         }
51         tx.commit();
52     }
53
54     private void deleteData(Class JavaDoc target)
55             throws Exception JavaDoc
56     {
57         Transaction tx = odmg.newTransaction();
58         tx.begin();
59         OQLQuery query = odmg.newOQLQuery();
60         query.create("select allStuff from " + target.getName());
61         Collection JavaDoc allTargets = (Collection JavaDoc) query.execute();
62         Iterator JavaDoc it = allTargets.iterator();
63         while (it.hasNext())
64         {
65             database.deletePersistent(it.next());
66         }
67         tx.commit();
68     }
69
70     public void testMultipleLoad() throws Exception JavaDoc
71     {
72         String JavaDoc name = "testMultipleLoad_" + System.currentTimeMillis();
73         Fish fish = new Fish();
74         fish.setName(name);
75         fish.setTypeOfWater("normal");
76
77         TransactionExt tx = (TransactionExt) odmg.newTransaction();
78         tx.begin();
79         odmg.getDatabase(null).makePersistent(fish);
80         tx.commit();
81
82         Fish newFish = (Fish) SerializationUtils.clone(fish);
83         newFish.setTypeOfWater("salty");
84
85         tx = (TransactionExt) odmg.newTransaction();
86         tx.begin();
87         tx.getBroker().clearCache();
88
89         OQLQuery query = odmg.newOQLQuery();
90
91         // Query current fish for comparison
92
query.create("select object from " + Fish.class.getName() + " where foodId=$1");
93         query.bind(new Integer JavaDoc(newFish.getFoodId()));
94         List JavaDoc result = (List JavaDoc) query.execute();
95
96         assertNotNull(result);
97         assertEquals(1, result.size());
98         Fish current = (Fish) result.get(0);
99
100         assertEquals(name, current.getName());
101         assertEquals("normal", current.getTypeOfWater());
102
103         assertEquals("salty", newFish.getTypeOfWater());
104         tx.lock(newFish, Transaction.WRITE);
105         tx.markDirty(newFish);
106         tx.commit();
107
108         // check for changes allow using cache
109
tx = (TransactionExt) odmg.newTransaction();
110         tx.begin();
111         query = odmg.newOQLQuery();
112
113         // Query current fish for comparison
114
query.create("select object from " + Fish.class.getName() + " where foodId=$1");
115         query.bind(new Integer JavaDoc(newFish.getFoodId()));
116         result = (List JavaDoc) query.execute();
117         tx.commit();
118         
119         assertNotNull(result);
120         assertEquals(1, result.size());
121         current = (Fish) result.get(0);
122         assertEquals(name, current.getName());
123         assertEquals("salty", current.getTypeOfWater());
124
125         // check for changes without using cache
126
tx = (TransactionExt) odmg.newTransaction();
127         tx.begin();
128         // clear cache to lookup object from DB
129
tx.getBroker().clearCache();
130         query = odmg.newOQLQuery();
131
132         // Query current fish for comparison
133
query.create("select object from " + Fish.class.getName() + " where foodId=$1");
134         query.bind(new Integer JavaDoc(newFish.getFoodId()));
135         result = (List JavaDoc) query.execute();
136         tx.commit();
137
138         assertNotNull(result);
139         assertEquals(1, result.size());
140         current = (Fish) result.get(0);
141         assertEquals(name, current.getName());
142         assertEquals("salty", current.getTypeOfWater());
143     }
144
145     /**
146      * test the following conditions:
147      * != support
148      * literal support at binding.
149      * A prior bug where criteria was reporting as bindable when it was in fact a literal.
150      */

151     public void testGetWithLiteral() throws Exception JavaDoc
152     {
153         createPersons();
154
155         // 3. Get a list of some articles
156
Transaction tx = odmg.newTransaction();
157         tx.begin();
158
159         OQLQuery query = odmg.newOQLQuery();
160         String JavaDoc sql = "select allPersons from " + Person.class.getName() + " where id != 5 and id > $1";
161         query.create(sql);
162         query.bind(new Integer JavaDoc(id_filter));
163
164         Collection JavaDoc allPersons = (Collection JavaDoc) query.execute();
165
166         // Iterator over the restricted articles objects
167
java.util.Iterator JavaDoc it = allPersons.iterator();
168
169         while (it.hasNext())
170         {
171             /**
172              * just make sure it's a string.
173              */

174             Object JavaDoc result = it.next();
175             Person value = (Person) result;
176             if (value.getId() <= id_filter)
177                 fail("oql didn't filter, got id (" + value.getId() + " where it should have been over " + id_filter);
178         }
179         tx.commit();
180     }
181
182     public void testQueryIn() throws Exception JavaDoc
183     {
184         // deleteData(database, odmg, Article.class);
185

186         Transaction tx = odmg.newTransaction();
187         tx.begin();
188
189         OQLQuery query1 = odmg.newOQLQuery();
190         query1.create("select anArticle from " + Article.class.getName() + " where articleId in(30, 31, 32) order by articleId");
191         List JavaDoc result1 = (List JavaDoc) query1.execute();
192
193         Collection JavaDoc ids = new ArrayList JavaDoc();
194         ids.add(new Integer JavaDoc(30));
195         ids.add(new Integer JavaDoc(31));
196         ids.add(new Integer JavaDoc(32));
197         OQLQuery query2 = odmg.newOQLQuery();
198         query2.create("select anArticle from " + Article.class.getName() + " where articleId in($1) order by articleId");
199         query2.bind(ids);
200         List JavaDoc result2 = (List JavaDoc) query2.execute();
201
202         assertEquals(result1.size(), result2.size());
203         tx.commit();
204     }
205
206     public void testQueryNull() throws Exception JavaDoc
207     {
208         Transaction tx = odmg.newTransaction();
209         tx.begin();
210
211         OQLQuery query1 = odmg.newOQLQuery();
212         query1.create("select anArticle from " + Article.class.getName() + " where is_undefined(articleName)");
213         List JavaDoc result1 = (List JavaDoc) query1.execute();
214
215         OQLQuery query2 = odmg.newOQLQuery();
216         query2.create("select anArticle from " + Article.class.getName() + " where articleName = nil");
217         List JavaDoc result2 = (List JavaDoc) query2.execute();
218
219         assertEquals(result1.size(), result2.size());
220         tx.commit();
221     }
222
223     public void testQueryNotNull() throws Exception JavaDoc
224     {
225         Transaction tx = odmg.newTransaction();
226         tx.begin();
227
228         OQLQuery query1 = odmg.newOQLQuery();
229         query1.create("select anArticle from " + Article.class.getName() + " where is_defined(articleName)");
230         List JavaDoc result1 = (List JavaDoc) query1.execute();
231
232         OQLQuery query2 = odmg.newOQLQuery();
233         query2.create("select anArticle from " + Article.class.getName() + " where articleName <> nil");
234         List JavaDoc result2 = (List JavaDoc) query2.execute();
235
236         assertEquals(result1.size(), result2.size());
237         tx.commit();
238     }
239
240     public void testQueryBetween() throws Exception JavaDoc
241     {
242         Transaction tx = odmg.newTransaction();
243
244         tx.begin();
245
246         OQLQuery query1 = odmg.newOQLQuery();
247         query1.create("select anArticle from " + Article.class.getName() + " where articleId between 30 and 32 order by articleId desc");
248         List JavaDoc result1 = (List JavaDoc) query1.execute();
249
250         OQLQuery query2 = odmg.newOQLQuery();
251         query2.create("select anArticle from " + Article.class.getName() + " where articleId between $1 and $2 order by articleId asc");
252         query2.bind(new Integer JavaDoc(30));
253         query2.bind(new Integer JavaDoc(32));
254         List JavaDoc result2 = (List JavaDoc) query2.execute();
255
256 // System.out.println("#### OQLTest#testQueryBetween(): Size result_1=" + result1.size()
257
// + ", size result_2=" + result2.size());
258
// for(int i = 0; i < result1.size(); i++)
259
// {
260
// Article a = (Article) result1.get(i);
261
// System.out.println("Article_Query_1: articles_in_group=" + a.getProductGroup().getAllArticlesInGroup().size()
262
// + " - " + a);
263
// }
264
//
265
// for(int i = 0; i < result2.size(); i++)
266
// {
267
// Article a = (Article) result2.get(i);
268
// System.out.println("Article_Query_2: articles_in_group=" + a.getProductGroup().getAllArticlesInGroup().size()
269
// + " - " + a);
270
// }
271
tx.commit();
272
273         tx.begin();
274         OQLQuery query3 = odmg.newOQLQuery();
275         query3.create("select Article from " + Article.class.getName() + " where articleId between $1 and $2 order by articleId asc");
276         query3.bind(new Integer JavaDoc(30));
277         query3.bind(new Integer JavaDoc(32));
278         List JavaDoc result3 = (List JavaDoc) query3.execute();
279
280         OQLQuery query4 = odmg.newOQLQuery();
281         query4.create("select Article from " + Article.class.getName() + " where articleId between 30 and 32 order by articleId desc");
282         List JavaDoc result4 = (List JavaDoc) query4.execute();
283
284 // for(int i = 0; i < result3.size(); i++)
285
// {
286
// Article a = (Article) result3.get(i);
287
// System.out.println("Article_Query_3: articles_in_group=" + a.getProductGroup().getAllArticlesInGroup().size()
288
// + " - " + a);
289
// }
290
//
291
// for(int i = 0; i < result4.size(); i++)
292
// {
293
// Article a = (Article) result4.get(i);
294
// System.out.println("Article_Query_4: articles_in_group=" + a.getProductGroup().getAllArticlesInGroup().size()
295
// + " - " + a);
296
// }
297
tx.commit();
298
299         assertEquals(result1.size(), result2.size());
300         assertEquals(result1.size(), result3.size());
301         assertEquals(result2.size(), result3.size());
302         assertEquals(result1.size(), result4.size());
303         assertEquals(result2.size(), result4.size());
304         assertEquals(result1, result4);
305         assertEquals(result2, result3);
306     }
307
308     public void testInListQuery() throws Exception JavaDoc
309     {
310         //objects that are part of a 1:n relation, i.e. they have fk-fields
311
Mammal elephant = new Mammal(4, "Minnie", 4);
312         Mammal cat = new Mammal(4, "Winston", 4);
313         Reptile snake = new Reptile(4, "Skuzzlebutt", "green");
314
315         Transaction tx = odmg.newTransaction();
316         tx.begin();
317         OQLQuery query = odmg.newOQLQuery();
318         query.create("select animals from " + InterfaceAnimal.class.getName() +
319                      " where name in list (\"Minnie\", \"Winston\", \"Skuzzlebutt\")");
320         int before = ((Collection JavaDoc) query.execute()).size();
321         tx.commit();
322
323
324         tx = odmg.newTransaction();
325         tx.begin();
326         database.makePersistent(elephant);
327         database.makePersistent(cat);
328         database.makePersistent(snake);
329         tx.commit();
330
331         tx = odmg.newTransaction();
332         tx.begin();
333         List JavaDoc animals = (List JavaDoc) query.execute();
334         tx.commit();
335         assertEquals(before + 3, animals.size());
336     }
337
338     public void testPrefetchQuery() throws Exception JavaDoc
339     {
340         String JavaDoc oql = "select allProductGroups from " + ProductGroup.class.getName()
341                 + " where groupId <= $1 order by groupId prefetch allArticlesInGroup";
342         OQLQuery query = odmg.newOQLQuery();
343         query.create(oql);
344         query.bind(new Integer JavaDoc(5));
345         Transaction tx = odmg.newTransaction();
346         tx.begin();
347         Collection JavaDoc results = (Collection JavaDoc) query.execute();
348         tx.commit();
349         assertNotNull(results);
350         assertTrue(results.size() > 0);
351     }
352
353     public void testInterfaceQuery() throws Exception JavaDoc
354     {
355         int age = (int)(Math.random() * Integer.MAX_VALUE);
356         int calories = (int)(Math.random() * Integer.MAX_VALUE);
357         int caloriesOther = (int)(Math.random() * Integer.MAX_VALUE);
358
359         //objects that are part of a 1:n relation, i.e. they have fk-fields
360
Mammal elephant = new Mammal(age, "Jumbo", 4);
361         Mammal cat = new Mammal(age, "Silvester", 4);
362         Reptile snake = new Reptile(age, "Kaa", "green");
363
364         //objects that are independent or part of m:n relations, i.e. they
365
//don't have fk-fields
366
Fish tuna = new Fish("tuna", calories, "salt");
367         Fish trout = new Fish("trout", calories, "fresh water");
368
369         Salad radiccio = new Salad("Radiccio", calories, "red");
370         Salad lolloverde = new Salad("Lollo verde", caloriesOther, "green");
371
372         deleteData(InterfaceAnimal.class);
373         deleteData(InterfaceFood.class);
374
375         Transaction tx = odmg.newTransaction();
376         tx.begin();
377         database.makePersistent(elephant);
378         database.makePersistent(cat);
379         database.makePersistent(snake);
380         database.makePersistent(tuna);
381         database.makePersistent(trout);
382         database.makePersistent(radiccio);
383         database.makePersistent(lolloverde);
384         tx.commit();
385
386         tx = odmg.newTransaction();
387         tx.begin();
388         OQLQuery query = odmg.newOQLQuery();
389         query.create("select animals from " + InterfaceAnimal.class.getName() +
390                      " where age=$1");
391         query.bind(new Integer JavaDoc(age));
392         List JavaDoc animals = (List JavaDoc) query.execute();
393         tx.commit();
394         Iterator JavaDoc it = animals.iterator();
395         while (it.hasNext())
396         {
397             Object JavaDoc obj = it.next();
398             // System.out.println(obj);
399
}
400         assertEquals(3, animals.size());
401
402         //test independent objects
403
query = odmg.newOQLQuery();
404         tx.begin();
405         query.create("select food from " + InterfaceFood.class.getName() +
406                      " where calories=$1");
407         query.bind(new Integer JavaDoc(calories));
408         List JavaDoc food = (List JavaDoc) query.execute();
409         tx.commit();
410         assertEquals(3, food.size());
411     }
412
413     /**
414      *
415      */

416     public void _testFunctions() throws Exception JavaDoc
417     {
418         Transaction tx = odmg.newTransaction();
419         tx.begin();
420
421         OQLQuery query = odmg.newOQLQuery();
422         query.create("select anArticle from " +
423                 Article.class.getName() +
424                 " where upper(articleName) like \"A%\" ");
425
426         List JavaDoc results = (List JavaDoc) query.execute();
427         tx.commit();
428         assertTrue(results.size() > 0);
429     }
430
431     /**
432      * ReportQuery returning rows with summed stock and price per article group
433      */

434     public void testReportQueryGroupBy() throws Exception JavaDoc
435     {
436         Transaction tx = odmg.newTransaction();
437         tx.begin();
438
439         OQLQuery query = odmg.newOQLQuery();
440         query.create("select p.groupName, p.allArticlesInGroup.stock, p.allArticlesInGroup.price" +
441                " from " + ProductGroup.class.getName() +
442                " group by groupName, allArticlesInGroup.stock, allArticlesInGroup.price");
443
444 // query.create("select p.groupName, sum(p.allArticlesInGroup.stock), sum(p.allArticlesInGroup.price)" +
445
// " from " + ProductGroup.class.getName() +
446
// "by groupName, allArticlesInGroup.stock, allArticlesInGroup.price");
447

448         List JavaDoc results = (List JavaDoc) query.execute();
449         tx.commit();
450         assertTrue(results.size() > 0);
451     }
452
453     public void testRepeatableQuery() throws Exception JavaDoc
454     {
455         deleteData(Person.class);
456         createPersons();
457
458         // 3. Get a list of some articles
459
Transaction tx = odmg.newTransaction();
460         tx.begin();
461
462         OQLQuery query = odmg.newOQLQuery();
463         String JavaDoc sql = "select allPersons from " + Person.class.getName() + " where id != $1 and id > $2";
464         query.create(sql);
465         query.bind(new Integer JavaDoc(5));
466         query.bind(new Integer JavaDoc(id_filter));
467
468         Collection JavaDoc allPersons = (Collection JavaDoc) query.execute();
469         // Iterator over the restricted articles objects
470
Iterator JavaDoc it = allPersons.iterator();
471         while (it.hasNext())
472         {
473             /**
474              * just make sure it's a string.
475              */

476             Object JavaDoc result = it.next();
477             Person value = (Person) result;
478             if (value.getId() <= id_filter || value.getId()==5)
479                 fail("oql didn't filter, got id (" + value.getId() + " where it should have been over "
480                         + id_filter + " and not 5");
481         }
482         tx.commit();
483
484         // now we try to reuse OQLQuery
485
tx.begin();
486
487         query.bind(new Integer JavaDoc(8));
488         query.bind(new Integer JavaDoc(id_filter));
489         allPersons = (Collection JavaDoc) query.execute();
490         // Iterator over the restricted articles objects
491
it = allPersons.iterator();
492         while (it.hasNext())
493         {
494             /**
495              * just make sure it's a string.
496              */

497             Object JavaDoc result = it.next();
498             Person value = (Person) result;
499             if (value.getId() <= id_filter || value.getId()==8)
500                 fail("oql didn't filter, got id (" + value.getId() + " where it should have been over "
501                         + id_filter + " and not 8");
502         }
503
504         // reuse OQLQuery within same tx
505
query.bind(new Integer JavaDoc(9));
506         query.bind(new Integer JavaDoc(id_filter));
507         allPersons = (Collection JavaDoc) query.execute();
508         // Iterator over the restricted articles objects
509
it = allPersons.iterator();
510         while (it.hasNext())
511         {
512             /**
513              * just make sure it's a string.
514              */

515             Object JavaDoc result = it.next();
516             Person value = (Person) result;
517             if (value.getId() <= id_filter || value.getId()==9)
518                 fail("oql didn't filter, got id (" + value.getId() + " where it should have been over "
519                         + id_filter + " and not 9");
520         }
521         tx.commit();
522     }
523
524
525     /**
526      * test Subquery
527      * get all articles with price > avg(price)
528      * PROBLEM: avg(price) is NOT extent aware !!
529      *
530      * test may fail if db does not support sub queries
531      */

532     public void _testSubQuery1() throws Exception JavaDoc
533     {
534         Transaction tx = odmg.newTransaction();
535         tx.begin();
536
537         OQLQuery query = odmg.newOQLQuery();
538         query.create("select anArticle from " +
539                 Article.class.getName() +
540                 " where " +
541                 " price >= (select avg(price) from " +
542                 Article.class.getName() +
543                 " where articleName like \"A%\") ");
544
545         List JavaDoc results = (List JavaDoc) query.execute();
546         tx.commit();
547         assertTrue(results.size() > 0);
548     }
549
550
551     //*******************************************************************
552
// test classes start here
553
//*******************************************************************
554
public interface InterfaceAnimal extends Serializable JavaDoc
555     {
556         int getAge();
557         String JavaDoc getName();
558     }
559
560     public interface InterfaceFood extends Serializable JavaDoc
561     {
562         String JavaDoc getName();
563         int getCalories();
564
565     }
566
567     public static abstract class AbstractAnimal
568     {
569         int animalId;
570         String JavaDoc name;
571         Integer JavaDoc zooId;
572
573         public int getAnimalId()
574         {
575             return animalId;
576         }
577
578         public void setAnimalId(int animalId)
579         {
580             this.animalId = animalId;
581         }
582
583         public String JavaDoc getName()
584         {
585             return name;
586         }
587
588         public void setName(String JavaDoc name)
589         {
590             this.name = name;
591         }
592
593         public Integer JavaDoc getZooId()
594         {
595             return zooId;
596         }
597
598         public void setZooId(Integer JavaDoc zooId)
599         {
600             this.zooId = zooId;
601         }
602     }
603
604     public static class Mammal extends AbstractAnimal implements InterfaceAnimal, Serializable JavaDoc
605     {
606         private int age;
607         private int numLegs;
608
609         public Mammal()
610         {
611             super();
612         }
613
614         public Mammal(int age, String JavaDoc name, int numLegs)
615         {
616             this.age = age;
617             this.name = name;
618             this.numLegs = numLegs;
619         }
620
621         public String JavaDoc toString()
622         {
623             return "Mammal: id = " + animalId + "\n name = " + name +
624                     "\n age = " + age +
625                     "\n Number of legs = " + numLegs +
626                     "\n zooId = " + zooId;
627         }
628
629         public int getAge()
630         {
631             return age;
632         }
633
634         public int getNumLegs()
635         {
636             return numLegs;
637         }
638
639         public void setNumLegs(int numLegs)
640         {
641             this.numLegs = numLegs;
642         }
643     }
644
645
646     public static class Reptile extends AbstractAnimal implements InterfaceAnimal, Serializable JavaDoc
647     {
648         private int age;
649         private String JavaDoc color;
650         /**
651          * Constructor for Plant.
652          */

653         public Reptile()
654         {
655             super();
656         }
657
658         public Reptile(int age, String JavaDoc name, String JavaDoc color)
659         {
660          this.age = age;
661          this.name = name;
662          this.color = color;
663         }
664
665         public int getAge()
666         {
667             return age;
668         }
669
670         public int getAnimalId()
671         {
672             return animalId;
673         }
674
675         public void setAnimalId(int animalId)
676         {
677             this.animalId = animalId;
678         }
679
680         public String JavaDoc getName()
681         {
682             return name;
683         }
684
685         public void setName(String JavaDoc name)
686         {
687             this.name = name;
688         }
689
690         public String JavaDoc getColor()
691         {
692             return color;
693         }
694
695         public void setColor(String JavaDoc color)
696         {
697             this.color = color;
698         }
699
700         public String JavaDoc toString()
701         {
702             return "Reptile: id = " + animalId + "\n name = " + name +
703                     "\n age = " + age +
704                     "\n color = " + color +
705                     "\n zooId = " + zooId;
706         }
707     }
708
709
710     public static class Fish implements InterfaceFood, Serializable JavaDoc
711     {
712         int foodId;
713         String JavaDoc name;
714         int calories;
715         String JavaDoc typeOfWater;
716
717         /**
718          * Constructor for Fish.
719          */

720         public Fish()
721         {
722             super();
723         }
724
725         public Fish(String JavaDoc name, int calories, String JavaDoc typeOfWater)
726         {
727             this.calories = calories;
728             this.name = name;
729             this.typeOfWater = typeOfWater;
730         }
731
732         /**
733          * @see org.apache.ojb.broker.InterfaceFood#getName()
734          */

735         public String JavaDoc getName()
736         {
737             return name;
738         }
739
740         /**
741          * @see org.apache.ojb.broker.InterfaceFood#getCalories()
742          */

743         public int getCalories()
744         {
745             return calories;
746         }
747
748
749
750         /**
751          * Returns the typeOfWater.
752          * @return String
753          */

754         public String JavaDoc getTypeOfWater()
755         {
756             return typeOfWater;
757         }
758
759         /**
760          * Returns the foodId.
761          * @return int
762          */

763         public int getFoodId()
764         {
765             return foodId;
766         }
767
768         public String JavaDoc toString()
769         {
770            return "Fish: id = " + foodId + "\n name = " + name +
771                     "\n calories = " + calories +
772                     "\n Type of water = " + typeOfWater;
773         }
774
775
776         /**
777          * Sets the calories.
778          * @param calories The calories to set
779          */

780         public void setCalories(int calories)
781         {
782             this.calories = calories;
783         }
784
785         /**
786          * Sets the foodId.
787          * @param foodId The foodId to set
788          */

789         public void setFoodId(int foodId)
790         {
791             this.foodId = foodId;
792         }
793
794         /**
795          * Sets the name.
796          * @param name The name to set
797          */

798         public void setName(String JavaDoc name)
799         {
800             this.name = name;
801         }
802
803         /**
804          * Sets the typeOfWater.
805          * @param typeOfWater The typeOfWater to set
806          */

807         public void setTypeOfWater(String JavaDoc typeOfWater)
808         {
809             this.typeOfWater = typeOfWater;
810         }
811
812     }
813
814
815
816     public static class Gourmet implements Serializable JavaDoc
817     {
818         int gourmetId;
819         String JavaDoc name;
820         List JavaDoc favoriteFood = new ArrayList JavaDoc();
821         /**
822          * Constructor for Gourmet.
823          */

824         public Gourmet()
825         {
826             super();
827         }
828
829         public Gourmet(String JavaDoc name)
830         {
831             this.name = name;
832         }
833
834         public List JavaDoc getFavoriteFood()
835         {
836             return favoriteFood;
837         }
838
839         public void addFavoriteFood(InterfaceFood food)
840         {
841             favoriteFood.add(food);
842         }
843
844         /**
845          * Returns the gourmetId.
846          * @return int
847          */

848         public int getGourmetId()
849         {
850             return gourmetId;
851         }
852
853         public String JavaDoc toString()
854         {
855          StringBuffer JavaDoc text = new StringBuffer JavaDoc("Gourmet: id = " + gourmetId + "\n");
856          text.append("name = ");
857          text.append(name);
858          text.append("\nFavoriteFood:\n");
859          for(Iterator JavaDoc it = favoriteFood.iterator(); it.hasNext();)
860          {
861             text.append(it.next().toString());
862             text.append("\n-------\n");
863          }
864          return text.toString();
865         }
866
867
868         /**
869          * Returns the name.
870          * @return String
871          */

872         public String JavaDoc getName()
873         {
874             return name;
875         }
876
877         /**
878          * Sets the favoriteFood.
879          * @param favoriteFood The favoriteFood to set
880          */

881         public void setFavoriteFood(List JavaDoc favoriteFood)
882         {
883             this.favoriteFood = favoriteFood;
884         }
885
886         /**
887          * Sets the gourmetId.
888          * @param gourmetId The gourmetId to set
889          */

890         public void setGourmetId(int gourmetId)
891         {
892             this.gourmetId = gourmetId;
893         }
894
895         /**
896          * Sets the name.
897          * @param name The name to set
898          */

899         public void setName(String JavaDoc name)
900         {
901             this.name = name;
902         }
903
904     }
905
906
907     public static class Salad implements InterfaceFood, Serializable JavaDoc
908     {
909         int foodId;
910         String JavaDoc name;
911         int calories;
912         String JavaDoc color;
913         /**
914          * Constructor for Salad.
915          */

916         public Salad()
917         {
918             super();
919         }
920
921         public Salad(String JavaDoc name, int calories, String JavaDoc color)
922         {
923             this.name = name;
924             this.calories = calories;
925             this.color = color;
926         }
927
928         /**
929          * @see org.apache.ojb.broker.InterfaceFood#getName()
930          */

931         public String JavaDoc getName()
932         {
933             return name;
934         }
935
936         /**
937          * @see org.apache.ojb.broker.InterfaceFood#getCalories()
938          */

939         public int getCalories()
940         {
941             return calories;
942         }
943
944
945
946         /**
947          * Returns the color.
948          * @return String
949          */

950         public String JavaDoc getColor()
951         {
952             return color;
953         }
954
955         /**
956          * Returns the foodId.
957          * @return int
958          */

959         public int getFoodId()
960         {
961             return foodId;
962         }
963
964         public String JavaDoc toString()
965         {
966            return "Salad: id = " + foodId + "\n name = " + name +
967                     "\n calories = " + calories +
968                     "\n Color = " + color;
969         }
970
971         /**
972          * Sets the calories.
973          * @param calories The calories to set
974          */

975         public void setCalories(int calories)
976         {
977             this.calories = calories;
978         }
979
980         /**
981          * Sets the color.
982          * @param color The color to set
983          */

984         public void setColor(String JavaDoc color)
985         {
986             this.color = color;
987         }
988
989         /**
990          * Sets the foodId.
991          * @param foodId The foodId to set
992          */

993         public void setFoodId(int foodId)
994         {
995             this.foodId = foodId;
996         }
997
998         /**
999          * Sets the name.
1000         * @param name The name to set
1001         */

1002        public void setName(String JavaDoc name)
1003        {
1004            this.name = name;
1005        }
1006
1007    }
1008
1009    public class Zoo implements Serializable JavaDoc
1010    {
1011        private int zooId;
1012        private String JavaDoc name;
1013        private List JavaDoc animals = new ArrayList JavaDoc();
1014
1015        /**
1016         * Constructor for Zoo.
1017         */

1018        public Zoo()
1019        {
1020            super();
1021        }
1022
1023        public Zoo(String JavaDoc name)
1024        {
1025            this.name = name;
1026        }
1027
1028        public List JavaDoc getAnimals()
1029        {
1030            return animals;
1031        }
1032
1033        public void addAnimal(InterfaceAnimal animal)
1034        {
1035            animals.add(animal);
1036        }
1037
1038        public int getZooId()
1039        {
1040            return zooId;
1041        }
1042
1043        public String JavaDoc toString()
1044        {
1045         StringBuffer JavaDoc text = new StringBuffer JavaDoc("Zoo: id = " + zooId + "\n");
1046         text.append("name = ");
1047         text.append(name);
1048         text.append("\nAnimals:\n");
1049         for(Iterator JavaDoc it = animals.iterator(); it.hasNext();)
1050         {
1051            text.append(it.next().toString());
1052            text.append("\n-------\n");
1053         }
1054         return text.toString();
1055        }
1056
1057        /**
1058         * Returns the name.
1059         * @return String
1060         */

1061        public String JavaDoc getName()
1062        {
1063            return name;
1064        }
1065
1066        /**
1067         * Sets the animals.
1068         * @param animals The animals to set
1069         */

1070        public void setAnimals(List JavaDoc animals)
1071        {
1072            this.animals = animals;
1073        }
1074
1075        /**
1076         * Sets the name.
1077         * @param name The name to set
1078         */

1079        public void setName(String JavaDoc name)
1080        {
1081            this.name = name;
1082        }
1083
1084        /**
1085         * Sets the zooId.
1086         * @param zooId The zooId to set
1087         */

1088        public void setZooId(int zooId)
1089        {
1090            this.zooId = zooId;
1091        }
1092
1093    }
1094
1095
1096}
1097
Popular Tags