KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > hipergate > Address


1 /*
2   Copyright (C) 2003 Know Gate S.L. All rights reserved.
3                       C/Oña, 107 1º2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.hipergate;
34
35 import java.util.HashMap JavaDoc;
36 import java.util.Iterator JavaDoc;
37
38 import java.sql.Connection JavaDoc;
39 import java.sql.SQLException JavaDoc;
40 import java.sql.Statement JavaDoc;
41 import java.sql.ResultSet JavaDoc;
42 import java.sql.PreparedStatement JavaDoc;
43 import java.sql.Types JavaDoc;
44
45 import com.knowgate.debug.DebugFile;
46 import com.knowgate.jdc.JDCConnection;
47 import com.knowgate.dataobjs.DB;
48 import com.knowgate.dataobjs.DBBind;
49 import com.knowgate.dataobjs.DBPersist;
50 import com.knowgate.misc.Gadgets;
51
52 /**
53  * <p>Address</p>
54  * A generic postal address object for being associated to any other objects
55  * that may require address information.
56  * @author Sergio Montoro Ten
57  * @version 3.0
58  */

59 public class Address extends DBPersist {
60
61   /**
62    * Create empty Address
63    */

64   public Address() {
65     super(DB.k_addresses, "Address");
66   }
67
68   /**
69    * Load Address from database
70    * @param oConn Database Connection
71    * @param guAddr GUID of address to be loaded
72    */

73
74   public Address(JDCConnection oConn, String JavaDoc guAddr) throws SQLException JavaDoc {
75     super(DB.k_addresses, "Address");
76
77     Object JavaDoc aAddr[] = { guAddr };
78
79     load(oConn, aAddr);
80   }
81
82   // ---------------------------------------------------------
83

84   /**
85    * <p>Load values set at table k_addresses</p>
86    * This method trims the value of id_country column
87    * @param oConn Database Connection
88    * @param PKVals An Array with a single String containing the GUID of the Address to be loaded
89    * @return <b>true</b> if Address was found, <b>false</b> otherwise.
90    * @throws SQLException
91    * @since 3.0
92    */

93
94   public boolean load(JDCConnection oConn, Object JavaDoc[] PKVals) throws SQLException JavaDoc {
95     boolean bRetVal = super.load(oConn, PKVals);
96     if (bRetVal) {
97       if (!isNull(DB.id_country)) {
98         AllVals.remove(DB.id_country);
99         AllVals.put(DB.id_country, getString(DB.id_country).trim());
100       }
101     }
102     return bRetVal;
103   }
104
105   // ---------------------------------------------------------
106

107   /**
108    * <p>Load values set at table k_addresses</p>
109    * This method trims the value of id_country column
110    * @param oConn JDCConnection
111    * @param sGuAddress String GUID of the Address to be loaded
112    * @return <b>true</b> if Address was found, <b>false</b> otherwise.
113    * @throws SQLException
114    * @since 3.0
115    */

116   public boolean load(JDCConnection oConn, String JavaDoc sGuAddress) throws SQLException JavaDoc {
117     return this.load(oConn, new Object JavaDoc[]{sGuAddress});
118   }
119
120   // ---------------------------------------------------------
121

122   /**
123    * <p>Delete Address</p>
124    * Registers at k_welcome_packs, k_x_company_addr and k_x_contact_addr are deleted on cascade
125    * @param oConn Database Connection
126    * @return <b>true<b> if Address existed at k_addresses table.
127    * @throws SQLException
128    */

129   public boolean delete(JDCConnection oConn) throws SQLException JavaDoc {
130     Statement JavaDoc oDlte = oConn.createStatement();
131     int iAffected;
132
133     if (DebugFile.trace) {
134       DebugFile.writeln("Begin Address.delete(" + getStringNull(DB.gu_address, "null") + ")");
135       DebugFile.incIdent();
136     }
137
138     if (DBBind.exists(oConn, DB.k_welcome_packs, "U")) {
139       if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(DELETE FROM " + DB.k_welcome_packs_changelog + " WHERE " + DB.gu_pack + " IN (SELECT " + DB.gu_pack + " FROM " + DB.k_welcome_packs + " WHERE " + DB.gu_address + "='" + getString(DB.gu_address) + "'))");
140
141       oDlte.executeUpdate("DELETE FROM " + DB.k_welcome_packs_changelog + " WHERE " + DB.gu_pack + " IN (SELECT " + DB.gu_pack + " FROM " + DB.k_welcome_packs + " WHERE " + DB.gu_address + "='" + getString(DB.gu_address) + "')");
142
143       if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(DELETE FROM " + DB.k_welcome_packs + " WHERE " + DB.gu_address + "='" + getString(DB.gu_address) + "')");
144
145       oDlte.executeUpdate("DELETE FROM " + DB.k_welcome_packs + " WHERE " + DB.gu_address + "='" + getString(DB.gu_address) + "'");
146     }
147
148     if (DBBind.exists(oConn, DB.k_x_company_addr, "U")) {
149       if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(DELETE FROM " + DB.k_x_company_addr + " WHERE " + DB.gu_address + "='" + getString(DB.gu_address) + "')");
150
151       oDlte.executeUpdate("DELETE FROM " + DB.k_x_company_addr + " WHERE " + DB.gu_address + "='" + getString(DB.gu_address) + "'");
152     }
153
154     if (DBBind.exists(oConn, DB.k_x_contact_addr, "U")) {
155       if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(DELETE FROM " + DB.k_x_contact_addr + " WHERE " + DB.gu_address + "='" + getString(DB.gu_address) + "')");
156
157       oDlte.executeUpdate("DELETE FROM " + DB.k_x_contact_addr + " WHERE " + DB.gu_address + "='" + getString(DB.gu_address) + "'");
158     }
159
160     if (DebugFile.trace) DebugFile.writeln("Statement.executeUpdate(DELETE FROM " + DB.k_addresses + " WHERE " + DB.gu_address + "='" + getString(DB.gu_address) + "')");
161
162     iAffected = oDlte.executeUpdate("DELETE FROM " + DB.k_addresses + " WHERE " + DB.gu_address + "='" + getString(DB.gu_address) + "'");
163
164     if (DebugFile.trace) {
165       DebugFile.decIdent();
166       DebugFile.writeln("End Address.delete() : " + String.valueOf((iAffected>0 ? true : false)));
167     }
168
169     return (iAffected>0 ? true : false);
170   } // delete
171

172   // ---------------------------------------------------------
173

174   /**
175    * <p>Store Address</p>
176    * If gu_address is null a new GUID is automatically assigned.<br>
177    * dt_modified field is set to current date.<br>
178    * For generating a new address index call nextLocalIndex() and set property ix_address.<br>
179    * For example:<br>
180    * this.put (DB.ix_address, Address.nextLocalIndex(Connection, "k_x_company_addr", "gu_company"))
181    * @param oConn Database Connection
182    * @return boolean <b>true</b> if Address was stored for the first time,
183    * <b>false</b> if register already existed.
184    * @throws SQLException
185    */

186   public boolean store(JDCConnection oConn) throws SQLException JavaDoc {
187     java.sql.Timestamp JavaDoc dtNow = new java.sql.Timestamp JavaDoc(DBBind.getTime());
188
189     if (null==get(DB.gu_address)) put(DB.gu_address, Gadgets.generateUUID());
190
191     replace(DB.dt_modified, dtNow);
192
193     return super.store (oConn);
194   } // store()
195

196   // **********************************************************
197
// Static Methods
198

199   /**
200    * <p>Get next free address index for a given object.</p>
201    * Address indexes are integers assigned on a per object basis.<br>
202    * @param oConn Database Connection
203    * @param sLinkTable Table used for linking addresses to instances of objects
204    * of a given class (for example "k_x_company_addr").
205    * @param sLinkField Foreign object column name at link table (for example "gu_company")
206    * @param sLinkValue Value for foreign object GUID
207    * @return SELECT COUNT(*)+1 FROM sLinkTable WHERE sLinkField = sLinkValue
208    * @throws SQLException
209    */

210   public static int nextLocalIndex(Connection JavaDoc oConn, String JavaDoc sLinkTable, String JavaDoc sLinkField, String JavaDoc sLinkValue) throws SQLException JavaDoc {
211     // Obtiene el siguiente valor del índice correlativo de direcciones
212
// asociadas a una determinada instancia de otro objeto.
213
if (DebugFile.trace) {
214       DebugFile.writeln("Begin Address.nextLocalIndex([Connection],"+sLinkTable+","+sLinkField+","+sLinkValue+")");
215       DebugFile.incIdent();
216     }
217     Statement JavaDoc oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
218     ResultSet JavaDoc oRSet = oStmt.executeQuery("SELECT COUNT(*) FROM " + sLinkTable + " WHERE " + sLinkField + "='" + sLinkValue + "'");
219     oRSet.next();
220     Object JavaDoc oCount = oRSet.getObject(1);
221     oRSet.close();
222     oStmt.close();
223     int iRetVal = Integer.parseInt(oCount.toString())+1;
224     if (DebugFile.trace) {
225       DebugFile.decIdent();
226       DebugFile.writeln("End Address.nextLocalIndex() : "+String.valueOf(iRetVal));
227     }
228     return iRetVal;
229   } // nextLocalIndex
230

231   /**
232    * <p>Get Address Unique Id. from its e-mail.</p>
233    * @param oConn Database Connection
234    * @param sEMail Address e-mail (tx_email from k_addresses table)
235    * @param sWorkArea WorkArea filter. If <b>null</b> all WorkAreas are seached
236    * @return User Unique Id. or <b>null</b> if no Address was found with such e-mail.
237    * @throws SQLException
238    * @since 2.2
239    */

240   public static String JavaDoc getIdFromEmail(JDCConnection oConn, String JavaDoc sEMail, String JavaDoc sWorkArea) throws SQLException JavaDoc {
241     String JavaDoc sRetVal;
242     PreparedStatement JavaDoc oStmt;
243     ResultSet JavaDoc oRSet;
244
245     if (DebugFile.trace) {
246       if (null==sWorkArea)
247         DebugFile.writeln("Connection.prepareStatement(SELECT " + DB.gu_address + " FROM " + DB.k_addresses + " WHERE " + DB.tx_email + "='" + sEMail + "')");
248       else
249         DebugFile.writeln("Connection.prepareStatement(SELECT " + DB.gu_address + " FROM " + DB.k_addresses + " WHERE " + DB.tx_email + "='" + sEMail + "' AND " + DB.gu_workarea+ "='" + sWorkArea + "')");
250     }
251
252     if (null==sWorkArea) {
253       oStmt = oConn.prepareStatement("SELECT " + DB.gu_address + " FROM " + DB.k_addresses + " WHERE " + DB.tx_email + "=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
254       oStmt.setString(1, sEMail);
255     } else {
256       oStmt = oConn.prepareStatement("SELECT " + DB.gu_address + " FROM " + DB.k_addresses + " WHERE " + DB.tx_email + "=? AND " + DB.gu_workarea + "=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
257       oStmt.setString(1, sEMail);
258       oStmt.setString(2, sWorkArea);
259     }
260
261     oRSet = oStmt.executeQuery();
262     if (oRSet.next())
263       sRetVal = oRSet.getString(1);
264     else
265       sRetVal = null;
266     oRSet.close();
267     oStmt.close();
268
269     return sRetVal;
270   } // getIdFromEmail
271

272   /**
273     * <p>Add a State for a given country at k_addresses_lookup table</p>
274     * This methods checks whether the given State Name exists at k_addresses_lookup
275     * for a given country and, if not, then inserts it.<br>
276     * Example of how to call this method:<br>
277     * Connection oConn = <i>// Code to get connection</i> ;<br>
278     * HashMap oTr = new HashMap();<br>
279     * oTr.put("en", "New York");<br>
280     * oTr.put("fr", "Nouveau York");<br>
281     * oTr.put("es", "Nueva York");<br>
282     * Address.addState(oConn, "0123456789ABCDEFGHIJKLMNOPQRST", "us", "NY", oTr);
283     * @param oConn Connection
284     * @param sGuWorkArea String GUID of WorkArea
285     * @param sIdCountry String Two letters country identifier from k_lu_countries table
286     * @param sNmState String Language Neutral State Name or Code
287     * @param oTranslations HashMap with one entry for each language
288     * Language codes must be those from id_language column of k_lu_languages table.
289     * @return boolean <b>true</b> if state was added, <b>false</b> if it already existed
290     * @throws SQLException
291     * @since 3.0
292     */

293    public static boolean addLookupState (Connection JavaDoc oConn, String JavaDoc sGuWorkArea, String JavaDoc sIdCountry, String JavaDoc sNmState, HashMap JavaDoc oTranslations)
294     throws SQLException JavaDoc {
295       return DBLanguages.addLookup(oConn,DB.k_addresses_lookup, sGuWorkArea, sIdCountry, sNmState, oTranslations);
296     }
297
298     /**
299      * <p>Add a Street Type lookup value</a>
300      * @param oConn Connection
301      * @param sGuWorkArea String GUID of WorkArea
302      * @param sTpStreet String Street Type Internal Value
303      * @param oTranslations HashMap with one entry for each language
304      * @return boolean <b>true</b> if new street type was added, <b>false</b> if it already existed
305      * @throws SQLException
306      * @since 3.0
307      */

308     public static boolean addLookupStreetType (Connection JavaDoc oConn, String JavaDoc sGuWorkArea, String JavaDoc sTpStreet, HashMap JavaDoc oTranslations)
309      throws SQLException JavaDoc {
310      return DBLanguages.addLookup(oConn,DB.k_addresses_lookup, sGuWorkArea, DB.tp_street, sTpStreet, oTranslations);
311    }
312
313   // **********************************************************
314
// Public Constants
315

316   public static final short ClassId = 7;
317 }
318
Popular Tags