1 4 package com.openedit.store.retailproconvert; 5 6 import java.io.File ; 7 import java.io.FileInputStream ; 8 import java.io.FilenameFilter ; 9 import java.util.ArrayList ; 10 import java.util.Arrays ; 11 import java.util.Collections ; 12 import java.util.Iterator ; 13 import java.util.List ; 14 15 import org.apache.commons.logging.Log; 16 import org.apache.commons.logging.LogFactory; 17 import org.dom4j.Document; 18 import org.dom4j.DocumentHelper; 19 import org.dom4j.Element; 20 import org.dom4j.io.SAXReader; 21 22 import com.openedit.store.Converter; 23 import com.openedit.store.CustomerArchive; 24 import com.openedit.store.Store; 25 import com.openedit.store.StoreException; 26 import com.openedit.store.customer.Address; 27 import com.openedit.store.customer.Customer; 28 29 33 public class CustomerListConverter extends Converter 34 { 35 protected CustomerArchive fieldCustomerArchive; 36 private static final Log log = LogFactory.getLog(CustomerListConverter.class); 37 public boolean convert(Store inStore, List inLog) throws Exception 38 { 39 47 50 List all = findCustomerList(inStore); 51 for (Iterator fiter = all.iterator(); fiter.hasNext();) 52 { 53 File file = (File ) fiter.next(); 54 log.info("Running " + file); 55 Element customers = getRootElement( file ); 56 57 for (Iterator iter = customers.elementIterator("Customer"); iter.hasNext();) 58 { 59 Element onecustomer = (Element) iter.next(); 60 Customer newCustomer = new Customer(); 61 String id = onecustomer.attributeValue("fldCustID"); 62 if ( id == null || id.trim().length() == 0) 63 { 64 throw new StoreException("Missing username or club number"); 65 } 66 newCustomer.setUserName(id); 67 newCustomer.setFirstName(onecustomer.attributeValue("fldFName")); 68 newCustomer.setLastName(onecustomer.attributeValue("fldLName")); 69 newCustomer.setReferenceNumber(onecustomer.attributeValue("fldCustSID")); 70 newCustomer.setTitle(onecustomer.attributeValue("fldTitle")); 71 Address address = newCustomer.getShippingAddress(); 72 address.setAddress1(onecustomer.attributeValue("fldAddr1")); 73 address.setAddress2(onecustomer.attributeValue("fldAddr3")); 74 address.setCityState(onecustomer.attributeValue("fldAddr2")); 75 address.setZipCode(onecustomer.attributeValue("fldZip")); 76 newCustomer.setPhone1(onecustomer.attributeValue("fldPhone1")); 77 newCustomer.setEmail(onecustomer.attributeValue("email")); 78 79 String password = newCustomer.cleanPhoneNumber(); 80 if ( password == null || password.length() == 0 ) 81 { 82 password = "0000000000"; 83 } 84 newCustomer.setPassword(password); 85 String email = newCustomer.getEmail(); 86 if ( email != null && "none@domain.com".equalsIgnoreCase(email)) 87 { 88 newCustomer.setEmail(null); 89 } 90 91 getCustomerArchive().saveCustomer(newCustomer); 92 } 93 } 94 if ( all.size() > 0) 95 { 96 getCustomerArchive().clearCustomers(); 97 } 98 return true; 99 100 } 101 102 private Element getRootElement( File inFile ) throws StoreException 103 { 104 if (inFile.exists()) 105 { 106 SAXReader reader = new SAXReader(); 107 Document document; 108 try 109 { 110 FileInputStream stream = new FileInputStream ( inFile ); 111 document = reader.read( stream ); 112 return document.getRootElement(); 113 } 114 catch( Exception e ) 115 { 116 throw new StoreException( e ); 117 } 118 } 119 else 120 { 121 return DocumentHelper.createElement( "Customers" ); 122 } 123 } 124 125 128 List findCustomerList(Store inStore) 129 { 130 File listinputs = new File ( inStore.getStoreDirectory(),"tmp"); 132 File [] all = listinputs.listFiles(new FilenameFilter () 133 { 134 public boolean accept(File dir, String name) { 135 if (name.startsWith("ECCustomer_") && name.endsWith(".xml")) 136 { 137 return true; 138 } 139 return false; 140 }; 141 }); 142 List sorted = new ArrayList (Arrays.asList(all)); 143 Collections.sort(sorted); 144 return sorted; 145 } 146 147 public CustomerArchive getCustomerArchive() 148 { 149 return fieldCustomerArchive; 150 } 151 public void setCustomerArchive(CustomerArchive inCustomerArchive) 152 { 153 fieldCustomerArchive = inCustomerArchive; 154 } 155 } 156 | Popular Tags |