Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/RuntimeCollective/webapps/bean/Address.java


1   /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/bean/Address.java,v 1.7 2003/09/30 15:13:09 joe Exp $
2    * $Revision: 1.7 $
3    * $Date: 2003/09/30 15:13:09 $
4    *
5    * ====================================================================
6    *
7    * Josephine : http://www.runtime-collective.com/josephine/index.html
8    *
9    * Copyright (C) 2003 Runtime Collective
10   * 
11   * This product includes software developed by the
12   * Apache Software Foundation (http://www.apache.org/).
13   *
14   * This library is free software; you can redistribute it and/or
15   * modify it under the terms of the GNU Lesser General Public
16   * License as published by the Free Software Foundation; either
17   * version 2.1 of the License, or (at your option) any later version.
18   *
19   * This library is distributed in the hope that it will be useful,
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22   * Lesser General Public License for more details.
23   *
24   * You should have received a copy of the GNU Lesser General Public
25   * License along with this library; if not, write to the Free Software
26   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27   *
28   */
29  
30  package com.RuntimeCollective.webapps.bean;
31  
32  import com.RuntimeCollective.webapps.RuntimeDataSource;
33  import com.RuntimeCollective.webapps.EntityBeanStore;
34  import com.RuntimeCollective.webapps.RuntimeParameters;
35  import java.sql.SQLException;
36  import java.util.ArrayList;
37  import java.util.List;
38  
39  /**
40   * An address.
41   * Each user may have many different addresses associated with them -- billing address, home address, etc -- each of which may have a different name and nationality. These attributes typically default to the name and nationality of the user, but may be changed.
42   * @version $Id: Address.java,v 1.7 2003/09/30 15:13:09 joe Exp $
43   */
44  public class Address implements EntityBean {
45  
46  
47      // == Entity Bean methods ===========================================
48  
49      public static String DATABASE_TABLE = "common_address";
50  
51      /** Default constructor generates a new blank bean with a new unique ID.
52      * @exception SQLException if unable to connect to the database.
53      */
54      public Address() throws SQLException {
55          this.id = RuntimeDataSource.nextId();
56      }
57  
58      /** Generate a bean from the database for the given primary key.
59      * @param id The primary key of the bean to find.
60      * @exception SQLException if no bean is found for that id.
61      */
62      public Address( int id ) throws SQLException { 
63          Object[] result = RuntimeDataSource.queryRow("SELECT id,first,last,nationality,address1,address2,address3,city,state,country,postcode,phone_country,phone_area,phone_number FROM common_address WHERE id="+id);
64          if ( result != null ) {
65              setId( Integer.parseInt( result[0].toString() ) );
66              setFirstName( (String) result[1] );
67              setLastName( (String) result[2] );
68              // replace null countries with ""
69              if (result[3] == null)
70                  setNationality("");
71              else
72                  setNationality( (String) result[3] );
73              setAddress1( (String) result[4] );
74              setAddress2( (String) result[5] );
75              setAddress3( (String) result[6] );
76              setCity( (String) result[7] );
77              setState( (String) result[8] );
78              // replace null countries with ""
79              if (result[9] == null)
80                  setCountry("");
81              else
82                  setCountry( (String) result[9] );
83              setPostcode( (String) result[10] );
84              setPhoneCountry( (String) result[11] );
85              setPhoneArea( (String) result[12] );
86              setPhoneNumber((String) result[13] );
87          }
88      }
89  
90      /** Save this bean to the database.
91       * @exception SQLException if unable to connect to the database.
92       */
93      public void save() throws SQLException {
94          // "" would break the ref constraints
95          String nationalitySaved = nationality;
96          if ("".equals(nationalitySaved))
97              nationalitySaved = null;
98          String countrySaved = country;
99          if ("".equals(countrySaved))
100             countrySaved = null;
101         
102         RuntimeDataSource.save( id, "common_address",
103                                 new String[] {"first","last","nationality","address1","address2","address3","city","state","country","postcode","phone_country","phone_area","phone_number"},
104                                 new String[] {firstName,lastName,nationalitySaved,address1,address2,address3,city,state,countrySaved,postcode,phoneCountry,phoneArea,phoneNumber } );
105     }
106     
107     /** Delete this bean from the database.
108      * @exception SQLException if unable to connect to the database.
109      */
110     public void delete() throws SQLException {
111         RuntimeDataSource.update( "delete from common_address where id="+id );
112     }
113 
114 
115     // == Properties ===================================================
116 
117     /** ID */
118     protected int id;
119     /** Get the address's ID. */
120     public int getId() { return (this.id); }
121     /** Set the address's ID. */
122     public void setId(int id) { this.id = id; }
123 
124     /** Email. */
125     protected String email = null;
126     /** Get the user's email. */
127     public String getEmail() { return (this.email); }
128     /** Set the user's email. */
129     public void setEmail(String email) { this.email = email; }
130 
131     /** First name */ 
132     protected String firstName = ""; 
133     /** Get first name */ 
134     public String getFirstName() { return this.firstName; } 
135     /** Set first name */ 
136     public void setFirstName(String firstName) { this.firstName = firstName; } 
137 
138     /** Last name */ 
139     protected String lastName = ""; 
140     /** Get last name */ 
141     public String getLastName() { return this.lastName; } 
142     /** Set last name */ 
143     public void setLastName(String lastName) { this.lastName = lastName; } 
144 
145     /** Address line 1. */
146     protected String address1 = null;
147     /** Get the user's address line 1. */
148     public String getAddress1() { return (this.address1); }
149     /** Set the user's address line 1. */
150     public void setAddress1(String address1) { this.address1 = address1; }
151 
152     /** Address line 2. */
153     protected String address2 = null;
154     /** Get the user's address line 2. */
155     public String getAddress2() { return (this.address2); }
156     /** Set the user's address line 2. */
157     public void setAddress2(String address2) { this.address2 = address2; }
158 
159     /** Address line 3. */
160     protected String address3 = null;
161     /** Get the user's address line 3. */
162     public String getAddress3() { return (this.address3); }
163     /** Set the user's address line 3. */
164     public void setAddress3(String address3) { this.address3 = address3; }
165 
166     /** City. */
167     protected String city = null;
168     /** Get the user's city. */
169     public String getCity() { return (this.city); }
170     /** Set the user's city. */
171     public void setCity(String city) { this.city = city; }
172 
173     /** State. */
174     protected String state = null;
175     /** Get the user's state. */
176     public String getState() { return (this.state); }
177     /** Set the user's state. */
178     public void setState(String state) { this.state = state; }
179 
180     /** Post code. */
181     protected String postcode = null;
182     /** Get the user's post code. */
183     public String getPostcode() { return (this.postcode); }
184     /** Set the user's post code. */
185     public void setPostcode(String postcode) { this.postcode = postcode; }
186 
187     /** The country code of the user's phone number. */ 
188     protected String phoneCountry; 
189     /** Get the country code of the user's phone number. */ 
190     public String getPhoneCountry() { return this.phoneCountry; } 
191     /** Set the country code of the user's phone number. */ 
192     public void setPhoneCountry(String phoneCountry) { this.phoneCountry = phoneCountry; } 
193 
194     /** The area code of the user's phone number. */ 
195     protected String phoneArea; 
196     /** Get the area code of the user's phone number. */ 
197     public String getPhoneArea() { return this.phoneArea; } 
198     /** Set the area code of the user's phone number. */ 
199     public void setPhoneArea(String phoneArea) { this.phoneArea = phoneArea; } 
200 
201     /** The user's local phone number. */ 
202     protected String phoneNumber; 
203     /** Get the user's local phone number. */ 
204     public String getPhoneNumber() { return this.phoneNumber; } 
205     /** Set the user's local phone number. */ 
206     public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } 
207 
208     /** Two letter ISO country code. */
209     protected String country = null;
210     /** Get the user's country code for country of residence. */
211     public String getCountry() { return (this.country); }
212     /** Set the user's country of residence code. This will also update the user's country name. */
213     public void setCountry(String country) { 
214   this.country = country;
215     }
216 
217     /** Two letter ISO nationality code. */
218     protected String nationality = null;
219     /** Get the user's nationality code. */
220     public String getNationality() { return (this.nationality); }
221     /** Set the user's nationality code. This will also update the user's nationality name. */
222     public void setNationality(String nationality) { 
223   this.nationality = nationality;
224     }
225 
226 
227     /**
228      * Get all the addresses of a given User.
229      * This is a dirty method which supposed that the firstname property
230      * of addresses stores the user's id.
231      * It is being used by the ecommerce module, to save time.
232      * Ecommerce also assumes that the lastname is used for the address' "alias".
233      * FIXME: this method shouldn't be use, set up a proper user-address
234      * one-to-many relationship.
235      */
236     public static List dirtyGetUserAddresses(User user) {
237 
238         if (user == null) {
239             return new ArrayList();
240         }
241 
242         try {
243             int[] ids = RuntimeDataSource.queryInts(SELECT_ID_FROM+DATABASE_TABLE+WHERE_FIRSTNAME+user.getId()+ORDER_BY_ID);
244             
245             List addresses = new ArrayList(ids.length);
246             for (int i=0; i<ids.length; i++ ) {
247                 addresses.add(RuntimeParameters.getStore().get(Address.class.getName(), ids[i]));
248             }
249             return addresses;
250 
251         } catch (SQLException e) {
252             RuntimeParameters.logError("Address", "Could not dirtyGetUserAddresses(), got an SQLException.", e);
253             e.printStackTrace();
254             return new ArrayList();
255         }
256     }
257 
258     private static final String SELECT_ID_FROM = "select t.id from ";
259     private static final String WHERE_FIRSTNAME = " t where t.first = ";
260     private static final String ORDER_BY_ID = " order by t.id";
261 }