001: /*
002: Copyright (C) 2003 Know Gate S.L. All rights reserved.
003: C/Oña, 107 1º2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.addrbook;
034:
035: import java.io.InputStream;
036: import java.io.ByteArrayOutputStream;
037:
038: import java.sql.Connection;
039: import java.sql.SQLException;
040: import java.sql.PreparedStatement;
041: import java.sql.CallableStatement;
042: import java.sql.ResultSet;
043: import java.sql.ResultSetMetaData;
044:
045: import com.knowgate.debug.DebugFile;
046:
047: import com.knowgate.jdc.JDCConnection;
048: import com.knowgate.dataobjs.DB;
049: import com.knowgate.dataobjs.DBBind;
050: import com.knowgate.dataobjs.DBPersist;
051: import com.knowgate.misc.Gadgets;
052: import com.knowgate.misc.Base64Encoder;
053:
054: import com.knowgate.misc.Gadgets;
055:
056: import java.util.Iterator;
057:
058: /**
059: * <p>Enterprise Fellow</p>
060: * @author Sergio Montoro ten
061: * @version 3.0
062: */
063:
064: public class Fellow extends DBPersist {
065: public Fellow() {
066: super (DB.k_fellows, "Fellow");
067: }
068:
069: //-------------------------------------------------------------------
070:
071: /**
072: * Construct object and set gu_fellow field.
073: * Object is not fully loaded from database.
074: * @param sFellowId Fellow Unique Identifier
075: */
076:
077: public Fellow(String sFellowId) {
078: super (DB.k_fellows, "Fellow");
079: put(DB.gu_fellow, sFellowId);
080: }
081:
082: //-------------------------------------------------------------------
083:
084: /**
085: * <p>Construct object and load fields from database.</p>
086: * @param oConn database Connection
087: * @param sFellowId Fellow Unique Identifier
088: * @throws SQLException
089: */
090: public Fellow(JDCConnection oConn, String sFellowId)
091: throws SQLException {
092: super (DB.k_fellows, "Fellow");
093: load((Connection) oConn, new Object[] { sFellowId });
094: }
095:
096: //-------------------------------------------------------------------
097:
098: /**
099: * <p>Create a fellow from a {@link ACLUser} or from another fellow
100: * <p>The fellow is internally stored at this object but NOT persisted to database.</p>
101: * @param oUsr ACLUser Identifier or Fellow identifier.
102: */
103: public void clone(DBPersist oUsr) {
104: String sField;
105: String sSurName;
106: String sSurName1 = "";
107: String sSurName2 = "";
108:
109: if (DebugFile.trace) {
110: DebugFile.writeln("Begin Fellow.clone("
111: + oUsr.getStringNull(DB.gu_user, "null") + ")");
112: DebugFile.incIdent();
113: }
114:
115: Iterator oFields = oUsr.getItems().iterator();
116:
117: while (oFields.hasNext()) {
118: sField = (String) oFields.next();
119:
120: if (sField.equals(DB.tx_main_email))
121: put(DB.tx_email, oUsr.get(DB.tx_main_email));
122: else if (sField.equals(DB.nm_user))
123: put(DB.tx_name, oUsr.get(DB.nm_user));
124: else if (sField.equals(DB.tx_surname1))
125: sSurName1 = oUsr.getStringNull(DB.tx_surname1, "");
126: else if (sField.equals(DB.tx_surname2))
127: sSurName2 = " "
128: + oUsr.getStringNull(DB.tx_surname2, "");
129: else
130: put(sField, oUsr.get(sField));
131: } // wend
132:
133: sSurName = sSurName1 + sSurName2;
134:
135: if (sSurName.trim().length() > 0)
136: put(DB.tx_surname, sSurName);
137:
138: if (DebugFile.trace) {
139: DebugFile.decIdent();
140: DebugFile.writeln("End Fellow.clone()");
141: }
142: } // clone
143:
144: // ----------------------------------------------------------
145: /**
146: * @param oConn Database Connection
147: * @return <b>true</b> if Fellow has an associated record at k_fellows_attach table</p>
148: * @throws SQLException
149: */
150: public boolean hasPhoto(JDCConnection oConn) throws SQLException {
151:
152: if (DebugFile.trace) {
153: DebugFile.writeln("Begin Fellow.hasPhoto([Connection])");
154: DebugFile.incIdent();
155:
156: DebugFile
157: .writeln("Connection.prepareStatement(SELECT NULL FROM "
158: + DB.k_fellows_attach
159: + " WHERE "
160: + DB.gu_fellow
161: + "="
162: + getStringNull(DB.gu_fellow, "null") + ")");
163: }
164:
165: PreparedStatement oStmt = oConn
166: .prepareStatement("SELECT NULL FROM "
167: + DB.k_fellows_attach + " WHERE "
168: + DB.gu_fellow + "=?");
169: oStmt.setString(1, getString(DB.gu_fellow));
170: ResultSet oRSet = oStmt.executeQuery();
171: boolean bHasAttach = oRSet.next();
172: oRSet.close();
173: oStmt.close();
174:
175: if (DebugFile.trace) {
176: DebugFile.decIdent();
177: DebugFile.writeln("End Fellow.hasPhoto() : "
178: + String.valueOf(bHasAttach));
179: }
180:
181: return bHasAttach;
182: } // hasPhoto
183:
184: // ----------------------------------------------------------
185:
186: /**
187: * <P>Get Fellow Photo as a byte array</P>
188: * @param oConn JDBC Connection
189: * @return Byte Array or <b>null</b> if Fellow does not have a photograph.
190: * @throws SQLException
191: */
192: public byte[] getPhoto(Connection oConn) throws SQLException {
193:
194: if (DebugFile.trace) {
195: DebugFile.writeln("Begin Fellow.getPhoto([Connection])");
196: DebugFile.incIdent();
197: }
198:
199: ByteArrayOutputStream oOut = null;
200: PreparedStatement oStmt = oConn
201: .prepareStatement("SELECT " + DB.len_file + ","
202: + DB.bin_file + " FROM " + DB.k_fellows_attach
203: + " WHERE " + DB.gu_fellow + "=?",
204: ResultSet.TYPE_FORWARD_ONLY,
205: ResultSet.CONCUR_READ_ONLY);
206: oStmt.setString(1, getString(DB.gu_fellow));
207: ResultSet oRSet = oStmt.executeQuery();
208:
209: if (oRSet.next()) {
210: int iLen = oRSet.getInt(1);
211:
212: if (iLen > 0)
213: oOut = new ByteArrayOutputStream(iLen);
214: else
215: oOut = new ByteArrayOutputStream();
216:
217: byte[] oBuffer = new byte[4000];
218:
219: InputStream oBlob = oRSet.getBinaryStream(2);
220:
221: int iReaded = 0;
222: do {
223: try {
224: iReaded = oBlob.read(oBuffer, 0, 4000);
225: if (iReaded > 0)
226: oOut.write(oBuffer, 0, iReaded);
227: } catch (java.io.IOException ignore) {
228: }
229: } while (4000 == iReaded);
230:
231: try {
232: oBlob.close();
233: } catch (java.io.IOException ignore) {
234: }
235:
236: oBlob = null;
237: }
238:
239: oRSet.close();
240: oStmt.close();
241:
242: if (DebugFile.trace) {
243: DebugFile.decIdent();
244: DebugFile.writeln("End Fellow.getPhoto()");
245: }
246:
247: if (oOut != null)
248: return oOut.toByteArray();
249: else
250: return null;
251: } // getPhoto
252:
253: // ----------------------------------------------------------
254:
255: public boolean load(JDCConnection oConn, Object[] PKVals)
256: throws SQLException {
257: return load((Connection) oConn, PKVals);
258: }
259:
260: // ----------------------------------------------------------
261:
262: public boolean load(Connection oConn, Object[] PKVals)
263: throws SQLException {
264:
265: Object oCol;
266: PreparedStatement oStmt;
267: ResultSet oRSet;
268: ResultSetMetaData oMDat;
269: int iCols;
270: boolean bRetVal = false;
271:
272: if (DebugFile.trace) {
273: DebugFile.writeln("Begin Fellow.load([Connection], "
274: + PKVals[0] + ")");
275: DebugFile.incIdent();
276: }
277:
278: oStmt = oConn
279: .prepareStatement("SELECT * FROM " + DB.k_fellows
280: + " WHERE " + DB.gu_fellow + "=?",
281: ResultSet.TYPE_FORWARD_ONLY,
282: ResultSet.CONCUR_READ_ONLY);
283: oStmt.setString(1, (String) PKVals[0]);
284: oRSet = oStmt.executeQuery();
285: oMDat = oRSet.getMetaData();
286: iCols = oMDat.getColumnCount();
287:
288: if (oRSet.next()) {
289: bRetVal = true;
290:
291: for (int c = 1; c <= iCols; c++) {
292: if (!oMDat.getColumnName(c).equalsIgnoreCase(
293: DB.dt_created)) {
294: oCol = oRSet.getObject(c);
295: if (!oRSet.wasNull())
296: AllVals.put(oMDat.getColumnName(c)
297: .toLowerCase(), oCol);
298: }
299: } // next
300: } // fi (oRSet.next())
301:
302: oRSet.close();
303: oStmt.close();
304:
305: if (bRetVal) {
306: if (DebugFile.trace)
307: DebugFile
308: .writeln("Connection.prepareStatement(SELECT * FROM "
309: + DB.k_users
310: + " WHERE "
311: + DB.gu_user
312: + "='"
313: + getStringNull(DB.gu_fellow, "null")
314: + "')");
315:
316: oStmt = oConn.prepareStatement("SELECT * FROM "
317: + DB.k_users + " WHERE " + DB.gu_user + "=?",
318: ResultSet.TYPE_FORWARD_ONLY,
319: ResultSet.CONCUR_READ_ONLY);
320: oStmt.setString(1, (String) PKVals[0]);
321: oRSet = oStmt.executeQuery();
322: oMDat = oRSet.getMetaData();
323: iCols = oMDat.getColumnCount();
324:
325: if (oRSet.next()) {
326: for (int c = 1; c <= iCols; c++) {
327: if (!oMDat.getColumnName(c).equalsIgnoreCase(
328: DB.dt_created)
329: && !AllVals.containsKey(oMDat
330: .getColumnName(c))) {
331: oCol = oRSet.getObject(c);
332: if (!oRSet.wasNull())
333: AllVals.put(oMDat.getColumnName(c)
334: .toLowerCase(), oCol);
335: }
336: } // next
337: } // fi
338:
339: oRSet.close();
340: oStmt.close();
341:
342: if (DebugFile.trace)
343: DebugFile.writeln("Connection.prepareStatement(SELECT "
344: + DB.tx_file + "," + DB.len_file + " FROM "
345: + DB.k_fellows_attach + " WHERE "
346: + DB.gu_fellow + "='"
347: + getStringNull(DB.gu_fellow, "null") + "')");
348:
349: oStmt = oConn.prepareStatement("SELECT " + DB.tx_file + ","
350: + DB.len_file + " FROM " + DB.k_fellows_attach
351: + " WHERE " + DB.gu_fellow + "=?");
352: oStmt.setString(1, (String) PKVals[0]);
353: oRSet = oStmt.executeQuery();
354: if (oRSet.next()) {
355: put(DB.tx_file, oRSet.getString(1));
356: put(DB.len_file, oRSet.getInt(2));
357: }
358: oRSet.close();
359: oStmt.close();
360: }
361:
362: if (DebugFile.trace) {
363: DebugFile.decIdent();
364: DebugFile.writeln("End Fellow.load() : "
365: + String.valueOf(bRetVal));
366: }
367:
368: return bRetVal;
369: } // load
370:
371: // ----------------------------------------------------------
372:
373: public boolean store(JDCConnection oConn) throws SQLException {
374: java.sql.Timestamp dtNow = new java.sql.Timestamp(DBBind
375: .getTime());
376:
377: // Si no se especificó un identificador, entonces añadirlo automáticamente
378: if (!AllVals.containsKey(DB.gu_fellow))
379: put(DB.gu_fellow, Gadgets.generateUUID());
380:
381: // Forzar la fecha de modificación del registro
382: replace(DB.dt_modified, dtNow);
383:
384: return super .store(oConn);
385: }
386:
387: // ----------------------------------------------------------
388:
389: public boolean delete(JDCConnection oConn) throws SQLException {
390:
391: return Fellow.delete(oConn, getString(DB.gu_fellow));
392: }
393:
394: // ----------------------------------------------------------
395:
396: /**
397: *
398: * @return
399: * @throws SQLException
400: * @since 2.1
401: * @see <A href="http://www.faqs.org/rfcs/rfc2426.html">RFC 2426 (Plain Text)</A><BR>
402: * @see <A href="http://www.imc.org/pdi/vcard-21.doc">vCard 2.1 Spec (MS Word format)</A><BR>
403: */
404: public String vCard(Connection oConn) throws SQLException {
405:
406: final String CrLf = new String(new byte[] { 13, 10 });
407:
408: StringBuffer oCardBuffer = new StringBuffer();
409:
410: oCardBuffer.append("BEGIN:VCARD" + CrLf);
411: oCardBuffer.append("VERSION:3.0" + CrLf);
412: oCardBuffer.append("PRODID:hipergate" + CrLf);
413: oCardBuffer.append("CLASS:PUBLIC" + CrLf);
414:
415: oCardBuffer.append("FN:");
416: if (!isNull(DB.tx_name))
417: oCardBuffer.append(getString(DB.tx_name));
418: if (!isNull(DB.tx_surname))
419: oCardBuffer.append(" " + getString(DB.tx_surname));
420: oCardBuffer.append(CrLf);
421:
422: oCardBuffer.append("N:");
423: oCardBuffer.append(getStringNull(DB.tx_surname1, "") + ";");
424: oCardBuffer.append(getStringNull(DB.nm_user, "") + ";");
425: oCardBuffer.append(getStringNull(DB.tx_surname2, "") + ";");
426: oCardBuffer.append(";;" + CrLf);
427:
428: if (!isNull(DB.tx_nickname)) {
429: oCardBuffer.append("NICKNAME:" + getString(DB.tx_nickname)
430: + CrLf);
431: }
432:
433: byte[] byPhoto = getPhoto(oConn);
434:
435: if (null != byPhoto) {
436: String sType = "";
437: int iDot = getStringNull(DB.tx_file, "").lastIndexOf('.');
438: if (iDot > 0
439: && iDot < getStringNull(DB.tx_file, "").length() - 1)
440: sType = getString(DB.tx_file).substring(iDot + 1)
441: .toUpperCase();
442: if (sType.equals("JPG"))
443: sType = "JPEG";
444:
445: if (sType.length() > 0)
446: oCardBuffer.append("PHOTO;ENCODING=BASE64;TYPE="
447: + sType + ":" + Base64Encoder.encode(byPhoto)
448: + CrLf);
449: }
450:
451: if (!isNull(DB.dt_birth)) {
452: oCardBuffer.append("BDAY:"
453: + getDateFormated(DB.dt_birth, "yyyyMMdd") + CrLf);
454: }
455:
456: if (!isNull(DB.work_phone)) {
457: oCardBuffer.append("TEL;WORK:" + getString(DB.work_phone)
458: + CrLf);
459: }
460:
461: if (!isNull(DB.home_phone)) {
462: oCardBuffer.append("TEL;HOME:" + getString(DB.home_phone)
463: + CrLf);
464: }
465:
466: if (!isNull(DB.mov_phone)) {
467: oCardBuffer.append("TEL;CELL:" + getString(DB.mov_phone)
468: + CrLf);
469: }
470:
471: if (!isNull(DB.tx_email)) {
472: oCardBuffer.append("EMAIL;INTERNET:"
473: + getString(DB.tx_email) + CrLf);
474: }
475:
476: if (!isNull(DB.tx_timezone)) {
477: oCardBuffer
478: .append("TZ:" + getString(DB.tx_timezone) + CrLf);
479: }
480:
481: if (!isNull(DB.de_title)) {
482: oCardBuffer
483: .append("TITLE:" + getString(DB.de_title) + CrLf);
484: }
485:
486: if (!isNull(DB.nm_company)) {
487: oCardBuffer.append("ORG:" + getString(DB.nm_company));
488: if (!isNull(DB.tx_division))
489: oCardBuffer.append(";" + getString(DB.tx_division));
490: if (!isNull(DB.tx_dept))
491: oCardBuffer.append(";" + getString(DB.tx_dept));
492: oCardBuffer.append(CrLf);
493: }
494:
495: if (!isNull(DB.tx_comments)) {
496: oCardBuffer.append("NOTE:" + getString(DB.tx_comments)
497: + CrLf);
498: }
499:
500: if (!isNull(DB.dt_modified)) {
501: oCardBuffer.append("REV:"
502: + getDateFormated(DB.dt_modified, "yyyyMMddhhmmss")
503: + CrLf);
504: }
505:
506: oCardBuffer.append("UID:" + getString(DB.gu_fellow) + CrLf);
507:
508: oCardBuffer.append("END:VCARD" + CrLf);
509:
510: return oCardBuffer.toString();
511: } // vCard
512:
513: // **********************************************************
514: // Metodos Estáticos
515:
516: /**
517: * <p>Delete Fellow</p>
518: * <p>Calls k_sp_del_fellow stored procedure</p>
519: * @param oConn Database Connection
520: * @param sFellowGUID Identifier of Fellow to delete
521: * @throws SQLException
522: */
523: public static boolean delete(JDCConnection oConn, String sFellowGUID)
524: throws SQLException {
525: boolean bRetVal;
526:
527: if (DebugFile.trace) {
528: DebugFile.writeln("Begin Fellow.delete([Connection],"
529: + sFellowGUID + ")");
530: DebugFile.incIdent();
531: DebugFile
532: .writeln("Connection.prepareCall({call k_sp_del_fellow ('"
533: + sFellowGUID + "')})");
534: }
535:
536: CallableStatement oCall = oConn
537: .prepareCall("{call k_sp_del_fellow ('" + sFellowGUID
538: + "')}");
539: bRetVal = oCall.execute();
540: oCall.close();
541:
542: if (DebugFile.trace) {
543: DebugFile.decIdent();
544: DebugFile.writeln("End Fellow.delete() : "
545: + String.valueOf(bRetVal));
546: }
547:
548: return bRetVal;
549: } // delete
550:
551: // **********************************************************
552: // Private Variables
553:
554: public static final short ClassId = 20;
555: }
|