1 32 33 package com.knowgate.addrbook; 34 35 import java.io.InputStream ; 36 import java.io.ByteArrayOutputStream ; 37 38 import java.sql.Connection ; 39 import java.sql.SQLException ; 40 import java.sql.PreparedStatement ; 41 import java.sql.CallableStatement ; 42 import java.sql.ResultSet ; 43 import java.sql.ResultSetMetaData ; 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 ; 57 58 63 64 public class Fellow extends DBPersist { 65 public Fellow() { 66 super(DB.k_fellows, "Fellow"); 67 } 68 69 71 76 77 public Fellow(String sFellowId) { 78 super(DB.k_fellows, "Fellow"); 79 put(DB.gu_fellow, sFellowId); 80 } 81 82 84 90 public Fellow(JDCConnection oConn, String sFellowId) throws SQLException { 91 super(DB.k_fellows, "Fellow"); 92 load ((Connection ) oConn, new Object [] { sFellowId }); 93 } 94 95 97 102 public void clone(DBPersist oUsr) { 103 String sField; 104 String sSurName; 105 String sSurName1 = ""; 106 String 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 oFields = oUsr.getItems().iterator(); 114 115 while (oFields.hasNext()) { 116 sField = (String ) 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 } 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 } 140 146 public boolean hasPhoto(JDCConnection oConn) throws SQLException { 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 oStmt = oConn.prepareStatement("SELECT NULL FROM " + DB.k_fellows_attach + " WHERE " + DB.gu_fellow + "=?"); 156 oStmt.setString(1, getString(DB.gu_fellow)); 157 ResultSet 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 } 170 172 178 public byte[] getPhoto (Connection oConn) 179 throws SQLException { 180 181 if (DebugFile.trace) { 182 DebugFile.writeln("Begin Fellow.getPhoto([Connection])"); 183 DebugFile.incIdent(); 184 } 185 186 ByteArrayOutputStream oOut = null; 187 PreparedStatement 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 oRSet = oStmt.executeQuery(); 190 191 if (oRSet.next()) { 192 int iLen = oRSet.getInt(1); 193 194 if (iLen>0) 195 oOut = new ByteArrayOutputStream (iLen); 196 else 197 oOut = new ByteArrayOutputStream (); 198 199 byte[] oBuffer = new byte[4000]; 200 201 InputStream 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 ignore) { } 210 } while (4000==iReaded); 211 212 try { oBlob.close(); } catch (java.io.IOException 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 } 231 233 public boolean load(JDCConnection oConn, Object [] PKVals) throws SQLException { 234 return load((Connection ) oConn, PKVals); 235 } 236 237 239 public boolean load(Connection oConn, Object [] PKVals) throws SQLException { 240 241 Object oCol; 242 PreparedStatement oStmt; 243 ResultSet oRSet; 244 ResultSetMetaData 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 ) 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 } } 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 ) 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 } } 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 ) 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 } 319 321 public boolean store(JDCConnection oConn) throws SQLException { 322 java.sql.Timestamp dtNow = new java.sql.Timestamp (DBBind.getTime()); 323 324 if (!AllVals.containsKey(DB.gu_fellow)) 326 put(DB.gu_fellow, Gadgets.generateUUID()); 327 328 replace(DB.dt_modified, dtNow); 330 331 return super.store(oConn); 332 } 333 334 336 public boolean delete(JDCConnection oConn) throws SQLException { 337 338 return Fellow.delete(oConn, getString(DB.gu_fellow)); 339 } 340 341 343 351 public String vCard(Connection oConn) 352 throws SQLException { 353 354 final String CrLf = new String (new byte[]{13,10}); 355 356 StringBuffer oCardBuffer = new StringBuffer (); 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 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 } 441 444 451 public static boolean delete(JDCConnection oConn, String sFellowGUID) throws SQLException { 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 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 } 472 475 public static final short ClassId = 20; 476 } 477 | Popular Tags |