1 4 package com.openedit.store.retailproconvert; 5 6 import java.io.BufferedInputStream ; 7 import java.io.File ; 8 import java.io.FileInputStream ; 9 import java.io.FileOutputStream ; 10 import java.io.FileWriter ; 11 import java.io.IOException ; 12 import java.io.InputStream ; 13 import java.text.SimpleDateFormat ; 14 import java.util.Date ; 15 import java.util.zip.ZipEntry ; 16 import java.util.zip.ZipOutputStream ; 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 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 zipFileNumber = nextZipFileNumber(); 43 44 exportToCustomersFile( inCustomer,zipFileNumber ); 46 47 } 48 protected String nextZipFileNumber( ) throws StoreException 49 { 50 int count = getIntCounter().incrementCount(); 51 String countString = String.valueOf(count); 52 String zeros = "000000000"; 53 zeros = zeros.substring(countString.length()); 54 return zeros + countString; 55 } 56 protected void zipUpFile( File inFile, File outFile ) throws StoreException 57 { 58 ZipOutputStream zipStream = null; 59 InputStream inStream = null; 60 try 61 { 62 zipStream = new ZipOutputStream ( new FileOutputStream ( outFile ) ); 63 inStream = new BufferedInputStream ( new FileInputStream ( inFile ) ); 64 zipStream.putNextEntry( new ZipEntry ( inFile.getName() ) ); 65 int b; 66 while ( (b = inStream.read()) >= 0 ) 67 { 68 zipStream.write( b ); 69 } 70 zipStream.closeEntry(); 71 } 72 catch ( IOException 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 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 format = new SimpleDateFormat ("yyyyMMddHHmmssSSS"); 105 106 String filename = "CUST_" + format.format(new Date ()) + ".xml"; 107 108 File customersFile = new File ( getOrdersDirectory(),filename); 109 try 110 { 111 new XmlUtil().saveXml(root,new FileWriter (customersFile),"UTF-8"); 112 113 File outFile = new File ( getOrdersDirectory( ), "C" + inZipNum + ".zip" ); 115 zipUpFile( customersFile, outFile); 116 customersFile.delete(); 117 118 File copy = new File ( getCustomerDirectory().getParentFile().getParentFile(),"xferorders/" + outFile.getName() ); 120 copy = copy.getAbsoluteFile(); 121 new OutputFiller().fill(outFile,copy); 122 } 123 catch ( IOException ex) 124 { 125 throw new StoreException(ex); 126 } 127 128 } 129 protected File getOrdersDirectory() 130 { 131 return new File ( getCustomerDirectory().getParentFile().getParentFile() , "/store/orders/"); 132 } 133 134 135 protected Element getRootElement( File inFile, String inElementName ) throws StoreException 136 { 137 if ( inFile.exists() ) 138 { 139 SAXReader reader = new SAXReader(); 140 Document document; 141 FileInputStream stream = null; 142 try 143 { 144 stream = new FileInputStream (inFile); 145 document = reader.read(stream); 146 return document.getRootElement(); 147 } 148 catch( Exception 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 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 (getOrdersDirectory(),"customer.properties")); 179 } 180 return fieldIntCounter; 181 } 182 183 public void setIntCounter(IntCounter inIntCounter) 184 { 185 fieldIntCounter = inIntCounter; 186 } 187 } | Popular Tags |