KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > store > retailproconvert > RetailProOrderArchive


1 /*
2  * Created on Oct 6, 2004
3  */

4 package com.openedit.store.retailproconvert;
5
6 import java.io.BufferedInputStream JavaDoc;
7 import java.io.File JavaDoc;
8 import java.io.FileInputStream JavaDoc;
9 import java.io.FileOutputStream JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.text.DateFormat JavaDoc;
13 import java.text.SimpleDateFormat JavaDoc;
14 import java.util.Date JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Properties JavaDoc;
17 import java.util.zip.ZipEntry JavaDoc;
18 import java.util.zip.ZipOutputStream JavaDoc;
19
20 import org.dom4j.Element;
21 import org.openedit.money.Fraction;
22 import org.openedit.money.Money;
23
24 import com.openedit.WebPageRequest;
25 import com.openedit.store.AbstractXmlOrderArchive;
26 import com.openedit.store.CartItem;
27 import com.openedit.store.CreditPaymentMethod;
28 import com.openedit.store.Order;
29 import com.openedit.store.ShippingMethod;
30 import com.openedit.store.Store;
31 import com.openedit.store.StoreException;
32 import com.openedit.store.customer.Address;
33 import com.openedit.store.customer.Customer;
34 import com.openedit.util.FileUtils;
35 import com.openedit.util.OutputFiller;
36
37 /**
38  * An order archive that exports orders in the Retail Pro XML format.
39  *
40  * @author cburkey
41  */

42 public class RetailProOrderArchive extends AbstractXmlOrderArchive
43 {
44     protected static final DateFormat JavaDoc DATE_FORMAT = new SimpleDateFormat JavaDoc( "M/d/yy h:mm:ss a" );
45     //protected static final String ORDERS_FILENAME = "SO_2005orders.xml";
46
//protected static final String CUSTOMERS_FILENAME = "CUST_2005customers.xml";
47

48     public void archiveOrderData( Store inStore ) throws StoreException
49     {
50 /* File ordersFile = getOrdersFile( inStore );
51         File customersFile = getCustomersFile( inStore );
52
53         if ( !ordersFile.exists() && !customersFile.exists() )
54         {
55             return;
56         }
57
58         String zipFileNumber;
59         try
60         {
61             zipFileNumber = nextZipFileNumber( inStore );
62         }
63         catch ( IOException e )
64         {
65             throw new StoreException( e );
66         }
67
68         if ( ordersFile.exists() )
69         {
70             zipUpFile( ordersFile, getOrdersZipFile( inStore, zipFileNumber ) );
71             ordersFile.delete();
72         }
73
74         if ( customersFile.exists() )
75         {
76             zipUpFile( customersFile, getCustomersZipFile( inStore, zipFileNumber ) );
77             customersFile.delete();
78         }
79         */

80     }
81
82     protected void zipUpFile( File JavaDoc inFile, File JavaDoc outFile ) throws StoreException
83     {
84         ZipOutputStream JavaDoc zipStream = null;
85         InputStream JavaDoc inStream = null;
86         try
87         {
88             zipStream = new ZipOutputStream JavaDoc( new FileOutputStream JavaDoc( outFile ) );
89             inStream = new BufferedInputStream JavaDoc( new FileInputStream JavaDoc( inFile ) );
90             zipStream.putNextEntry( new ZipEntry JavaDoc( inFile.getName() ) );
91             int b;
92             while ( (b = inStream.read()) >= 0 )
93             {
94                 zipStream.write( b );
95             }
96             zipStream.closeEntry();
97         }
98         catch ( IOException JavaDoc e )
99         {
100             throw new StoreException( e );
101         }
102         finally
103         {
104             FileUtils.safeClose( zipStream );
105             FileUtils.safeClose( inStream );
106         }
107     }
108
109     /*
110      * (non-javadoc)
111      *
112      * @see com.openedit.store.EmailOrderArchive#exportNewOrder(com.openedit.WebPageContext,
113      * com.openedit.store.Store, com.openedit.store.Cart)
114      */

115     public void exportNewOrder( WebPageRequest inContext, Store inStore, Order inOrder )
116             throws StoreException
117     {
118         String JavaDoc zipFileNumber = nextZipFileNumber( inStore );
119
120         try
121         {
122             // Export the order to the existing customers file
123
//exportToCustomersFile( inStore, inOrder,zipFileNumber );
124
// Export the order to the existing orders file
125
exportToOrdersFile( inStore, inOrder,zipFileNumber );
126     
127         } catch ( Exception JavaDoc ex)
128         {
129             if ( ex instanceof StoreException)
130             {
131                 throw (StoreException)ex;
132             }
133             throw new StoreException(ex);
134         }
135     }
136     protected void exportToOrdersFile( Store inStore, Order inOrder, String JavaDoc inZipNum ) throws Exception JavaDoc
137     {
138         SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc("yyyyMMddHHmmssSSS");
139         
140         String JavaDoc filename = "SO_" + format.format(new Date JavaDoc()) + ".xml";
141
142         File JavaDoc salesOrdersFile = new File JavaDoc( getOrdersDirectory( inStore ),filename);
143         Element root = getRootElement( salesOrdersFile, "SALEORDERS" );
144         Element so = root.addElement( "SO" );
145         appendHeader( so, inStore, inOrder );
146         appendItems( so, inStore, inOrder );
147         appendSubTotals( so, inStore, inOrder );
148         writeXmlFile( root, salesOrdersFile );
149
150         File JavaDoc outFile = getOrdersZipFile( inStore, inZipNum );
151         zipUpFile( salesOrdersFile, outFile);
152         
153         File JavaDoc copy = new File JavaDoc( inStore.getRootDirectory(),"xferorders/" + outFile.getName() );
154         copy = copy.getAbsoluteFile();
155         copy.getParentFile().mkdirs();
156         new OutputFiller().fill(outFile,copy);
157         
158         salesOrdersFile.delete();
159
160     }
161
162     /**
163      * @param inRoot
164      * @param inStore
165      * @param inOrder
166      */

167     private void appendSubTotals( Element inRoot, Store inStore, Order inOrder )
168     {
169         Element so_subtotal = inRoot.addElement( "SO_SUBTOTALS" );
170         so_subtotal.addAttribute( "disc_percent", "0" );
171         so_subtotal.addAttribute( "ship_percent", "0" );
172         so_subtotal.addAttribute( "disc_amount", "0" );
173         so_subtotal.addAttribute( "ship_amount", inOrder.getTotalShipping().toShortString() );
174         so_subtotal.addAttribute( "total_tax", inOrder.getTax().toShortString() );
175         so_subtotal.addAttribute( "subtotal_used", inOrder.getSubTotal().toShortString() );
176         so_subtotal.addAttribute( "tax_area", "clermont" ); //TODO: Fix this
177
}
178
179     /**
180      * @param inRoot
181      * @param inStore
182      * @param inOrder
183      */

184     private void appendItems( Element inRoot, Store inStore, Order inOrder )
185     {
186         int i = -1;
187         for ( Iterator JavaDoc iter = inOrder.getItems().iterator(); iter.hasNext(); )
188         {
189             i++;
190             CartItem item = (CartItem) iter.next();
191
192             Element soitem = inRoot.addElement( "SO_ITEM" );
193             soitem.addAttribute( "item_sid", item.getSku() ); //item.get("RTP_sid" )
194

195             soitem.addAttribute( "item_no", item.getSku() );
196             soitem.addAttribute( "row_num", String.valueOf( i ) );
197             //soitem.addAttribute("dcs",item.get("RTP_dcs"));
198
//soitem.addAttribute("vc",item.get("RTP_item_no"));
199
soitem.addAttribute( "tax_code", "1" ); //TODO: Is this the
200
// ID or the
201
// Description
202

203             soitem.addAttribute( "price", item.getYourPrice().toShortString() );
204             soitem.addAttribute( "orig_price", item.getProduct().getRetailPrice().toShortString() );
205
206             soitem.addAttribute( "qty_ordered", String.valueOf( item.getQuantity() ) );
207             Fraction rate = inOrder.getCustomer().getTaxRate();
208             Money tax = item.calculateTax( rate );
209             soitem.addAttribute( "tax_amount", tax.toShortString() );
210             soitem.addAttribute( "orig_tax_code", "1" );
211             soitem.addAttribute( "orig_tax_amount", tax.toShortString() );
212             soitem.addAttribute( "tax_percent", rate.toString() );
213             /*
214              * item_sid="PAAPILPBGDILMAOA" item_no="24203" row_num="0" dcs="1A"
215              * vc="" desc1="3310 BLK CAN CHK" desc2="" tax_code="1" attr="D"
216              * size="6.5" misc1="" misc2="" misc3="" misc4="" decimals="0"
217              * price="36.99" cost="16.67" orig_price="36.99" qty_ordered="10"
218              * tax_amount="17.33" orig_tax_code="1" orig_tax_amount="17.33"
219              * tax_percent="5.50" upc="0022859063692" alu="" case_qty="" />
220              */

221         }
222     }
223     
224     // Should be "WEB#########"
225
/* public String nextOrderNumber( Store inStore ) throws IOException
226     {
227         InputStream inStream = null;
228         OutputStream outStream = null;
229         try
230         {
231             File orderProperties = getOrderPropertiesFile( inStore );
232             Properties props = new Properties();
233             if ( orderProperties.exists() )
234             {
235                 inStream = new FileInputStream( orderProperties );
236                 props.load( inStream );
237             }
238             String countString = props.getProperty("count");
239             int count = 0;
240             if ( countString != null )
241             {
242                 count = Integer.valueOf( countString ).intValue();
243             }
244             count++;
245             countString = String.valueOf(count);
246             props.setProperty( "count", countString );
247             outStream = new FileOutputStream( orderProperties );
248             props.store( outStream, "Order properties count" );
249             String zeros = "000000000";
250             zeros = zeros.substring(countString.length());
251             return "WEB" + zeros + countString;
252         }
253         finally
254         {
255             FileUtils.safeClose( outStream );
256             FileUtils.safeClose( inStream);
257         }
258     }
259 */

260     // Should be "#########"
261
protected String JavaDoc nextZipFileNumber( Store inStore ) throws StoreException
262     {
263         FileInputStream JavaDoc inStream = null;
264         FileOutputStream JavaDoc outStream = null;
265         try
266         {
267             File JavaDoc orderProperties = getOrderPropertiesFile( inStore );
268             Properties JavaDoc props = new Properties JavaDoc();
269             if ( orderProperties.exists() )
270             {
271                 inStream = new FileInputStream JavaDoc( orderProperties );
272                 props.load( inStream );
273             }
274             final String JavaDoc ZIP_FILE_COUNT_PROPERTY = "zipFileNumber";
275             String JavaDoc countString = props.getProperty(ZIP_FILE_COUNT_PROPERTY);
276             int count = 0;
277             if ( countString != null )
278             {
279                 count = Integer.valueOf( countString ).intValue();
280             }
281             count++;
282             countString = String.valueOf(count);
283             props.setProperty( ZIP_FILE_COUNT_PROPERTY, countString );
284             outStream = new FileOutputStream JavaDoc( orderProperties );
285             props.store( outStream, "Order properties count" );
286             String JavaDoc zeros = "000000000";
287             zeros = zeros.substring(countString.length());
288             return zeros + countString;
289         }
290         catch ( Exception JavaDoc ex)
291         {
292             throw new StoreException(ex);
293         }
294         finally
295         {
296             FileUtils.safeClose(outStream);
297             FileUtils.safeClose(inStream);
298         }
299     }
300
301     protected void appendHeader( Element inRoot, Store inStore, Order inOrder ) throws StoreException
302     {
303         Element headerElem = inRoot.addElement( "SO_HEADER" );
304         Customer customer = inOrder.getCustomer();
305
306         //An order number is something like WEB0000123 total of 10 digits
307
headerElem.addAttribute( "order_sid", inOrder.getId().substring(5) );
308         
309         headerElem.addAttribute( "so_number", inOrder.getId() );
310         headerElem.addAttribute( "so_type", "0" );
311         headerElem.addAttribute( "date_ordered", DATE_FORMAT.format( inOrder.getDate() ) );
312
313         saveAddress( inOrder, headerElem, customer, customer.getBillingAddress(), "billto" );
314         saveAddress( inOrder, headerElem, customer, customer.getShippingAddress(), "shipto" );
315
316         ShippingMethod method = inOrder.getShippingMethod();
317         if ( method != null)
318         {
319             headerElem.addAttribute( "shipping_provider", method.getId() );
320             headerElem.addAttribute( "shipping_method", method.getId() );
321         }
322         if ( inOrder.getPaymentMethod() != null)
323         {
324             CreditPaymentMethod creditCard = (CreditPaymentMethod) inOrder.getPaymentMethod();
325             
326             headerElem.addAttribute( "cc_type", creditCard.getCreditCardType().getId() ); // FIXME:
327
// Is
328
// this
329
// right?
330
headerElem.addAttribute( "cc_name", creditCard.getCreditCardType().getName() ); // FIXME:
331
// Is
332
// this
333
// right?
334
headerElem.addAttribute( "cc_number", creditCard.getCardNumber() );
335             headerElem.addAttribute( "cc_expire", creditCard.getExpirationMonth() + "/"
336                     + creditCard.getExpirationYear() );
337         }
338     }
339
340     /**
341      * @param inOrder
342      * @param headerElem
343      * @param customer
344      */

345     private void saveAddress( Order inOrder, Element headerElem, Customer customer, Address inAddress, String JavaDoc inShipTo )
346     {
347         headerElem.addAttribute( "so_" + inShipTo + "_cust_sid", customer.getUserName() ); // FIXME:
348
// Need
349
//headerElem.addAttribute( "so_" + inShipTo + "_rpro_cust_sid", customer.getReferenceNumber() );
350
headerElem.addAttribute( "so_" + inShipTo + "_date_created", DATE_FORMAT.format( inOrder
351                 .getDate() ) ); // FIXME: This should not be required, but is.
352
// Perhaps we can use a bogus value.
353
headerElem.addAttribute( "so_" + inShipTo + "_title", customer.getTitle() );
354         headerElem.addAttribute( "so_" + inShipTo + "_first_name", customer.getFirstName() );
355         headerElem.addAttribute( "so_" + inShipTo + "_last_name", customer.getLastName() );
356         if ( inAddress == null )
357         {
358             inAddress = customer.getBillingAddress();
359         }
360         headerElem.addAttribute( "so_" + inShipTo + "_address1", inAddress.getAddress1() );
361         headerElem.addAttribute( "so_" + inShipTo + "_address2", inAddress.getAddress2() );
362         headerElem.addAttribute( "so_" + inShipTo + "_city", inAddress.getCity() );
363         headerElem.addAttribute( "so_" + inShipTo + "_state_or_province", inAddress.getState() ); // FIXME:
364
// name
365
headerElem.addAttribute( "so_" + inShipTo + "_state_short", inAddress.getState() ); // FIXME:
366
// state
367
headerElem.addAttribute( "so_" + inShipTo + "_country", inAddress.getCountry() ); // FIXME:
368
// name
369
headerElem.addAttribute( "so_" + inShipTo + "_country_short", inAddress.getCountry() ); // FIXME:
370

371         headerElem.addAttribute( "so_" + inShipTo + "_postal_code", inAddress.getZipCode() );
372         headerElem.addAttribute( "so_" + inShipTo + "_phone1", customer.getPhone1() );
373         headerElem.addAttribute( "so_" + inShipTo + "_email", customer.getEmail() );
374     }
375
376     protected File JavaDoc getOrdersZipFile( Store inStore, String JavaDoc inZipFileNumber )
377     {
378         return new File JavaDoc( getOrdersDirectory( inStore ), "O" + inZipFileNumber + ".zip" );
379     }
380
381
382     protected File JavaDoc getOrderPropertiesFile( Store inStore )
383     {
384         return new File JavaDoc( getOrdersDirectory( inStore ), "order.properties" );
385     }
386 }
Popular Tags