1 24 package org.objectweb.jalisto.test.core.remote; 25 26 import org.objectweb.jalisto.se.JalistoFactory; 27 import org.objectweb.jalisto.se.api.Session; 28 import org.objectweb.jalisto.se.test.data.Book; 29 30 import java.util.ArrayList ; 31 import java.util.Iterator ; 32 33 public class TestRemote { 34 35 public static void main(String [] args) { 36 Session session = JalistoFactory.getSession("jalisto-client.properties"); 37 session.openSession(); 38 session.defineClass(Book.getMetaDescription()); 39 40 for (int i = 0; i < 5; i++) { 41 createAndReadBook(session); 42 } 43 44 for (int i = 0; i < 3; i++) { 45 ArrayList oids = createBooks(session, 5); 46 updateBooks(session, oids); 47 deleteBooks(session, oids); 48 } 49 50 { 51 ArrayList oids = createBooks(session, 20); 52 updateBooks(session, oids); 53 testExtent(session); 54 } 55 56 session.closeSession(); 57 } 58 59 public static void createAndReadBook(Session session) { 60 session.currentTransaction().begin(); 61 Object oid = session.createObject(Book.newBook().toArray(), Book.class); 62 session.currentTransaction().commit(); 63 64 session.currentTransaction().begin(); 65 System.out.println(Book.toBook(session.readObjectByOid(oid))); 66 session.currentTransaction().commit(); 67 68 try { 69 Thread.sleep(1000); 70 } catch (InterruptedException e) { 71 e.printStackTrace(); 72 } 73 } 74 75 public static ArrayList createBooks(Session session, int nbrBooks) { 76 ArrayList oids = new ArrayList (); 77 78 session.currentTransaction().begin(); 79 for (int i = 0; i < nbrBooks; i++) { 80 Object oid = session.makeNewFileOid(Book.class); 81 System.out.println("make oid : " + oid); 82 oids.add(oid); 83 } 84 session.currentTransaction().commit(); 85 86 session.currentTransaction().begin(); 87 for (int i = 0; i < nbrBooks; i++) { 88 Book book = Book.newBook(); 89 System.out.println("create : " + oids.get(i) + " " + book); 90 session.createObject(oids.get(i), book.toArray()); 91 } 92 session.currentTransaction().commit(); 93 94 return oids; 95 } 96 97 public static void updateBooks(Session session, ArrayList oids) { 98 session.currentTransaction().begin(); 99 for (int i = 0; i < oids.size(); i++) { 100 Object oid = oids.get(i); 101 Book book = Book.toBook(session.readObjectByOid(oid)); 102 book.setAuthor(book.getAuthor() + "pouet"); 103 System.out.println("update : " + book); 104 session.updateObjectByOid(oid, book.toArray()); 105 } 106 session.currentTransaction().commit(); 107 } 108 109 public static void deleteBooks(Session session, ArrayList oids) { 110 session.currentTransaction().begin(); 111 for (int i = 0; i < oids.size(); i++) { 112 Object oid = oids.get(i); 113 System.out.println("delete : " + oid); 114 session.deleteObjectByOid(oid); 115 } 116 session.currentTransaction().commit(); 117 } 118 119 public static void testExtent(Session session) { 120 session.currentTransaction().begin(); 121 Iterator extent = session.getExtent(Book.class).readFully().iterator(); 122 while (extent.hasNext()) { 123 Object oid = extent.next(); 124 Book book = Book.toBook(session.readObjectByOid(oid)); 125 System.out.println("readed book : " + String.valueOf(book)); 126 } 127 session.currentTransaction().commit(); 128 } 129 } 130 | Popular Tags |