001: /*
002: Copyright (C) 2006 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.crm;
034:
035: import java.io.File;
036: import java.io.IOException;
037: import java.io.FileNotFoundException;
038: import java.sql.PreparedStatement;
039: import java.sql.ResultSet;
040:
041: import com.knowgate.debug.DebugFile;
042: import com.knowgate.debug.StackTraceUtil;
043: import com.knowgate.jdc.JDCConnection;
044: import com.knowgate.dataobjs.DB;
045: import com.knowgate.dataobjs.DBBind;
046: import com.knowgate.crm.Contact;
047:
048: /**
049: * Scan a directory structure and attach files to Contacts
050: * @author Sergio Montoro Ten
051: * @version 3.0
052: */
053: public class AttachmentUploader {
054:
055: private AttachmentUploader() {
056: }
057:
058: // ---------------------------------------------------------------------------
059:
060: public static void main(String[] argv) {
061:
062: if (argv.length < 3 || argv.length > 4) {
063: System.out.println("");
064: System.out.println("Usage:\n");
065: System.out
066: .println("AttachmentUploader profile_name base_path writer_guid delete_files");
067: System.out.println("where");
068: System.out
069: .println("profile_name is the name without extension of a properties files for hipergate such as hipergate.cnf");
070: System.out
071: .println("base_path is a directory path. For example /tmp/upload/");
072: System.out
073: .println("writer_guid is a GUID of the user attaching the files");
074: System.out
075: .println("delete_files is true or false. The default value is true.");
076: }
077:
078: File oBase = new File(argv[1]);
079: if (!oBase.exists()) {
080: System.out.println("Directory " + argv[1]
081: + " does not exist");
082: } else if (!oBase.isDirectory()) {
083: System.out.println(argv[1] + " is not a directory");
084: } else {
085: boolean bDeleteFiles;
086: if (argv.length == 4)
087: bDeleteFiles = new Boolean(argv[3]).booleanValue();
088: else
089: bDeleteFiles = true;
090:
091: try {
092: File[] aDirs = oBase.listFiles();
093: if (aDirs != null) {
094: DBBind oDbbd = new DBBind(argv[0]);
095: Contact oCont = new Contact();
096: JDCConnection oConn = oDbbd
097: .getConnection("AttachmentUploader");
098: PreparedStatement oStmt = oConn.prepareStatement(
099: "SELECT " + DB.gu_contact + ","
100: + DB.gu_workarea + " FROM "
101: + DB.k_contacts + " WHERE "
102: + DB.gu_contact + "=? OR "
103: + DB.id_ref + "=? OR "
104: + DB.sn_passport + "=?",
105: ResultSet.TYPE_FORWARD_ONLY,
106: ResultSet.CONCUR_READ_ONLY);
107: oConn.setAutoCommit(false);
108: int nDirs = aDirs.length;
109: for (int d = 0; d < nDirs; d++) {
110: if (aDirs[d].isDirectory()) {
111: String sDirName = aDirs[d].getName();
112: oStmt.setString(1, sDirName);
113: oStmt.setString(2, sDirName);
114: oStmt.setString(3, sDirName);
115: ResultSet oRSet = oStmt.executeQuery();
116: if (oRSet.next()) {
117: oCont.replace(DB.gu_contact, oRSet
118: .getString(1));
119: oCont.replace(DB.gu_workarea, oRSet
120: .getString(2));
121: oRSet.close();
122: oCont.addAttachments(oConn, argv[2],
123: aDirs[d].getAbsolutePath(),
124: true);
125: oConn.commit();
126: aDirs[d].delete();
127: } else {
128: DebugFile
129: .writeln("AttachmentUploader.main() SQLException: No data found for Contact "
130: + sDirName);
131: oRSet.close();
132: } // fi (next)
133: } // fi (isDirectory)
134: } // next
135: oStmt.close();
136: oConn.close("AttachmentUploader");
137: oDbbd.close();
138: } // fi (aDirs)
139: } catch (Exception xcpt) {
140: if (DebugFile.trace) {
141: DebugFile.writeln(xcpt.getClass().getName() + " "
142: + xcpt.getMessage());
143: try {
144: DebugFile.writeln(StackTraceUtil
145: .getStackTrace(xcpt));
146: } catch (IOException ignore) {
147: }
148: }
149: System.out.println(xcpt.getClass().getName() + " "
150: + xcpt.getMessage());
151: }
152: } // fi
153: }
154: }
|