KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > Main


1 package test;
2
3 import java.util.List JavaDoc;
4
5 import org.objectstyle.cayenne.access.DataContext;
6 import org.objectstyle.cayenne.conf.Configuration;
7 import org.objectstyle.cayenne.exp.Expression;
8 import org.objectstyle.cayenne.exp.ExpressionFactory;
9 import org.objectstyle.cayenne.query.SelectQuery;
10
11 public class Main {
12
13     private DataContext ctxt;
14
15     /**
16      * Runs tutorial.
17      * Usage:
18      * java test.Main galleryPattern
19      */

20     public static void main(String JavaDoc[] args) {
21
22         if (args.length != 1) {
23             System.err.println("Usage:");
24             System.err.println(" java test.Main galleryPattern");
25             System.exit(1);
26         }
27
28         Main tutorialObj = new Main();
29         tutorialObj.runTutorial(args[0]);
30     }
31
32     public Main() {
33         this.ctxt = createContext();
34     }
35
36     public void runTutorial(String JavaDoc galleryPattern) {
37         Gallery gallery = findGallery(galleryPattern);
38         if (gallery != null) {
39             addArtist(gallery);
40         }
41     }
42
43     /** Creates and returns DataContext object. */
44     private DataContext createContext() {
45         Configuration.bootstrapSharedConfiguration(this.getClass());
46         return Configuration.getSharedConfiguration().getDomain().createDataContext();
47     }
48
49     /**
50      * Searches for matching galleries in the database.
51      * If one and only one matching gallery is found, it is returned,
52      * otherwise null is returned.
53      */

54     private Gallery findGallery(String JavaDoc galleryPattern) {
55         String JavaDoc likePattern = "%" + galleryPattern + "%";
56         Expression qual = ExpressionFactory.likeIgnoreCaseExp("galleryName", likePattern);
57
58         SelectQuery query = new SelectQuery(Gallery.class, qual);
59
60         List JavaDoc galleries = ctxt.performQuery(query);
61         if (galleries.size() == 1) {
62             Gallery gallery = (Gallery) galleries.get(0);
63             System.out.println("\nFound gallery '" + gallery.getGalleryName() + "'.\n");
64             return gallery;
65         }
66         else if (galleries.size() == 0) {
67             System.out.println("No matching galleries found.");
68             return null;
69         }
70         else {
71             System.out.println("Found more than one matching gallery. Be more specific.");
72             return null;
73         }
74     }
75
76     /** Adds new artist and his paintings to the gallery. */
77     private void addArtist(Gallery gallery) {
78         // create new Artist object
79
Artist dali = (Artist) ctxt.createAndRegisterNewObject(Artist.class);
80         dali.setArtistName("Salvador Dali");
81
82         // create new Painting object
83
Painting painting = (Painting) ctxt.createAndRegisterNewObject(Painting.class);
84         painting.setPaintingTitle("Sleep");
85
86         // establish relationship between artist and painting
87
dali.addToPaintingArray(painting);
88
89         // establish relationship between painting and gallery
90
gallery.addToPaintingArray(painting);
91
92         // commit to the database
93
ctxt.commitChanges();
94     }
95 }
Popular Tags