📄 castorsqlcontactmanagement.java
字号:
/*** * jwma Java WebMail * Copyright (c) 2000-2003 jwma team * * jwma is free software; you can distribute and use this source * under the terms of the BSD-style license received along with * the distribution. ***/package dtw.webmail.plugin.std;import dtw.webmail.JwmaKernel;import dtw.webmail.model.JwmaContact;import dtw.webmail.model.JwmaContacts;import dtw.webmail.model.JwmaContactsImpl;import dtw.webmail.model.JwmaException;import dtw.webmail.plugin.ContactManagementPlugin;import dtw.webmail.util.CastorDatabase;import dtw.webmail.util.CastorDatabasePool;import dtw.webmail.util.JwmaSettings;import dtw.webmail.util.config.JwmaConfiguration;import org.apache.log4j.Logger;import java.io.InputStream;import java.io.OutputStream;/** * A <tt>ContactManagementPlugin</tt> implementation * based on Castor JDO. * <p> * Stores the contacts in a database. * * @author Dieter Wimberger * @version 0.9.7 07/02/2003 */public class CastorSQLContactManagement implements ContactManagementPlugin { //class attributes private static Logger log = Logger.getLogger(CastorSQLContactManagement.class); //instance attributes private CastorPreferences m_PreferencesTemplate; private CastorDatabasePool m_DBPool; public CastorSQLContactManagement() { }//constructor public JwmaContactsImpl loadContacts(String cuid) throws JwmaException { CastorDatabase db = null; CastorContacts cts = null; try { db = m_DBPool.leaseDatabase(); //Begin transaction db.begin(); //load contacts cts = (CastorContacts) db.load(CastorContacts.class, cuid); //end transaction db.commit(); } catch (Exception ex) { throw new JwmaException("").setException(ex); } finally { //release the database m_DBPool.releaseDatabase(db); } return cts; }//loadContacts public void saveContacts(JwmaContactsImpl contacts) throws JwmaException { CastorDatabase db = null; CastorContacts ctdb = (CastorContacts) contacts; try { db = m_DBPool.leaseDatabase(); // Begin a transaction log.debug("Persisting contact database..."); db.begin(); //check if the preferences are not persistent yet. //simplest is if the timestamp is not zero. if (ctdb.jdoGetTimeStamp() != 0) { log.debug("udpate..."); ctdb.updateContacts(db.getDatabase()); } else { log.debug("create..."); ctdb.persistContacts(db.getDatabase()); } // Commit the transaction db.commit(); } catch (Exception ex) { log.error("saveContacts()", ex); throw new JwmaException("").setException(ex); } finally { m_DBPool.releaseDatabase(db); } }//savePreferences public boolean isPersistent(String cuid) throws JwmaException { //FIXME:This is definately not very efficient, but //if ok, and castor caches..hmm if (cuid == null || cuid.length() == 0) { return false; } CastorDatabase db = null; boolean exists = false; try { db = m_DBPool.leaseDatabase(); //Begin transaction db.begin(); //retrieve prefs instance if (db.load(CastorContacts.class, cuid) != null) { exists = true; } //end transaction db.commit(); } catch (Exception ex) { String msg = JwmaKernel.getReference().getLogMessage("jwma.plugin.castor.objcheck"); log.error(msg, ex); throw new JwmaException(msg).setException(ex); } finally { //release the database m_DBPool.releaseDatabase(db); } return exists; }//isPersistent public JwmaContactsImpl createContacts() { return new CastorContacts(); }//createContacts public void activate() throws JwmaException { JwmaKernel kernel = JwmaKernel.getReference(); try { String etc = kernel.getDirectoryPath(JwmaKernel.ETC_DIR); //Setup castor helper CastorHelper helper = CastorHelper.getReference(); //prepare jdo helper.prepareJDO(etc + "database.xml"); //set the pool m_DBPool = helper.getDatabasePool(); log.info(JwmaKernel.getReference().getLogMessage("jwma.plugin.activation")); } catch (Exception ex) { log.info(JwmaKernel.getReference().getLogMessage("jwma.plugin.failedactivation")); throw new JwmaException("").setException(ex); } }//activate public void deactivate() { //FIXME: shut down pool, close down connections? }//deactivate public String exportContact(JwmaContact contact, String TYPE) throws JwmaException { return ""; /* if(TYPE.equals(TYPE_VCARD3)) { ByteArrayOutputStream bout=new ByteArrayOutputStream(); m_vCardMarshaller.marshallContact(bout,((JpimContact)contact).getContact()); return new String(bout.toByteArray()); } else { return ""; } */ }//export public JwmaContact importContact(InputStream in, String TYPE) throws JwmaException { return null; /* if(TYPE.equals(TYPE_VCARD3) || TYPE.equals(TYPE_VCARD2)) { try { return new JpimContact( new vCardUnmarshaller() .unmarshallContact(in) ); } catch (Exception ex) { log.error("importContact():",ex); return null; } } else { return null; } */ }//importContact public JwmaContact[] importDatabase(InputStream in, String TYPE) throws JwmaException { return null; }//importDatabase public void exportDatabase(OutputStream out, JwmaContacts db, String TYPE) throws JwmaException { return; }//exportDatabase public String[] getSupportedTypes(int IMEX_TYPE) { switch (IMEX_TYPE) { case CONTACT_IMPORT: return CONTACT_IMPORT_TYPES; case CONTACT_EXPORT: return CONTACT_EXPORT_TYPES; case DATABASE_EXPORT: return DATABASE_EXPORT_TYPES; case DATABASE_IMPORT: return DATABASE_IMPORT_TYPES; default: return NO_TYPES; } }//getSupportedTypes public boolean isSupportedContactImportType(String type) { for (int i = 0; i < CONTACT_IMPORT_TYPES.length; i++) { if (type.equalsIgnoreCase(CONTACT_IMPORT_TYPES[i])) { return true; } } return false; }//isSupportedContactImportType public static final String TYPE_VCARD3 = "text/directory"; public static final String TYPE_VCARD2 = "application/directory"; private static final String[] NO_TYPES = new String[0]; private static final String[] CONTACT_IMPORT_TYPES = NO_TYPES; private static final String[] CONTACT_EXPORT_TYPES = NO_TYPES; private static final String[] DATABASE_EXPORT_TYPES = NO_TYPES; private static final String[] DATABASE_IMPORT_TYPES = NO_TYPES;}//class CastorContactManagement
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -