Package invoice

Source Code of invoice.InvoiceAppsHelper

package invoice;

import org.objectweb.util.monolog.Monolog;
import org.objectweb.util.monolog.api.BasicLevel;
import org.objectweb.util.monolog.api.Logger;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import java.util.Properties;
import java.util.Collection;
import java.util.Iterator;
import java.util.ArrayList;
import java.io.FileInputStream;
import java.io.IOException;

/**
* @author S.Chassande-Barrioz
*/
public class InvoiceAppsHelper {

    final static String[] colors = {"blue", "red", "yellow", "black", "white"};
    final static char[] sizes = {'L', 'M', 'S'};

    public PersistenceManagerFactory pmf = null;

    public Logger logger;

    public InvoiceAppsHelper(String propertiesFileName) throws IOException {
        Properties p = new Properties();
        p.load(new FileInputStream(propertiesFileName));
        pmf = JDOHelper.getPersistenceManagerFactory(p);
        logger = Monolog.initialize().getLogger(getLoggerName());
        logger.log(BasicLevel.DEBUG,
            "PersistenceManagerFactory instanciated ("
            + pmf.getConnectionURL() + ")");
    }

    protected String getLoggerName() {
        return getClass().getName();
    }

    public void createProducts() {
        PersistenceManager pm = pmf.getPersistenceManager();
        pm.currentTransaction().begin();
        for(int i=0 ; i<colors.length; i++) {
            for(int j=0; j<sizes.length; j++) {
                pm.makePersistent(new Product(
                    "t-shirt_" + colors[i] + "_" + sizes[j],
                    sizes[j], colors[i], 200, 15));
                pm.makePersistent(new Product(
                    "short_" + colors[i] + "_" + sizes[j],
                    sizes[j], colors[i], 100, 10));
                pm.makePersistent(new Product(
                    "sweat-shirt_" + colors[i] + "_" + sizes[j],
                    sizes[j], colors[i], 300, 30));
            }
        }
        pm.currentTransaction().commit();
        pm.close();
    }

    public void createAdresses() {
        PersistenceManager pm = pmf.getPersistenceManager();
        pm.currentTransaction().begin();
        pm.makePersistent(
            new Address("Seb", "edelweiss", "38610", "Gieres", "France"));
        pm.makePersistent(
            new Address("Pascal", "?", "38000", "Saint Ismier", "France"));
        pm.makePersistent(
            new Address("Alex", "?", "38000", "Grenoble", "France"));
        pm.makePersistent(
            new Address("Bruno", "?", "750XX", "Paris", "France"));
        pm.currentTransaction().commit();
        pm.close();
    }

    public Invoice createInvoice() {
        PersistenceManager pm = pmf.getPersistenceManager();
        pm.currentTransaction().begin();

        //Fetch the products of the invoice
        Query q = pm.newQuery(Product.class);
        //q.declareParameters("String c");
        q.declareParameters("String c, char s");
        //q.setFilter("(color == c)");
        q.setFilter("((color == c) && (size == s))");
        Collection products = (Collection)
            //q.execute(colors[0]);
            q.execute(colors[0], new Character(sizes[0]));
        ArrayList pus = new ArrayList();
        Iterator it = products.iterator();
        while(it.hasNext()) {
            pus.add(new ProductUnits(
                    (Product) it.next(), null, 20, pus.size()));
        }
        q.closeAll();

        //Fetch the address of the invoice
        q = pm.newQuery(Address.class);
        q.setFilter("(name == n)");
        q.declareParameters("String n");
        Collection addresses = (Collection) q.execute("Seb");
        Address sebAddress = (Address) addresses.iterator().next();
        q.closeAll();

        //Build the invoice
        Invoice invoice = new Invoice(217, sebAddress, pus);
        //manage the reverse reference between Invoice and ProductUnits
        for(int i=0; i<pus.size(); i++) {
            ((ProductUnits) pus.get(i)).setInvoice(invoice);
        }
        pm.makePersistent(invoice);
        System.out.println(invoice);

        pm.currentTransaction().commit();
        pm.close();

        return invoice;
    }

    public void printAll() {
        PersistenceManager pm = pmf.getPersistenceManager();
        Query q = pm.newQuery(Product.class);
        q.setOrdering("name ascending");
        Collection col = (Collection) q.execute();
        Iterator it = col.iterator();
        int i = 0;
        while(it.hasNext()) {
            Product p = (Product) it.next();
            System.out.println("Product (" + p + ")");
            i++;
        }
        q.closeAll();
        System.out.println("There is " + i + " products.");

        q = pm.newQuery(Address.class);
        col = (Collection) q.execute();
        it = col.iterator();
        i = 0;
        while(it.hasNext()) {
            Address a = (Address) it.next();
            System.out.println("Address (" + a + ")");
            i++;
        }
        q.closeAll();
        System.out.println("There is " + i + " addresses.");

        q = pm.newQuery(Invoice.class);
        col = (Collection) q.execute();
        it = col.iterator();
        i = 0;
        while(it.hasNext()) {
            Invoice in = (Invoice) it.next();
            System.out.println("Invoice (" + in + ")");
            i++;
        }
        q.closeAll();
        System.out.println("There is " + i + " invoices.");
        pm.close();
    }

    public void removeProducts() {
        PersistenceManager pm = pmf.getPersistenceManager();
        pm.currentTransaction().begin();
        Query q = pm.newQuery(Product.class);

        pm.deletePersistentAll((Collection) q.execute());
        q.closeAll();
        pm.currentTransaction().commit();
        pm.close();
    }

    public void removeAddresses() {
        PersistenceManager pm = pmf.getPersistenceManager();
        pm.currentTransaction().begin();
        Query q = pm.newQuery(Address.class);
        pm.deletePersistentAll((Collection) q.execute());
        q.closeAll();
        pm.currentTransaction().commit();
        pm.close();
    }

    public void removeInvoices() {
        PersistenceManager pm = pmf.getPersistenceManager();
        pm.currentTransaction().begin();
        Query q = pm.newQuery(Invoice.class);
        Iterator it = ((Collection) q.execute()).iterator();
        while(it.hasNext()) {
            Invoice i = (Invoice) it.next();
            Collection pus = i.getProductUnits();
            Iterator puit = pus.iterator();
            if (!puit.hasNext()) {
                System.out.println("No ProductUnits referenced by the invoice");
            }
            while(puit.hasNext()) {
                ProductUnits pu = (ProductUnits) puit.next();
                System.out.println("remove : " + pu);
                pm.deletePersistent(pu);
            }
            pm.deletePersistent(i);
        }
        q.closeAll();
        pm.currentTransaction().commit();
        pm.close();
    }

    public void removeInvoice(Invoice i) {
        PersistenceManager pm = pmf.getPersistenceManager();
        pm.currentTransaction().begin();
        pm.deletePersistentAll(i.getProductUnits());
        pm.deletePersistent(i);
        pm.currentTransaction().commit();
        pm.close();
    }

}
TOP

Related Classes of invoice.InvoiceAppsHelper

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.