1 4 package com.openedit.store; 5 6 import java.io.File ; 7 import java.io.FileInputStream ; 8 import java.io.FileOutputStream ; 9 import java.util.ArrayList ; 10 import java.util.Properties ; 11 12 16 public class BaseOrderGenerator implements OrderGenerator 17 { 18 public BaseOrderGenerator() 19 { 20 } 21 22 public Order createNewOrder(Store inStore, Cart inCart) throws StoreException 23 { 24 Order order = new Order(); 25 order.setShippingMethod(inCart.getShippingMethod()); 26 order.setTotalPrice(inCart.getTotalPrice()); 27 order.setSubTotal(inCart.getSubTotal()); 28 order.setTax(inCart.getTotalTax()); 29 order.setTotalShipping(inCart.getTotalShipping()); 30 order.setItems(new ArrayList ( inCart.getItems() ) ); 31 order.setCustomer(inCart.getCustomer()); 32 order.setPaymentMethod(inCart.getCustomer().getPaymentMethod()); 33 order.setShippingMethod(inCart.getShippingMethod()); 34 order.setId(nextOrderNumber(inStore)); 35 OrderState state = inStore.getOrderState(Order.ACCEPTED); 36 order.setOrderState(state); 37 return order; 38 } 39 40 public String nextOrderNumber( Store inStore ) throws StoreException 42 { 43 File orderProperties = new File ( inStore.getStoreDirectory(), "orders/order.properties" ); 44 orderProperties.getParentFile().mkdirs(); 45 Properties props = new Properties (); 46 try 47 { 48 if ( orderProperties.exists() ) 49 { 50 props.load( new FileInputStream ( orderProperties ) ); 51 } 52 String countString = props.getProperty("count"); 53 int count = 0; 54 if ( countString != null ) 55 { 56 count = Integer.valueOf( countString ).intValue(); 57 } 58 count++; 59 countString = String.valueOf(count); 60 props.setProperty( "count", countString ); 61 props.store( new FileOutputStream ( orderProperties ), "Order properties count" ); 62 StringBuffer id = new StringBuffer ("WEB"); 64 65 int zeros = 10 - 3 - countString.length(); 66 for (int i = 0; i < zeros; i++) 67 { 68 id.append( "0"); 69 } 70 id.append(countString); 71 return id.toString(); 72 } catch ( Exception ex ) 73 { 74 if ( ex instanceof StoreException) 75 { 76 throw (StoreException)ex; 77 } 78 else 79 { 80 throw new StoreException(ex); 81 } 82 } 83 } 84 } 85 | Popular Tags |