KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > crm > AttachmentUploader


1 /*
2   Copyright (C) 2006 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.crm;
34
35 import java.io.File JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.FileNotFoundException JavaDoc;
38 import java.sql.PreparedStatement JavaDoc;
39 import java.sql.ResultSet JavaDoc;
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 /**
49  * Scan a directory structure and attach files to Contacts
50  * @author Sergio Montoro Ten
51  * @version 3.0
52  */

53 public class AttachmentUploader {
54
55   private AttachmentUploader() { }
56
57   // ---------------------------------------------------------------------------
58

59   public static void main(String JavaDoc[] 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 JavaDoc oBase = new File JavaDoc(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 JavaDoc(argv[3]).booleanValue();
81       else
82         bDeleteFiles = true;
83
84       try {
85         File JavaDoc[] 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 JavaDoc 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 JavaDoc sDirName = aDirs[d].getName();
97               oStmt.setString(1, sDirName);
98               oStmt.setString(2, sDirName);
99               oStmt.setString(3, sDirName);
100               ResultSet JavaDoc 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               } // fi (next)
112
} // fi (isDirectory)
113
} // next
114
oStmt.close();
115           oConn.close("AttachmentUploader");
116           oDbbd.close();
117         } // fi (aDirs)
118
} catch (Exception JavaDoc xcpt) {
119         if (DebugFile.trace) {
120           DebugFile.writeln(xcpt.getClass().getName()+" "+xcpt.getMessage());
121           try { DebugFile.writeln(StackTraceUtil.getStackTrace(xcpt)); } catch (IOException JavaDoc ignore) {}
122         }
123         System.out.println(xcpt.getClass().getName()+" "+xcpt.getMessage());
124       }
125     } // fi
126
}
127 }
128
Popular Tags