ojb-dev

[Prev] Thread [Next]  |  [Prev] Date [Next]

svn commit: r615701 - in /db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample: BasicObject.java Book.java CD.java DVD.java Medium.java ProductGroup.java Review.java SampleTest.java arminw Wed May 21 00:03:56 2008

Author: arminw
Date: Sun Jan 27 18:05:59 2008
New Revision: 615701

URL: http://svn.apache.org/viewvc?rev=615701&view=rev
Log:
improve sample

Modified:
    
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/BasicObject.java
    
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/Book.java
    
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/CD.java
    
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/DVD.java
    
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/Medium.java
    
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/ProductGroup.java
    
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/Review.java
    
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/SampleTest.java

Modified: 
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/BasicObject.java
URL: 
http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/BasicObject.java?rev=615701&r1=615700&r2=615701&view=diff
==============================================================================
--- 
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/BasicObject.java
 (original)
+++ 
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/BasicObject.java
 Sun Jan 27 18:05:59 2008
@@ -22,7 +22,7 @@
  */
 
 /**
- * A base class.
+ * A base class for persistence capable objects.
  * 
  * IMPORTANT: Don't declare a 'ojb.class' tag, because this is only a base 
class
  * and we only need the base fields in the sub-classes (xdoclet shouldn't 
generate
@@ -30,7 +30,7 @@
  * 
  * @version $Id$
  */
-class BasicObject implements Serializable
+abstract public class BasicObject implements Serializable
 {
     /**
      * @ojb.field autoincrement="ojb" primarykey="true" field-class="direct"

Modified: 
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/Book.java
URL: 
http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/Book.java?rev=615701&r1=615700&r2=615701&view=diff
==============================================================================
--- 
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/Book.java
 (original)
+++ 
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/Book.java
 Sun Jan 27 18:05:59 2008
@@ -19,7 +19,7 @@
  * under the License.
  */
 
-import java.util.Date;
+import java.util.Calendar;
 
 /**
  * The Book class
@@ -33,7 +33,7 @@
     {
     }
 
-    public Book(String title, Date publicationDate, byte[] cover)
+    public Book(String title, Calendar publicationDate, byte[] cover)
     {
         super(title, publicationDate, cover);
     }

Modified: 
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/CD.java
URL: 
http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/CD.java?rev=615701&r1=615700&r2=615701&view=diff
==============================================================================
--- 
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/CD.java
 (original)
+++ 
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/CD.java
 Sun Jan 27 18:05:59 2008
@@ -19,7 +19,7 @@
  * under the License.
  */
 
-import java.util.Date;
+import java.util.Calendar;
 
 /**
  * The CD class
@@ -33,7 +33,7 @@
     {
     }
 
-    public CD(String title, Date publicationDate, byte[] cover)
+    public CD(String title, Calendar publicationDate, byte[] cover)
     {
         super(title, publicationDate, cover);
     }

Modified: 
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/DVD.java
URL: 
http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/DVD.java?rev=615701&r1=615700&r2=615701&view=diff
==============================================================================
--- 
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/DVD.java
 (original)
+++ 
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/DVD.java
 Sun Jan 27 18:05:59 2008
@@ -19,7 +19,7 @@
  * under the License.
  */
 
-import java.util.Date;
+import java.util.Calendar;
 
 /**
  * The DVD class
@@ -33,7 +33,7 @@
     {
     }
 
-    public DVD(String title, Date publicationDate, byte[] cover)
+    public DVD(String title, Calendar publicationDate, byte[] cover)
     {
         super(title, publicationDate, cover);
     }

Modified: 
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/Medium.java
URL: 
http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/Medium.java?rev=615701&r1=615700&r2=615701&view=diff
==============================================================================
--- 
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/Medium.java
 (original)
+++ 
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/Medium.java
 Sun Jan 27 18:05:59 2008
@@ -20,7 +20,7 @@
  */
 
 import java.util.ArrayList;
-import java.util.Date;
+import java.util.Calendar;
 import java.util.List;
 
 import org.apache.commons.lang.builder.ToStringBuilder;
@@ -41,9 +41,9 @@
      */
     private String title;
     /**
-     * @ojb.field jdbc-type="TIMESTAMP" conversion="JavaData2SqlTimestamp"
+     * @ojb.field jdbc-type="DATE" conversion="Calendar2Date"
      */
-    private Date publicationDate;
+    private Calendar publicationDate;
     /**
      * @ojb.field jdbc-type="LONGVARBINARY"
      */
@@ -82,10 +82,12 @@
     /**
      * @ojb.collection element-class-ref="org.apache.ojb.sample.Review"
      * foreignkey="fkMedium"
+     * database-foreignkey="false"
      * auto-retrieve="true"
      * auto-update="create" 
      * auto-delete="none" 
      * proxy="true"
+     * orderby="vote=ASC"
      */
     private List reviews;
 
@@ -93,7 +95,7 @@
     {
     }
 
-    public Medium(String title, Date publicationDate, byte[] cover)
+    public Medium(String title, Calendar publicationDate, byte[] cover)
     {
         this.title = title;
         this.publicationDate = publicationDate;
@@ -170,12 +172,12 @@
         this.title = title;
     }
 
-    public Date getPublicationDate()
+    public Calendar getPublicationDate()
     {
         return publicationDate;
     }
 
-    public void setPublicationDate(Date publicationDate)
+    public void setPublicationDate(Calendar publicationDate)
     {
         this.publicationDate = publicationDate;
     }

Modified: 
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/ProductGroup.java
URL: 
http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/ProductGroup.java?rev=615701&r1=615700&r2=615701&view=diff
==============================================================================
--- 
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/ProductGroup.java
 (original)
+++ 
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/ProductGroup.java
 Sun Jan 27 18:05:59 2008
@@ -41,6 +41,7 @@
      * auto-update="create" 
      * auto-delete="none" 
      * proxy="true"
+     * orderby="publicationDate=ASC"
      */
     private List media;
 

Modified: 
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/Review.java
URL: 
http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/Review.java?rev=615701&r1=615700&r2=615701&view=diff
==============================================================================
--- 
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/Review.java
 (original)
+++ 
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/Review.java
 Sun Jan 27 18:05:59 2008
@@ -19,7 +19,6 @@
  * under the License.
  */
 
-import org.apache.commons.lang.builder.EqualsBuilder;
 import org.apache.commons.lang.builder.ToStringBuilder;
 import org.apache.commons.lang.builder.ToStringStyle;
 

Modified: 
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/SampleTest.java
URL: 
http://svn.apache.org/viewvc/db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/SampleTest.java?rev=615701&r1=615700&r2=615701&view=diff
==============================================================================
--- 
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/SampleTest.java
 (original)
+++ 
db/ojb/branches/OJB_1_0_RELEASE/src/xdoclet/sample/java/org/apache/ojb/sample/SampleTest.java
 Sun Jan 27 18:05:59 2008
@@ -19,13 +19,16 @@
  * under the License.
  */
 
-import java.lang.Object;
-import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.List;
 
 import junit.framework.TestCase;
+import org.apache.ojb.broker.Identity;
 import org.apache.ojb.broker.PersistenceBroker;
 import org.apache.ojb.broker.PersistenceBrokerFactory;
-import org.apache.ojb.broker.platforms.Platform;
+import org.apache.ojb.odmg.ImplementationExt;
+import org.apache.ojb.odmg.OJB;
+import org.odmg.Transaction;
 
 /**
  * This class
@@ -37,22 +40,186 @@
     public PersistenceBroker broker;
     byte[] cover = new byte[]{10,11,12,13,14,15,16,17,18,19,20};
 
-    public void test_A()
+    public void testExampleObjects()
     {
         String name = ojbTestMethodIdentifier();
+        System.out.println("Prepare complex sample object graph");
 
+        // store an complex object graph
+        broker.beginTransaction();
+
+        Book book = new Book(name, new GregorianCalendar(2000, 1, 1), cover);
+        CD cd = new CD(name, new GregorianCalendar(2001, 1, 1), cover);
+        DVD dvd = new DVD(name, new GregorianCalendar(2002, 1, 1), cover);
+        Author author_A = new Author(name, null);
+        Author author_B = new Author(name, null);
         ProductGroup pg = new ProductGroup(name);
-        Book book = new Book(name, new Date(), cover);
-        CD cd = new CD(name, new Date(), cover);
-        DVD dvd = new DVD(name, new Date(), cover);
+        Publisher publisher = new PublisherImpl(name);
+
         pg.addMedium(book);
         pg.addMedium(cd);
         pg.addMedium(dvd);
         Review review_book_1 = new Review(name, new Integer(5), book);
         Review review_book_2 = new Review(name, new Integer(6), book);
-        Review review_book_3 = new Review(name, new Integer(6), book);
-        Review review_dvd_1 = new Review(name, new Integer(6), dvd);
+        Review review_book_3 = new Review(name, new Integer(7), book);
+        Review review_dvd_1 = new Review(name, new Integer(8), dvd);
+        author_A.addMedia(book);
+        author_A.addMedia(cd);
+        author_B.addMedia(dvd);
+        author_A.setAuthorDetail(new AuthorDetail(name));
+        publisher.addAuthor(author_A);
+        publisher.addAuthor(author_B);
+
+        System.out.println("Insert complex sample object graph");
+        broker.store(pg);
+
+        review_dvd_1.setAuthor(author_A);
+        review_book_1.setAuthor(author_B);
+        broker.store(review_dvd_1);
+        broker.store(review_book_1);
+        broker.store(author_A);
+        broker.store(author_B);
+
+        broker.commitTransaction();
+
+        checkResultsInsert(name, pg, author_A, author_B);
+    }
+
+    public void testExampleObjects_2()
+    {
+        String name = ojbTestMethodIdentifier();
+
+        System.out.println("Prepare complex sample object graph");
+        Book book = new Book(name, new GregorianCalendar(2000, 1, 1), cover);
+        CD cd = new CD(name, new GregorianCalendar(2001, 1, 1), cover);
+        DVD dvd = new DVD(name, new GregorianCalendar(2002, 1, 1), cover);
+        Author author_A = new Author(name, null);
+        Author author_B = new Author(name, null);
+        ProductGroup pg = new ProductGroup(name);
+        Publisher publisher = new PublisherImpl(name);
+
+        pg.addMedium(book);
+        pg.addMedium(cd);
+        pg.addMedium(dvd);
+
+        Review review_book_1 = new Review(name, new Integer(5), book);
+        Review review_book_2 = new Review(name, new Integer(6), book);
+        Review review_book_3 = new Review(name, new Integer(7), book);
+        Review review_dvd_1 = new Review(name, new Integer(8), dvd);
+
+        author_A.addMedia(book);
+        author_A.addMedia(cd);
+        author_B.addMedia(dvd);
+        author_A.setAuthorDetail(new AuthorDetail(name));
+
+        publisher.addAuthor(author_A);
+        publisher.addAuthor(author_B);
+
+        review_dvd_1.setAuthor(author_A);
+        review_book_1.setAuthor(author_B);
+
+        // Use inject access to ODMG-api to store the object
+        ImplementationExt impl = OJB.getInstance();
+        Transaction tx = impl.newTransaction(broker, false);
+        tx.begin();
+        System.out.println("Insert complex sample object graph");
+        impl.getDatabase(null).makePersistent(pg);
+        tx.commit();
+
+        checkResultsInsert(name, pg, author_A, author_B);
+    }
+
+    private void checkResultsInsert(String name, ProductGroup pg, Author 
author_A, Author author_B)
+    {
+        System.out.println("Check insert result");
+        // clear cache and lookup the persisted object
+        broker.clearCache();
+        Identity oid = 
broker.serviceIdentity().buildIdentity(ProductGroup.class, pg.getId());
+        ProductGroup new_pg = (ProductGroup) broker.getObjectByIdentity(oid);
 
+        assertEquals(name, new_pg.getName());
+        List media = new_pg.getMedia();
+//        System.out.println("### PG: " + pg.getName());
+//        for(int i = 0; i < media.size(); i++)
+//        {
+//            Medium medium = (Medium) media.get(i);
+//            System.out.println("# entry: " + medium);
+//        }
+        assertEquals(3, media.size());
+        for(int i = 0; i < media.size(); i++)
+        {
+            Medium m = (Medium) media.get(i);
+            assertEquals(new_pg, m.getProductGroup());
+            // we specified order by 'publicationDate'
+            if(i==2)
+            {
+                // Book
+                List reviews = m.getReviews();
+//                System.out.println("List: " + reviews);
+//                for(int j = 0; j < reviews.size(); j++)
+//                {
+//                    Review review = (Review) reviews.get(j);
+//                    System.out.println("Entry: " + review);
+//                }
+                assertEquals(3, reviews.size());
+                for(int j = 0; j < reviews.size(); j++)
+                {
+                    Review r = (Review) reviews.get(j);
+                    // we specified order by 'vote', thus we expect lowest 
vote first
+                    if(j==0)
+                    {
+                        assertNotNull(r.getAuthor());
+                        assertEquals(author_B.getId(), r.getAuthor().getId());
+                    }
+                    else
+                    {
+                        assertNull(r.getAuthor());
+                    }
+                }
+                List authors = m.getAuthors();
+                assertEquals(1, authors.size());
+                for(int j = 0; j < authors.size(); j++)
+                {
+                    Author a = (Author) authors.get(j);
+                    assertNotNull(a.getAuthorDetail());
+                }
+            }
+            else if(i==1)
+            {
+                // CD
+                assertEquals(0, m.getReviews().size());
+                List authors = m.getAuthors();
+                assertEquals(1, authors.size());
+                for(int j = 0; j < authors.size(); j++)
+                {
+                    Author a = (Author) authors.get(j);
+                    assertNotNull(a.getAuthorDetail());
+                }
+            }
+            else if(i==0)
+            {
+                // DVD
+                List reviews = m.getReviews();
+                assertEquals(1, reviews.size());
+                for(int j = 0; j < reviews.size(); j++)
+                {
+                    Review r = (Review) reviews.get(j);
+                    // we specified order by 'vote', thus we expect
+                    if(j==0)
+                    {
+                        assertNotNull(r.getAuthor());
+                        assertEquals(author_A.getId(), r.getAuthor().getId());
+                    }
+                }
+                List authors = m.getAuthors();
+                assertEquals(1, authors.size());
+                for(int j = 0; j < authors.size(); j++)
+                {
+                    Author a = (Author) authors.get(j);
+                    assertNull(a.getAuthorDetail());
+                }
+            }
+        }
     }
 
     public void setUp() throws Exception



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]