KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright (C) 2005 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.sql.SQLException JavaDoc;
36 import java.sql.PreparedStatement JavaDoc;
37 import java.sql.ResultSet JavaDoc;
38
39 import java.util.Properties JavaDoc;
40
41 import com.knowgate.jdc.JDCConnection;
42 import com.knowgate.dataobjs.DB;
43 import com.knowgate.dataobjs.DBPersist;
44 import com.knowgate.acl.ACLUser;
45 import com.knowgate.misc.Gadgets;
46
47 /**
48  * @author Sergio Montoro Ten
49  * @version 1.0
50  */

51
52 public class MailAccount extends DBPersist {
53
54   public MailAccount() {
55     super (DB.k_user_mail, "MailAccount");
56   }
57
58   // ----------------------------------------------------------
59

60   public MailAccount(JDCConnection oConn, String JavaDoc sGuAccount) throws SQLException JavaDoc {
61     super (DB.k_user_mail, "MailAccount");
62     load(oConn, new Object JavaDoc[]{sGuAccount});
63   }
64
65   // ----------------------------------------------------------
66

67   /**
68    * <p>Store mail account</p>
69    * This method has a special side effect: only one mail account may be the
70    * default one for a given user. So each time that an account is stored with
71    * it bo_default flag set to 1 the other account for the same user are set to
72    * bo_default=0.<br>
73    * This method also calls Gadgets.checkEMail() on tx_main_email and tx_reply_mail
74    * if they do not have a valid syntax a SQLException is thrown
75    * @param oConn JDCConnection
76    * @return boolean
77    * @throws SQLException If tx_main_email or tx_reply_mail do not have a valid syntax
78    */

79   public boolean store (JDCConnection oConn) throws SQLException JavaDoc {
80     if (isNull(DB.gu_account)) {
81       put(DB.gu_account, Gadgets.generateUUID());
82     }
83     if (isNull(DB.bo_default)) {
84       replace (DB.bo_default, (short)0);
85     }
86     if (isNull(DB.incoming_spa)) {
87       replace (DB.incoming_spa, (short)0);
88     }
89     if (isNull(DB.incoming_ssl)) {
90       replace (DB.incoming_ssl, (short)0);
91     }
92     if (isNull(DB.outgoing_spa)) {
93       replace (DB.outgoing_spa, (short)0);
94     }
95     if (isNull(DB.outgoing_ssl)) {
96       replace (DB.outgoing_ssl, (short)0);
97     }
98     if (!isNull(DB.tx_main_email)) {
99       if (!Gadgets.checkEMail(getString(DB.tx_main_email)))
100         throw new SQLException JavaDoc ("Mail account "+getString(DB.tx_main_email)+" is not valid","23000");
101     }
102     if (!isNull(DB.tx_reply_email)) {
103       if (!Gadgets.checkEMail(getString(DB.tx_reply_email)))
104         throw new SQLException JavaDoc ("Mail account "+getString(DB.tx_reply_email)+" is not valid","23000");
105     }
106     if (!isNull(DB.bo_default)) {
107       if (getShort(DB.bo_default)==(short)1) {
108         PreparedStatement JavaDoc oStmt = oConn.prepareStatement("UPDATE "+DB.k_user_mail+" SET "+DB.bo_default+"=0 WHERE "+DB.gu_user+"=?");
109         oStmt.setString(1, getStringNull(DB.gu_user,null));
110         oStmt.executeUpdate();
111         oStmt.close();
112       }
113     }
114     return super.store(oConn);
115   } // store
116

117   // ----------------------------------------------------------
118

119   public Properties JavaDoc getProperties() {
120     Properties JavaDoc oProps = new Properties JavaDoc();
121     oProps.put("mail.store.protocol", getStringNull(DB.incoming_protocol,"pop3"));
122     oProps.put("mail.transport.protocol", getStringNull(DB.outgoing_protocol,"smtp"));
123     oProps.put("mail.incoming", getStringNull(DB.incoming_server,"localhost"));
124     oProps.put("mail.outgoing", getStringNull(DB.outgoing_server,"localhost"));
125     if (isNull(DB.incoming_port))
126       oProps.put("mail.pop3.port", "110");
127     else
128       oProps.put("mail."+getString(DB.incoming_protocol)+".port", String.valueOf(getShort(DB.incoming_port)));
129     if (isNull(DB.outgoing_port))
130       oProps.put("mail.smtp.port", "25");
131     else
132       oProps.put("mail."+getString(DB.outgoing_protocol)+".port", String.valueOf(getShort(DB.outgoing_port)));
133     return oProps;
134   }
135
136   // ----------------------------------------------------------
137

138   public void setProperties(Properties JavaDoc oProps) {
139     replace(DB.incoming_protocol, oProps.getProperty("mail.store.protocol","pop3"));
140     replace(DB.outgoing_protocol, oProps.getProperty("mail.transport.protocol","smtp"));
141     replace(DB.incoming_server, oProps.getProperty("mail."+getString(DB.incoming_protocol)+".host",oProps.getProperty("mail.incoming","localhost")));
142     replace(DB.outgoing_server, oProps.getProperty("mail."+getString(DB.outgoing_protocol)+".host",oProps.getProperty("mail.outgoing","localhost")));
143     replace(DB.incoming_port, oProps.getProperty("mail."+getString(DB.incoming_protocol)+".port","110"));
144     replace(DB.outgoing_port, oProps.getProperty("mail."+getString(DB.outgoing_protocol)+".port","25"));
145   }
146
147   // **********************************************************
148
// Static Methods
149

150   /**
151    * <p>Get MailAccount for ACLUser</p>
152    * Get the default mail account for an ACLUser or the first account if there is no default.
153    * @param oConn JDCConnection
154    * @param sGuUser String ACLUser GUID (from k_users.gu_user)
155    * @return MailAccount instance or <b>null</b> if there are no mail accounts for the given user
156    * @throws SQLException
157    */

158   public static MailAccount forUser(JDCConnection oConn, String JavaDoc sGuUser)
159     throws SQLException JavaDoc {
160     String JavaDoc sGuAccount;
161     PreparedStatement JavaDoc oStmt;
162     ResultSet JavaDoc oRSet;
163     MailAccount oRetVal;
164     oStmt = oConn.prepareStatement("SELECT "+DB.gu_account+" FROM "+DB.k_user_mail+ " WHERE "+DB.gu_user+"=? AND "+DB.bo_default+"=?");
165     oStmt.setString(1, sGuUser);
166     oStmt.setShort (2, (short)1);
167     oRSet = oStmt.executeQuery();
168     if (oRSet.next())
169       sGuAccount = oRSet.getString(1);
170     else
171       sGuAccount = null;
172     oRSet.close();
173     oStmt.close();
174     if (null==sGuAccount) {
175       oStmt = oConn.prepareStatement("SELECT "+DB.gu_account+" FROM "+DB.k_user_mail+ " WHERE "+DB.gu_user+"=?");
176       oStmt.setString(1, sGuUser);
177       oRSet = oStmt.executeQuery();
178       if (oRSet.next())
179         sGuAccount = oRSet.getString(1);
180       oRSet.close();
181       oStmt.close();
182     }
183     if (null==sGuAccount) {
184       oRetVal = null;
185     } else {
186       oRetVal = new MailAccount();
187       oRetVal.load(oConn, new Object JavaDoc[]{sGuAccount});
188     }
189     return oRetVal;
190   } // forUser
191

192   /**
193    * <p>Get MailAccount for ACLUser or create a default one</p>
194    * If no account for given user is found at k_user_mail then one is temporaly
195    * created by using properties given at parameter oProps and taking user's
196    * tx_main_email and tx_pwd as mail address and password.
197    * @param oConn JDCConnection
198    * @param sGuUser String ACLUser GUID (from k_users.gu_user)
199    * @param oProps Properties usually take from an hipergate.cnf file by calling
200    * Environment.getProfile("hipergate")
201    * @return MailAccount instance or <b>null</b> if there are no user with the given GUID
202    * @throws SQLException
203    */

204   public static MailAccount forUser(JDCConnection oConn, String JavaDoc sGuUser, Properties JavaDoc oProps)
205     throws SQLException JavaDoc {
206     MailAccount oRetVal = MailAccount.forUser(oConn, sGuUser);
207     if (null==oRetVal) {
208       ACLUser oUser = new ACLUser();
209       if (oUser.load(oConn, new Object JavaDoc[]{sGuUser})) {
210         oRetVal = new MailAccount();
211         oRetVal.setProperties(oProps);
212         oRetVal.put(DB.gu_user, oUser.getString(DB.gu_user));
213         oRetVal.put(DB.tl_account, "Default account for " + oUser.getString(DB.tx_nickname));
214         oRetVal.put(DB.bo_default, (short)1);
215         oRetVal.put(DB.bo_synchronize, (short)0);
216         oRetVal.put(DB.tx_main_email, oUser.getString(DB.tx_main_email));
217         oRetVal.put(DB.tx_reply_email, oUser.getString(DB.tx_main_email));
218         oRetVal.put(DB.incoming_password, oUser.getString(DB.tx_pwd));
219         oRetVal.put(DB.outgoing_password, oUser.getString(DB.tx_pwd));
220       }
221     }
222     return oRetVal;
223   }
224
225   // **********************************************************
226
// Public Constants
227

228   public static final short ClassId = 810;
229
230 }
231
Popular Tags