001: /*
002: * Created on Mar 4, 2004
003: *
004: */
005: package org.openedit.store.customer;
006:
007: import java.util.ArrayList;
008: import java.util.Iterator;
009: import java.util.List;
010:
011: import org.openedit.money.Fraction;
012: import org.openedit.store.PaymentMethod;
013:
014: import com.openedit.users.User;
015: import com.openedit.users.filesystem.FileSystemUser;
016:
017: /**
018: * A customer with nice getters and setters on top of the standard user object.
019: *
020: * @author dbrown
021: */
022: public class Customer {
023: public static final String PHONE1 = "Phone1";
024: public static final String FAX = "Fax";
025: public static final String TITLE = "Title";
026: public static final String ALLOW_EMAIL = "AllowEmail";
027: public static final String REFERENCE = "ReferenceNumber";
028: public static final String TAX_RATE = "TaxRate";
029: public static final String TAX_EXEMPT_ID = "TaxExemptId";
030: public static final String COMPANY = "Company";
031: public static final String USERFIELD1 = "userfield1";
032: public static final String USERFIELD2 = "userfield2";
033:
034: protected User fieldUser; // used to store information in Open Edit
035: protected PaymentMethod fieldPaymentMethod;
036: // protected Address fieldBillingAddress;
037: // protected Address fieldShippingAddress;
038: protected String fieldUserName;
039: protected List addressList;
040:
041: /**
042: * Creates a customer that uses the given user to store its information.
043: *
044: * @param inUser
045: */
046: public Customer(User inUser) {
047: fieldUser = inUser;
048:
049: }
050:
051: public Customer() {
052: FileSystemUser user = new FileSystemUser();
053: fieldUser = user;
054: }
055:
056: public User getUser() {
057: return fieldUser;
058: }
059:
060: public String getUserName() {
061: return getUser().getUserName();
062: }
063:
064: public String getFirstName() {
065: return getUser().getFirstName();
066: }
067:
068: public void setFirstName(String inFirstName) {
069: getUser().setFirstName(inFirstName);
070: }
071:
072: public String getLastName() {
073: return getUser().getLastName();
074: }
075:
076: public void setLastName(String inLastName) {
077: getUser().setLastName(inLastName);
078: }
079:
080: public String getEmail() {
081: return getUser().getEmail();
082: }
083:
084: public void setEmail(String inEmail) {
085: getUser().setEmail(inEmail);
086: }
087:
088: public String getPassword() {
089: return getUser().getPassword();
090: }
091:
092: public boolean isAllowEmail() {
093: return getUser().getBoolean(ALLOW_EMAIL);
094: }
095:
096: public void setAllowEmail(boolean inValue) {
097: getUser().safePut(ALLOW_EMAIL, String.valueOf(inValue));
098: }
099:
100: public String getTaxExemptId() {
101: return getUser().getString(TAX_EXEMPT_ID);
102: }
103:
104: public void setTaxExemptId(String inString) {
105: getUser().safePut(TAX_EXEMPT_ID, inString);
106: }
107:
108: public String getName() {
109: return getFirstName() + " " + getLastName();
110: }
111:
112: public PaymentMethod getPaymentMethod() {
113: return fieldPaymentMethod;
114: }
115:
116: public void setPaymentMethod(PaymentMethod inPaymentMethod) {
117: fieldPaymentMethod = inPaymentMethod;
118: }
119:
120: public String getPhone1() {
121: return getUser().getString(PHONE1);
122: }
123:
124: public void setPhone1(String inPhone1) {
125: getUser().safePut(PHONE1, inPhone1);
126: }
127:
128: public String getCompany() {
129: return getUser().getString(COMPANY);
130: }
131:
132: public void setCompany(String inCompany) {
133: getUser().safePut(COMPANY, inCompany);
134: }
135:
136: public Fraction getTaxRate() {
137: String taxRateString = getUser().getString(TAX_RATE);
138: if (taxRateString == null) {
139: taxRateString = Fraction.ZERO.toString();
140: getUser().safePut(TAX_RATE, taxRateString);
141: }
142: return new Fraction(taxRateString);
143: }
144:
145: public void setTaxRate(Fraction inTaxRate) {
146: if (inTaxRate == null) {
147: inTaxRate = Fraction.ZERO;
148: }
149: getUser().safePut(TAX_RATE, inTaxRate.toString());
150: }
151:
152: public String getReferenceNumber() {
153: return getUser().getString(REFERENCE);
154: }
155:
156: public void setReferenceNumber(String inReferenceNumber) {
157: getUser().safePut(REFERENCE, inReferenceNumber);
158: }
159:
160: public String getTitle() {
161: return getUser().getString(TITLE);
162: }
163:
164: public void setTitle(String inTitle) {
165: getUser().safePut(TITLE, inTitle);
166: }
167:
168: // See UserManagerModule.clean
169: public String cleanPhoneNumber() {
170: String phoneNumber = getPhone1();
171: if (phoneNumber != null) {
172: StringBuffer out = new StringBuffer();
173: for (int i = 0; i < phoneNumber.length(); i++) {
174: if (Character.isDigit(phoneNumber.charAt(i))) {
175: out.append(phoneNumber.charAt(i));
176: }
177: }
178: phoneNumber = out.toString();
179: }
180: return phoneNumber;
181: }
182:
183: public Address getBillingAddress() {
184: Address billing = getAddress("billing");
185: if (billing == null) {
186: billing = new Address(getUser());
187: billing.setPrefix("billing"); // only used to store the data
188: }
189: return billing;
190: }
191:
192: public void setBillingAddress(Address inAddress) {
193: inAddress.setPrefix("billing");
194: Address billing = getAddress("billing");
195: if (getAddress("billing") != null) {
196: getAddressList().remove("billing");
197: }
198: getAddressList().add(inAddress);
199:
200: }
201:
202: public Address getAddress(String inString) {
203: for (Iterator iterator = getAddressList().iterator(); iterator
204: .hasNext();) {
205: Address address = (Address) iterator.next();
206: if (inString.equals(address.getPrefix())) {
207: return address;
208: }
209: }
210: return null;
211: }
212:
213: public Address getShippingAddress() {
214: Address shipping = getAddress("shipping");
215: if (shipping == null) {
216: shipping = new Address(getUser());
217: shipping.setPrefix("shipping");
218: addAddress(shipping);
219: }
220: return shipping;
221: }
222:
223: public void setShippingAddress(Address inShippingAddress) {
224: inShippingAddress.setPrefix("shipping");
225: Address shipping = getAddress("shipping");
226: if (shipping != null) {
227: getAddressList().remove("shipping");
228: }
229: getAddressList().add(inShippingAddress);
230: }
231:
232: public String getFax() {
233: return getUser().getString(FAX);
234: }
235:
236: public void setFax(String inFax) {
237: getUser().safePut(FAX, inFax);
238: }
239:
240: public String getUserField1() {
241: return getUser().getString(USERFIELD1);
242: }
243:
244: public void setUserField1(String inUserField1) {
245: getUser().safePut(USERFIELD1, inUserField1);
246: }
247:
248: public String getUserField2() {
249: return getUser().getString(USERFIELD2);
250: }
251:
252: public void setUserField2(String inUserField2) {
253: getUser().safePut(USERFIELD2, inUserField2);
254: }
255:
256: public void setUserName(String inUserName) {
257: getUser().setUserName(inUserName);
258: }
259:
260: public void setPassword(String inPassword) {
261: getUser().setPassword(inPassword);
262: }
263:
264: public List getAddressList() {
265: if (addressList == null) {
266: addressList = new ArrayList();
267: getShippingAddress();
268:
269: }
270:
271: return addressList;
272: }
273:
274: public void setAddressList(List addressList) {
275: this .addressList = addressList;
276: }
277:
278: public void addAddress(Address inAddress) {
279: removeAddress(inAddress);
280: getAddressList().add(inAddress);
281: }
282:
283: public void removeAddress(Address inAddress) {
284: Address toRemove = getAddress(inAddress.getPrefix());
285: if (toRemove != null) {
286: getAddressList().remove(toRemove);
287: }
288:
289: }
290:
291: }
|