KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > addrbook > Fellow


1 /*
2   Copyright (C) 2003 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.addrbook;
34
35 import java.io.InputStream JavaDoc;
36 import java.io.ByteArrayOutputStream JavaDoc;
37
38 import java.sql.Connection JavaDoc;
39 import java.sql.SQLException JavaDoc;
40 import java.sql.PreparedStatement JavaDoc;
41 import java.sql.CallableStatement JavaDoc;
42 import java.sql.ResultSet JavaDoc;
43 import java.sql.ResultSetMetaData JavaDoc;
44
45 import com.knowgate.debug.DebugFile;
46
47 import com.knowgate.jdc.JDCConnection;
48 import com.knowgate.dataobjs.DB;
49 import com.knowgate.dataobjs.DBBind;
50 import com.knowgate.dataobjs.DBPersist;
51 import com.knowgate.misc.Gadgets;
52 import com.knowgate.misc.Base64Encoder;
53
54 import com.knowgate.misc.Gadgets;
55
56 import java.util.Iterator JavaDoc;
57
58 /**
59  * <p>Enterprise Fellow</p>
60  * @author Sergio Montoro ten
61  * @version 3.0
62  */

63
64 public class Fellow extends DBPersist {
65   public Fellow() {
66     super(DB.k_fellows, "Fellow");
67   }
68
69   //-------------------------------------------------------------------
70

71   /**
72    * Construct object and set gu_fellow field.
73    * Object is not fully loaded from database.
74    * @param sFellowId Fellow Unique Identifier
75    */

76
77   public Fellow(String JavaDoc sFellowId) {
78     super(DB.k_fellows, "Fellow");
79     put(DB.gu_fellow, sFellowId);
80   }
81
82   //-------------------------------------------------------------------
83

84   /**
85    * <p>Construct object and load fields from database.</p>
86    * @param oConn database Connection
87    * @param sFellowId Fellow Unique Identifier
88    * @throws SQLException
89    */

90   public Fellow(JDCConnection oConn, String JavaDoc sFellowId) throws SQLException JavaDoc {
91     super(DB.k_fellows, "Fellow");
92     load ((Connection JavaDoc) oConn, new Object JavaDoc[] { sFellowId });
93   }
94
95   //-------------------------------------------------------------------
96

97   /**
98    * <p>Create a fellow from a {@link ACLUser} or from another fellow
99    * <p>The fellow is internally stored at this object but NOT persisted to database.</p>
100    * @param oUsr ACLUser Identifier or Fellow identifier.
101    */

102   public void clone(DBPersist oUsr) {
103     String JavaDoc sField;
104     String JavaDoc sSurName;
105     String JavaDoc sSurName1 = "";
106     String JavaDoc sSurName2 = "";
107
108     if (DebugFile.trace) {
109       DebugFile.writeln("Begin Fellow.clone(" + oUsr.getStringNull(DB.gu_user,"null") + ")");
110       DebugFile.incIdent();
111     }
112
113     Iterator JavaDoc oFields = oUsr.getItems().iterator();
114
115     while (oFields.hasNext()) {
116       sField = (String JavaDoc) oFields.next();
117
118       if (sField.equals(DB.tx_main_email))
119         put(DB.tx_email, oUsr.get(DB.tx_main_email));
120       else if (sField.equals(DB.nm_user))
121         put(DB.tx_name, oUsr.get(DB.nm_user));
122       else if (sField.equals(DB.tx_surname1))
123         sSurName1 = oUsr.getStringNull(DB.tx_surname1, "");
124       else if (sField.equals(DB.tx_surname2))
125         sSurName2 = " " + oUsr.getStringNull(DB.tx_surname2, "");
126       else
127         put(sField, oUsr.get(sField));
128     } // wend
129

130     sSurName = sSurName1 + sSurName2;
131
132     if (sSurName.trim().length()>0) put(DB.tx_surname, sSurName);
133
134     if (DebugFile.trace) {
135       DebugFile.decIdent();
136       DebugFile.writeln("End Fellow.clone()");
137     }
138   } // clone
139

140   // ----------------------------------------------------------
141
/**
142    * @param oConn Database Connection
143    * @return <b>true</b> if Fellow has an associated record at k_fellows_attach table</p>
144    * @throws SQLException
145    */

146   public boolean hasPhoto(JDCConnection oConn) throws SQLException JavaDoc {
147
148     if (DebugFile.trace) {
149       DebugFile.writeln("Begin Fellow.hasPhoto([Connection])");
150       DebugFile.incIdent();
151
152       DebugFile.writeln("Connection.prepareStatement(SELECT NULL FROM " + DB.k_fellows_attach + " WHERE " + DB.gu_fellow + "=" + getStringNull(DB.gu_fellow,"null") + ")");
153     }
154
155     PreparedStatement JavaDoc oStmt = oConn.prepareStatement("SELECT NULL FROM " + DB.k_fellows_attach + " WHERE " + DB.gu_fellow + "=?");
156     oStmt.setString(1, getString(DB.gu_fellow));
157     ResultSet JavaDoc oRSet = oStmt.executeQuery();
158     boolean bHasAttach = oRSet.next();
159     oRSet.close();
160     oStmt.close();
161
162     if (DebugFile.trace) {
163       DebugFile.decIdent();
164       DebugFile.writeln("End Fellow.hasPhoto() : " + String.valueOf(bHasAttach));
165     }
166
167     return bHasAttach;
168   } // hasPhoto
169

170   // ----------------------------------------------------------
171

172   /**
173    * <P>Get Fellow Photo as a byte array</P>
174    * @param oConn JDBC Connection
175    * @return Byte Array or <b>null</b> if Fellow does not have a photograph.
176    * @throws SQLException
177    */

178   public byte[] getPhoto (Connection JavaDoc oConn)
179      throws SQLException JavaDoc {
180
181      if (DebugFile.trace) {
182        DebugFile.writeln("Begin Fellow.getPhoto([Connection])");
183        DebugFile.incIdent();
184      }
185
186      ByteArrayOutputStream JavaDoc oOut = null;
187      PreparedStatement JavaDoc oStmt = oConn.prepareStatement("SELECT " + DB.len_file + "," + DB.bin_file + " FROM " + DB.k_fellows_attach + " WHERE " + DB.gu_fellow + "=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
188      oStmt.setString(1, getString(DB.gu_fellow));
189      ResultSet JavaDoc oRSet = oStmt.executeQuery();
190
191      if (oRSet.next()) {
192        int iLen = oRSet.getInt(1);
193
194        if (iLen>0)
195          oOut = new ByteArrayOutputStream JavaDoc(iLen);
196        else
197          oOut = new ByteArrayOutputStream JavaDoc();
198
199        byte[] oBuffer = new byte[4000];
200
201        InputStream JavaDoc oBlob = oRSet.getBinaryStream(2);
202
203        int iReaded = 0;
204        do {
205          try {
206            iReaded = oBlob.read(oBuffer, 0, 4000);
207            if (iReaded > 0)
208              oOut.write(oBuffer, 0, iReaded);
209          } catch (java.io.IOException JavaDoc ignore) { }
210        } while (4000==iReaded);
211
212        try { oBlob.close(); } catch (java.io.IOException JavaDoc ignore) { }
213
214        oBlob = null;
215      }
216
217      oRSet.close();
218      oStmt.close();
219
220      if (DebugFile.trace) {
221        DebugFile.decIdent();
222        DebugFile.writeln("End Fellow.getPhoto()");
223      }
224
225      if (oOut!=null)
226        return oOut.toByteArray();
227      else
228        return null;
229   } // getPhoto
230

231   // ----------------------------------------------------------
232

233   public boolean load(JDCConnection oConn, Object JavaDoc[] PKVals) throws SQLException JavaDoc {
234     return load((Connection JavaDoc) oConn, PKVals);
235   }
236
237   // ----------------------------------------------------------
238

239   public boolean load(Connection JavaDoc oConn, Object JavaDoc[] PKVals) throws SQLException JavaDoc {
240
241     Object JavaDoc oCol;
242     PreparedStatement JavaDoc oStmt;
243     ResultSet JavaDoc oRSet;
244     ResultSetMetaData JavaDoc oMDat;
245     int iCols;
246     boolean bRetVal = false;
247
248     if (DebugFile.trace) {
249       DebugFile.writeln("Begin Fellow.load([Connection], " + PKVals[0] + ")");
250       DebugFile.incIdent();
251     }
252
253     oStmt = oConn.prepareStatement("SELECT * FROM " + DB.k_fellows + " WHERE " + DB.gu_fellow + "=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
254     oStmt.setString(1, (String JavaDoc) PKVals[0]);
255     oRSet = oStmt.executeQuery();
256     oMDat = oRSet.getMetaData();
257     iCols = oMDat.getColumnCount();
258
259     if (oRSet.next()) {
260       bRetVal = true;
261
262       for (int c=1; c<=iCols; c++) {
263         if (!oMDat.getColumnName(c).equalsIgnoreCase(DB.dt_created)) {
264           oCol = oRSet.getObject(c);
265           if (!oRSet.wasNull())
266             AllVals.put(oMDat.getColumnName(c).toLowerCase(), oCol);
267         }
268       } // next
269
} // fi (oRSet.next())
270

271     oRSet.close();
272     oStmt.close();
273
274     if (bRetVal) {
275       if (DebugFile.trace)
276         DebugFile.writeln("Connection.prepareStatement(SELECT * FROM " + DB.k_users + " WHERE " + DB.gu_user + "='" + getStringNull(DB.gu_fellow, "null") + "')");
277
278       oStmt = oConn.prepareStatement("SELECT * FROM " + DB.k_users + " WHERE " + DB.gu_user + "=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
279       oStmt.setString(1, (String JavaDoc) PKVals[0]);
280       oRSet = oStmt.executeQuery();
281       oMDat = oRSet.getMetaData();
282       iCols = oMDat.getColumnCount();
283
284       if (oRSet.next()) {
285         for (int c=1; c<=iCols; c++) {
286           if (!oMDat.getColumnName(c).equalsIgnoreCase(DB.dt_created) && !AllVals.containsKey(oMDat.getColumnName(c))) {
287             oCol = oRSet.getObject(c);
288             if (!oRSet.wasNull())
289               AllVals.put (oMDat.getColumnName(c).toLowerCase(), oCol);
290           }
291         } // next
292
} // fi
293

294       oRSet.close();
295       oStmt.close();
296
297       if (DebugFile.trace)
298         DebugFile.writeln("Connection.prepareStatement(SELECT " + DB.tx_file + "," + DB.len_file + " FROM " + DB.k_fellows_attach + " WHERE " + DB.gu_fellow + "='" + getStringNull(DB.gu_fellow, "null") + "')");
299
300       oStmt = oConn.prepareStatement("SELECT " + DB.tx_file + "," + DB.len_file + " FROM " + DB.k_fellows_attach + " WHERE " + DB.gu_fellow + "=?");
301       oStmt.setString(1, (String JavaDoc) PKVals[0]);
302       oRSet = oStmt.executeQuery();
303       if (oRSet.next()) {
304         put(DB.tx_file, oRSet.getString(1));
305         put(DB.len_file, oRSet.getInt(2));
306       }
307       oRSet.close();
308       oStmt.close();
309     }
310
311     if (DebugFile.trace) {
312       DebugFile.decIdent();
313       DebugFile.writeln("End Fellow.load() : " + String.valueOf(bRetVal));
314     }
315
316     return bRetVal;
317   } // load
318

319   // ----------------------------------------------------------
320

321   public boolean store(JDCConnection oConn) throws SQLException JavaDoc {
322     java.sql.Timestamp JavaDoc dtNow = new java.sql.Timestamp JavaDoc(DBBind.getTime());
323
324     // Si no se especificó un identificador, entonces añadirlo automáticamente
325
if (!AllVals.containsKey(DB.gu_fellow))
326       put(DB.gu_fellow, Gadgets.generateUUID());
327
328     // Forzar la fecha de modificación del registro
329
replace(DB.dt_modified, dtNow);
330
331     return super.store(oConn);
332   }
333
334   // ----------------------------------------------------------
335

336   public boolean delete(JDCConnection oConn) throws SQLException JavaDoc {
337
338     return Fellow.delete(oConn, getString(DB.gu_fellow));
339   }
340
341   // ----------------------------------------------------------
342

343   /**
344    *
345    * @return
346    * @throws SQLException
347    * @since 2.1
348    * @see <A HREF="http://www.faqs.org/rfcs/rfc2426.html">RFC 2426 (Plain Text)</A><BR>
349    * @see <A HREF="http://www.imc.org/pdi/vcard-21.doc">vCard 2.1 Spec (MS Word format)</A><BR>
350    */

351   public String JavaDoc vCard(Connection JavaDoc oConn)
352     throws SQLException JavaDoc {
353
354     final String JavaDoc CrLf = new String JavaDoc(new byte[]{13,10});
355
356     StringBuffer JavaDoc oCardBuffer = new StringBuffer JavaDoc();
357
358     oCardBuffer.append("BEGIN:VCARD" + CrLf);
359     oCardBuffer.append("VERSION:3.0" + CrLf);
360     oCardBuffer.append("PRODID:hipergate" + CrLf);
361     oCardBuffer.append("CLASS:PUBLIC" + CrLf);
362
363     oCardBuffer.append("FN:");
364     if (!isNull(DB.tx_name)) oCardBuffer.append(getString(DB.tx_name));
365     if (!isNull(DB.tx_surname)) oCardBuffer.append(" " + getString(DB.tx_surname));
366     oCardBuffer.append(CrLf);
367
368     oCardBuffer.append("N:");
369     oCardBuffer.append(getStringNull(DB.tx_surname1, "") + ";");
370     oCardBuffer.append(getStringNull(DB.nm_user, "") + ";");
371     oCardBuffer.append(getStringNull(DB.tx_surname2, "") + ";");
372     oCardBuffer.append(";;" + CrLf);
373
374     if (!isNull(DB.tx_nickname)) {
375       oCardBuffer.append("NICKNAME:" + getString(DB.tx_nickname) + CrLf);
376     }
377
378     byte[] byPhoto = getPhoto(oConn);
379
380     if (null!=byPhoto) {
381       String JavaDoc sType = "";
382       int iDot = getStringNull(DB.tx_file, "").lastIndexOf('.');
383       if (iDot>0 && iDot<getStringNull(DB.tx_file, "").length()-1)
384         sType = getString(DB.tx_file).substring(iDot+1).toUpperCase();
385       if (sType.equals("JPG")) sType = "JPEG";
386
387       if (sType.length()>0)
388         oCardBuffer.append("PHOTO;ENCODING=BASE64;TYPE=" + sType + ":" + Base64Encoder.encode(byPhoto) + CrLf);
389     }
390
391     if (!isNull(DB.dt_birth)) {
392       oCardBuffer.append("BDAY:" + getDateFormated(DB.dt_birth, "yyyyMMdd") + CrLf);
393     }
394
395     if (!isNull(DB.work_phone)) {
396       oCardBuffer.append("TEL;WORK:" + getString(DB.work_phone) + CrLf);
397     }
398
399     if (!isNull(DB.home_phone)) {
400       oCardBuffer.append("TEL;HOME:" + getString(DB.home_phone) + CrLf);
401     }
402
403     if (!isNull(DB.mov_phone)) {
404       oCardBuffer.append("TEL;CELL:" + getString(DB.mov_phone) + CrLf);
405     }
406
407     if (!isNull(DB.tx_email)) {
408       oCardBuffer.append("EMAIL;INTERNET:" + getString(DB.tx_email) + CrLf);
409     }
410
411     if (!isNull(DB.tx_timezone)) {
412       oCardBuffer.append("TZ:" + getString(DB.tx_timezone) + CrLf);
413     }
414
415     if (!isNull(DB.de_title)) {
416       oCardBuffer.append("TITLE:" + getString(DB.de_title) + CrLf);
417     }
418
419     if (!isNull(DB.nm_company)) {
420       oCardBuffer.append("ORG:" + getString(DB.nm_company));
421       if (!isNull(DB.tx_division)) oCardBuffer.append(";" + getString(DB.tx_division));
422       if (!isNull(DB.tx_dept)) oCardBuffer.append(";" + getString(DB.tx_dept));
423       oCardBuffer.append(CrLf);
424     }
425
426     if (!isNull(DB.tx_comments)) {
427       oCardBuffer.append("NOTE:" + getString(DB.tx_comments) + CrLf);
428     }
429
430     if (!isNull(DB.dt_modified)) {
431       oCardBuffer.append("REV:" + getDateFormated(DB.dt_modified, "yyyyMMddhhmmss") + CrLf);
432     }
433
434     oCardBuffer.append("UID:" + getString(DB.gu_fellow) + CrLf);
435
436     oCardBuffer.append("END:VCARD" + CrLf);
437
438     return oCardBuffer.toString();
439   } // vCard
440

441   // **********************************************************
442
// Metodos Estáticos
443

444   /**
445    * <p>Delete Fellow</p>
446    * <p>Calls k_sp_del_fellow stored procedure</p>
447    * @param oConn Database Connection
448    * @param sFellowGUID Identifier of Fellow to delete
449    * @throws SQLException
450    */

451   public static boolean delete(JDCConnection oConn, String JavaDoc sFellowGUID) throws SQLException JavaDoc {
452     boolean bRetVal;
453
454     if (DebugFile.trace) {
455       DebugFile.writeln("Begin Fellow.delete([Connection]," + sFellowGUID + ")");
456       DebugFile.incIdent();
457       DebugFile.writeln("Connection.prepareCall({call k_sp_del_fellow ('" + sFellowGUID + "')})");
458     }
459
460     CallableStatement JavaDoc oCall = oConn.prepareCall("{call k_sp_del_fellow ('" + sFellowGUID + "')}");
461     bRetVal = oCall.execute();
462     oCall.close();
463
464     if (DebugFile.trace) {
465       DebugFile.decIdent();
466       DebugFile.writeln("End Fellow.delete() : " + String.valueOf(bRetVal));
467     }
468
469     return bRetVal;
470   } // delete
471

472   // **********************************************************
473
// Private Variables
474

475   public static final short ClassId = 20;
476 }
477
Popular Tags