package org.julp.examples;
import java.util.*;
import org.julp.*;
import java.sql.*;
public class ProductFactory extends org.julp.DomainObjectFactory implements java.io.Serializable, Cloneable{
public ProductFactory() {
this.setRequestor(Product.class);
/* IT IS NOT NESSESARY TO LOAD MAPPINGS THIS WAY, COULD BE ANYTHING: XML, JNDI, DATABASE, ETC... */
setMapping(loadMappings("Product.properties"));
sqlMap = loadMappings("Product.sql");
}
protected Properties sqlMap = null;
public Properties loadMappings(String path){
java.io.InputStream inStream = null;
Properties props = new Properties();
try{
inStream = this.getClass().getResourceAsStream(path);
props.load(inStream);
}catch(java.io.IOException ioe){
throw new RuntimeException(ioe);
}finally{
try{
inStream.close();
}catch(java.io.IOException ioe){
throw new RuntimeException(ioe);
}
}
return props;
}
public int findAllProducts(){
int records = 0;
try{
records = this.load(this.dbServices.getResultSet(sqlMap.getProperty("findAllProducts")));
printAllProducts();
}catch (SQLException sqle){
throw new RuntimeException(sqle);
}
return records;
}
public void createAndStoreProductsLater(){
this.setGenerateSQLOnly(true);
int records = findAllProducts();
Product product = new Product();
product.setProductId(new Integer(records + 1)); // this is NOT proper way to genarate id !!!
product.setName("Zaurus SL-5600");
product.setPrice(299.98);
product.setComments("Good deal!");
this.create(product);
System.out.println("\n=============== Created product: " + product + "\n");
/* another way:
product.create();
product.setObjectId(this.getNextObjectId());
this.getObjectList().add(product);
or:
product.create();
this.setObject(product);
*/
/*
Product productToRemove = (Product) this.objectList.get(0);
this.remove(productToRemove);
System.out.println("\nRemoved objects: " + this.getRemovedObjects() + "\n");
// another way:
//((Product) this.objectList.get(0)).remove();
*/
java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
ListIterator li = this.objectList.listIterator();
while (li.hasNext()){
Product productToUpdate = (Product) li.next();
double currentPrice = productToUpdate.getPrice();
if (currentPrice < 10){
double newPrice = currentPrice * 1.1;
productToUpdate.setPrice(Double.parseDouble(nf.format(newPrice)));
productToUpdate.store();
}
}
//System.out.println("\n======================= this is after data modifications ===========================\n");
//printAllProducts();
/*
try{
this.dbServices.beginTran();
boolean success = this.writeData();
Throwable t = this.getWhatIsWrong();
if (t != null){
throw t;
}
if (success){
this.dbServices.commitTran();
}else{
throw new SQLException("Data modification: failed");
}
this.synchronizePersistentState()
}catch (Throwable t){
try{
this.dbServices.rollbackTran();
}catch (SQLException sqle){
sqle.printStackTrace();
throw new RuntimeException(sqle);
}
t.printStackTrace();
}finally{
try{
this.dbServices.release(true);
}catch (SQLException sqle){
sqle.printStackTrace();
throw new RuntimeException(sqle);
}
}
*/
String generatedSQLasXML = null;
List generatedSQL = null;
try{
boolean success = this.writeData();
Throwable t = this.getWhatIsWrong();
if (t != null){
throw t;
}
if (!success){
throw new SQLException("Data modification: failed");
}
// make sure you get this values BEFORE synchronizePersistentState()
generatedSQLasXML = this.getGeneratedSQLasXML();
generatedSQL = this.getGeneratedSQL();
this.synchronizePersistentState();
}catch (Throwable t){
t.printStackTrace();
}finally{
try{
this.dbServices.release(true);
}catch (SQLException sqle){
sqle.printStackTrace();
throw new RuntimeException(sqle);
}
}
System.out.println("\n======================= this generated XML which can be sent to another Application/ApplicationServer to update database ===========================\n");
System.out.println(generatedSQLasXML);
System.out.println("\n======================= this generated stetements and parameters which can be sent to another Application/ApplicationServer to update database ===========================\n");
Iterator it = generatedSQL.iterator();
while(it.hasNext()){
Object[] obj = (Object[]) it.next();
Object[] params = (Object[]) obj[1];
System.out.println("sql: " + obj[0] + " params: " + Arrays.asList(params));
}
}
public void getProductPages(){
System.out.println("\n======================= this is all products ===========================\n");
findAllProducts();
this.setPageSize(10);
PageHolder page = this.getPage(1);
System.out.println("\n=============== Total records: " + page.getObjectsTotal() + ", Page " + page.getPageNumber() + " of " + page.getPagesTotal() + " ===============\n");
Iterator iter1 = page.getPage().iterator();
while (iter1.hasNext()){
Product product = (Product) iter1.next();
System.out.println(product);
}
PageHolder thirdPage = this.getPage(3);
System.out.println("\n=============== Total records: " + thirdPage.getObjectsTotal() + ", Page " + thirdPage.getPageNumber() + " of " + thirdPage.getPagesTotal() + " ===============\n");
Iterator iter2 = thirdPage.getPage().iterator();
while (iter2.hasNext()){
Product product = (Product) iter2.next();
System.out.println(product);
}
}
protected void printAllProducts(){
List products = this.getObjectList();
Iterator productsIter = products.iterator();
while (productsIter.hasNext()){
Product product = (Product) productsIter.next();
System.out.println(product);
}
}
}
|