1 32 33 package com.knowgate.hipermail; 34 35 import java.sql.SQLException ; 36 import java.sql.PreparedStatement ; 37 import java.sql.ResultSet ; 38 39 import java.util.Properties ; 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 51 52 public class MailAccount extends DBPersist { 53 54 public MailAccount() { 55 super (DB.k_user_mail, "MailAccount"); 56 } 57 58 60 public MailAccount(JDCConnection oConn, String sGuAccount) throws SQLException { 61 super (DB.k_user_mail, "MailAccount"); 62 load(oConn, new Object []{sGuAccount}); 63 } 64 65 67 79 public boolean store (JDCConnection oConn) throws SQLException { 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 ("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 ("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 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 } 117 119 public Properties getProperties() { 120 Properties oProps = new Properties (); 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 138 public void setProperties(Properties 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 150 158 public static MailAccount forUser(JDCConnection oConn, String sGuUser) 159 throws SQLException { 160 String sGuAccount; 161 PreparedStatement oStmt; 162 ResultSet 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 []{sGuAccount}); 188 } 189 return oRetVal; 190 } 192 204 public static MailAccount forUser(JDCConnection oConn, String sGuUser, Properties oProps) 205 throws SQLException { 206 MailAccount oRetVal = MailAccount.forUser(oConn, sGuUser); 207 if (null==oRetVal) { 208 ACLUser oUser = new ACLUser(); 209 if (oUser.load(oConn, new Object []{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 228 public static final short ClassId = 810; 229 230 } 231 | Popular Tags |