1 package invoice; 2 3 import org.objectweb.speedo.log.Log; 4 import org.objectweb.util.monolog.api.BasicLevel; 5 import org.objectweb.util.monolog.api.Logger; 6 7 import javax.jdo.JDOHelper; 8 import javax.jdo.PersistenceManagerFactory; 9 import javax.jdo.PersistenceManager; 10 import javax.jdo.Query; 11 import java.util.Properties ; 12 import java.util.Collection ; 13 import java.util.Iterator ; 14 import java.util.ArrayList ; 15 import java.io.FileInputStream ; 16 import java.io.IOException ; 17 18 21 public class InvoiceAppsHelper { 22 23 final static String [] colors = {"blue", "red", "yellow", "black", "white"}; 24 final static char[] sizes = {'L', 'M', 'S'}; 25 26 public PersistenceManagerFactory pmf = null; 27 28 public Logger logger; 29 30 public InvoiceAppsHelper(String propertiesFileName) throws IOException { 31 Properties p = new Properties (); 32 p.load(new FileInputStream (propertiesFileName)); 33 pmf = JDOHelper.getPersistenceManagerFactory(p); 34 logger = Log.getLoggerFactory().getLogger(getLoggerName()); 35 logger.log(BasicLevel.DEBUG, 36 "PersistenceManagerFactory instanciated (" 37 + pmf.getConnectionURL() + ")"); 38 } 39 40 protected String getLoggerName() { 41 return getClass().getName(); 42 } 43 44 public void createProducts() { 45 PersistenceManager pm = pmf.getPersistenceManager(); 46 pm.currentTransaction().begin(); 47 for(int i=0 ; i<colors.length; i++) { 48 for(int j=0; j<sizes.length; j++) { 49 pm.makePersistent(new Product( 50 "t-shirt_" + colors[i] + "_" + sizes[j], 51 sizes[j], colors[i], 200, 15)); 52 pm.makePersistent(new Product( 53 "short_" + colors[i] + "_" + sizes[j], 54 sizes[j], colors[i], 100, 10)); 55 pm.makePersistent(new Product( 56 "sweat-shirt_" + colors[i] + "_" + sizes[j], 57 sizes[j], colors[i], 300, 30)); 58 } 59 } 60 pm.currentTransaction().commit(); 61 pm.close(); 62 } 63 64 public void createAdresses() { 65 PersistenceManager pm = pmf.getPersistenceManager(); 66 pm.currentTransaction().begin(); 67 pm.makePersistent( 68 new Address("Seb", "edelweiss", "38610", "Gieres", "France")); 69 pm.makePersistent( 70 new Address("Pascal", "?", "38000", "Saint Ismier", "France")); 71 pm.makePersistent( 72 new Address("Alex", "?", "38000", "Grenoble", "France")); 73 pm.makePersistent( 74 new Address("Bruno", "?", "750XX", "Paris", "France")); 75 pm.currentTransaction().commit(); 76 pm.close(); 77 } 78 79 public Invoice createInvoice() { 80 PersistenceManager pm = pmf.getPersistenceManager(); 81 pm.currentTransaction().begin(); 82 83 Query q = pm.newQuery(Product.class); 85 q.declareParameters("String c, char s"); 87 q.setFilter("((color == c) && (size == s))"); 89 Collection products = (Collection ) 90 q.execute(colors[0], new Character (sizes[0])); 92 ArrayList pus = new ArrayList (); 93 Iterator it = products.iterator(); 94 while(it.hasNext()) { 95 pus.add(new ProductUnits( 96 (Product) it.next(), null, 20, pus.size())); 97 } 98 q.closeAll(); 99 100 q = pm.newQuery(Address.class); 102 q.setFilter("(name == n)"); 103 q.declareParameters("String n"); 104 Collection addresses = (Collection ) q.execute("Seb"); 105 Address sebAddress = (Address) addresses.iterator().next(); 106 q.closeAll(); 107 108 Invoice invoice = new Invoice(217, sebAddress, pus); 110 for(int i=0; i<pus.size(); i++) { 112 ((ProductUnits) pus.get(i)).setInvoice(invoice); 113 } 114 pm.makePersistent(invoice); 115 System.out.println(invoice); 116 117 pm.currentTransaction().commit(); 118 pm.close(); 119 120 return invoice; 121 } 122 123 public void printAll() { 124 PersistenceManager pm = pmf.getPersistenceManager(); 125 Query q = pm.newQuery(Product.class); 126 q.setOrdering("name ascending"); 127 Collection col = (Collection ) q.execute(); 128 Iterator it = col.iterator(); 129 int i = 0; 130 while(it.hasNext()) { 131 Product p = (Product) it.next(); 132 System.out.println("Product (" + p + ")"); 133 i++; 134 } 135 q.closeAll(); 136 System.out.println("There is " + i + " products."); 137 138 q = pm.newQuery(Address.class); 139 col = (Collection ) q.execute(); 140 it = col.iterator(); 141 i = 0; 142 while(it.hasNext()) { 143 Address a = (Address) it.next(); 144 System.out.println("Address (" + a + ")"); 145 i++; 146 } 147 q.closeAll(); 148 System.out.println("There is " + i + " addresses."); 149 150 q = pm.newQuery(Invoice.class); 151 col = (Collection ) q.execute(); 152 it = col.iterator(); 153 i = 0; 154 while(it.hasNext()) { 155 Invoice in = (Invoice) it.next(); 156 System.out.println("Invoice (" + in + ")"); 157 i++; 158 } 159 q.closeAll(); 160 System.out.println("There is " + i + " invoices."); 161 pm.close(); 162 } 163 164 public void removeProducts() { 165 PersistenceManager pm = pmf.getPersistenceManager(); 166 pm.currentTransaction().begin(); 167 Query q = pm.newQuery(Product.class); 168 169 pm.deletePersistentAll((Collection ) q.execute()); 170 q.closeAll(); 171 pm.currentTransaction().commit(); 172 pm.close(); 173 } 174 175 public void removeAddresses() { 176 PersistenceManager pm = pmf.getPersistenceManager(); 177 pm.currentTransaction().begin(); 178 Query q = pm.newQuery(Address.class); 179 pm.deletePersistentAll((Collection ) q.execute()); 180 q.closeAll(); 181 pm.currentTransaction().commit(); 182 pm.close(); 183 } 184 185 public void removeInvoices() { 186 PersistenceManager pm = pmf.getPersistenceManager(); 187 pm.currentTransaction().begin(); 188 Query q = pm.newQuery(Invoice.class); 189 Iterator it = ((Collection ) q.execute()).iterator(); 190 while(it.hasNext()) { 191 Invoice i = (Invoice) it.next(); 192 Collection pus = i.getProductUnits(); 193 Iterator puit = pus.iterator(); 194 if (!puit.hasNext()) { 195 System.out.println("No ProductUnits referenced by the invoice"); 196 } 197 while(puit.hasNext()) { 198 ProductUnits pu = (ProductUnits) puit.next(); 199 System.out.println("remove : " + pu); 200 pm.deletePersistent(pu); 201 } 202 pm.deletePersistent(i); 203 } 204 q.closeAll(); 205 pm.currentTransaction().commit(); 206 pm.close(); 207 } 208 209 public void removeInvoice(Invoice i) { 210 PersistenceManager pm = pmf.getPersistenceManager(); 211 pm.currentTransaction().begin(); 212 pm.deletePersistentAll(i.getProductUnits()); 213 pm.deletePersistent(i); 214 pm.currentTransaction().commit(); 215 pm.close(); 216 } 217 218 } 219 | Popular Tags |