001: /***
002: * jwma Java WebMail
003: * Copyright (c) 2000-2003 jwma team
004: *
005: * jwma is free software; you can distribute and use this source
006: * under the terms of the BSD-style license received along with
007: * the distribution.
008: ***/package dtw.webmail.plugin.std;
009:
010: import dtw.webmail.JwmaKernel;
011: import dtw.webmail.model.JwmaContact;
012: import dtw.webmail.model.JwmaContacts;
013: import dtw.webmail.model.JwmaContactsImpl;
014: import dtw.webmail.model.JwmaException;
015: import dtw.webmail.plugin.ContactManagementPlugin;
016: import dtw.webmail.util.CastorDatabase;
017: import dtw.webmail.util.CastorDatabasePool;
018: import dtw.webmail.util.JwmaSettings;
019: import dtw.webmail.util.config.JwmaConfiguration;
020: import org.apache.log4j.Logger;
021:
022: import java.io.InputStream;
023: import java.io.OutputStream;
024:
025: /**
026: * A <tt>ContactManagementPlugin</tt> implementation
027: * based on Castor JDO.
028: * <p>
029: * Stores the contacts in a database.
030: *
031: * @author Dieter Wimberger
032: * @version 0.9.7 07/02/2003
033: */
034: public class CastorSQLContactManagement implements
035: ContactManagementPlugin {
036:
037: //class attributes
038: private static Logger log = Logger
039: .getLogger(CastorSQLContactManagement.class);
040:
041: //instance attributes
042: private CastorPreferences m_PreferencesTemplate;
043: private CastorDatabasePool m_DBPool;
044:
045: public CastorSQLContactManagement() {
046: }//constructor
047:
048: public JwmaContactsImpl loadContacts(String cuid)
049: throws JwmaException {
050:
051: CastorDatabase db = null;
052: CastorContacts cts = null;
053:
054: try {
055: db = m_DBPool.leaseDatabase();
056: //Begin transaction
057: db.begin();
058:
059: //load contacts
060: cts = (CastorContacts) db.load(CastorContacts.class, cuid);
061:
062: //end transaction
063: db.commit();
064:
065: } catch (Exception ex) {
066: throw new JwmaException("").setException(ex);
067: } finally {
068: //release the database
069: m_DBPool.releaseDatabase(db);
070: }
071: return cts;
072: }//loadContacts
073:
074: public void saveContacts(JwmaContactsImpl contacts)
075: throws JwmaException {
076:
077: CastorDatabase db = null;
078: CastorContacts ctdb = (CastorContacts) contacts;
079:
080: try {
081: db = m_DBPool.leaseDatabase();
082: // Begin a transaction
083: log.debug("Persisting contact database...");
084: db.begin();
085:
086: //check if the preferences are not persistent yet.
087: //simplest is if the timestamp is not zero.
088: if (ctdb.jdoGetTimeStamp() != 0) {
089: log.debug("udpate...");
090: ctdb.updateContacts(db.getDatabase());
091: } else {
092: log.debug("create...");
093: ctdb.persistContacts(db.getDatabase());
094: }
095:
096: // Commit the transaction
097: db.commit();
098: } catch (Exception ex) {
099: log.error("saveContacts()", ex);
100: throw new JwmaException("").setException(ex);
101: } finally {
102: m_DBPool.releaseDatabase(db);
103: }
104: }//savePreferences
105:
106: public boolean isPersistent(String cuid) throws JwmaException {
107:
108: //FIXME:This is definately not very efficient, but
109: //if ok, and castor caches..hmm
110: if (cuid == null || cuid.length() == 0) {
111: return false;
112: }
113: CastorDatabase db = null;
114: boolean exists = false;
115: try {
116: db = m_DBPool.leaseDatabase();
117: //Begin transaction
118: db.begin();
119:
120: //retrieve prefs instance
121: if (db.load(CastorContacts.class, cuid) != null) {
122: exists = true;
123: }
124:
125: //end transaction
126: db.commit();
127:
128: } catch (Exception ex) {
129: String msg = JwmaKernel.getReference().getLogMessage(
130: "jwma.plugin.castor.objcheck");
131: log.error(msg, ex);
132: throw new JwmaException(msg).setException(ex);
133: } finally {
134: //release the database
135: m_DBPool.releaseDatabase(db);
136: }
137: return exists;
138: }//isPersistent
139:
140: public JwmaContactsImpl createContacts() {
141: return new CastorContacts();
142: }//createContacts
143:
144: public void activate() throws JwmaException {
145: JwmaKernel kernel = JwmaKernel.getReference();
146: try {
147: String etc = kernel.getDirectoryPath(JwmaKernel.ETC_DIR);
148:
149: //Setup castor helper
150: CastorHelper helper = CastorHelper.getReference();
151:
152: //prepare jdo
153: helper.prepareJDO(etc + "database.xml");
154:
155: //set the pool
156: m_DBPool = helper.getDatabasePool();
157:
158: log.info(JwmaKernel.getReference().getLogMessage(
159: "jwma.plugin.activation"));
160: } catch (Exception ex) {
161: log.info(JwmaKernel.getReference().getLogMessage(
162: "jwma.plugin.failedactivation"));
163: throw new JwmaException("").setException(ex);
164: }
165: }//activate
166:
167: public void deactivate() {
168: //FIXME: shut down pool, close down connections?
169: }//deactivate
170:
171: public String exportContact(JwmaContact contact, String TYPE)
172: throws JwmaException {
173: return "";
174: /*
175: if(TYPE.equals(TYPE_VCARD3)) {
176: ByteArrayOutputStream bout=new ByteArrayOutputStream();
177:
178: m_vCardMarshaller.marshallContact(bout,((JpimContact)contact).getContact());
179: return new String(bout.toByteArray());
180: } else {
181: return "";
182: }
183: */
184: }//export
185:
186: public JwmaContact importContact(InputStream in, String TYPE)
187: throws JwmaException {
188: return null;
189: /*
190: if(TYPE.equals(TYPE_VCARD3) || TYPE.equals(TYPE_VCARD2)) {
191: try {
192: return new JpimContact(
193: new vCardUnmarshaller()
194: .unmarshallContact(in)
195: );
196: } catch (Exception ex) {
197: log.error("importContact():",ex);
198: return null;
199: }
200: } else {
201: return null;
202: }
203: */
204: }//importContact
205:
206: public JwmaContact[] importDatabase(InputStream in, String TYPE)
207: throws JwmaException {
208: return null;
209: }//importDatabase
210:
211: public void exportDatabase(OutputStream out, JwmaContacts db,
212: String TYPE) throws JwmaException {
213: return;
214: }//exportDatabase
215:
216: public String[] getSupportedTypes(int IMEX_TYPE) {
217: switch (IMEX_TYPE) {
218: case CONTACT_IMPORT:
219: return CONTACT_IMPORT_TYPES;
220: case CONTACT_EXPORT:
221: return CONTACT_EXPORT_TYPES;
222: case DATABASE_EXPORT:
223: return DATABASE_EXPORT_TYPES;
224: case DATABASE_IMPORT:
225: return DATABASE_IMPORT_TYPES;
226: default:
227: return NO_TYPES;
228: }
229: }//getSupportedTypes
230:
231: public boolean isSupportedContactImportType(String type) {
232: for (int i = 0; i < CONTACT_IMPORT_TYPES.length; i++) {
233: if (type.equalsIgnoreCase(CONTACT_IMPORT_TYPES[i])) {
234: return true;
235: }
236: }
237: return false;
238: }//isSupportedContactImportType
239:
240: public static final String TYPE_VCARD3 = "text/directory";
241: public static final String TYPE_VCARD2 = "application/directory";
242: private static final String[] NO_TYPES = new String[0];
243:
244: private static final String[] CONTACT_IMPORT_TYPES = NO_TYPES;
245: private static final String[] CONTACT_EXPORT_TYPES = NO_TYPES;
246: private static final String[] DATABASE_EXPORT_TYPES = NO_TYPES;
247: private static final String[] DATABASE_IMPORT_TYPES = NO_TYPES;
248:
249: }//class CastorContactManagement
|