1 32 33 package com.knowgate.crm; 34 35 import java.io.FileNotFoundException ; 36 import java.io.IOException ; 37 import java.io.UnsupportedEncodingException ; 38 39 import java.sql.SQLException ; 40 import java.sql.Statement ; 41 import java.sql.ResultSet ; 42 43 import com.knowgate.debug.DebugFile; 44 import com.knowgate.jdc.JDCConnection; 45 import com.knowgate.misc.Gadgets; 46 import com.knowgate.misc.CSVParser; 47 import com.knowgate.dataobjs.DB; 48 49 57 58 public class DirectList extends DistributionList { 59 60 private CSVParser oCSV; 61 62 64 67 public DirectList() { 68 oCSV = new CSVParser(); 69 } 70 71 73 78 public DirectList(String sCharSetName) { 79 oCSV = new CSVParser(sCharSetName); 80 } 81 82 84 88 public int errorLine() { 89 return oCSV.errorLine(); 90 } 91 92 94 98 public int getLineCount() { 99 return oCSV.getLineCount(); 100 } 101 102 104 private int[] checkValues() throws IllegalStateException { 105 106 String sMail, sName, sSurN, sSalt, sFrmt; 107 108 if (DebugFile.trace) { 109 DebugFile.writeln("Begin DirectList.checkValues()"); 110 DebugFile.incIdent(); 111 } 112 113 if (0 == oCSV.getLineCount()) 114 throw new IllegalStateException ("Trying to parse an empty file"); 115 116 int iMail = getColumnPosition(DB.tx_email); 117 int iName = getColumnPosition(DB.tx_name); 118 int iSurN = getColumnPosition(DB.tx_surname); 119 int iSalt = getColumnPosition(DB.tx_salutation); 120 int iFrmt = getColumnPosition(DB.id_format); 121 122 int CheckCodes[] = new int[oCSV.getLineCount()]; 123 124 int iLines = oCSV.getLineCount(); 125 126 for (int r=0; r<iLines; r++) { 127 CheckCodes[r] = CHECK_OK; 128 129 if (iMail>=0) { 130 sMail = getField(iMail, r); 131 if (sMail.length()>100) 132 CheckCodes[r] = CHECK_INVALID_EMAIL; 133 else if (!Gadgets.checkEMail(sMail)) 134 CheckCodes[r] = CHECK_INVALID_EMAIL; 135 } 136 137 if (iName>=0) { 138 sName = getField(iName, r); 139 if (sName.length()>100) 140 CheckCodes[r] = CHECK_NAME_TOO_LONG; 141 else if (sName.indexOf(',')>=0 || sName.indexOf(';')>=0 || sName.indexOf('`')>=0 || sName.indexOf('¨')>=0 || sName.indexOf('?')>=0 || sName.indexOf('"')>=0) 142 CheckCodes[r] = CHECK_INVALID_NAME; 143 } 144 145 if (iSurN>=0) { 146 sSurN = getField(iName, r); 147 if (sSurN.length()>100) 148 CheckCodes[r] = CHECK_SURNAME_TOO_LONG; 149 else if (sSurN.indexOf(',')>=0 || sSurN.indexOf(';')>=0 || sSurN.indexOf('`')>=0 || sSurN.indexOf('¨')>=0 || sSurN.indexOf('?')>=0 || sSurN.indexOf('"')>=0) 150 CheckCodes[r] = CHECK_INVALID_SURNAME; 151 } 152 153 if (iSalt>=0) { 154 sSalt = getField(iSalt, r); 155 if (sSalt.length()>16) 156 CheckCodes[r] = CHECK_SALUTATION_TOO_LONG; 157 else if (sSalt.indexOf(',')>=0 || sSalt.indexOf(';')>=0 || sSalt.indexOf('`')>=0 || sSalt.indexOf('¨')>=0 || sSalt.indexOf('?')>=0 || sSalt.indexOf('"')>=0) 158 CheckCodes[r] = CHECK_INVALID_SALUTATION; 159 } 160 161 if (iFrmt>=0) { 162 sFrmt = getField(iFrmt, r); 163 if (sFrmt.length()>4) 164 CheckCodes[r] = CHECK_INVALID_FORMAT; 165 else if (sFrmt.indexOf(',')>=0 || sFrmt.indexOf(';')>=0 || sFrmt.indexOf('`')>=0 || sFrmt.indexOf('¨')>=0 || sFrmt.indexOf('?')>=0 || sFrmt.indexOf('"')>=0) 166 CheckCodes[r] = CHECK_INVALID_SALUTATION; 167 } 168 169 } 171 if (DebugFile.trace) { 172 DebugFile.decIdent(); 173 DebugFile.writeln("End DirectList.checkValues()"); 174 } 175 176 return CheckCodes; 177 } 179 181 207 public int[] parseFile(String sFilePath, String sFileDescriptor) 208 throws ArrayIndexOutOfBoundsException ,NullPointerException , 209 IllegalArgumentException ,UnsupportedEncodingException , 210 IOException ,FileNotFoundException { 211 int[] aRetVals; 212 213 if (DebugFile.trace) { 214 DebugFile.writeln("Begin DirectList.parseFile(" + sFilePath + "," + sFileDescriptor + "," + ")"); 215 DebugFile.incIdent(); 216 } 217 218 oCSV.parseFile(sFilePath, sFileDescriptor); 219 220 aRetVals = checkValues(); 221 222 if (DebugFile.trace) { 223 DebugFile.decIdent(); 224 DebugFile.writeln("End DirectList.parseFile()"); 225 } 226 227 return aRetVals; 228 } 230 232 236 public int getColumnPosition(String sColumnName) { 237 return oCSV.getColumnPosition(sColumnName); 238 } 240 242 249 public String getLine(int iLine) throws IllegalStateException { 250 String sRetVal; 251 252 try { 253 sRetVal = oCSV.getLine(iLine); 254 } catch (java.io.UnsupportedEncodingException e) { sRetVal = null; } 255 256 return sRetVal; 257 } 259 261 272 273 public String getField(int iCol, int iRow) throws ArrayIndexOutOfBoundsException { 274 String sRetVal; 275 276 try { 277 sRetVal = oCSV.getField(iCol, iRow); 278 } catch (java.io.UnsupportedEncodingException e) { sRetVal = null; } 279 280 return sRetVal; 281 } 283 285 291 public String getField(String sCol, int iRow) throws ArrayIndexOutOfBoundsException { 292 293 int iCol = getColumnPosition(sCol); 294 295 if (iCol==-1) 296 throw new ArrayIndexOutOfBoundsException ("Column " + sCol + " not found"); 297 298 return getField(iCol, iRow); 299 } 300 301 303 314 public void updateList(JDCConnection oConn, String sListId, short iStatus) throws IllegalArgumentException ,IllegalStateException ,ClassCastException ,SQLException { 315 Statement oStmt; 316 ResultSet oRSet; 317 boolean bExists; 318 short iTpList; 319 320 if (DebugFile.trace) { 321 DebugFile.writeln("Begin DirectList.updateList([Connection], " + sListId + ")"); 322 DebugFile.incIdent(); 323 } 324 325 oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 326 oRSet = oStmt.executeQuery("SELECT " + DB.tp_list + " FROM " + DB.k_lists + " WHERE " + DB.gu_list + "='" + sListId + "'"); 327 bExists = oRSet.next(); 328 if (bExists) 329 iTpList = oRSet.getShort(1); 330 else 331 iTpList = -1; 332 oRSet.close(); 333 oStmt.close(); 334 335 if (!bExists) 336 throw new IllegalArgumentException ("List does not exist"); 337 338 if (iTpList==com.knowgate.crm.DistributionList.TYPE_DYNAMIC) 339 throw new IllegalArgumentException ("Dynamic lists cannot be updated by directly loading members"); 340 341 if (0 == oCSV.getLineCount()) 342 throw new IllegalStateException ("Must call parseFile() on a valid non-empty delimited file before calling updateList() method"); 343 344 int iMail = getColumnPosition(DB.tx_email); 345 int iName = getColumnPosition(DB.tx_name); 346 int iSurN = getColumnPosition(DB.tx_surname); 347 int iSalt = getColumnPosition(DB.tx_salutation); 348 int iFrmt = getColumnPosition(DB.id_format); 349 350 int ChkCods[] = checkValues(); 351 352 ListMember oMbr = new ListMember(); 353 354 oMbr.put(DB.tp_member, ListMember.ClassId); 355 oMbr.put(DB.bo_active, iStatus); 356 357 int iLines = oCSV.getLineCount(); 358 359 for (int r=0; r<iLines; r++) { 360 361 if (ChkCods[r] == CHECK_OK) { 362 363 oMbr.replace(DB.gu_member, Gadgets.generateUUID()); 364 365 oMbr.replace(DB.tx_email, getField(iMail, r)); 366 367 oMbr.replace(DB.tx_name, getField(iName, r)); 368 369 oMbr.replace(DB.tx_surname, getField(iSurN, r)); 370 371 oMbr.replace(DB.tx_salutation, getField(iSalt, r)); 372 373 oMbr.replace(DB.tp_member, ListMember.ClassId); 374 375 if (iFrmt>=0) 376 oMbr.replace(DB.id_format, getField(iFrmt, r).toUpperCase()); 377 else 378 oMbr.replace(DB.id_format, "TXT"); 379 380 try { 381 oMbr.store(oConn, sListId); 382 } 383 catch (NoSuchFieldException nsfe) { 384 385 } 386 } } 389 if (DebugFile.trace) { 390 DebugFile.decIdent(); 391 DebugFile.writeln("End DirectList.updateList()"); 392 } 393 } 395 396 398 408 public void removeFromList(JDCConnection oConn, String sListId) throws IllegalArgumentException ,SQLException ,ClassCastException { 409 Statement oDlte; 410 Statement oStmt; 411 ResultSet oRSet; 412 boolean bExists; 413 short iTpList; 414 String sSQL; 415 416 if (DebugFile.trace) { 417 DebugFile.writeln("Begin DirectList.removeFromList([Connection], " + sListId + ")"); 418 DebugFile.incIdent(); 419 } 420 421 oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 422 oRSet = oStmt.executeQuery("SELECT " + DB.tp_list + " FROM " + DB.k_lists + " WHERE " + DB.gu_list + "='" + sListId + "'"); 423 bExists = oRSet.next(); 424 if (bExists) 425 iTpList = oRSet.getShort(1); 426 else 427 iTpList = -1; 428 oRSet.close(); 429 oStmt.close(); 430 431 if (!bExists) 432 throw new IllegalArgumentException ("List does not exist"); 433 434 if (iTpList==com.knowgate.crm.DistributionList.TYPE_DYNAMIC) 435 throw new ClassCastException ("Dynamic lists cannot be updated by directly removing members"); 436 437 if (0 == oCSV.getLineCount()) 438 throw new IllegalStateException ("Must call parseFile() on a valid non-empty delimited file before calling updateList() method"); 439 440 int iMail = getColumnPosition(DB.tx_email); 441 442 int ChkCods[] = checkValues(); 443 444 int iLines = oCSV.getLineCount(); 445 446 sSQL = "DELETE FROM " + DB.k_x_list_members + " WHERE " + DB.gu_list + "='" + sListId + "' AND " + DB.tx_email + "="; 447 448 oDlte = oConn.createStatement(); 449 450 for (int r=0; r<iLines; r++) { 451 if (ChkCods[r] == CHECK_OK) 452 oDlte.addBatch(sSQL + "'" + getField(iMail, r) + "'"); 453 } 455 oDlte.executeBatch(); 456 oDlte.close(); 457 458 if (DebugFile.trace) { 459 DebugFile.decIdent(); 460 DebugFile.writeln("End DirectList.removeFromList()"); 461 } 462 } 464 466 469 public static final int CHECK_OK = 0; 470 public static final int CHECK_INVALID_EMAIL = 1; 471 public static final int CHECK_NAME_TOO_LONG = 2; 472 public static final int CHECK_SURNAME_TOO_LONG = 4; 473 public static final int CHECK_INVALID_FORMAT = 8; 474 public static final int CHECK_SALUTATION_TOO_LONG = 16; 475 public static final int CHECK_INVALID_NAME = 32; 476 public static final int CHECK_INVALID_SURNAME = 64; 477 public static final int CHECK_INVALID_SALUTATION = 128; 478 479 482 public static final short ClassId = 96; 483 484 } 485 | Popular Tags |