KickJava   Java API By Example, From Geeks To Geeks.

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


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

4 package com.openedit.store.retailproconvert;
5
6 import java.io.File JavaDoc;
7 import java.io.FileInputStream JavaDoc;
8 import java.io.FilenameFilter JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.util.Arrays JavaDoc;
11 import java.util.Collections JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
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 /**
30  * @author cburkey
31  *
32  */

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 JavaDoc inLog) throws Exception JavaDoc
38     {
39         //read in customer list and output user objects to save
40
//loop over all users and see if they are already in our DB
41
//update or all all users with a password the same as the username, that is the WEB ID
42
/*
43          * <Customer fldCustSID="PAAAAAMMEELANDBH" fldTitle="Miss." fldFName="Madison" fldLName="Brown" fldAddr1="7241 Oak Grove Rd." fldAddr3="GEORGETOWN, OH" fldZIP="45121" fldPhone1="937-378-4315" fldCustID="3452"
44          * web_cust_sid="PAAAAAMMEELANDBH" email="kim_boothby_brown@hotmail.com"/>
45          *
46          */

47         //read in the retail pro mapping
48
//read in the old format
49

50         List JavaDoc all = findCustomerList(inStore);
51         for (Iterator JavaDoc fiter = all.iterator(); fiter.hasNext();)
52         {
53             File JavaDoc file = (File JavaDoc) fiter.next();
54             log.info("Running " + file);
55             Element customers = getRootElement( file );
56     
57             for (Iterator JavaDoc iter = customers.elementIterator("Customer"); iter.hasNext();)
58             {
59                 Element onecustomer = (Element) iter.next();
60                 Customer newCustomer = new Customer();
61                 String JavaDoc 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 JavaDoc password = newCustomer.cleanPhoneNumber();
80                 if ( password == null || password.length() == 0 )
81                 {
82                     password = "0000000000";
83                 }
84                 newCustomer.setPassword(password);
85                 String JavaDoc 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 JavaDoc inFile ) throws StoreException
103     {
104         if (inFile.exists())
105         {
106             SAXReader reader = new SAXReader();
107             Document document;
108             try
109             {
110                 FileInputStream JavaDoc stream = new FileInputStream JavaDoc( inFile );
111                 document = reader.read( stream );
112                 return document.getRootElement();
113             }
114             catch( Exception JavaDoc e )
115             {
116                 throw new StoreException( e );
117             }
118         }
119         else
120         {
121             return DocumentHelper.createElement( "Customers" );
122         }
123     }
124     
125     /**
126      * @return
127      */

128     List JavaDoc findCustomerList(Store inStore)
129     {
130         //search
131
File JavaDoc listinputs = new File JavaDoc( inStore.getStoreDirectory(),"tmp");
132         File JavaDoc[] all = listinputs.listFiles(new FilenameFilter JavaDoc()
133             {
134             public boolean accept(File JavaDoc dir, String JavaDoc name) {
135                 if (name.startsWith("ECCustomer_") && name.endsWith(".xml"))
136                 {
137                     return true;
138                 }
139                 return false;
140             };
141         });
142         List JavaDoc sorted = new ArrayList JavaDoc(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