Update of /cvsroot/cayenne/cayenne/src/tutorials/cayenne-cmd-app/java/test In directory usw-pr-cvs1:/tmp/cvs-serv26389/src/tutorials/cayenne-cmd-app/java/test Added Files: Artist.java Gallery.java Main.java Painting.java _Artist.java _Gallery.java _Painting.java Log Message: relocated tutorials to a subproject (old tutorials are still around but will be deleted soon) --- NEW FILE: Artist.java --- package test; import org.objectstyle.cayenne.*; public class Artist extends _Artist { } --- NEW FILE: Gallery.java --- package test; import org.objectstyle.cayenne.*; public class Gallery extends _Gallery { } --- NEW FILE: Main.java --- package test; import org.objectstyle.cayenne.access.DataContext; import org.objectstyle.cayenne.access.DataDomain; import org.objectstyle.cayenne.conf.Configuration; import org.objectstyle.cayenne.exp.Expression; import org.objectstyle.cayenne.exp.ExpressionFactory; import org.objectstyle.cayenne.query.SelectQuery; import java.util.List; import org.apache.log4j.Level; public class Main { private DataContext ctxt; /** * Runs tutorial. * Usage: * java test.Main galleryPattern */ public static void main(String[] args) { if (args.length != 1) { System.err.println("Usage:"); System.err.println(" java test.Main galleryPattern"); System.exit(1); } Main tutorialObj = new Main(); tutorialObj.runTutorial(args[0]); } public Main() { this.ctxt = createContext(); } public void runTutorial(String galleryPattern) { Gallery gallery = findGallery(galleryPattern); if (gallery != null) { addArtist(gallery); } } /** Creates and returns DataContext object. */ private DataContext createContext() { Configuration.bootstrapSharedConfig(this.getClass()); DataDomain sharedDomain = Configuration.getSharedConfig().getDomain(); return sharedDomain.createDataContext(); } /** * Searches for matching galleries in the database. * If one and only one matching gallery is found, it is returned, * otherwise null is returned. */ private Gallery findGallery(String galleryPattern) { String likePattern = "%" + galleryPattern + "%"; Expression qual = ExpressionFactory.binaryPathExp( Expression.LIKE_IGNORE_CASE, "galleryName", likePattern); SelectQuery query = new SelectQuery("Gallery", qual); // using log level of ERROR to make sure that query // execution is logged to STDOUT query.setLoggingLevel(Level.ERROR); List galleries = ctxt.performQuery(query); if (galleries.size() == 1) { Gallery gallery = (Gallery) galleries.get(0); System.out.println("\nFound gallery '" + gallery.getGalleryName() + "'.\n"); return gallery; } else if (galleries.size() == 0) { System.out.println("No matching galleries found."); return null; } else { System.out.println("Found more than one matching gallery. Be more specific."); return null; } } /** Adds new artist and his paintings to the gallery. */ private void addArtist(Gallery gallery) { // create new Artist object Artist dali = (Artist)ctxt.createAndRegisterNewObject("Artist"); dali.setArtistName("Salvador Dali"); // create new Painting object Painting paint = (Painting)ctxt.createAndRegisterNewObject("Painting"); paint.setPaintingTitle("Sleep"); // establish relationship between artist and painting dali.addToPaintingArray(paint); // commit to the database // using log level of ERROR to show the query execution ctxt.commitChanges(Level.ERROR); } } --- NEW FILE: Painting.java --- package test; import org.objectstyle.cayenne.*; public class Painting extends _Painting { } --- NEW FILE: _Artist.java --- package test; import java.util.List; import org.objectstyle.cayenne.*; /** Class _Artist was generated by Cayenne. * It is probably a good idea to avoid changing this class manually, * since it may be overwritten next time code is regenerated. * If you need to make any customizations, please use subclass. */ public class _Artist extends CayenneDataObject { public void setDateOfBirth(java.sql.Time dateOfBirth) { writeProperty("dateOfBirth", dateOfBirth); } public java.sql.Time getDateOfBirth() { return (java.sql.Time)readProperty("dateOfBirth"); } public void setArtistName(java.lang.String artistName) { writeProperty("artistName", artistName); } public java.lang.String getArtistName() { return (java.lang.String)readProperty("artistName"); } public void addToPaintingArray(test.Painting obj) { addToManyTarget("paintingArray", obj, true); } public void removeFromPaintingArray(test.Painting obj) { removeToManyTarget("paintingArray", obj, true); } public List getPaintingArray() { return (List)readProperty("paintingArray"); } } --- NEW FILE: _Gallery.java --- package test; import java.util.List; import org.objectstyle.cayenne.*; /** Class _Gallery was generated by Cayenne. * It is probably a good idea to avoid changing this class manually, * since it may be overwritten next time code is regenerated. * If you need to make any customizations, please use subclass. */ public class _Gallery extends CayenneDataObject { public void setGalleryName(java.lang.String galleryName) { writeProperty("galleryName", galleryName); } public java.lang.String getGalleryName() { return (java.lang.String)readProperty("galleryName"); } public void addToPaintingArray(test.Painting obj) { addToManyTarget("paintingArray", obj, true); } public void removeFromPaintingArray(test.Painting obj) { removeToManyTarget("paintingArray", obj, true); } public List getPaintingArray() { return (List)readProperty("paintingArray"); } } --- NEW FILE: _Painting.java --- package test; import java.util.List; import org.objectstyle.cayenne.*; /** Class _Painting was generated by Cayenne. * It is probably a good idea to avoid changing this class manually, * since it may be overwritten next time code is regenerated. * If you need to make any customizations, please use subclass. */ public class _Painting extends CayenneDataObject { public void setEstimatedPrice(java.math.BigDecimal estimatedPrice) { writeProperty("estimatedPrice", estimatedPrice); } public java.math.BigDecimal getEstimatedPrice() { return (java.math.BigDecimal)readProperty("estimatedPrice"); } public void setPaintingTitle(java.lang.String paintingTitle) { writeProperty("paintingTitle", paintingTitle); } public java.lang.String getPaintingTitle() { return (java.lang.String)readProperty("paintingTitle"); } public void setToGallery(test.Gallery toGallery) { setToOneTarget("toGallery", toGallery, true); } public test.Gallery getToGallery() { return (test.Gallery)readProperty("toGallery"); } public void setToArtist(test.Artist toArtist) { setToOneTarget("toArtist", toArtist, true); } public test.Artist getToArtist() { return (test.Artist)readProperty("toArtist"); } } |