1 4 package com.openedit.store; 5 6 import java.io.File ; 7 import java.io.FileWriter ; 8 import java.io.IOException ; 9 import java.util.Iterator ; 10 import java.util.List ; 11 12 import org.dom4j.DocumentException; 13 import org.dom4j.Element; 14 15 18 public class TextExport 19 { 20 protected String fieldDelimiter = "\t"; 21 protected Dom4JHelper fieldHelper; 22 23 protected Dom4JHelper getHelper() 24 { 25 if( fieldHelper == null ) 26 { 27 fieldHelper = new Dom4JHelper(); 28 } 29 return fieldHelper; 30 } 31 32 protected Element getRootElement( File inFile, String inName ) throws IOException , DocumentException 33 { 34 return getHelper().getRootElement( inFile, inName ); 35 } 36 37 public void exportOrders( File inOrderFile, File inOrderExportFile ) throws Exception 38 { 39 40 Element root = getRootElement( inOrderFile, "orders" ); 41 FileWriter writer = new FileWriter ( inOrderExportFile ); 42 StringBuffer header = new StringBuffer (); 43 header.append( "order_number" ); 44 header.append( getDelimiter() ); 45 header.append( "shipping_method" ); 46 header.append( getDelimiter() ); 47 header.append( "subtotal" ); 48 header.append( getDelimiter() ); 49 header.append( "shipping_cost" ); 50 header.append( getDelimiter() ); 51 header.append( "customerid" ); 52 header.append( getDelimiter() ); 53 header.append( "payment_type" ); 54 header.append( getDelimiter() ); 55 header.append( "card_type" ); 56 header.append( getDelimiter() ); 57 header.append( "card_number" ); 58 header.append( getDelimiter() ); 59 header.append( "expiration_date" ); 60 writer.write( header.toString() + "\n"); 61 for ( Iterator iter = root.elementIterator("order"); iter.hasNext(); ) 62 { 63 Element orderElement = (Element) iter.next(); 64 StringBuffer sb = new StringBuffer (); 65 sb.append( orderElement.attributeValue( "order_number" ) ); 66 sb.append( getDelimiter() ); 67 sb.append( orderElement.attributeValue( "shipping_method" ) ); 68 sb.append( getDelimiter() ); 69 sb.append( orderElement.attributeValue( "subtotal" ) ); 70 sb.append( getDelimiter() ); 71 sb.append( orderElement.attributeValue( "shipping_cost" ) ); 72 sb.append( getDelimiter() ); 73 Element customerElement = orderElement.element( "customer" ); 74 sb.append( customerElement.attributeValue("customerid")); 75 sb.append( getDelimiter() ); 76 77 78 Element paymentElement = orderElement.element( "payment_method" ); 79 80 sb.append( paymentElement.attributeValue("payment_type")); 81 sb.append( getDelimiter() ); 82 sb.append( paymentElement.attributeValue("po_number")); 83 sb.append( getDelimiter() ); 84 sb.append( paymentElement.attributeValue("card_type")); 85 sb.append( getDelimiter() ); 86 sb.append( paymentElement.attributeValue("card_number")); 87 sb.append( getDelimiter() ); 88 sb.append( paymentElement.attributeValue("expiration_date")); 89 sb.append( getDelimiter() ); 90 writer.write( sb.toString() + "\n"); 91 } 92 93 writer.flush(); 94 writer.close(); 95 } 96 97 public void exportItems( File inOrderFile, File inItemsFile ) throws Exception 98 { 99 100 Element root = getRootElement( inOrderFile, "orders" ); 101 FileWriter writer = new FileWriter ( inItemsFile ); 102 StringBuffer header = new StringBuffer (); 103 header.append( "order_number" ); 104 header.append( getDelimiter() ); 105 header.append( "sku" ); 106 header.append( getDelimiter() ); 107 header.append( "product_id" ); 108 header.append( getDelimiter() ); 109 header.append( "quantity" ); 110 header.append( getDelimiter() ); 111 header.append( "price" ); 112 writer.write( header.toString() + "\n"); 113 for ( Iterator iter = root.elementIterator("order"); iter.hasNext(); ) 114 { 115 Element orderElement = (Element) iter.next(); 116 StringBuffer sb = new StringBuffer (); 117 sb.append( orderElement.attributeValue( "order_number" ) ); 118 sb.append( getDelimiter() ); 119 for ( Iterator iterator = orderElement.elementIterator( "item"); iterator.hasNext(); ) 120 { 121 Element itemElement = (Element) iterator.next(); 122 sb.append( itemElement.attributeValue("sku")); 123 sb.append( getDelimiter() ); 124 sb.append( itemElement.attributeValue("product_id")); 125 sb.append( getDelimiter() ); 126 sb.append( itemElement.attributeValue("quantity")); 127 sb.append( getDelimiter() ); 128 sb.append( itemElement.attributeValue("price")); 129 } 130 writer.write( sb.toString() + "\n"); 131 } 132 133 writer.flush(); 134 writer.close(); 135 } 136 137 public void exportCustomers( File inOrderFile, File inCustomerFile ) throws Exception 138 { 139 Element root = getRootElement( inOrderFile, "orders" ); 140 FileWriter writer = new FileWriter ( inCustomerFile ); 141 StringBuffer header = new StringBuffer (); 142 header.append( "customerid" ); 143 header.append( getDelimiter() ); 144 header.append( "first_name" ); 145 header.append( getDelimiter() ); 146 header.append( "last_name" ); 147 header.append( getDelimiter() ); 148 header.append( "phone1" ); 149 header.append( getDelimiter() ); 150 header.append( "email" ); 151 String prefix = "bill_to_"; 152 header.append( prefix + "address1" ); 153 header.append( getDelimiter() ); 154 header.append( prefix + "address2" ); 155 header.append( getDelimiter() ); 156 header.append( prefix + "city" ); 157 header.append( getDelimiter() ); 158 header.append( prefix + "state" ); 159 header.append( getDelimiter() ); 160 header.append( prefix + "zip_code" ); 161 header.append( getDelimiter() ); 162 header.append( prefix + "country" ); 163 header.append( getDelimiter() ); 164 prefix = "ship_to_"; 165 header.append( prefix + "address1" ); 166 header.append( getDelimiter() ); 167 header.append( prefix + "address2" ); 168 header.append( getDelimiter() ); 169 header.append( prefix + "city" ); 170 header.append( getDelimiter() ); 171 header.append( prefix + "state" ); 172 header.append( getDelimiter() ); 173 header.append( prefix + "zip_code" ); 174 header.append( getDelimiter() ); 175 header.append( prefix + "country" ); 176 header.append( getDelimiter() ); 177 writer.write( header.toString() + "\n"); 178 for ( Iterator iter = root.elementIterator("order"); iter.hasNext(); ) 179 { 180 Element orderElement = (Element) iter.next(); 181 StringBuffer sb = new StringBuffer (); 182 Element customerElement = orderElement.element( "customer" ); 183 sb.append( customerElement.attributeValue( "customerid" ) ); 184 sb.append( getDelimiter() ); 185 sb.append( customerElement.attributeValue( "first_name" ) ); 186 sb.append( getDelimiter() ); 187 sb.append( customerElement.attributeValue( "last_name" ) ); 188 sb.append( getDelimiter() ); 189 sb.append( customerElement.attributeValue( "phone1" ) ); 190 sb.append( getDelimiter() ); 191 sb.append( customerElement.attributeValue( "email" ) ); 192 sb.append( getDelimiter() ); 193 List addressElements = customerElement.elements("address"); 194 for ( Iterator iterator = addressElements.iterator(); iterator.hasNext(); ) 195 { 196 Element addressElement = (Element) iterator.next(); 198 prefix = ""; 199 if( addressElement.attributeValue("type").equals("shipping") ) 200 { 201 prefix = ""; 203 } 204 if( addressElement.attributeValue("type").equals("billing") ) 205 { 206 prefix = ""; 208 } 209 sb.append( prefix + addressElement.attributeValue( "address1" ) ); 210 sb.append( getDelimiter() ); 211 sb.append( prefix + addressElement.attributeValue( "address2" ) ); 212 sb.append( getDelimiter() ); 213 sb.append( prefix + addressElement.attributeValue( "city" ) ); 214 sb.append( getDelimiter() ); 215 sb.append( prefix + addressElement.attributeValue( "state" ) ); 216 sb.append( getDelimiter() ); 217 sb.append( prefix + addressElement.attributeValue( "zip_code" ) ); 218 sb.append( getDelimiter() ); 219 sb.append( prefix + addressElement.attributeValue( "country" ) ); 220 sb.append( getDelimiter() ); 221 } 222 writer.write( sb.toString() + "\n"); 223 } 224 writer.flush(); 225 writer.close(); 226 } 227 228 229 public String getDelimiter() 230 { 231 return fieldDelimiter; 232 } 233 public void setDelimiter( String delimiter ) 234 { 235 fieldDelimiter = delimiter; 236 } 237 } 238 | Popular Tags |