KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > store > BaseOrderGenerator


1 /*
2  * Created on Jan 17, 2005
3  */

4 package com.openedit.store;
5
6 import java.io.File JavaDoc;
7 import java.io.FileInputStream JavaDoc;
8 import java.io.FileOutputStream JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.util.Properties JavaDoc;
11
12 /**
13  * @author dbrown
14  *
15  */

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 JavaDoc( 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     // Should be "WEB#########"
41
public String JavaDoc nextOrderNumber( Store inStore ) throws StoreException
42     {
43         File JavaDoc orderProperties = new File JavaDoc( inStore.getStoreDirectory(), "orders/order.properties" );
44         orderProperties.getParentFile().mkdirs();
45         Properties JavaDoc props = new Properties JavaDoc();
46         try
47         {
48             if ( orderProperties.exists() )
49             {
50                 props.load( new FileInputStream JavaDoc( orderProperties ) );
51             }
52             String JavaDoc 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 JavaDoc( orderProperties ), "Order properties count" );
62             //no more than 12 total characters
63
StringBuffer JavaDoc id = new StringBuffer JavaDoc("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 JavaDoc 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