KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.CallableStatement JavaDoc;
36 import java.sql.SQLException JavaDoc;
37 import java.sql.PreparedStatement JavaDoc;
38 import java.sql.ResultSet JavaDoc;
39 import java.sql.ResultSetMetaData JavaDoc;
40
41 import com.knowgate.debug.DebugFile;
42 import com.knowgate.jdc.JDCConnection;
43 import com.knowgate.dataobjs.DB;
44 import com.knowgate.dataobjs.DBBind;
45 import com.knowgate.dataobjs.DBPersist;
46 import com.knowgate.misc.Gadgets;
47
48 /**
49  * <p>List Member</p>
50  * <p>List Members are kept at database differently depending on the type of list
51  * they belong to.<br><br>
52  * For Members of Dynamic Lists (k_lists.tp_list=2) no member record is explicitly
53  * stored at database, but members are dynamically extracted from k_member_address
54  * view each time that the DistributionList is used.<br><br>
55  * For Members of Static Lists (k_lists.tp_list=1) a record is stored at
56  * k_x_list_members table. Members of Static Lists can be Companies from k_companies
57  * table of Contacts from k_contacts table. If Member is a Company then tp_member is
58  * set to 91 and gu_company is set to Company GUID. If Member is a Contact then
59  * tp_member is set to 90 and gu_contact is set to Contact GUID.<br><br>
60  * For Members of Direct and Black Lists (k_lists.tp_list=3 and k_lists.tp_list=4)
61  * tp_member is set to 95. One record is stored at k_x_list_members table and
62  * another record is stored at k_list_members. There is one unique record per
63  * Member at k_list_members and one record per member and list at k_x_list_members.<br><br>
64  * <b>List Member Storage Summary<b><br>
65  * <table cellpadding="4" border="1">
66  * <tr><td>List Type</td><td>k_lists.tp_member</td><td>k_x_list_member.gu_contact</td><td>k_x_list_member.gu_company</td><td>k_list_member.gu_member</td></tr>
67  * <tr><td>DYNAMIC</td><td align="middle">90 or 91</td><td align="middle">NONE</td><td align="middle">NONE</td><td align="middle">NONE</td></tr>
68  * <tr><td>STATIC</td><td align="middle">90 or 91</td><td>References k_contacts if tp_member=90</td><td align="middle">References k_companies if tp_member=91</td><td align="middle">NONE</td></tr>
69  * <tr><td>DIRECT</td><td align="middle">95</td><td>References k_list_members</td><td align="middle">NULL</td><td align="middle">Member GUID</td></tr>
70  * </table>
71  * @author Sergio Montoro Ten
72  * @version 2.0
73  */

74
75 public class ListMember {
76
77   private DBPersist oMember;
78
79   // ----------------------------------------------------------
80

81   private short getListType (JDCConnection oConn, String JavaDoc sListGUID) throws SQLException JavaDoc {
82
83     PreparedStatement JavaDoc oList;
84     ResultSet JavaDoc oRSet;
85     short iTpList;
86
87     if (DebugFile.trace) {
88       DebugFile.writeln("Begin ListMember.getListType([Connection], " + (null!=sListGUID ? sListGUID : "null") + ")");
89       DebugFile.incIdent();
90     }
91
92     if (null==sListGUID)
93       iTpList = 0;
94
95     else {
96
97       if (!oMember.getItemMap().containsKey(DB.tp_list)) {
98
99         if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(SELECT " + DB.tp_list + " FROM " + DB.k_lists + " WHERE " + DB.gu_list + "='" + (null!=sListGUID ? sListGUID : "null") + "'");
100
101         oList = oConn.prepareStatement("SELECT " + DB.tp_list + " FROM " + DB.k_lists + " WHERE " + DB.gu_list + "=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
102         oList.setString(1, sListGUID);
103
104         try { oList.setQueryTimeout(20); } catch (SQLException JavaDoc sqle) { /* ignore */}
105
106         if (DebugFile.trace) DebugFile.writeln("PreparedStatement.executeQuery()");
107
108         oRSet = oList.executeQuery();
109
110         if (oRSet.next()) {
111           iTpList = oRSet.getShort(1);
112           oMember.put(DB.tp_list, iTpList);
113         }
114         else
115           iTpList = 0;
116
117         oRSet.close();
118         oList.close();
119
120       }
121       else
122
123         iTpList = oMember.getShort(DB.tp_list);
124     } // fi (sListGUID)
125

126     if (DebugFile.trace) {
127       DebugFile.decIdent();
128       DebugFile.writeln("End ListMember.getListType() : " + String.valueOf(iTpList));
129     }
130
131     return iTpList;
132   } // getListType
133

134   // ----------------------------------------------------------
135

136   public ListMember() {
137     oMember = new DBPersist(DB.k_list_members, "ListMember");
138   }
139
140   /**
141    * Create ListMember and load its fields from database.
142    * ListMember with given GUID or e-amil is seeked at k_x_list_members table.
143    * @param oConn Database Connection
144    * @param sMemberId One of: GUID for Contact, GUID for Company or Member e-mail.
145    * @param sListGUID DistributionList GUID
146    * @throws SQLException
147    */

148   public ListMember(JDCConnection oConn, String JavaDoc sMemberId, String JavaDoc sListGUID) throws SQLException JavaDoc {
149
150     PreparedStatement JavaDoc oStmt;
151     ResultSet JavaDoc oRSet;
152     ResultSetMetaData JavaDoc oMDat;
153     Object JavaDoc oFld;
154     int iCols;
155
156     oMember = new DBPersist(DB.k_list_members, "ListMember");
157
158       if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(SELECT * FROM " + DB.k_x_list_members + " WHERE " + DB.gu_list + "='" + sListGUID + "' AND (" + DB.gu_contact + "='" + sMemberId + "' OR " + DB.gu_company + "='" + sMemberId + "' OR " + DB.tx_email + "='" + sMemberId + "'))");
159
160       oStmt = oConn.prepareStatement("SELECT * FROM " + DB.k_x_list_members + " WHERE " + DB.gu_list + "=? AND (" + DB.gu_contact + "=? OR " + DB.gu_company + "=? OR " + DB.tx_email + "=?)", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
161
162       try { oStmt.setQueryTimeout(20); } catch (SQLException JavaDoc sqle) { /* ignore */}
163
164       oStmt.setString(1, sListGUID);
165       oStmt.setString(2, sMemberId);
166       oStmt.setString(3, sMemberId);
167       oStmt.setString(4, sMemberId);
168
169       if (DebugFile.trace) DebugFile.writeln("PreparedStatement.executeQuery()");
170
171       oRSet = oStmt.executeQuery();
172
173       oMDat = oRSet.getMetaData();
174       iCols = oMDat.getColumnCount();
175
176       if (oRSet.next()) {
177         for (int c=1; c<=iCols; c++) {
178           if (!oMDat.getColumnName(c).equalsIgnoreCase(DB.dt_created)) {
179             oFld = oRSet.getObject(c);
180             if (!oRSet.wasNull())
181               oMember.put(oMDat.getColumnName(c).toLowerCase(), oFld);
182           } // fi (getColumnName(c)!=DB.dt_created)
183
} // next
184
} // fi (oRSet)
185
else {
186         if (DebugFile.trace) DebugFile.writeln("member " + sMemberId + " not found at list " + sListGUID);
187       }
188
189       oRSet.close();
190       oStmt.close();
191   }
192
193   // ----------------------------------------------------------
194

195   /**
196    * Internal DBPersist object reference for k_list_members register.
197    */

198   public DBPersist member() {
199     return oMember;
200   }
201
202   // ----------------------------------------------------------
203

204   public java.util.Date JavaDoc getDate(String JavaDoc sKey) {
205     return oMember.getDate(sKey);
206   }
207
208   // ----------------------------------------------------------
209

210   public String JavaDoc getDateFormated(String JavaDoc sKey, String JavaDoc sFormat) {
211     return oMember.getDateFormated(sKey, sFormat);
212   }
213
214   // ----------------------------------------------------------
215

216   public short getShort(String JavaDoc sKey) throws java.lang.NullPointerException JavaDoc {
217     return oMember.getShort(sKey);
218   }
219
220   // ----------------------------------------------------------
221

222   public String JavaDoc getString(String JavaDoc sKey) throws java.lang.NullPointerException JavaDoc {
223     return oMember.getString(sKey);
224   }
225
226   // ----------------------------------------------------------
227

228   public String JavaDoc getStringNull(String JavaDoc sKey, String JavaDoc sDefault) {
229     return oMember.getStringNull(sKey, sDefault);
230   }
231
232   // ----------------------------------------------------------
233

234   public boolean isNull(String JavaDoc sColumnName) {
235     return oMember.isNull(sColumnName);
236   }
237
238   // ----------------------------------------------------------
239

240   public void put(String JavaDoc sKey, String JavaDoc sVal) {
241     oMember.put(sKey, sVal);
242   }
243
244   // ----------------------------------------------------------
245

246   public void put(String JavaDoc sKey, short iVal) {
247     oMember.put(sKey, iVal);
248   }
249
250   // ----------------------------------------------------------
251

252   public void put(String JavaDoc sKey, java.util.Date JavaDoc dtVal) {
253     oMember.put(sKey, dtVal);
254   }
255
256   // ----------------------------------------------------------
257

258   public void put(String JavaDoc sKey, Object JavaDoc oObj) {
259     oMember.put(sKey, oObj);
260   }
261
262   // ----------------------------------------------------------
263

264   public void replace(String JavaDoc sKey, Object JavaDoc oObj) {
265     oMember.replace(sKey,oObj);
266   }
267
268   // ----------------------------------------------------------
269

270   public void replace(String JavaDoc sKey, short iVal) {
271     oMember.replace(sKey,iVal);
272   }
273
274   // ----------------------------------------------------------
275

276   public void remove(String JavaDoc sKey) {
277     oMember.remove(sKey);
278   }
279
280
281   // ----------------------------------------------------------
282

283   /**
284    * <p>Get whether or not a List Member is Blocked</p>
285    * Blocked Members are those present on the associated black list.<br>
286    * Companies and Contacts are considered to be in the black list if its
287    * gu_company or gu_contact is into black list register at k_x_list_members<br>
288    * Direct List Members are considered to be in the black list if its
289    * tx_email is into black list register at k_x_list_members<br>
290    * This way, by searching Companies and Contacts by GUID and Direct Members by
291    * e-mail, a Contact or Company may have several e-mail addresses all of them
292    * blocked by a single black list entry.<br>
293    * This method calls stored procedures: k_sp_company_blocked, k_sp_contact_blocked and k_sp_email_blocked
294    * @param oConn Database Connection
295    * @return <b>true</b> if member is blocked (present at associated black list for this list) <b>false</b> otherwise.
296    * @throws SQLException
297    */

298   public boolean isBlocked (JDCConnection oConn) throws SQLException JavaDoc {
299     boolean bBlocked;
300     CallableStatement JavaDoc oCall;
301     PreparedStatement JavaDoc oStmt;
302     ResultSet JavaDoc oRSet;
303     String JavaDoc sList;
304     String JavaDoc sProc;
305     String JavaDoc sParm;
306
307     if (DebugFile.trace) {
308       DebugFile.writeln("Begin ListMember.isBlocked([Connection])");
309       DebugFile.incIdent();
310     }
311
312     sList = oMember.getString(DB.gu_list);
313
314     switch (oMember.getShort(DB.tp_member)) {
315       case Company.ClassId:
316         sProc = "k_sp_company_blocked";
317         sParm = oMember.getString(DB.gu_company);
318         if (DebugFile.trace) DebugFile.writeln("gu_company=" + sParm);
319         break;
320       case Contact.ClassId:
321         sProc = "k_sp_contact_blocked";
322         sParm = oMember.getString(DB.gu_contact);
323         if (DebugFile.trace) DebugFile.writeln("gu_contact=" + sParm);
324         break;
325       default:
326         sProc = "k_sp_email_blocked";
327         sParm = oMember.getString(DB.tx_email);
328         if (DebugFile.trace) DebugFile.writeln("tx_email=" + sParm);
329     }
330
331     switch (oConn.getDataBaseProduct()) {
332
333       case JDCConnection.DBMS_POSTGRESQL:
334         if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement(SELECT " + sProc + "(?,?))");
335
336         oStmt = oConn.prepareStatement("SELECT " + sProc + "(?,?)", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
337         oStmt.setString(1, sList);
338         oStmt.setString(2, sParm);
339         oRSet = oStmt.executeQuery();
340         oRSet.next();
341         bBlocked = (oRSet.getShort(1)!=(short)0);
342         oRSet.close();
343         oStmt.close();
344         break;
345
346       case JDCConnection.DBMS_ORACLE:
347         if (DebugFile.trace) DebugFile.writeln("Connection.prepareCall({ call " + sProc + "(?,?,?)})");
348
349         oCall = oConn.prepareCall("{ call " + sProc + "(?,?,?)}");
350         oCall.setString(1, sList);
351         oCall.setString(2, sParm);
352         oCall.registerOutParameter(3, java.sql.Types.DECIMAL);
353         oCall.execute();
354         bBlocked = (oCall.getBigDecimal(3).intValue()!=0);
355         oCall.close();
356         break;
357
358       default:
359         if (DebugFile.trace) DebugFile.writeln("Connection.prepareCall({ call " + sProc + "(?,?,?)})");
360
361         oCall = oConn.prepareCall("{ call " + sProc + "(?,?,?)}");
362         oCall.setString(1, sList);
363         oCall.setString(2, sParm);
364         oCall.registerOutParameter(3, java.sql.Types.SMALLINT);
365         oCall.execute();
366         bBlocked = (oCall.getShort(3)!=(short)0);
367         oCall.close();
368     }
369
370     if (DebugFile.trace) {
371       DebugFile.decIdent();
372       DebugFile.writeln("End ListMember.isBlocked() : " + String.valueOf(bBlocked));
373     }
374
375     return bBlocked;
376   }
377
378   // ----------------------------------------------------------
379

380   /**
381    * <p>Remove Member from a DistributionList.</p>
382    * Member is also removed from associated black list.
383    * @param oConn Database Connection
384    * @param sListId GUID of DistributionList from witch Member is to be removed.
385    * @throws SQLException
386    */

387   public boolean delete(JDCConnection oConn, String JavaDoc sListId) throws SQLException JavaDoc {
388     PreparedStatement JavaDoc oDlte;
389     PreparedStatement JavaDoc oStmt;
390     ResultSet JavaDoc oRSet;
391     boolean bRetVal;
392     String JavaDoc sBlackList;
393
394     if (DebugFile.trace) {
395       DebugFile.writeln("Begin ListMember.delete([Connection], " + sListId + ")");
396       DebugFile.incIdent();
397     }
398
399     // Find associated Black List
400

401     if (DebugFile.trace)
402       DebugFile.writeln("Connection.prepareStatement(SELECT " + DB.gu_list + " FROM " + DB.k_lists + " WHERE " + DB.gu_query + "='" + sListId + "' AND " + DB.tp_list + "=" + String.valueOf(DistributionList.TYPE_BLACK) + ")");
403
404     oStmt = oConn.prepareStatement("SELECT " + DB.gu_list + " FROM " + DB.k_lists + " WHERE " + DB.gu_query + "=? AND " + DB.tp_list + "=" + String.valueOf(DistributionList.TYPE_BLACK), ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
405     oStmt.setString(1, sListId);
406     oRSet = oStmt.executeQuery();
407     if (oRSet.next())
408       sBlackList = oRSet.getString(1);
409     else
410       sBlackList = null;
411     oRSet.close();
412     oStmt.close();
413
414     // Delete Member from Black List
415

416     if (null!=sBlackList) {
417       if (DebugFile.trace)
418         DebugFile.writeln("Connection.prepareStatement(DELETE FROM " + DB.k_x_list_members + " WHERE " + DB.gu_list + "='" + sBlackList + "' AND (" + DB.gu_contact + "='" + oMember.getStringNull(DB.gu_member,"null") + "' OR " + DB.gu_company + "='" + oMember.getStringNull(DB.gu_member,"null") + "' OR " + DB.tx_email + "='" + oMember.getStringNull(DB.tx_email,"null") + "'))");
419
420       oDlte = oConn.prepareStatement("DELETE FROM " + DB.k_x_list_members + " WHERE " + DB.gu_list + "=? AND (" + DB.gu_contact + "=? OR " + DB.gu_company + "=? OR " + DB.tx_email + "=?)");
421
422       oDlte.setString(1, sBlackList);
423       oDlte.setString(2, oMember.getString(DB.gu_member));
424       oDlte.setString(3, oMember.getString(DB.gu_member));
425       oDlte.setString(4, oMember.getString(DB.tx_email));
426
427       if (DebugFile.trace) DebugFile.writeln("PreparedStatement.executeUpdate()");
428
429       bRetVal = oDlte.executeUpdate()>0;
430       oDlte.close();
431     }
432
433     // Delete Member from List
434

435     if (DebugFile.trace)
436       DebugFile.writeln("Connection.prepareStatement(DELETE FROM " + DB.k_x_list_members + " WHERE " + DB.gu_list + "='" + sListId + "' AND (" + DB.gu_contact + "='" + oMember.getStringNull(DB.gu_member,"null") + "' OR " + DB.gu_company + "='" + oMember.getStringNull(DB.gu_member,"null") + "' OR " + DB.tx_email + "='" + oMember.getStringNull(DB.tx_email,"null") + "'))");
437
438     oDlte = oConn.prepareStatement("DELETE FROM " + DB.k_x_list_members + " WHERE " + DB.gu_list + "=? AND (" + DB.gu_contact + "=? OR " + DB.gu_company + "=? OR " + DB.tx_email + "=?)");
439
440     oDlte.setString(1, sListId);
441     oDlte.setString(2, oMember.getString(DB.gu_member));
442     oDlte.setString(3, oMember.getString(DB.gu_member));
443     oDlte.setString(4, oMember.getString(DB.tx_email));
444
445     if (DebugFile.trace) DebugFile.writeln("PreparedStatement.executeUpdate()");
446
447     bRetVal = oDlte.executeUpdate()>0;
448     oDlte.close();
449
450     int iTpList = getListType(oConn, sListId);
451
452     if (DistributionList.TYPE_DIRECT==iTpList || DistributionList.TYPE_BLACK==iTpList)
453       bRetVal = oMember.delete(oConn);
454
455     if (DebugFile.trace) {
456       DebugFile.decIdent();
457       DebugFile.writeln("End ListMember.delete() : " + String.valueOf(bRetVal));
458     }
459
460     return bRetVal;
461   } // delete
462

463   // ----------------------------------------------------------
464

465   /**
466    * Remove Member from all DistributionList.
467    * @param oConn Database Connection
468    * @throws SQLException
469    */

470   public boolean delete(JDCConnection oConn) throws SQLException JavaDoc {
471     PreparedStatement JavaDoc oDlte;
472     boolean bRetVal;
473
474     if (DebugFile.trace) {
475       DebugFile.writeln("Begin ListMember.delete([Connection])");
476       DebugFile.incIdent();
477     }
478
479     oDlte = oConn.prepareStatement("DELETE FROM " + DB.k_x_list_members + " WHERE " + DB.gu_contact + "=? OR " + DB.gu_company + "=?");
480     oDlte.setString(1, oMember.getString(DB.gu_member));
481     oDlte.setString(2, oMember.getString(DB.gu_member));
482     oDlte.execute();
483     oDlte.close();
484
485     bRetVal = oMember.delete(oConn);
486
487     if (DebugFile.trace) {
488       DebugFile.decIdent();
489       DebugFile.writeln("End ListMember.delete() : " + String.valueOf(bRetVal));
490     }
491
492     return bRetVal;
493   } // delete
494

495   // ----------------------------------------------------------
496

497   /**
498    * <p>Store Member at a DistributionList</p>
499    * <p>Automatically generates gu_member GUID for Direct List Members and dt_modified DATE if not explicitly set.</p>
500    * @param oConn Database Connection
501    * @param sListGUID GUID of Distribution List
502    * @throws ClassCastException If sListId type is DYNAMIC.
503    * @throws NoSuchFieldException If List is Static and gu_member field is not set
504    * @throws SQLException
505    */

506
507   public boolean store(JDCConnection oConn, String JavaDoc sListGUID)
508     throws ClassCastException JavaDoc,NoSuchFieldException JavaDoc,SQLException JavaDoc {
509     boolean bRetVal;
510     String JavaDoc sSQL;
511     java.sql.Timestamp JavaDoc dtNow = new java.sql.Timestamp JavaDoc(DBBind.getTime());
512     PreparedStatement JavaDoc oStmt;
513     int iAffected;
514
515     if (DebugFile.trace) {
516       DebugFile.writeln("Begin ListMember.store([Connection])");
517       DebugFile.incIdent();
518     }
519
520     int iTpList = getListType(oConn, sListGUID);
521
522     if (iTpList==DistributionList.TYPE_DYNAMIC)
523       throw new ClassCastException JavaDoc("Dynamic Distribution Lists cannot have additional Members directly added");
524
525     if (!oMember.getItemMap().containsKey(DB.gu_member)) {
526       if (iTpList==DistributionList.TYPE_STATIC)
527         throw new NoSuchFieldException JavaDoc("gu_member field must be set before storing a Member from a Static Distribution List");
528
529       oMember.put(DB.gu_member, Gadgets.generateUUID());
530     }
531
532     // Forzar la fecha de modificación del registro
533
oMember.replace(DB.dt_modified, dtNow);
534
535     if (DistributionList.TYPE_DIRECT==iTpList || DistributionList.TYPE_BLACK==iTpList)
536       bRetVal = oMember.store(oConn);
537
538     if (DebugFile.trace) {
539       String JavaDoc sActive;
540       if (oMember.getItemMap().containsKey(DB.bo_active))
541         sActive = String.valueOf(oMember.getShort(DB.bo_active));
542       else
543         sActive = "null";
544
545       sSQL = "UPDATE " + DB.k_x_list_members + " SET " + DB.tx_email + "='" + oMember.getStringNull(DB.tx_email,"null") + "'," + DB.tx_name + "=?," + DB.tx_surname + "=?," + DB.tx_salutation + "=?,"+ DB.bo_active + "=" + sActive + "," + DB.id_format + "='" + oMember.getStringNull(DB.id_format, "TXT") + "'," + DB.dt_modified + "=" + DBBind.escape(new java.util.Date JavaDoc(), "ts") + " WHERE " + DB.gu_list + "='" + sListGUID + "' AND (" + DB.gu_contact + "='" + oMember.getStringNull(DB.gu_member,"null") + "' OR " + DB.gu_company + "=? OR " + DB.tx_email + "='" + oMember.getStringNull(DB.tx_email,"null") + "')";
546
547       DebugFile.writeln("Connection.prepareStatement(" + sSQL + ")");
548     }
549
550     sSQL = "UPDATE " + DB.k_x_list_members + " SET " + DB.tx_email + "=?," + DB.tx_name + "=?," + DB.tx_surname + "=?," + DB.tx_salutation + "=?,"+ DB.bo_active + "=?," + DB.id_format + "=?," + DB.dt_modified + "=" + DBBind.escape(new java.util.Date JavaDoc(), "ts") + " WHERE " + DB.gu_list + "=? AND (" + DB.gu_contact + "=? OR " + DB.gu_company + "=? OR " + DB.tx_email + "=?)";
551
552     oStmt = oConn.prepareStatement(sSQL);
553
554     oStmt.setString(1, oMember.getString(DB.tx_email));
555     oStmt.setString(2, oMember.getStringNull(DB.tx_name, null));
556     oStmt.setString(3, oMember.getStringNull(DB.tx_surname, null));
557     oStmt.setString(4, oMember.getStringNull(DB.tx_salutation, null));
558
559     if (oMember.getItemMap().containsKey(DB.bo_active))
560       oStmt.setShort (5, oMember.getShort(DB.bo_active));
561     else
562       oStmt.setShort (5, (short)1);
563
564     oStmt.setString(6, oMember.getStringNull(DB.id_format, "TXT"));
565
566     oStmt.setString(7, sListGUID);
567
568     if (oMember.getItemMap().containsKey(DB.tp_member)) {
569
570       if (DebugFile.trace) DebugFile.writeln("tp_member=" + String.valueOf(oMember.getShort(DB.tp_member)));
571
572       if (oMember.getShort(DB.tp_member)==Company.ClassId) {
573
574         if (DebugFile.trace)
575           DebugFile.writeln("gu_contact=" + oMember.getStringNull(DB.gu_member,"null") + " , gu_company=null");
576
577         oStmt.setString(8, oMember.getString(DB.gu_member));
578         oStmt.setString(9, null);
579       }
580       else {
581         if (DebugFile.trace)
582           DebugFile.writeln("gu_contact=null, gu_company=" + oMember.getStringNull(DB.gu_member,"null"));
583
584         oStmt.setString(8, null);
585         oStmt.setString(9, oMember.getString(DB.gu_member));
586       }
587     }
588     else {
589       if (DebugFile.trace) DebugFile.writeln("tp_member not set");
590
591       if (DebugFile.trace)
592         DebugFile.writeln("gu_contact=null, gu_company=" + oMember.getStringNull(DB.gu_member,"null"));
593
594       oStmt.setString(8, null);
595       oStmt.setString(9, oMember.getString(DB.gu_member));
596     }
597
598     oStmt.setString(10, oMember.getString(DB.tx_email));
599
600     if (DebugFile.trace) DebugFile.writeln("PreparedStatement.executeUpdate()");
601
602     iAffected = oStmt.executeUpdate();
603
604     if (DebugFile.trace) DebugFile.writeln("affected rows = " + String.valueOf(iAffected));
605
606     oStmt.close();
607
608     if (0==iAffected) {
609
610       if (DebugFile.trace) {
611         sSQL = "INSERT INTO " + DB.k_x_list_members + " (" + DB.gu_list + "," + DB.tx_email + "," + DB.tx_name + "," + DB.tx_surname + "," + DB.tx_salutation + "," + DB.bo_active + "," + DB.tp_member + "," + DB.gu_company + "," + DB.gu_contact + "," + DB.id_format + ") VALUES ('" + sListGUID + "','" + oMember.getStringNull(DB.tx_email,"null") + "',?,?,?,?,?,?,'" + oMember.getStringNull(DB.gu_member,"null") + "','" + oMember.getStringNull(DB.id_format,"TXT") + "')";
612
613         DebugFile.writeln("Connection.prepareStatement(" + sSQL + ")");
614       }
615
616       sSQL = "INSERT INTO " + DB.k_x_list_members + " (" + DB.gu_list + "," + DB.tx_email + "," + DB.tx_name + "," + DB.tx_surname + "," + DB.tx_salutation + "," + DB.bo_active + "," + DB.tp_member + "," + DB.gu_company + "," + DB.gu_contact + "," + DB.id_format + ") VALUES (?,?,?,?,?,?,?,?,?,?)";
617
618       oStmt = oConn.prepareStatement(sSQL);
619       oStmt.setString(1, sListGUID);
620       oStmt.setString(2, oMember.getString(DB.tx_email));
621       oStmt.setString(3, oMember.getStringNull(DB.tx_name,null));
622       oStmt.setString(4, oMember.getStringNull(DB.tx_surname,null));
623       oStmt.setString(5, oMember.getStringNull(DB.tx_salutation,null));
624
625       if (oMember.getItemMap().containsKey(DB.bo_active))
626         oStmt.setShort (6, oMember.getShort(DB.bo_active));
627       else
628         oStmt.setShort (6, (short)1);
629
630       if (oMember.getItemMap().containsKey(DB.tp_member)) {
631         if (DebugFile.trace) DebugFile.writeln ("member type is " + String.valueOf(oMember.getShort(DB.tp_member)));
632
633         oStmt.setShort(7, oMember.getShort(DB.tp_member));
634
635         if (oMember.getShort(DB.tp_member)==Company.ClassId) {
636           oStmt.setString(8, oMember.getString(DB.gu_member));
637           oStmt.setString(9, null);
638         }
639         else {
640           oStmt.setString(8, null);
641           oStmt.setString(9, oMember.getString(DB.gu_member));
642         }
643       }
644       else {
645
646         if (new Contact(getString(DB.gu_member)).exists(oConn)) {
647           if (DebugFile.trace) DebugFile.writeln("member type automatically set to " + String.valueOf(Contact.ClassId));
648
649           oStmt.setShort (7, Contact.ClassId);
650           oStmt.setString(8, null);
651           oStmt.setString(9, oMember.getString(DB.gu_member));
652         }
653         else if (new Company(getString(DB.gu_member)).exists(oConn)) {
654           if (DebugFile.trace) DebugFile.writeln("member type automatically set to " + String.valueOf(Company.ClassId));
655
656           oStmt.setShort (7, Company.ClassId);
657           oStmt.setString(8, oMember.getString(DB.gu_member));
658           oStmt.setString(9, null);
659         }
660         else {
661           if (DebugFile.trace) DebugFile.writeln("member type automatically set to " + String.valueOf(ListMember.ClassId));
662
663           oStmt.setShort (7, ListMember.ClassId);
664           oStmt.setString(8, null);
665           oStmt.setString(9, oMember.getString(DB.gu_member));
666         }
667       }
668
669       oStmt.setString(10, oMember.getStringNull(DB.id_format,"TXT"));
670
671       if (DebugFile.trace) DebugFile.writeln("PreparedStatement.execute()");
672
673       bRetVal = oStmt.execute();
674
675       oStmt.close();
676     }
677     else
678       bRetVal = true;
679
680     if (DebugFile.trace) {
681       DebugFile.decIdent();
682       DebugFile.writeln("End ListMember.store() : " + String.valueOf(bRetVal));
683     }
684
685     return bRetVal;
686   } // store()
687

688   /**
689    * <p>Block Member</p>
690    * <p>Add member to Black List associated to Base List.</p>
691    * @param oConn Database Connection
692    * @param sListGUID Base List GUID
693    * @throws SQLException
694    * @throws NoSuchFieldException
695    */

696
697   public void block (JDCConnection oConn, String JavaDoc sListGUID)
698     throws SQLException JavaDoc, NoSuchFieldException JavaDoc {
699
700     if (DebugFile.trace) {
701       DebugFile.writeln("Begin ListMember.block([Connection], " + sListGUID + ")");
702       DebugFile.incIdent();
703     }
704
705     DistributionList oList = new DistributionList(oConn, sListGUID);
706
707     String JavaDoc sBlack = oList.blackList (oConn);
708
709     if (null==sBlack) {
710       sBlack = Gadgets.generateUUID();
711
712       if (DebugFile.trace)
713         DebugFile.writeln("Connection.prepareStatement(INSERT INTO " + DB.k_lists + "(" + DB.gu_list + "," + DB.gu_workarea + "," + DB.tp_list + "," + DB.gu_query + "," + DB.de_list + ") VALUES('" + sBlack + "','" + getStringNull(DB.gu_workarea,"null") + "',4,'" + getStringNull(DB.gu_list,"null") + "','" + getStringNull(DB.de_list,"null") + "'))");
714
715       PreparedStatement JavaDoc oStmt = oConn.prepareStatement("INSERT INTO " + DB.k_lists + "(" + DB.gu_list + "," + DB.gu_workarea + "," + DB.tp_list + "," + DB.gu_query + "," + DB.de_list + ") VALUES(?,?,?,?,?)");
716       oStmt.setString(1, sBlack);
717       oStmt.setString(2, oList.getString(DB.gu_workarea));
718       oStmt.setShort (3, DistributionList.TYPE_BLACK);
719       oStmt.setString(4, sListGUID);
720       oStmt.setString(5, oList.getString(DB.de_list));
721       oStmt.executeUpdate();
722       oStmt.close();
723     }
724
725     store (oConn, sBlack);
726
727     if (DebugFile.trace) {
728       DebugFile.decIdent();
729       DebugFile.writeln("End ListMember.block()");
730     }
731   }
732
733   /**
734    * <p>Unblock Member</p>
735    * <p>Remove member from Black List associated to Base List.</p>
736    * @param oConn Database Connection
737    * @param sListGUID Base List GUID
738    * @throws SQLException
739    */

740
741   public void unblock (JDCConnection oConn, String JavaDoc sListGUID)
742     throws SQLException JavaDoc {
743
744     if (DebugFile.trace) {
745       DebugFile.writeln("Begin ListMember.unblock([Connection], " + sListGUID + ")");
746       DebugFile.incIdent();
747     }
748
749     DistributionList oList = new DistributionList(oConn, sListGUID);
750
751     String JavaDoc sBlack = oList.blackList (oConn);
752
753     if (null!=sBlack) {
754       if (DebugFile.trace)
755         DebugFile.writeln("Connection.prepareStatement(DELETE FROM " + DB.k_x_list_members + " WHERE " + DB.gu_list + "='" + sBlack + "' AND " + DB.tx_email + "='" + getStringNull(DB.tx_email,"null")+ "')");
756
757       PreparedStatement JavaDoc oDlte = oConn.prepareStatement("DELETE FROM " + DB.k_x_list_members + " WHERE " + DB.gu_list + "=? AND " + DB.tx_email + "=?");
758       oDlte.setString (1, sBlack);
759       oDlte.setString (2, getString(DB.tx_email));
760       oDlte.executeUpdate();
761       oDlte.close();
762     }
763
764     if (DebugFile.trace) {
765       DebugFile.decIdent();
766       DebugFile.writeln("End ListMember.unblock()");
767     }
768   } // unblock
769

770
771   // **********************************************************
772
// Constantes Publicas
773

774   public static final short ClassId = 95;
775
776 }
Popular Tags