1 32 33 package com.knowgate.crm; 34 35 import java.io.File ; 36 import java.io.IOException ; 37 import java.io.FileNotFoundException ; 38 import java.sql.PreparedStatement ; 39 import java.sql.ResultSet ; 40 41 import com.knowgate.debug.DebugFile; 42 import com.knowgate.debug.StackTraceUtil; 43 import com.knowgate.jdc.JDCConnection; 44 import com.knowgate.dataobjs.DB; 45 import com.knowgate.dataobjs.DBBind; 46 import com.knowgate.crm.Contact; 47 48 53 public class AttachmentUploader { 54 55 private AttachmentUploader() { } 56 57 59 public static void main(String [] argv) { 60 61 if (argv.length<3 || argv.length>4) { 62 System.out.println(""); 63 System.out.println("Usage:\n"); 64 System.out.println("AttachmentUploader profile_name base_path writer_guid delete_files"); 65 System.out.println("where"); 66 System.out.println("profile_name is the name without extension of a properties files for hipergate such as hipergate.cnf"); 67 System.out.println("base_path is a directory path. For example /tmp/upload/"); 68 System.out.println("writer_guid is a GUID of the user attaching the files"); 69 System.out.println("delete_files is true or false. The default value is true."); 70 } 71 72 File oBase = new File (argv[1]); 73 if (!oBase.exists()) { 74 System.out.println("Directory "+argv[1]+" does not exist"); 75 } else if (!oBase.isDirectory()) { 76 System.out.println(argv[1]+" is not a directory"); 77 } else { 78 boolean bDeleteFiles; 79 if (argv.length==4) 80 bDeleteFiles = new Boolean (argv[3]).booleanValue(); 81 else 82 bDeleteFiles = true; 83 84 try { 85 File [] aDirs = oBase.listFiles(); 86 if (aDirs!=null) { 87 DBBind oDbbd = new DBBind(argv[0]); 88 Contact oCont = new Contact(); 89 JDCConnection oConn = oDbbd.getConnection("AttachmentUploader"); 90 PreparedStatement oStmt = oConn.prepareStatement("SELECT "+DB.gu_contact+","+DB.gu_workarea+" FROM "+DB.k_contacts+" WHERE "+DB.gu_contact+"=? OR "+DB.id_ref+"=? OR "+DB.sn_passport+"=?", 91 ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 92 oConn.setAutoCommit(false); 93 int nDirs = aDirs.length; 94 for (int d=0; d<nDirs; d++) { 95 if (aDirs[d].isDirectory()) { 96 String sDirName = aDirs[d].getName(); 97 oStmt.setString(1, sDirName); 98 oStmt.setString(2, sDirName); 99 oStmt.setString(3, sDirName); 100 ResultSet oRSet = oStmt.executeQuery(); 101 if (oRSet.next()) { 102 oCont.replace(DB.gu_contact , oRSet.getString(1)); 103 oCont.replace(DB.gu_workarea, oRSet.getString(2)); 104 oRSet.close(); 105 oCont.addAttachments(oConn, argv[2], aDirs[d].getAbsolutePath(), true); 106 oConn.commit(); 107 aDirs[d].delete(); 108 } else { 109 DebugFile.writeln("AttachmentUploader.main() SQLException: No data found for Contact "+sDirName); 110 oRSet.close(); 111 } } } oStmt.close(); 115 oConn.close("AttachmentUploader"); 116 oDbbd.close(); 117 } } catch (Exception xcpt) { 119 if (DebugFile.trace) { 120 DebugFile.writeln(xcpt.getClass().getName()+" "+xcpt.getMessage()); 121 try { DebugFile.writeln(StackTraceUtil.getStackTrace(xcpt)); } catch (IOException ignore) {} 122 } 123 System.out.println(xcpt.getClass().getName()+" "+xcpt.getMessage()); 124 } 125 } } 127 } 128 | Popular Tags |