001: package invoice;
002:
003: import org.objectweb.util.monolog.Monolog;
004: import org.objectweb.util.monolog.api.BasicLevel;
005: import org.objectweb.util.monolog.api.Logger;
006:
007: import javax.jdo.JDOHelper;
008: import javax.jdo.PersistenceManagerFactory;
009: import javax.jdo.PersistenceManager;
010: import javax.jdo.Query;
011: import java.util.Properties;
012: import java.util.Collection;
013: import java.util.Iterator;
014: import java.util.ArrayList;
015: import java.io.FileInputStream;
016: import java.io.IOException;
017:
018: /**
019: * @author S.Chassande-Barrioz
020: */
021: public class InvoiceAppsHelper {
022:
023: final static String[] colors = { "blue", "red", "yellow", "black",
024: "white" };
025: final static char[] sizes = { 'L', 'M', 'S' };
026:
027: public PersistenceManagerFactory pmf = null;
028:
029: public Logger logger;
030:
031: public InvoiceAppsHelper(String propertiesFileName)
032: throws IOException {
033: Properties p = new Properties();
034: p.load(new FileInputStream(propertiesFileName));
035: pmf = JDOHelper.getPersistenceManagerFactory(p);
036: logger = Monolog.initialize().getLogger(getLoggerName());
037: logger.log(BasicLevel.DEBUG,
038: "PersistenceManagerFactory instanciated ("
039: + pmf.getConnectionURL() + ")");
040: }
041:
042: protected String getLoggerName() {
043: return getClass().getName();
044: }
045:
046: public void createProducts() {
047: PersistenceManager pm = pmf.getPersistenceManager();
048: pm.currentTransaction().begin();
049: for (int i = 0; i < colors.length; i++) {
050: for (int j = 0; j < sizes.length; j++) {
051: pm
052: .makePersistent(new Product("t-shirt_"
053: + colors[i] + "_" + sizes[j], sizes[j],
054: colors[i], 200, 15));
055: pm
056: .makePersistent(new Product("short_"
057: + colors[i] + "_" + sizes[j], sizes[j],
058: colors[i], 100, 10));
059: pm.makePersistent(new Product("sweat-shirt_"
060: + colors[i] + "_" + sizes[j], sizes[j],
061: colors[i], 300, 30));
062: }
063: }
064: pm.currentTransaction().commit();
065: pm.close();
066: }
067:
068: public void createAdresses() {
069: PersistenceManager pm = pmf.getPersistenceManager();
070: pm.currentTransaction().begin();
071: pm.makePersistent(new Address("Seb", "edelweiss", "38610",
072: "Gieres", "France"));
073: pm.makePersistent(new Address("Pascal", "?", "38000",
074: "Saint Ismier", "France"));
075: pm.makePersistent(new Address("Alex", "?", "38000", "Grenoble",
076: "France"));
077: pm.makePersistent(new Address("Bruno", "?", "750XX", "Paris",
078: "France"));
079: pm.currentTransaction().commit();
080: pm.close();
081: }
082:
083: public Invoice createInvoice() {
084: PersistenceManager pm = pmf.getPersistenceManager();
085: pm.currentTransaction().begin();
086:
087: //Fetch the products of the invoice
088: Query q = pm.newQuery(Product.class);
089: //q.declareParameters("String c");
090: q.declareParameters("String c, char s");
091: //q.setFilter("(color == c)");
092: q.setFilter("((color == c) && (size == s))");
093: Collection products = (Collection)
094: //q.execute(colors[0]);
095: q.execute(colors[0], new Character(sizes[0]));
096: ArrayList pus = new ArrayList();
097: Iterator it = products.iterator();
098: while (it.hasNext()) {
099: pus.add(new ProductUnits((Product) it.next(), null, 20, pus
100: .size()));
101: }
102: q.closeAll();
103:
104: //Fetch the address of the invoice
105: q = pm.newQuery(Address.class);
106: q.setFilter("(name == n)");
107: q.declareParameters("String n");
108: Collection addresses = (Collection) q.execute("Seb");
109: Address sebAddress = (Address) addresses.iterator().next();
110: q.closeAll();
111:
112: //Build the invoice
113: Invoice invoice = new Invoice(217, sebAddress, pus);
114: //manage the reverse reference between Invoice and ProductUnits
115: for (int i = 0; i < pus.size(); i++) {
116: ((ProductUnits) pus.get(i)).setInvoice(invoice);
117: }
118: pm.makePersistent(invoice);
119: System.out.println(invoice);
120:
121: pm.currentTransaction().commit();
122: pm.close();
123:
124: return invoice;
125: }
126:
127: public void printAll() {
128: PersistenceManager pm = pmf.getPersistenceManager();
129: Query q = pm.newQuery(Product.class);
130: q.setOrdering("name ascending");
131: Collection col = (Collection) q.execute();
132: Iterator it = col.iterator();
133: int i = 0;
134: while (it.hasNext()) {
135: Product p = (Product) it.next();
136: System.out.println("Product (" + p + ")");
137: i++;
138: }
139: q.closeAll();
140: System.out.println("There is " + i + " products.");
141:
142: q = pm.newQuery(Address.class);
143: col = (Collection) q.execute();
144: it = col.iterator();
145: i = 0;
146: while (it.hasNext()) {
147: Address a = (Address) it.next();
148: System.out.println("Address (" + a + ")");
149: i++;
150: }
151: q.closeAll();
152: System.out.println("There is " + i + " addresses.");
153:
154: q = pm.newQuery(Invoice.class);
155: col = (Collection) q.execute();
156: it = col.iterator();
157: i = 0;
158: while (it.hasNext()) {
159: Invoice in = (Invoice) it.next();
160: System.out.println("Invoice (" + in + ")");
161: i++;
162: }
163: q.closeAll();
164: System.out.println("There is " + i + " invoices.");
165: pm.close();
166: }
167:
168: public void removeProducts() {
169: PersistenceManager pm = pmf.getPersistenceManager();
170: pm.currentTransaction().begin();
171: Query q = pm.newQuery(Product.class);
172:
173: pm.deletePersistentAll((Collection) q.execute());
174: q.closeAll();
175: pm.currentTransaction().commit();
176: pm.close();
177: }
178:
179: public void removeAddresses() {
180: PersistenceManager pm = pmf.getPersistenceManager();
181: pm.currentTransaction().begin();
182: Query q = pm.newQuery(Address.class);
183: pm.deletePersistentAll((Collection) q.execute());
184: q.closeAll();
185: pm.currentTransaction().commit();
186: pm.close();
187: }
188:
189: public void removeInvoices() {
190: PersistenceManager pm = pmf.getPersistenceManager();
191: pm.currentTransaction().begin();
192: Query q = pm.newQuery(Invoice.class);
193: Iterator it = ((Collection) q.execute()).iterator();
194: while (it.hasNext()) {
195: Invoice i = (Invoice) it.next();
196: Collection pus = i.getProductUnits();
197: Iterator puit = pus.iterator();
198: if (!puit.hasNext()) {
199: System.out
200: .println("No ProductUnits referenced by the invoice");
201: }
202: while (puit.hasNext()) {
203: ProductUnits pu = (ProductUnits) puit.next();
204: System.out.println("remove : " + pu);
205: pm.deletePersistent(pu);
206: }
207: pm.deletePersistent(i);
208: }
209: q.closeAll();
210: pm.currentTransaction().commit();
211: pm.close();
212: }
213:
214: public void removeInvoice(Invoice i) {
215: PersistenceManager pm = pmf.getPersistenceManager();
216: pm.currentTransaction().begin();
217: pm.deletePersistentAll(i.getProductUnits());
218: pm.deletePersistent(i);
219: pm.currentTransaction().commit();
220: pm.close();
221: }
222:
223: }
|