KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Created on Oct 25, 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.FileWriter JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.InputStream JavaDoc;
13 import java.text.SimpleDateFormat JavaDoc;
14 import java.util.Date JavaDoc;
15 import java.util.zip.ZipEntry JavaDoc;
16 import java.util.zip.ZipOutputStream JavaDoc;
17
18 import org.dom4j.Document;
19 import org.dom4j.DocumentHelper;
20 import org.dom4j.Element;
21 import org.dom4j.io.SAXReader;
22
23 import com.openedit.modules.cart.XmlCustomerArchive;
24 import com.openedit.store.StoreException;
25 import com.openedit.store.customer.Address;
26 import com.openedit.store.customer.Customer;
27 import com.openedit.util.FileUtils;
28 import com.openedit.util.IntCounter;
29 import com.openedit.util.OutputFiller;
30 import com.openedit.util.XmlUtil;
31
32 /**
33  * @author Matthew Avery, mavery@einnovation.com
34  */

35 public class RetailProCustomerArchive extends XmlCustomerArchive
36 {
37     protected IntCounter fieldIntCounter;
38     
39     public void saveAndExportCustomer( Customer inCustomer ) throws StoreException
40     {
41         saveCustomer( inCustomer );
42         String JavaDoc zipFileNumber = nextZipFileNumber();
43
44         // Export the order to the existing customers file
45
exportToCustomersFile( inCustomer,zipFileNumber );
46
47     }
48     protected String JavaDoc nextZipFileNumber( ) throws StoreException
49     {
50         int count = getIntCounter().incrementCount();
51         String JavaDoc countString = String.valueOf(count);
52         String JavaDoc zeros = "000000000";
53         zeros = zeros.substring(countString.length());
54         return zeros + countString;
55     }
56     protected void zipUpFile( File JavaDoc inFile, File JavaDoc outFile ) throws StoreException
57     {
58             ZipOutputStream JavaDoc zipStream = null;
59             InputStream JavaDoc inStream = null;
60             try
61             {
62                 zipStream = new ZipOutputStream JavaDoc( new FileOutputStream JavaDoc( outFile ) );
63                 inStream = new BufferedInputStream JavaDoc( new FileInputStream JavaDoc( inFile ) );
64                 zipStream.putNextEntry( new ZipEntry JavaDoc( inFile.getName() ) );
65                 int b;
66                 while ( (b = inStream.read()) >= 0 )
67                 {
68                     zipStream.write( b );
69                 }
70                 zipStream.closeEntry();
71             }
72             catch ( IOException JavaDoc e )
73             {
74                 throw new StoreException( e );
75             }
76             finally
77             {
78                 FileUtils.safeClose( zipStream );
79                 FileUtils.safeClose( inStream );
80             }
81     }
82         
83         
84     protected void exportToCustomersFile( Customer customer, String JavaDoc inZipNum ) throws StoreException
85     {
86         Element root = DocumentHelper.createElement( "Customers" );
87         Element newCustomer = root.addElement( "CUSTOMER" );
88         newCustomer.addAttribute( "cust_sid", customer.getUserName() );
89         newCustomer.addAttribute( "rpro_cust_sid", customer.getReferenceNumber() );
90         newCustomer.addAttribute( "first_name", customer.getFirstName() );
91         newCustomer.addAttribute( "last_name", customer.getLastName() );
92         Address address = customer.getShippingAddress();
93         newCustomer.addAttribute( "address1", address.getAddress1() );
94         newCustomer.addAttribute( "address2", address.getAddress2() );
95         newCustomer.addAttribute( "city", address.getCity() );
96         newCustomer.addAttribute( "state_or_province", address.getState() );
97         newCustomer.addAttribute( "region_code", address.getState() );
98         newCustomer.addAttribute( "country", address.getCountry() );
99         newCustomer.addAttribute( "postal_code", address.getZipCode() );
100         newCustomer.addAttribute( "phone1", customer.getPhone1() );
101         newCustomer.addAttribute( "email", customer.getEmail() );
102         newCustomer.addAttribute( "has_so", "0" );
103
104         SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc("yyyyMMddHHmmssSSS");
105         
106         String JavaDoc filename = "CUST_" + format.format(new Date JavaDoc()) + ".xml";
107         
108         File JavaDoc customersFile = new File JavaDoc( getOrdersDirectory(),filename);
109         try
110         {
111             new XmlUtil().saveXml(root,new FileWriter JavaDoc(customersFile),"UTF-8");
112             
113             //now zip it up
114
File JavaDoc outFile = new File JavaDoc( getOrdersDirectory( ), "C" + inZipNum + ".zip" );
115             zipUpFile( customersFile, outFile);
116             customersFile.delete();
117     
118             //copy to xfer dir
119
File JavaDoc copy = new File JavaDoc( getCustomerDirectory().getParentFile().getParentFile(),"xferorders/" + outFile.getName() );
120             copy = copy.getAbsoluteFile();
121             new OutputFiller().fill(outFile,copy);
122         }
123         catch ( IOException JavaDoc ex)
124         {
125             throw new StoreException(ex);
126         }
127         
128     }
129     protected File JavaDoc getOrdersDirectory()
130     {
131         return new File JavaDoc( getCustomerDirectory().getParentFile().getParentFile() , "/store/orders/");
132     }
133
134     
135     protected Element getRootElement( File JavaDoc inFile, String JavaDoc inElementName ) throws StoreException
136     {
137         if ( inFile.exists() )
138         {
139             SAXReader reader = new SAXReader();
140             Document document;
141             FileInputStream JavaDoc stream = null;
142             try
143             {
144                 stream = new FileInputStream JavaDoc(inFile);
145                 document = reader.read(stream);
146                 return document.getRootElement();
147             }
148             catch( Exception JavaDoc e )
149             {
150                 throw new StoreException( e );
151             }
152             finally
153             {
154                 if ( stream != null )
155                 {
156                     try
157                     {
158                         stream.close();
159                     }
160                     catch ( IOException JavaDoc e )
161                     {
162                         throw new StoreException( e );
163                     }
164                 }
165             }
166         }
167         else
168         {
169             return DocumentHelper.createElement( inElementName );
170         }
171     }
172
173     public IntCounter getIntCounter()
174     {
175         if ( fieldIntCounter == null)
176         {
177             fieldIntCounter = new IntCounter();
178             fieldIntCounter.setCounterFile(new File JavaDoc(getOrdersDirectory(),"customer.properties"));
179         }
180         return fieldIntCounter;
181     }
182
183     public void setIntCounter(IntCounter inIntCounter)
184     {
185         fieldIntCounter = inIntCounter;
186     }
187 }
Popular Tags