KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > hipermail > DBInetAddr


1 /*
2   Copyright (C) 2004 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.hipermail;
34
35 import java.io.File JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.UnsupportedEncodingException JavaDoc;
38
39 import java.sql.SQLException JavaDoc;
40 import java.sql.CallableStatement JavaDoc;
41 import java.sql.PreparedStatement JavaDoc;
42 import java.sql.ResultSet JavaDoc;
43 import java.sql.Types JavaDoc;
44
45 import javax.mail.Address JavaDoc;
46 import javax.mail.Message JavaDoc;
47 import javax.mail.internet.AddressException JavaDoc;
48
49 import com.knowgate.debug.DebugFile;
50 import com.knowgate.jdc.JDCConnection;
51 import com.knowgate.dataobjs.DB;
52 import com.knowgate.dataobjs.DBPersist;
53
54 import javax.mail.internet.InternetAddress JavaDoc;
55
56 /**
57  * <p>Internet Address register from k_inet_addrs</p>
58  * @author Sergio Montoro Ten
59  * @version 2.1
60  */

61
62 public class DBInetAddr extends InternetAddress JavaDoc {
63   private DBPersist oAddr;
64
65   public DBInetAddr(String JavaDoc sMsgGUID, int iPart) {
66
67     oAddr = new DBPersist(DB.k_inet_addrs, "InetAddrDB");
68
69     oAddr.put(DB.gu_mimemsg, sMsgGUID);
70     oAddr.put(DB.id_part, iPart);
71   }
72
73   /**
74    * Constructor
75    * @param sMsgGUID Message GUID
76    * @param sMsgId Mime Message Identifier
77    * @param sTxEMail Mail address
78    * @param sTxPersonal Address displyed name
79    * @param sTpRecipient Recipient type { from, to, cc, bcc } case sensitive
80    * @param sGuUser GUID of User which tx_main_email is the same as this address
81    * @param sGuContact GUID of Contact which tx_main_email is the same as this address
82    * @param sGuCompany GUID of Copany which tx_main_email is the same as this address
83    * @throws NullPointerException If sMsgGUID or sMsgId or sTxEMail or sTpRecipient is <b>null</b>.
84    * @throws IllegalArgumentException If sTpRecipient is not one of { from, to, cc, bcc }
85    */

86   public DBInetAddr(String JavaDoc sMsgGUID, String JavaDoc sMsgId, String JavaDoc sTxEMail,
87                     String JavaDoc sTxPersonal, String JavaDoc sTpRecipient,
88                     String JavaDoc sGuUser, String JavaDoc sGuContact, String JavaDoc sGuCompany)
89     throws NullPointerException JavaDoc, IllegalArgumentException JavaDoc {
90
91     if (null==sMsgGUID) throw new NullPointerException JavaDoc("DBInetAddr message GUID cannot be null");
92     if (null==sMsgId) throw new NullPointerException JavaDoc("DBInetAddr message identifier cannot be null");
93     if (null==sTxEMail) throw new NullPointerException JavaDoc("DBInetAddr mail address cannot be null");
94     if (null==sTpRecipient) throw new NullPointerException JavaDoc("DBInetAddr recipient type cannot be null");
95
96     if (!sTpRecipient.equals("from") && !sTpRecipient.equals("to") && !!sTpRecipient.equals("cc") && !!sTpRecipient.equals("bcc"))
97       throw new java.lang.IllegalArgumentException JavaDoc("Recipient type must be either from, to, cc or bcc");
98
99     oAddr = new DBPersist(DB.k_inet_addrs, "InetAddrDB");
100
101     oAddr.put(DB.gu_mimemsg, sMsgGUID);
102     oAddr.put(DB.id_message, sMsgId);
103     oAddr.put(DB.tx_email, sTxEMail);
104     oAddr.put(DB.tp_recipient, sTpRecipient);
105
106     if (null!=sTxPersonal) oAddr.put(DB.tx_personal, sTxPersonal);
107     if (null!=sGuUser) oAddr.put(DB.gu_user, sGuUser);
108     if (null!=sGuContact) oAddr.put(DB.gu_contact, sGuContact);
109     if (null!=sGuCompany) oAddr.put(DB.gu_company, sGuCompany);
110   }
111
112   // ---------------------------------------------------------------------------
113

114   /**
115    * Get e-mail address
116    * @return String
117    */

118   public String JavaDoc getAddress () { return oAddr.getStringNull (DB.tx_email, null); }
119
120   // ---------------------------------------------------------------------------
121

122   public String JavaDoc getString (String JavaDoc sKey) { return oAddr.getString (sKey); }
123
124   // ---------------------------------------------------------------------------
125

126   public String JavaDoc getStringNull (String JavaDoc sKey, String JavaDoc sDefault) { return oAddr.getStringNull (sKey, sDefault); }
127
128   // ---------------------------------------------------------------------------
129

130   /**
131    * Two addresses are equal if they hold the same e-mail
132    * @param oOtherAddr DBInetAddr
133    * @return boolean <b>true</b> if e-mail of this is equal to e-mail of oOtherAddr (case insensitive comparison)
134    * @throws ClassCastException if oOtherAddr is not of type DBInetAddr
135    */

136   public boolean equals (Object JavaDoc oOtherAddr)
137     throws ClassCastException JavaDoc {
138     DBInetAddr oAddr2 = (DBInetAddr) oOtherAddr;
139
140     if (getAddress()==null || oAddr2.getAddress()==null)
141       return false;
142     else
143       return getAddress().equalsIgnoreCase(oAddr2.getAddress());
144   }
145
146   // ---------------------------------------------------------------------------
147

148   /**
149    *
150    * @return String "rfc822"
151    */

152   public String JavaDoc getType () { return "rfc822"; }
153
154   // ---------------------------------------------------------------------------
155

156   /**
157    * Get display (personal) name
158    * @return String
159    */

160   public String JavaDoc getPersonal () { return oAddr.getStringNull (DB.tx_personal, null); }
161
162
163   // ---------------------------------------------------------------------------
164

165   /**
166    * This method is unsupported and will always throw an exception when called
167    * @throws UnsupportedOperationException
168    */

169   public boolean store (JDCConnection oConn) throws UnsupportedOperationException JavaDoc {
170     if (true) throw new UnsupportedOperationException JavaDoc("Method InetAddrDB.store() is not supported");
171     return false;
172   }
173
174   // ---------------------------------------------------------------------------
175

176   /**
177    * <p>Write address resolving e-mails to contact and company GUIDs</p>
178    * This method writes to k_inet_addr table but it first lookup the given e-mail
179    * at table k_member_address and fills gu_contact and gu_company fields of
180    * k_inet_addr if there is a contact or company at the specified workarea that
181    * has that e-mail
182    * @param oConn JDCConnection
183    * @param iDomainId int Domain Identifier
184    * @param sWorkAreaId String WorkArea GUID (search for matches will be restricted to this WorkArea)
185    * @param sGuMimeMsg String Message GUID
186    * @param sIdMimeMsg String Message Identifier
187    * @param sTxEMail String e-mail
188    * @param sTpRecipient String One of {to, cc, bcc}
189    * @param sTxPersonal String Display Name
190    * @return boolean <b>true</b>
191    * @throws SQLException
192    */

193   public static boolean write (JDCConnection oConn, int iDomainId, String JavaDoc sWorkAreaId,
194                                String JavaDoc sGuMimeMsg, String JavaDoc sIdMimeMsg, String JavaDoc sTxEMail,
195                                String JavaDoc sTpRecipient, String JavaDoc sTxPersonal)
196     throws SQLException JavaDoc {
197     if (DebugFile.trace) {
198       DebugFile.writeln("Begin DBInetAddr.write([Connection], " + String.valueOf(iDomainId) + "," + sWorkAreaId + "," +
199                         sGuMimeMsg + "," + sIdMimeMsg + "," + sTxEMail + "," + sTpRecipient + "," + sTxPersonal + ")");
200       DebugFile.incIdent();
201     }
202     if (oConn.getDataBaseProduct()==JDCConnection.DBMS_POSTGRESQL) {
203       if (DebugFile.trace) DebugFile.writeln("SELECT k_sp_write_inet_addr ("+String.valueOf(iDomainId)+",'"+sWorkAreaId+"',"+(sGuMimeMsg==null ? "null" : "'"+sGuMimeMsg+"'")+",'"+sIdMimeMsg+"','"+sTxEMail+"','"+sTpRecipient+"','"+(sTxPersonal==null ? "null" : "'"+sTxPersonal+"'")+"')");
204       PreparedStatement JavaDoc oStmt = oConn.prepareStatement("SELECT k_sp_write_inet_addr (?,?,?,?,?,?,?)");
205       oStmt.setInt (1, iDomainId);
206       oStmt.setString(2, sWorkAreaId);
207       oStmt.setString(3, sGuMimeMsg);
208       oStmt.setString(4, sIdMimeMsg);
209       oStmt.setString(5, sTxEMail);
210       oStmt.setString(6, sTpRecipient);
211       if (sTxPersonal!=null)
212         oStmt.setString(7, sTxPersonal);
213       else
214         oStmt.setNull(7, Types.VARCHAR);
215       if (DebugFile.trace) DebugFile.writeln("PreparedStatement.executeQuery()");
216       oStmt.executeQuery();
217       oStmt.close();
218     }
219     else if (oConn.getDataBaseProduct()==JDCConnection.DBMS_ORACLE) {
220       if (DebugFile.trace) DebugFile.writeln("{call K_SP_WRITE_INET_ADDR (?,?,?,?,?,?,?)}");
221       CallableStatement JavaDoc oCall = oConn.prepareCall("{call K_SP_WRITE_INET_ADDR (?,?,?,?,?,?,?)}");
222       oCall.setBigDecimal(1 , new java.math.BigDecimal JavaDoc(iDomainId));
223       oCall.setString(2, sWorkAreaId);
224       oCall.setString(3, sGuMimeMsg);
225       oCall.setString(4, sIdMimeMsg);
226       if (sTpRecipient!=null)
227         oCall.setString(5, sTpRecipient);
228       else
229         oCall.setNull(5, Types.VARCHAR);
230       oCall.setString(6, sTxEMail);
231       if (sTxPersonal!=null)
232         oCall.setString(7, sTxPersonal);
233       else
234         oCall.setNull(7, Types.VARCHAR);
235       if (DebugFile.trace) DebugFile.writeln("CallableStatement.execute()");
236       oCall.execute();
237       oCall.close();
238     }
239     else {
240       if (DebugFile.trace) DebugFile.writeln("{call k_sp_write_inet_addr (?,?,?,?,?,?,?)}");
241       CallableStatement JavaDoc oCall = oConn.prepareCall("{call k_sp_write_inet_addr (?,?,?,?,?,?,?)}");
242       oCall.setInt (1, iDomainId);
243       oCall.setString(2, sWorkAreaId);
244       oCall.setString(3, sGuMimeMsg);
245       oCall.setString(4, sIdMimeMsg);
246       oCall.setString(5, sTpRecipient);
247       oCall.setString(6, sTxEMail);
248       if (sTxPersonal!=null)
249         oCall.setString(7, sTxPersonal);
250       else
251         oCall.setNull(7, Types.VARCHAR);
252       if (DebugFile.trace) DebugFile.writeln("CallableStatement.execute()");
253       oCall.execute();
254       oCall.close();
255     }
256     if (DebugFile.trace) {
257       DebugFile.decIdent();
258       DebugFile.writeln("End DBInetAddr.write()");
259     }
260     return true;
261   } // write
262

263   // ---------------------------------------------------------------------------
264

265   /**
266    * <p>Forward call to static method DBInetAddr.write()</p>
267    * @param oConn JDCConnection
268    * @param iDomainId int Domain Identifier
269    * @param sWorkAreaId String WorkArea GUID (search for matches will be restricted to this WorkArea)
270    * @return boolean <b>true</b>
271    * @throws SQLException
272    */

273   public boolean write (JDCConnection oConn, int iDomainId, String JavaDoc sWorkAreaId) throws SQLException JavaDoc {
274     if (DebugFile.trace) {
275       DebugFile.writeln("Begin DBInetAddrDB.write([Connection], " + String.valueOf(iDomainId) + "," + sWorkAreaId + ")");
276       DebugFile.incIdent();
277     }
278     boolean bRetVal =
279     DBInetAddr.write(oConn,iDomainId,sWorkAreaId,oAddr.getString(DB.gu_mimemsg),
280                      oAddr.getStringNull(DB.id_message,null),
281                      oAddr.getString(DB.tx_email),oAddr.getString(DB.tp_recipient),
282                      oAddr.getStringNull(DB.tx_personal,null));
283     if (DebugFile.trace) {
284       DebugFile.decIdent();
285       DebugFile.writeln("End InetAddrDB.write()");
286     }
287     return bRetVal;
288   } // write
289

290   // ---------------------------------------------------------------------------
291

292   /**
293    * Parse address of the form (Name) <user@domain.com>, <user@domain.com> (Name),
294    * "Name" <user@domain.com>, <user@domain.com> "Name", Name <user@domain.com>,
295    * <user@domain.com> , user@domain.com
296    * @param sNamePlusEMail String Display name and e-mail address
297    * @return InternetAddress
298    * @throws AddressException
299    * @throws NullPointerException
300    * @throws UnsupportedEncodingException
301    */

302   public static InternetAddress JavaDoc parseAddress(String JavaDoc sNamePlusEMail)
303     throws AddressException JavaDoc,NullPointerException JavaDoc,UnsupportedEncodingException JavaDoc {
304     InternetAddress JavaDoc oRetAdr = null;
305
306     String JavaDoc sAddr = sNamePlusEMail.trim();
307
308     int iLeftAng = sAddr.indexOf('<');
309     int iRightAng= sAddr.indexOf('>');
310     int iLeftPar = sAddr.indexOf('(');
311     int iRightPar= sAddr.indexOf(')');
312     int iLeftQuo = sAddr.indexOf('"');
313     int iRightQuo;
314     if (iLeftQuo>=0) iRightQuo = sAddr.indexOf('"',iLeftQuo+1); else iRightQuo = -1;
315
316     if (iRightAng<iLeftAng) throw new AddressException JavaDoc("Misplaced right angle");
317     if (iLeftAng<0 && iRightAng>=0) throw new AddressException JavaDoc("Missing left angle");
318     if (iLeftAng>=0 && iRightAng<0) throw new AddressException JavaDoc("Missing right angle");
319     if (iLeftPar<0 && iRightPar>=0) throw new AddressException JavaDoc("Missing left parenthesis");
320     if (iLeftPar>=0 && iRightPar<0) throw new AddressException JavaDoc("Missing right parenthesis");
321     if (iRightPar<iLeftPar) throw new AddressException JavaDoc("Misplaced right parenthesis");
322     if (iLeftQuo>=0 && iRightQuo<0) throw new AddressException JavaDoc("Unclosed quote");
323
324     if (iLeftAng>=0 && iRightAng>=0 && iLeftPar>=0 && iRightPar>=0) {
325       // Address is (Name) <user@domain.com> or <user@domain.com> (Name)
326
oRetAdr = new InternetAddress JavaDoc(sAddr.substring(iLeftAng+1,iRightAng),sAddr.substring(iLeftPar+1,iRightPar));
327     } else if (iLeftAng>=0 && iRightAng>=0 && iLeftQuo>=0 && iRightQuo>=0) {
328       // Address is "Name" <user@domain.com> or "Name" <user@domain.com>
329
oRetAdr = new InternetAddress JavaDoc(sAddr.substring(iLeftAng+1,iRightAng),sAddr.substring(iLeftQuo+1,iRightQuo));
330     } else if (iLeftAng>=0 && iRightAng>=0) {
331       // Address is Name <user@domain.com> or <user@domain.com> Name
332
if (0==iLeftAng)
333         oRetAdr = new InternetAddress JavaDoc(sAddr.substring(1,iRightAng),sAddr.substring(iRightAng+1));
334       else
335         oRetAdr = new InternetAddress JavaDoc(sAddr.substring(iLeftAng+1,iRightAng),sAddr.substring(0,iLeftAng));
336     } else {
337       oRetAdr = new InternetAddress JavaDoc(sAddr);
338     }
339     return oRetAdr;
340   }
341
342   // ---------------------------------------------------------------------------
343

344   /**
345    * Get Display name concatenated with e-mail into angles
346    * @return String "Personal Name <user@domain.com>"
347    */

348   public String JavaDoc toString() {
349     return getPersonal()+" <"+getAddress()+">";
350   }
351 }
352
Popular Tags