1 4 package com.openedit.modules.cart; 5 6 import java.io.File ; 7 import java.util.ArrayList ; 8 import java.util.HashMap ; 9 import java.util.List ; 10 import java.util.Map ; 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 29 public class XmlCustomerArchive implements CustomerArchive 30 { 31 private static final Log log = LogFactory.getLog(CustomerArchive.class); 32 33 protected Map fieldCustomers; 34 protected UserManager fieldUserManager; 35 36 protected CustomerSearch fieldCustomerSearch; 37 protected File fieldCustomerDirectory; 38 39 public Customer getCustomer(String 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 getCustomers() 75 { 76 if ( fieldCustomers == null) 77 { 78 fieldCustomers = new HashMap (); 79 } 80 return fieldCustomers; 81 } 82 83 86 public Customer createNewCustomer( String inUsername, String 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 ex ) 108 { 109 throw new StoreException( ex ); 110 } 111 } 112 113 116 public List findCustomer(String inField1, String inValue1, String inField2, String inValue2) throws StoreException 117 { 118 StringBuffer out = new StringBuffer (); 120 out.append(inField1); 121 out.append(":"); 122 out.append(inValue1); 123 124 out.append(" +"); 125 out.append(inField2); 128 out.append(":"); 129 try 130 { 131 String clean = clean(inValue2); 132 out.append(clean); 133 List customers = findUser( out.toString() ); 134 return customers; 135 } 136 catch ( Exception ex ) 137 { 138 throw new StoreException(ex); 139 } 140 } 141 142 145 public void clearCustomers() throws StoreException 146 { 147 getCustomers().clear(); 148 setCustomerSearch(null); 149 } 150 151 154 public void setCustomersDirectory(File inCustomerDirectory) 155 { 156 fieldCustomerDirectory = inCustomerDirectory; 157 } 158 159 public File getCustomerDirectory() 160 { 161 return fieldCustomerDirectory; 162 } 163 164 165 168 public List findUser(String inQuery) throws UserManagerException 169 { 170 try 172 { 173 Hits hits = getCustomerSearch().search(inQuery); 174 List users = new ArrayList (hits.length()); 175 for (int i = 0; i < hits.length(); i++) 176 { 177 Document doc = hits.doc(i); 178 String 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 ex) 191 { 192 throw new UserManagerException( ex); 193 } 194 } 195 196 197 public CustomerSearch getCustomerSearch() throws Exception 198 { 199 if ( fieldCustomerSearch == null) 200 { 201 fieldCustomerSearch = new CustomerSearch(); 202 fieldCustomerSearch.setSearchDirectory(getCustomerDirectory()); 203 fieldCustomerSearch.setCustomerArchive(this); 204 fieldCustomerSearch.reIndexAll(); } 206 return fieldCustomerSearch; 207 } 208 209 public void setCustomerSearch(CustomerSearch inCustomerSearch) 210 { 211 fieldCustomerSearch = inCustomerSearch; 212 } 213 214 218 public String clean(String inValue2) 219 { 220 if ( inValue2 == null) 221 { 222 return null; 223 } 224 String 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 } 247 } 248 | Popular Tags |