KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > cart > XmlCustomerArchive


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

4 package com.openedit.modules.cart;
5
6 import java.io.File JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.Map JavaDoc;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.apache.lucene.document.Document;
15 import org.apache.lucene.search.Hits;
16
17 import com.openedit.store.CustomerArchive;
18 import com.openedit.store.StoreException;
19 import com.openedit.store.customer.Customer;
20 import com.openedit.store.customer.CustomerSearch;
21 import com.openedit.users.User;
22 import com.openedit.users.UserManager;
23 import com.openedit.users.UserManagerException;
24
25 /**
26  * @author cburkey
27  *
28  */

29 public class XmlCustomerArchive implements CustomerArchive
30 {
31     private static final Log log = LogFactory.getLog(CustomerArchive.class);
32     
33     protected Map JavaDoc fieldCustomers;
34     protected UserManager fieldUserManager;
35     
36     protected CustomerSearch fieldCustomerSearch;
37     protected File JavaDoc fieldCustomerDirectory;
38     
39     public Customer getCustomer(String JavaDoc inId) throws StoreException
40     {
41         Customer cust = (Customer) getCustomers().get( inId );
42         if ( cust == null )
43         {
44             try
45             {
46                 User user = getUserManager().getUser( inId );
47                 if ( user != null )
48                 {
49                     cust = new Customer( user );
50                     getCustomers().put( inId, cust );
51                 }
52             }
53             catch ( UserManagerException ex )
54             {
55                 throw new StoreException( ex );
56             }
57         }
58         return cust;
59     }
60     
61     public void saveCustomer(Customer inCustomer) throws StoreException
62     {
63         try
64         {
65             getUserManager().saveUser(inCustomer.getUser());
66         }
67         catch (UserManagerException ex)
68         {
69             log.error( ex );
70             throw new StoreException( ex );
71         }
72     }
73     
74     public Map JavaDoc getCustomers()
75     {
76         if ( fieldCustomers == null)
77         {
78             fieldCustomers = new HashMap JavaDoc();
79         }
80         return fieldCustomers;
81     }
82     
83     /* (non-javadoc)
84      * @see com.openedit.store.CustomerArchive#createNewCustomer()
85      */

86     public Customer createNewCustomer( String JavaDoc inUsername, String JavaDoc inPassword ) throws StoreException
87     {
88         try
89         {
90             User user = null;
91             if ( inUsername != null )
92             {
93                 user = getUserManager().getUser(inUsername);
94             }
95             if ( inPassword == null )
96             {
97                 inPassword = inUsername;
98             }
99             if ( user == null)
100             {
101                 user = getUserManager().createUser( inUsername, inPassword );
102             }
103             Customer newCustomer = new Customer( user );
104             getCustomers().put(newCustomer.getUserName(),newCustomer);
105             return newCustomer;
106         }
107         catch ( Exception JavaDoc ex )
108         {
109             throw new StoreException( ex );
110         }
111     }
112     
113     /* (non-javadoc)
114      * @see com.openedit.store.CustomerArchive#findCustomer(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
115      */

116     public List JavaDoc findCustomer(String JavaDoc inField1, String JavaDoc inValue1, String JavaDoc inField2, String JavaDoc inValue2) throws StoreException
117     {
118         //the search will handle the case problem
119
StringBuffer JavaDoc out = new StringBuffer JavaDoc();
120         out.append(inField1);
121         out.append(":");
122         out.append(inValue1);
123
124         out.append(" +");
125 //
126
// //remove any - or () from phone number
127
out.append(inField2);
128         out.append(":");
129         try
130         {
131             String JavaDoc clean = clean(inValue2);
132             out.append(clean);
133             List JavaDoc customers = findUser( out.toString() );
134             return customers;
135         }
136         catch ( Exception JavaDoc ex )
137         {
138             throw new StoreException(ex);
139         }
140     }
141     
142     /* (non-javadoc)
143      * @see com.openedit.store.CustomerArchive#clearCustomers()
144      */

145     public void clearCustomers() throws StoreException
146     {
147         getCustomers().clear();
148         setCustomerSearch(null);
149     }
150
151     /* (non-javadoc)
152      * @see com.openedit.store.CustomerArchive#setCustomersDirectory(java.io.File)
153      */

154     public void setCustomersDirectory(File JavaDoc inCustomerDirectory)
155     {
156         fieldCustomerDirectory = inCustomerDirectory;
157     }
158     
159     public File JavaDoc getCustomerDirectory()
160     {
161         return fieldCustomerDirectory;
162     }
163     
164     
165     /* (non-javadoc)
166      * @see com.openedit.users.filesystem.FileSystemUserManager#findUser(java.lang.String)
167      */

168     public List JavaDoc findUser(String JavaDoc inQuery) throws UserManagerException
169     {
170         //use Lucene
171
try
172         {
173             Hits hits = getCustomerSearch().search(inQuery);
174             List JavaDoc users = new ArrayList JavaDoc(hits.length());
175             for (int i = 0; i < hits.length(); i++)
176             {
177                 Document doc = hits.doc(i);
178                 String JavaDoc username = doc.get(User.USERNAME_PROPERTY);
179                 User user = getUserManager().getUser(username);
180                 if ( user == null )
181                 {
182                     log.error( "Index is out of date, user " + username + " has since been deleted" );
183                 }
184                 else
185                 {
186                     users.add(user);
187                 }
188             }
189             return users;
190         } catch ( Exception JavaDoc ex)
191         {
192             throw new UserManagerException( ex);
193         }
194     }
195
196     
197     public CustomerSearch getCustomerSearch() throws Exception JavaDoc
198     {
199         if ( fieldCustomerSearch == null)
200         {
201             fieldCustomerSearch = new CustomerSearch();
202             fieldCustomerSearch.setSearchDirectory(getCustomerDirectory());
203             fieldCustomerSearch.setCustomerArchive(this);
204             fieldCustomerSearch.reIndexAll(); //cheap way to keep index updated, move to a time event or maintain index
205
}
206         return fieldCustomerSearch;
207     }
208     
209     public void setCustomerSearch(CustomerSearch inCustomerSearch)
210     {
211         fieldCustomerSearch = inCustomerSearch;
212     }
213     
214     /**
215      * @param inValue2
216      * @return
217      */

218     public String JavaDoc clean(String JavaDoc inValue2)
219     {
220         if ( inValue2 == null)
221         {
222             return null;
223         }
224         String JavaDoc clean = inValue2.replaceAll("-","");
225         clean = clean.replaceAll("\\(","");
226         clean = clean.replaceAll("\\)","");
227         clean = clean.replaceAll("\\ ","");
228
229         return clean;
230     }
231     
232     public UserManager getUserManager()
233     {
234         return fieldUserManager;
235     }
236     
237     public void setUserManager( UserManager inUserManager )
238     {
239         fieldUserManager = inUserManager;
240     }
241
242     public void saveAndExportCustomer(Customer inC) throws StoreException
243     {
244         saveCustomer(inC);
245         //no need to export anything else
246
}
247 }
248
Popular Tags