KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > crm > Contact


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.crm;
34
35 import java.io.File JavaDoc;
36 import java.io.FileNotFoundException JavaDoc;
37
38 import java.util.Date JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import java.util.ListIterator JavaDoc;
41
42 import java.sql.Connection JavaDoc;
43 import java.sql.SQLException JavaDoc;
44 import java.sql.Statement JavaDoc;
45 import java.sql.PreparedStatement JavaDoc;
46 import java.sql.CallableStatement JavaDoc;
47 import java.sql.ResultSet JavaDoc;
48
49 import com.knowgate.debug.DebugFile;
50 import com.knowgate.misc.Environment;
51 import com.knowgate.misc.Gadgets;
52 import com.knowgate.jdc.JDCConnection;
53 import com.knowgate.acl.ACLDomain;
54 import com.knowgate.dataobjs.DB;
55 import com.knowgate.dataobjs.DBBind;
56 import com.knowgate.dataobjs.DBSubset;
57 import com.knowgate.dataobjs.DBPersist;
58
59 import com.knowgate.hipergate.Product;
60 import com.knowgate.hipergate.Address;
61 import com.knowgate.hipergate.DBLanguages;
62 import com.knowgate.hipergate.Product;
63 import com.knowgate.hipergate.ProductLocation;
64 import com.knowgate.workareas.FileSystemWorkArea;
65
66 /**
67  * <p>Contact</p>
68  * <p>Copyright: Copyright (c) KnowGate 2003</p>
69  * @author Sergio Montoro Ten
70  * @version 3.0
71  */

72
73 public class Contact extends DBPersist {
74
75   /**
76    * Create Empty Contact.
77    */

78   public Contact() {
79      super(DB.k_contacts, "Contact");
80   }
81
82   /**
83    * Create Contact and set gu_contact field.
84    * Does not load other fields from database.
85    * @param sIdContact Contact GUID
86    */

87   public Contact(String JavaDoc sIdContact) throws SQLException JavaDoc {
88     super(DB.k_contacts,"Contact");
89     put(DB.gu_contact, sIdContact);
90   }
91
92   /**
93    * Create Contact and load fields from database.
94    * @param oConn Database Connection
95    * @param sIdContact Contact GUID
96    */

97   public Contact(JDCConnection oConn, String JavaDoc sIdContact) throws SQLException JavaDoc {
98     super(DB.k_contacts,"Contact");
99
100     Object JavaDoc aCont[] = { sIdContact };
101
102     load (oConn,aCont);
103   }
104
105   // ----------------------------------------------------------
106

107   /**
108    * <p>Store Contact</p>
109    * Automatically generates gu_contact GUID and dt_modified DATE if not explicitly set.<br>
110    * If gu_company field is <b>null</b> and nm_legal field is not <b>null</b> then gu_company
111    * field is automatically looked up at k_companies table and put in this DBPersist before storing it.
112    * @param oConn Database Connection
113    * @throws SQLException
114    */

115   public boolean store(JDCConnection oConn) throws SQLException JavaDoc {
116     PreparedStatement JavaDoc oStmt;
117     ResultSet JavaDoc oRSet;
118
119     if (DebugFile.trace) {
120       DebugFile.writeln("Begin Contact.store([Connection])");
121       DebugFile.incIdent();
122     }
123
124     java.sql.Timestamp JavaDoc dtNow = new java.sql.Timestamp JavaDoc(DBBind.getTime());
125
126     if (!AllVals.containsKey(DB.gu_contact))
127       put(DB.gu_contact, Gadgets.generateUUID());
128
129     if (!AllVals.containsKey(DB.gu_company) && AllVals.containsKey(DB.nm_legal)) {
130
131       if (DebugFile.trace)
132         DebugFile.writeln("Connection.prepareStatement(SELECT " + DB.gu_company + " FROM " + DB.k_companies + " WHERE " + DB.gu_workarea + "='" + getStringNull(DB.gu_workarea, "null") + "' AND " + DB.nm_legal + "='" + getStringNull(DB.nm_legal, "null") + "')");
133
134       oStmt = oConn.prepareStatement("SELECT " + DB.gu_company + " FROM " + DB.k_companies + " WHERE " + DB.gu_workarea + "=? AND " + DB.nm_legal + "=?",
135                                      ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
136
137       oStmt.setString(1, getStringNull(DB.gu_workarea, null));
138       oStmt.setString(2, getStringNull(DB.nm_legal, null));
139       oRSet = oStmt.executeQuery();
140
141       if (oRSet.next()) {
142         if (DebugFile.trace) DebugFile.writeln("gu_company=" + oRSet.getString(1));
143
144         AllVals.put(DB.gu_company, oRSet.getString(1));
145       }
146       else
147         if (DebugFile.trace) DebugFile.writeln("no company guid found for " + getStringNull(DB.nm_legal, null));
148
149       oRSet.close();
150       oStmt.close();
151     } // fi (gu_company==null && nm_legal!=null)
152

153     replace(DB.dt_modified, dtNow);
154
155     boolean bRetVal = super.store(oConn);
156
157     if (DebugFile.trace) {
158       DebugFile.decIdent();
159       DebugFile.writeln("End Contact.store() : " + String.valueOf(bRetVal));
160     }
161
162     return bRetVal;
163   } // store
164

165   // ----------------------------------------------------------
166

167   /**
168    * Delete Contact.
169    * @throws SQLException
170    */

171   public boolean delete(JDCConnection oConn) throws SQLException JavaDoc {
172     return Contact.delete(oConn, getString(DB.gu_contact));
173   }
174
175   // ----------------------------------------------------------
176

177   /**
178    * <P>Add an Address to this Contact</P>
179    * If contact is already associated to the given address then a foreign key violation is thrown
180    * @param oConn Database Connection
181    * @throws SQLException
182    * @since 3.0
183    */

184   public boolean addAddress(JDCConnection oConn, String JavaDoc sAddrGUID) throws SQLException JavaDoc {
185     PreparedStatement JavaDoc oStmt = null;
186     boolean bRetVal;
187
188     try {
189       oStmt = oConn.prepareStatement("INSERT INTO " + DB.k_x_contact_addr + " (" + DB.gu_contact + "," + DB.gu_address + ") VALUES (?,?)");
190       oStmt.setString(1, getStringNull(DB.gu_contact, null));
191       oStmt.setString(2, sAddrGUID);
192       int iAffected = oStmt.executeUpdate();
193       oStmt.close();
194       oStmt = null;
195       bRetVal = (iAffected > 0);
196     } catch (SQLException JavaDoc sqle) {
197       bRetVal = false;
198       try { if (oStmt!=null) oStmt.close(); } catch (Exception JavaDoc ignore) {}
199     }
200     return bRetVal;
201   } // addAddress
202

203   // ----------------------------------------------------------
204

205   /**
206    * <P>Add a bank account to this Contact</P>
207    * If contact is already associated to the given bank account then a foreign key violation SQLException is thrown
208    * @param oConn Database Connection
209    * @throws SQLException
210    * @since 3.0
211    */

212   public boolean addBankAccount(JDCConnection oConn, String JavaDoc sFullBankAccount) throws SQLException JavaDoc {
213     PreparedStatement JavaDoc oStmt = null;
214     boolean bRetVal;
215
216     try {
217       oStmt = oConn.prepareStatement("INSERT INTO " + DB.k_x_contact_bank + " (" + DB.gu_contact + "," + DB.nu_bank_acc + "," + DB.gu_workarea + ") VALUES (?,?,?)");
218       oStmt.setString(1, getStringNull(DB.gu_contact, null));
219       oStmt.setString(2, sFullBankAccount);
220       oStmt.setString(3, getStringNull(DB.gu_workarea, null));
221       int iAffected = oStmt.executeUpdate();
222       oStmt.close();
223       oStmt = null;
224       bRetVal = (iAffected > 0);
225     } catch (SQLException JavaDoc sqle) {
226       bRetVal = false;
227       try { if (oStmt!=null) oStmt.close(); } catch (Exception JavaDoc ignore) {}
228     }
229     return bRetVal;
230   } // addBankAccount
231
// ----------------------------------------------------------
232

233   /**
234    * Get Contact Address by index
235    * @param oConn JDCConnection
236    * @param iIndex int Address index as set at column ix_address of k_addresses table
237    * @return Address or <b>null</b> if no Address with such index was found
238    * @throws SQLException
239    * @since 3.0
240    */

241   public Address getAddress(JDCConnection oConn, int iIndex) throws SQLException JavaDoc {
242     String JavaDoc sGuAddr;
243     PreparedStatement JavaDoc oStmt = oConn.prepareStatement("SELECT a."+DB.gu_address+" FROM "+DB.k_addresses+" a,"+DB.k_x_contact_addr+" x WHERE a."+DB.gu_address+"=x."+DB.gu_address+" AND x."+DB.gu_contact+"=? AND a."+DB.ix_address+"=?",ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
244     oStmt.setString(1, getStringNull(DB.gu_contact,null));
245     oStmt.setInt(2, iIndex);
246     ResultSet JavaDoc oRSet = oStmt.executeQuery();
247     if (oRSet.next())
248       sGuAddr = oRSet.getString(1);
249     else
250       sGuAddr = null;
251     oRSet.close();
252     oStmt.close();
253     if (null!=sGuAddr)
254       return new Address(oConn, sGuAddr);
255     else
256       return null;
257   }
258
259   // ----------------------------------------------------------
260

261   /**
262    * Get Contact Address by location type
263    * @param oConn JDCConnection
264    * @param sTpLocation String Address type as set at column tp_location of k_addresses table
265    * @return Address or <b>null</b> if no Address with such location type was found
266    * @throws SQLException
267    * @since 2.2
268    */

269   public Address getAddress(JDCConnection oConn, String JavaDoc sTpLocation) throws SQLException JavaDoc {
270     String JavaDoc sGuAddr;
271     PreparedStatement JavaDoc oStmt = oConn.prepareStatement("SELECT a."+DB.gu_address+" FROM "+DB.k_addresses+" a,"+DB.k_x_contact_addr+" x WHERE a."+DB.gu_address+"=x."+DB.gu_address+" AND x."+DB.gu_contact+"=? AND a."+DB.tp_location+"=?",ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
272     oStmt.setString(1, getStringNull(DB.gu_contact,null));
273     oStmt.setString(2, sTpLocation);
274     ResultSet JavaDoc oRSet = oStmt.executeQuery();
275     if (oRSet.next())
276       sGuAddr = oRSet.getString(1);
277     else
278       sGuAddr = null;
279     oRSet.close();
280     oStmt.close();
281     if (null!=sGuAddr)
282       return new Address(oConn, sGuAddr);
283     else
284       return null;
285   }
286
287   // ----------------------------------------------------------
288

289   /**
290    * <p>Get Contact Addresses</p>
291    * @param oConn Database Connection
292    * @return A DBSubset with all columns from k_addresses for Contact
293    * @throws SQLException
294    * @throws NullPointerException If gu_contact is <b>null</b>
295    */

296   public DBSubset getAddresses(JDCConnection oConn) throws SQLException JavaDoc {
297     if (DebugFile.trace) {
298       DebugFile.writeln("Begin Contact.getAddresses([Connection])" );
299       DebugFile.incIdent();
300     }
301
302     if (isNull(DB.gu_contact)) throw new NullPointerException JavaDoc ("gu_contact not set");
303
304     Address oAddr = new Address();
305
306     DBSubset oAddrs = new DBSubset (DB.k_addresses,
307                                     oAddr.getTable(oConn).getColumnsStr(),
308                                     DB.gu_address + " IN (SELECT " + DB.gu_address + " FROM " + DB.k_x_contact_addr + " WHERE " + DB.gu_contact + "='" + getString(DB.gu_contact) + "')", 10);
309     int iAddrs = oAddrs.load (oConn);
310
311     oAddr = null;
312
313     if (DebugFile.trace) {
314       DebugFile.decIdent();
315       DebugFile.writeln("End Contact.getAddresses() : " + String.valueOf(iAddrs));
316     }
317
318     return oAddrs;
319   } // getAddresses
320

321   // ----------------------------------------------------------
322

323   /**
324    * Get all bank accounts associated with Contact
325    * @param oConn JDCConnection
326    * @return DBSubset nu_bank_acc,dt_created,bo_active,tp_account,nm_bank,tx_addr,nm_cardholder,nu_card,tp_card,tx_expire,nu_pin,nu_cvv2,im_credit_limit,de_bank_acc
327    * @throws SQLException
328    * @throws IllegalStateException if gu_contact or gu_workarea are not set
329    * @since 3.0
330    */

331   public DBSubset getAllBankAccounts(JDCConnection oConn)
332     throws SQLException JavaDoc,IllegalStateException JavaDoc {
333     if (isNull(DB.gu_contact))
334       throw new IllegalStateException JavaDoc("Contact.getAllBankAccounts() gu_contact property is not set");
335     if (isNull(DB.gu_workarea))
336       throw new IllegalStateException JavaDoc("Contact.getAllBankAccounts() gu_workarea property is not set");
337
338     DBSubset oAccs = new DBSubset (DB.k_bank_accounts,
339                                    DB.nu_bank_acc+","+DB.dt_created+","+DB.bo_active+","+DB.tp_account+","+DB.nm_bank+","+DB.tx_addr+","+DB.nm_cardholder+","+DB.nu_card+","+DB.tp_card+","+DB.tx_expire+","+DB.nu_pin+","+DB.nu_cvv2+","+DB.im_credit_limit+","+DB.de_bank_acc,
340                                    DB.gu_workarea+"=? AND "+DB.nu_bank_acc+" IN (SELECT "+DB.nu_bank_acc+" FROM "+DB.k_x_contact_bank+" WHERE "+DB.gu_workarea+"=? AND "+DB.gu_contact+"=?)",10);
341
342     oAccs.load(oConn, new Object JavaDoc[]{get(DB.gu_workarea),get(DB.gu_workarea),get(DB.gu_contact)});
343     return oAccs;
344   } // getAllBankAccounts
345

346   // ----------------------------------------------------------
347

348   /**
349    * Get active bank accounts for this Contact
350    * @param oConn JDCConnection
351    * @return DBSubset nu_bank_acc,dt_created,tp_account,nm_bank,tx_addr,nm_cardholder,nu_card,tp_card,tx_expire,nu_pin,nu_cvv2,im_credit_limit,de_bank_acc
352    * @throws SQLException
353    * @throws IllegalStateException if gu_contact or gu_workarea are not set
354    * @since 3.0
355    */

356   public DBSubset getActiveBankAccounts(JDCConnection oConn)
357     throws SQLException JavaDoc,IllegalStateException JavaDoc {
358     if (isNull(DB.gu_contact))
359       throw new IllegalStateException JavaDoc("Contact.getActiveBankAccounts() gu_contact property is not set");
360     if (isNull(DB.gu_workarea))
361       throw new IllegalStateException JavaDoc("Contact.getActiveBankAccounts() gu_workarea property is not set");
362
363     DBSubset oAccs = new DBSubset (DB.k_bank_accounts,
364                                    DB.nu_bank_acc+","+DB.dt_created+","+DB.tp_account+","+DB.nm_bank+","+DB.tx_addr+","+DB.nm_cardholder+","+DB.nu_card+","+DB.tp_card+","+DB.tx_expire+","+DB.nu_pin+","+DB.nu_cvv2+","+DB.im_credit_limit+","+DB.de_bank_acc,
365                                    DB.gu_workarea+"=? AND "+DB.bo_active+"<>0 AND "+DB.nu_bank_acc+" IN (SELECT "+DB.nu_bank_acc+" FROM "+DB.k_x_contact_bank+" WHERE "+DB.gu_workarea+"=? AND "+DB.gu_contact+"=?)",10);
366
367     oAccs.load(oConn, new Object JavaDoc[]{get(DB.gu_workarea),get(DB.gu_workarea),get(DB.gu_contact)});
368     return oAccs;
369   } // getActiveBankAccounts
370

371   // ----------------------------------------------------------
372

373   /**
374    * Get unactive bank accounts for this Contact
375    * @param oConn JDCConnection
376    * @return DBSubset nu_bank_acc,dt_created,tp_account,nm_bank,tx_addr,nm_cardholder,nu_card,tp_card,tx_expire,nu_pin,nu_cvv2,im_credit_limit,de_bank_acc
377    * @throws SQLException
378    * @throws IllegalStateException if gu_contact or gu_workarea are not set
379    * @since 3.0
380    */

381   public DBSubset getUnactiveBankAccounts(JDCConnection oConn)
382     throws SQLException JavaDoc,IllegalStateException JavaDoc {
383     if (isNull(DB.gu_company))
384       throw new IllegalStateException JavaDoc("Contact.getUnactiveBankAccounts() gu_company property is not set");
385     if (isNull(DB.gu_workarea))
386       throw new IllegalStateException JavaDoc("Contact.getUnactiveBankAccounts() gu_contact property is not set");
387
388     DBSubset oAccs = new DBSubset (DB.k_bank_accounts,
389                                    DB.nu_bank_acc+","+DB.dt_created+","+DB.tp_account+","+DB.nm_bank+","+DB.tx_addr+","+DB.nm_cardholder+","+DB.nu_card+","+DB.tp_card+","+DB.tx_expire+","+DB.nu_pin+","+DB.nu_cvv2+","+DB.im_credit_limit+","+DB.de_bank_acc,
390                                    DB.gu_workarea+"=? AND "+DB.bo_active+"=0 AND "+DB.nu_bank_acc+" IN (SELECT "+DB.nu_bank_acc+" FROM "+DB.k_x_contact_bank+" WHERE "+DB.gu_workarea+"=? AND "+DB.gu_company+"=?)",10);
391
392     oAccs.load(oConn, new Object JavaDoc[]{get(DB.gu_workarea),get(DB.gu_workarea),get(DB.gu_contact)});
393     return oAccs;
394   } // getUnactiveBankAccounts
395

396   // ----------------------------------------------------------
397

398   /**
399    * Add an Attachment to a Contact
400    * @param oConn JDCConnection
401    * @param sGuWriter String GUID of user (from k_users table) who is uploading the attachment
402    * @param sDirPath String Physical path (directory) where file to be attached ir located
403    * @param sFileName String Name of file to be attached
404    * @param bDeleteOriginalFile boolean <b>true</b> if original file must be deleted after being attached
405    * @return Attachment
406    * @throws SQLException
407    * @throws NullPointerException
408    * @throws FileNotFoundException
409    * @throws Exception
410    * @since 3.0
411    */

412   public Attachment addAttachment(JDCConnection oConn, String JavaDoc sGuWriter,
413                                   String JavaDoc sDirPath, String JavaDoc sFileName,
414                                   boolean bDeleteOriginalFile)
415     throws SQLException JavaDoc,NullPointerException JavaDoc,FileNotFoundException JavaDoc,Exception JavaDoc {
416
417   if (DebugFile.trace) {
418     DebugFile.writeln("Begin Contact.addAttachment([Connection],"+sGuWriter+","+
419                       sDirPath+","+sFileName+","+String.valueOf(bDeleteOriginalFile)+")" );
420     DebugFile.incIdent();
421   }
422
423     Date JavaDoc dtNow = new Date JavaDoc();
424     PreparedStatement JavaDoc oStmt;
425     ResultSet JavaDoc oRSet;
426     String JavaDoc sNmLegal;
427     String JavaDoc sProfile;
428
429     // Check that Contact is loaded
430
if (isNull(DB.gu_contact) || isNull(DB.gu_workarea))
431       throw new NullPointerException JavaDoc("Contact.addAttachment() Contact not loaded");
432
433     if (null==sDirPath)
434       throw new NullPointerException JavaDoc("Contact.addAttachment() File path may not be null");
435
436     if (null==sFileName)
437       throw new NullPointerException JavaDoc("Contact.addAttachment() File name may not be null");
438
439     File JavaDoc oDir = new File JavaDoc(sDirPath);
440     if (!oDir.isDirectory())
441       throw new FileNotFoundException JavaDoc("Contact.addAttachment() "+sDirPath+" is not a directory");
442
443     if (!oDir.exists())
444       throw new FileNotFoundException JavaDoc("Contact.addAttachment() Directory "+sDirPath+" not found");
445
446     File JavaDoc oFile = new File JavaDoc(Gadgets.chomp(sDirPath,File.separatorChar)+sFileName);
447     if (!oFile.exists())
448       throw new FileNotFoundException JavaDoc("Contact.addAttachment() File "+Gadgets.chomp(sDirPath,File.separatorChar)+sFileName+" not found");
449
450     // Get Id. of Domain to which Contact belongs
451
Integer JavaDoc iDom = ACLDomain.forWorkArea(oConn, getString(DB.gu_workarea));
452
453     if (DebugFile.trace) DebugFile.writeln("id_domain="+iDom);
454
455     switch (oConn.getDataBaseProduct()) {
456       case JDCConnection.DBMS_ORACLE:
457         oStmt = oConn.prepareStatement("SELECT k." + DB.nm_legal + " FROM " + DB.k_contacts + " c, " + DB.k_companies + " k WHERE c." + DB.gu_company + "=k." + DB.gu_company + "(+) AND c." + DB.gu_contact + "=? AND c." + DB.gu_company + " IS NOT NULL",
458                                        ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
459         break;
460       default:
461         oStmt = oConn.prepareStatement("SELECT k." + DB.nm_legal + " FROM " + DB.k_contacts + " c LEFT OUTER JOIN " + DB.k_companies + " k ON c." + DB.gu_company + "=k." + DB.gu_company + " WHERE c." + DB.gu_contact + "=? AND c." + DB.gu_company + " IS NOT NULL",
462                                        ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
463     }
464     oStmt.setString(1, getString(DB.gu_contact));
465     oRSet = oStmt.executeQuery();
466     if (oRSet.next())
467       sNmLegal = Gadgets.ASCIIEncode(oRSet.getString(1));
468     else
469       sNmLegal = "_NOCOMPANY";
470     oRSet.close();
471     oStmt.close();
472
473     String JavaDoc sCatPath = "apps/Sales/"+sNmLegal+"/"+getString(DB.gu_contact)+"/";
474
475     if (DebugFile.trace) DebugFile.writeln("category path = "+sCatPath);
476
477     if (null==oConn.getPool())
478       sProfile = "hipergate";
479     else
480       sProfile = ((DBBind) oConn.getPool().getDatabaseBinding()).getProfileName();
481
482     if (DebugFile.trace) DebugFile.writeln("profile = "+sProfile);
483
484     FileSystemWorkArea oFileSys = new FileSystemWorkArea(Environment.getProfile(sProfile));
485     oFileSys.mkstorpath(iDom.intValue(), getString(DB.gu_workarea), sCatPath);
486
487     String JavaDoc sStorage = Environment.getProfilePath(sProfile, "storage");
488     String JavaDoc sFileProtocol = Environment.getProfileVar(sProfile, "fileprotocol", "file://");
489     String JavaDoc sFileServer = Environment.getProfileVar(sProfile, "fileserver", "localhost");
490
491     String JavaDoc sWrkAHome = sStorage + "domains" + File.separator + iDom.toString() + File.separator + "workareas" + File.separator + getString(DB.gu_workarea) + File.separator;
492     if (DebugFile.trace) DebugFile.writeln("workarea home = "+sWrkAHome);
493
494     Product oProd = new Product();
495     oProd.put(DB.nm_product,Gadgets.left(sFileName, 128));
496     oProd.put(DB.gu_owner, sGuWriter);
497     oProd.put(DB.dt_uploaded, dtNow);
498     oProd.store(oConn);
499
500     ProductLocation oLoca = new ProductLocation();
501     oLoca.put(DB.gu_owner, sGuWriter);
502     oLoca.put(DB.gu_product, oProd.get(DB.gu_product));
503     oLoca.put(DB.dt_uploaded, dtNow);
504     oLoca.setPath (sFileProtocol, sFileServer, sWrkAHome + sCatPath, sFileName, sFileName);
505     oLoca.setLength(oFile.length());
506     oLoca.replace(DB.id_cont_type, oLoca.getContainerType());
507     oLoca.store(oConn);
508
509     if (sFileProtocol.equalsIgnoreCase("ftp://"))
510       oLoca.upload(oConn, oFileSys, "file://" + sDirPath, sFileName, "ftp://" + sFileServer + sWrkAHome + sCatPath, sFileName);
511     else
512       oLoca.upload(oConn, oFileSys, "file://" + sDirPath, sFileName, sFileProtocol + sWrkAHome + sCatPath, sFileName);
513
514     Attachment oAttach = new Attachment();
515     oAttach.put(DB.gu_contact, getString(DB.gu_contact));
516     oAttach.put(DB.gu_product, oProd.getString(DB.gu_product));
517     oAttach.put(DB.gu_location, oLoca.getString(DB.gu_location));
518     oAttach.put(DB.gu_writer, sGuWriter);
519     oAttach.store(oConn);
520
521     if (bDeleteOriginalFile) {
522       if (DebugFile.trace) DebugFile.writeln("deleting file "+oFile.getAbsolutePath());
523       oFile.delete();
524       if (DebugFile.trace) DebugFile.writeln("deleting file "+sFileName+" deleted");
525     }
526
527     if (DebugFile.trace) {
528       DebugFile.decIdent();
529       DebugFile.writeln("End Contact.addAttachment() : " + String.valueOf(oAttach.getInt(DB.pg_product)));
530     }
531
532     return oAttach;
533   } // addAttachment
534

535   // ----------------------------------------------------------
536

537   /**
538    * Attach all files from a given directory
539    * @param oConn JDCConnection
540    * @param sGuWriter String GUID of user attaching the files
541    * @param sDirPath String Directory Path
542    * @param bDeleteOriginalFiles boolean <b>true</b> if original files must be deleted after being attached
543    * @throws SQLException
544    * @throws NullPointerException
545    * @throws FileNotFoundException
546    * @throws Exception
547    * @since 3.0
548    */

549   public void addAttachments(JDCConnection oConn, String JavaDoc sGuWriter,
550                              String JavaDoc sDirPath, boolean bDeleteOriginalFiles)
551     throws SQLException JavaDoc,NullPointerException JavaDoc,FileNotFoundException JavaDoc,Exception JavaDoc {
552
553     File JavaDoc oDir = new File JavaDoc(sDirPath);
554
555     if (!oDir.exists())
556       throw new FileNotFoundException JavaDoc("Contact.addAttachment() Directory "+sDirPath+" not found");
557
558     if (!oDir.isDirectory())
559       throw new FileNotFoundException JavaDoc("Contact.addAttachment() "+sDirPath+" is not a directory");
560
561     if (!oDir.exists())
562       throw new FileNotFoundException JavaDoc("Contact.addAttachment() Directory "+sDirPath+" not found");
563
564     File JavaDoc[] aFiles = oDir.listFiles();
565     if (null!=aFiles) {
566       int nFiles = aFiles.length;
567       for (int f=0; f<nFiles; f++)
568         addAttachment(oConn, sGuWriter, sDirPath, aFiles[f].getName(), false);
569       if (bDeleteOriginalFiles) {
570         for (int f=0; f<nFiles; f++)
571           aFiles[f].delete();
572       } // fi (bDeleteOriginalFiles)
573
} // fi
574
} // addAttachments
575

576   // ----------------------------------------------------------
577

578   /**
579    * Remove attachment
580    * @param oConn JDCConnection
581    * @param iPgAttachment int
582    * @return boolean
583    * @throws SQLException
584    * @throws NullPointerException
585    * @since 3.0
586    */

587   public boolean removeAttachment(JDCConnection oConn, int iPgAttachment)
588     throws SQLException JavaDoc {
589     Attachment oAttach = new Attachment();
590     if (oAttach.load(oConn, new Object JavaDoc[]{get(DB.gu_contact),new Integer JavaDoc(iPgAttachment)}))
591       return oAttach.delete(oConn);
592     else
593       return false;
594   } // removeAttachment
595

596   // ----------------------------------------------------------
597

598   /**
599    * Get array of products attached to this Contact
600    * @param oConn JDCConnection
601    * @return Attachment[] array or <b>null</b> if no products are attached to this contact
602    * @throws SQLException
603    * @throws NullPointerException if gu_contact is <b>null</b>
604    * @since 3.0
605    */

606   public Attachment[] getAttachments(JDCConnection oConn)
607     throws SQLException JavaDoc,NullPointerException JavaDoc {
608
609     if (isNull(DB.gu_contact))
610       throw new NullPointerException JavaDoc("Contact.getAttachments() Contact not loaded");
611
612     Attachment oAttach = new Attachment();
613     Attachment[] aAttachs;
614     DBSubset oAttachs = new DBSubset(DB.k_contact_attachs,
615                                      oAttach.getTable(oConn).getColumnsStr(),
616                                      DB.gu_contact+"=?", 10);
617     int iAttachs = oAttachs.load(oConn, new Object JavaDoc[]{get(DB.gu_contact)});
618     if (0==iAttachs) {
619       aAttachs = null;
620     } else {
621       aAttachs = new Attachment[iAttachs];
622       ListIterator JavaDoc oCols = oAttach.getTable(oConn).getColumns().listIterator();
623       while (oCols.hasNext()) {
624         String JavaDoc sKey = (String JavaDoc) oCols.next();
625         if (!sKey.equalsIgnoreCase(DB.dt_created)) {
626           for (int a=0; a<iAttachs; a++) {
627             aAttachs[a].put(sKey, oAttachs.get(sKey,a));
628           } // next (a)
629
} // fi (sKey!="dt_created")
630
} // wend
631
} // fi (iAttachs)
632
return aAttachs;
633   } // getAttachments
634

635   // ----------------------------------------------------------
636

637   /**
638    * Get most recent WelCome Pack associated to this contact
639    * @param oConn JDCConnection
640    * @return WelcomePack
641    * @throws SQLException
642    * @since 3.0
643    */

644   public WelcomePack getWelcomePack(JDCConnection oConn) throws SQLException JavaDoc {
645     return WelcomePack.forContact(oConn, getString(DB.gu_contact));
646   }
647
648   // **********************************************************
649
// Static Methods
650

651   /**
652    * Delete Contact.
653    * The delete step by step is as follws:<br>
654    * If k_x_meeting_contact table exists, then Contact is deleted from Meetings.<br>
655    * If k_orders table exists, then Orders for this Contact are deleted.<br>
656    * All Contact Attachments are deleted.<br>
657    * Stored Procedure k_sp_del_contact is called
658    * @param oConn Database Connection
659    * @param sContactGUID GUID of Contact to be deleted
660    * @throws SQLException
661    */

662
663   public static boolean delete(JDCConnection oConn, String JavaDoc sContactGUID) throws SQLException JavaDoc {
664     boolean bRetVal;
665     Statement JavaDoc oUpdt;
666     PreparedStatement JavaDoc oDlte;
667
668     if (DebugFile.trace) {
669       DebugFile.writeln("Begin Contact.delete([Connection], " + sContactGUID + ")");
670       DebugFile.incIdent();
671     }
672
673     /* Desasociar los e-mails */
674     if (DBBind.exists(oConn, DB.k_inet_addrs, "U")) {
675       oUpdt = oConn.createStatement();
676
677       if (DebugFile.trace)
678         DebugFile.writeln("Statement.executeUpdate(UPDATE " + DB.k_inet_addrs + " SET " + DB.gu_contact + "=NULL WHERE " + DB.gu_contact + "='" + sContactGUID + "')");
679
680       oUpdt.executeUpdate("UPDATE " + DB.k_inet_addrs + " SET " + DB.gu_contact + "=NULL WHERE " + DB.gu_contact + "='" + sContactGUID + "'");
681
682       oUpdt.close();
683     }
684
685     /* Desasociar los proyectos */
686     if (DBBind.exists(oConn, DB.k_projects, "U")) {
687       oUpdt = oConn.createStatement();
688
689       if (DebugFile.trace)
690         DebugFile.writeln("Statement.executeUpdate(UPDATE " + DB.k_projects + " SET " + DB.gu_contact + "=NULL WHERE " + DB.gu_contact + "='" + sContactGUID + "')");
691
692       oUpdt.executeUpdate("UPDATE " + DB.k_projects + " SET " + DB.gu_contact + "=NULL WHERE " + DB.gu_contact + "='" + sContactGUID + "'");
693       oUpdt.close();
694       oUpdt = null;
695     }
696
697     /* Borrar los pedidos, si existen */
698     if (DBBind.exists(oConn, DB.k_orders, "U")) {
699       DBSubset oOrders = new DBSubset(DB.k_orders, DB.gu_order, DB.gu_contact + "='" + sContactGUID + "'", 1000);
700
701       int iOrders = oOrders.load(oConn);
702
703       for (int o=0; o<iOrders; o++)
704         com.knowgate.hipergate.Order.delete (oConn, oOrders.getString(0,o));
705     } // fi (exists(DB.k_orders))
706

707     /* Borrar las convocatorias a actividades, si existen */
708     if (DBBind.exists(oConn, DB.k_x_meeting_contact, "U")) {
709       if (DebugFile.trace)
710         DebugFile.writeln("Connection.prepareStatement(DELETE FROM " + DB.k_x_meeting_contact + " WHERE " + DB.gu_contact + "='" + sContactGUID + "')");
711
712       oDlte = oConn.prepareStatement("DELETE FROM " + DB.k_x_meeting_contact + " WHERE " + DB.gu_contact + "=?");
713
714       oDlte.setString(1, sContactGUID);
715
716       oDlte.executeUpdate();
717
718       oDlte.close();
719
720       oDlte = null;
721     } // fi (exists(oConn, DB.k_x_meeting_contact))
722

723     /* Borrar las llamadas telefónicas */
724     if (DBBind.exists(oConn, DB.k_phone_calls, "U")) {
725       if (DebugFile.trace)
726         DebugFile.writeln("Connection.prepareStatement(DELETE FROM " + DB.k_phone_calls + " WHERE " + DB.gu_contact + "='" + sContactGUID + "')");
727
728       oDlte = oConn.prepareStatement("DELETE FROM " + DB.k_phone_calls + " WHERE " + DB.gu_contact + "=?");
729
730       oDlte.setString(1,sContactGUID);
731
732       oDlte.executeUpdate();
733
734       oDlte.close();
735     } // fi (exists(oConn, DB.k_phone_calls))
736

737     /* Borrar las reservas en cursos */
738     if (DBBind.exists(oConn, DB.k_x_course_bookings, "U")) {
739       if (DebugFile.trace)
740         DebugFile.writeln("Connection.prepareStatement(DELETE FROM " + DB.k_x_course_bookings + " WHERE " + DB.gu_contact + "='" + sContactGUID + "')");
741
742       oDlte = oConn.prepareStatement("DELETE FROM " + DB.k_x_course_bookings + " WHERE " + DB.gu_contact + "=?");
743
744       oDlte.setString(1,sContactGUID);
745
746       oDlte.executeUpdate();
747
748       oDlte.close();
749     } // fi (exists(oConn, DB.k_x_course_bookings))
750

751     DBSubset oAttachs = new DBSubset(DB.k_contact_attachs, DB.gu_product, DB.gu_contact + "='" + sContactGUID + "'" , 64);
752     int iAttachs = oAttachs.load(oConn);
753
754     if (DebugFile.trace) DebugFile.writeln("new Product()");
755
756     Product oProd = new Product();
757
758     for (int a=0;a<iAttachs; a++) {
759       oProd.replace(DB.gu_product, oAttachs.getString(0,a));
760       oProd.delete(oConn);
761     } // next (a)
762

763     oProd = null;
764
765     oAttachs = null;
766
767     Statement JavaDoc oStmt = oConn.createStatement();
768
769     if (DebugFile.trace)
770       DebugFile.writeln("Statement.executeUpdate(DELETE FROM " + DB.k_contact_attachs + " WHERE " + DB.gu_contact + "='" + sContactGUID + "')");
771
772     oStmt.executeUpdate("DELETE FROM " + DB.k_contact_attachs + " WHERE " + DB.gu_contact + "='" + sContactGUID + "'");
773     oStmt.close();
774
775     if (oConn.getDataBaseProduct()==JDCConnection.DBMS_POSTGRESQL) {
776       oStmt = oConn.createStatement();
777       if (DebugFile.trace) DebugFile.writeln("Statement.executeQuery(SELECT k_sp_del_contact ('" + sContactGUID + "')");
778       oStmt.executeQuery("SELECT k_sp_del_contact ('" + sContactGUID + "')");
779       oStmt.close();
780       bRetVal = true;
781     }
782     else {
783       if (DebugFile.trace)
784         DebugFile.writeln("Connection.prepareCall({ call k_sp_del_contact ('" + sContactGUID + "') }");
785
786       CallableStatement JavaDoc oCall = oConn.prepareCall("{ call k_sp_del_contact ('" + sContactGUID + "') }");
787       bRetVal = oCall.execute();
788       oCall.close();
789     }
790
791     if (DebugFile.trace) {
792       DebugFile.decIdent();
793       DebugFile.writeln("End Contact.delete() : " + String.valueOf(bRetVal));
794     }
795
796     return bRetVal;
797   } // delete
798

799   /**
800    * <p>Add a Street Type lookup value</a>
801    * @param oConn Connection
802    * @param sGuWorkArea String GUID of WorkArea
803    * @param sTpPassport String Passport Type Internal Value
804    * @param oTranslations HashMap with one entry for each language
805    * @return boolean <b>true</b> if new passport type was added, <b>false</b> if it already existed
806    * @throws SQLException
807    * @since 3.0
808    */

809   public static boolean addLookupPassportType (Connection JavaDoc oConn, String JavaDoc sGuWorkArea, String JavaDoc sTpPassport, HashMap JavaDoc oTranslations)
810     throws SQLException JavaDoc {
811     return DBLanguages.addLookup(oConn,DB.k_contacts_lookup, sGuWorkArea, DB.tp_passport, sTpPassport, oTranslations);
812   }
813
814   /**
815    * <p>Add a Job Title lookup value</a>
816    * @param oConn Connection
817    * @param sGuWorkArea String GUID of WorkArea
818    * @param sDeTitle String Passport Type Internal Value
819    * @param oTranslations HashMap with one entry for each language
820    * @return boolean <b>true</b> if new job title was added, <b>false</b> if it already existed
821    * @throws SQLException
822    * @since 3.0
823    */

824   public static boolean addLookupJobTitle (Connection JavaDoc oConn, String JavaDoc sGuWorkArea, String JavaDoc sDeTitle, HashMap JavaDoc oTranslations)
825     throws SQLException JavaDoc {
826     return DBLanguages.addLookup(oConn,DB.k_contacts_lookup, sGuWorkArea, DB.de_title, sDeTitle, oTranslations);
827   }
828
829   // **********************************************************
830
// Constantes Publicas
831

832   public static final short ClassId = 90;
833 }
834
Popular Tags