0001: /*
0002: Copyright (C) 2003 Know Gate S.L. All rights reserved.
0003: C/Oña, 107 1º2 28050 Madrid (Spain)
0004:
0005: Redistribution and use in source and binary forms, with or without
0006: modification, are permitted provided that the following conditions
0007: are met:
0008:
0009: 1. Redistributions of source code must retain the above copyright
0010: notice, this list of conditions and the following disclaimer.
0011:
0012: 2. The end-user documentation included with the redistribution,
0013: if any, must include the following acknowledgment:
0014: "This product includes software parts from hipergate
0015: (http://www.hipergate.org/)."
0016: Alternately, this acknowledgment may appear in the software itself,
0017: if and wherever such third-party acknowledgments normally appear.
0018:
0019: 3. The name hipergate must not be used to endorse or promote products
0020: derived from this software without prior written permission.
0021: Products derived from this software may not be called hipergate,
0022: nor may hipergate appear in their name, without prior written
0023: permission.
0024:
0025: This library is distributed in the hope that it will be useful,
0026: but WITHOUT ANY WARRANTY; without even the implied warranty of
0027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
0028:
0029: You should have received a copy of hipergate License with this code;
0030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
0031: */
0032:
0033: package com.knowgate.crm;
0034:
0035: import java.io.File;
0036: import java.io.FileNotFoundException;
0037:
0038: import java.util.Date;
0039: import java.util.HashMap;
0040: import java.util.ListIterator;
0041:
0042: import java.sql.Connection;
0043: import java.sql.SQLException;
0044: import java.sql.Statement;
0045: import java.sql.PreparedStatement;
0046: import java.sql.CallableStatement;
0047: import java.sql.ResultSet;
0048:
0049: import com.knowgate.debug.DebugFile;
0050: import com.knowgate.misc.Environment;
0051: import com.knowgate.misc.Gadgets;
0052: import com.knowgate.jdc.JDCConnection;
0053: import com.knowgate.acl.ACLDomain;
0054: import com.knowgate.dataobjs.DB;
0055: import com.knowgate.dataobjs.DBBind;
0056: import com.knowgate.dataobjs.DBSubset;
0057: import com.knowgate.dataobjs.DBPersist;
0058:
0059: import com.knowgate.hipergate.Product;
0060: import com.knowgate.hipergate.Address;
0061: import com.knowgate.hipergate.DBLanguages;
0062: import com.knowgate.hipergate.Product;
0063: import com.knowgate.hipergate.ProductLocation;
0064: import com.knowgate.workareas.FileSystemWorkArea;
0065:
0066: /**
0067: * <p>Contact</p>
0068: * <p>Copyright: Copyright (c) KnowGate 2003</p>
0069: * @author Sergio Montoro Ten
0070: * @version 3.0
0071: */
0072:
0073: public class Contact extends DBPersist {
0074:
0075: /**
0076: * Create Empty Contact.
0077: */
0078: public Contact() {
0079: super (DB.k_contacts, "Contact");
0080: }
0081:
0082: /**
0083: * Create Contact and set gu_contact field.
0084: * Does not load other fields from database.
0085: * @param sIdContact Contact GUID
0086: */
0087: public Contact(String sIdContact) throws SQLException {
0088: super (DB.k_contacts, "Contact");
0089: put(DB.gu_contact, sIdContact);
0090: }
0091:
0092: /**
0093: * Create Contact and load fields from database.
0094: * @param oConn Database Connection
0095: * @param sIdContact Contact GUID
0096: */
0097: public Contact(JDCConnection oConn, String sIdContact)
0098: throws SQLException {
0099: super (DB.k_contacts, "Contact");
0100:
0101: Object aCont[] = { sIdContact };
0102:
0103: load(oConn, aCont);
0104: }
0105:
0106: // ----------------------------------------------------------
0107:
0108: /**
0109: * <p>Store Contact</p>
0110: * Automatically generates gu_contact GUID and dt_modified DATE if not explicitly set.<br>
0111: * If gu_company field is <b>null</b> and nm_legal field is not <b>null</b> then gu_company
0112: * field is automatically looked up at k_companies table and put in this DBPersist before storing it.
0113: * @param oConn Database Connection
0114: * @throws SQLException
0115: */
0116: public boolean store(JDCConnection oConn) throws SQLException {
0117: PreparedStatement oStmt;
0118: ResultSet oRSet;
0119:
0120: if (DebugFile.trace) {
0121: DebugFile.writeln("Begin Contact.store([Connection])");
0122: DebugFile.incIdent();
0123: }
0124:
0125: java.sql.Timestamp dtNow = new java.sql.Timestamp(DBBind
0126: .getTime());
0127:
0128: if (!AllVals.containsKey(DB.gu_contact))
0129: put(DB.gu_contact, Gadgets.generateUUID());
0130:
0131: if (!AllVals.containsKey(DB.gu_company)
0132: && AllVals.containsKey(DB.nm_legal)) {
0133:
0134: if (DebugFile.trace)
0135: DebugFile.writeln("Connection.prepareStatement(SELECT "
0136: + DB.gu_company + " FROM " + DB.k_companies
0137: + " WHERE " + DB.gu_workarea + "='"
0138: + getStringNull(DB.gu_workarea, "null")
0139: + "' AND " + DB.nm_legal + "='"
0140: + getStringNull(DB.nm_legal, "null") + "')");
0141:
0142: oStmt = oConn.prepareStatement("SELECT " + DB.gu_company
0143: + " FROM " + DB.k_companies + " WHERE "
0144: + DB.gu_workarea + "=? AND " + DB.nm_legal + "=?",
0145: ResultSet.TYPE_FORWARD_ONLY,
0146: ResultSet.CONCUR_READ_ONLY);
0147:
0148: oStmt.setString(1, getStringNull(DB.gu_workarea, null));
0149: oStmt.setString(2, getStringNull(DB.nm_legal, null));
0150: oRSet = oStmt.executeQuery();
0151:
0152: if (oRSet.next()) {
0153: if (DebugFile.trace)
0154: DebugFile.writeln("gu_company="
0155: + oRSet.getString(1));
0156:
0157: AllVals.put(DB.gu_company, oRSet.getString(1));
0158: } else if (DebugFile.trace)
0159: DebugFile.writeln("no company guid found for "
0160: + getStringNull(DB.nm_legal, null));
0161:
0162: oRSet.close();
0163: oStmt.close();
0164: } // fi (gu_company==null && nm_legal!=null)
0165:
0166: replace(DB.dt_modified, dtNow);
0167:
0168: boolean bRetVal = super .store(oConn);
0169:
0170: if (DebugFile.trace) {
0171: DebugFile.decIdent();
0172: DebugFile.writeln("End Contact.store() : "
0173: + String.valueOf(bRetVal));
0174: }
0175:
0176: return bRetVal;
0177: } // store
0178:
0179: // ----------------------------------------------------------
0180:
0181: /**
0182: * Delete Contact.
0183: * @throws SQLException
0184: */
0185: public boolean delete(JDCConnection oConn) throws SQLException {
0186: return Contact.delete(oConn, getString(DB.gu_contact));
0187: }
0188:
0189: // ----------------------------------------------------------
0190:
0191: /**
0192: * <P>Add an Address to this Contact</P>
0193: * If contact is already associated to the given address then a foreign key violation is thrown
0194: * @param oConn Database Connection
0195: * @throws SQLException
0196: * @since 3.0
0197: */
0198: public boolean addAddress(JDCConnection oConn, String sAddrGUID)
0199: throws SQLException {
0200: PreparedStatement oStmt = null;
0201: boolean bRetVal;
0202:
0203: try {
0204: oStmt = oConn.prepareStatement("INSERT INTO "
0205: + DB.k_x_contact_addr + " (" + DB.gu_contact + ","
0206: + DB.gu_address + ") VALUES (?,?)");
0207: oStmt.setString(1, getStringNull(DB.gu_contact, null));
0208: oStmt.setString(2, sAddrGUID);
0209: int iAffected = oStmt.executeUpdate();
0210: oStmt.close();
0211: oStmt = null;
0212: bRetVal = (iAffected > 0);
0213: } catch (SQLException sqle) {
0214: bRetVal = false;
0215: try {
0216: if (oStmt != null)
0217: oStmt.close();
0218: } catch (Exception ignore) {
0219: }
0220: }
0221: return bRetVal;
0222: } // addAddress
0223:
0224: // ----------------------------------------------------------
0225:
0226: /**
0227: * <P>Add a bank account to this Contact</P>
0228: * If contact is already associated to the given bank account then a foreign key violation SQLException is thrown
0229: * @param oConn Database Connection
0230: * @throws SQLException
0231: * @since 3.0
0232: */
0233: public boolean addBankAccount(JDCConnection oConn,
0234: String sFullBankAccount) throws SQLException {
0235: PreparedStatement oStmt = null;
0236: boolean bRetVal;
0237:
0238: try {
0239: oStmt = oConn.prepareStatement("INSERT INTO "
0240: + DB.k_x_contact_bank + " (" + DB.gu_contact + ","
0241: + DB.nu_bank_acc + "," + DB.gu_workarea
0242: + ") VALUES (?,?,?)");
0243: oStmt.setString(1, getStringNull(DB.gu_contact, null));
0244: oStmt.setString(2, sFullBankAccount);
0245: oStmt.setString(3, getStringNull(DB.gu_workarea, null));
0246: int iAffected = oStmt.executeUpdate();
0247: oStmt.close();
0248: oStmt = null;
0249: bRetVal = (iAffected > 0);
0250: } catch (SQLException sqle) {
0251: bRetVal = false;
0252: try {
0253: if (oStmt != null)
0254: oStmt.close();
0255: } catch (Exception ignore) {
0256: }
0257: }
0258: return bRetVal;
0259: } // addBankAccount
0260:
0261: // ----------------------------------------------------------
0262:
0263: /**
0264: * Get Contact Address by index
0265: * @param oConn JDCConnection
0266: * @param iIndex int Address index as set at column ix_address of k_addresses table
0267: * @return Address or <b>null</b> if no Address with such index was found
0268: * @throws SQLException
0269: * @since 3.0
0270: */
0271: public Address getAddress(JDCConnection oConn, int iIndex)
0272: throws SQLException {
0273: String sGuAddr;
0274: PreparedStatement oStmt = oConn
0275: .prepareStatement("SELECT a." + DB.gu_address
0276: + " FROM " + DB.k_addresses + " a,"
0277: + DB.k_x_contact_addr + " x WHERE a."
0278: + DB.gu_address + "=x." + DB.gu_address
0279: + " AND x." + DB.gu_contact + "=? AND a."
0280: + DB.ix_address + "=?",
0281: ResultSet.TYPE_FORWARD_ONLY,
0282: ResultSet.CONCUR_READ_ONLY);
0283: oStmt.setString(1, getStringNull(DB.gu_contact, null));
0284: oStmt.setInt(2, iIndex);
0285: ResultSet oRSet = oStmt.executeQuery();
0286: if (oRSet.next())
0287: sGuAddr = oRSet.getString(1);
0288: else
0289: sGuAddr = null;
0290: oRSet.close();
0291: oStmt.close();
0292: if (null != sGuAddr)
0293: return new Address(oConn, sGuAddr);
0294: else
0295: return null;
0296: }
0297:
0298: // ----------------------------------------------------------
0299:
0300: /**
0301: * Get Contact Address by location type
0302: * @param oConn JDCConnection
0303: * @param sTpLocation String Address type as set at column tp_location of k_addresses table
0304: * @return Address or <b>null</b> if no Address with such location type was found
0305: * @throws SQLException
0306: * @since 2.2
0307: */
0308: public Address getAddress(JDCConnection oConn, String sTpLocation)
0309: throws SQLException {
0310: String sGuAddr;
0311: PreparedStatement oStmt = oConn
0312: .prepareStatement("SELECT a." + DB.gu_address
0313: + " FROM " + DB.k_addresses + " a,"
0314: + DB.k_x_contact_addr + " x WHERE a."
0315: + DB.gu_address + "=x." + DB.gu_address
0316: + " AND x." + DB.gu_contact + "=? AND a."
0317: + DB.tp_location + "=?",
0318: ResultSet.TYPE_FORWARD_ONLY,
0319: ResultSet.CONCUR_READ_ONLY);
0320: oStmt.setString(1, getStringNull(DB.gu_contact, null));
0321: oStmt.setString(2, sTpLocation);
0322: ResultSet oRSet = oStmt.executeQuery();
0323: if (oRSet.next())
0324: sGuAddr = oRSet.getString(1);
0325: else
0326: sGuAddr = null;
0327: oRSet.close();
0328: oStmt.close();
0329: if (null != sGuAddr)
0330: return new Address(oConn, sGuAddr);
0331: else
0332: return null;
0333: }
0334:
0335: // ----------------------------------------------------------
0336:
0337: /**
0338: * <p>Get Contact Addresses</p>
0339: * @param oConn Database Connection
0340: * @return A DBSubset with all columns from k_addresses for Contact
0341: * @throws SQLException
0342: * @throws NullPointerException If gu_contact is <b>null</b>
0343: */
0344: public DBSubset getAddresses(JDCConnection oConn)
0345: throws SQLException {
0346: if (DebugFile.trace) {
0347: DebugFile
0348: .writeln("Begin Contact.getAddresses([Connection])");
0349: DebugFile.incIdent();
0350: }
0351:
0352: if (isNull(DB.gu_contact))
0353: throw new NullPointerException("gu_contact not set");
0354:
0355: Address oAddr = new Address();
0356:
0357: DBSubset oAddrs = new DBSubset(DB.k_addresses, oAddr.getTable(
0358: oConn).getColumnsStr(), DB.gu_address + " IN (SELECT "
0359: + DB.gu_address + " FROM " + DB.k_x_contact_addr
0360: + " WHERE " + DB.gu_contact + "='"
0361: + getString(DB.gu_contact) + "')", 10);
0362: int iAddrs = oAddrs.load(oConn);
0363:
0364: oAddr = null;
0365:
0366: if (DebugFile.trace) {
0367: DebugFile.decIdent();
0368: DebugFile.writeln("End Contact.getAddresses() : "
0369: + String.valueOf(iAddrs));
0370: }
0371:
0372: return oAddrs;
0373: } // getAddresses
0374:
0375: // ----------------------------------------------------------
0376:
0377: /**
0378: * Get all bank accounts associated with Contact
0379: * @param oConn JDCConnection
0380: * @return DBSubset nu_bank_acc,dt_created,bo_active,tp_account,nm_bank,tx_addr,nm_cardholder,nu_card,tp_card,tx_expire,nu_pin,nu_cvv2,im_credit_limit,de_bank_acc
0381: * @throws SQLException
0382: * @throws IllegalStateException if gu_contact or gu_workarea are not set
0383: * @since 3.0
0384: */
0385: public DBSubset getAllBankAccounts(JDCConnection oConn)
0386: throws SQLException, IllegalStateException {
0387: if (isNull(DB.gu_contact))
0388: throw new IllegalStateException(
0389: "Contact.getAllBankAccounts() gu_contact property is not set");
0390: if (isNull(DB.gu_workarea))
0391: throw new IllegalStateException(
0392: "Contact.getAllBankAccounts() gu_workarea property is not set");
0393:
0394: DBSubset oAccs = new DBSubset(DB.k_bank_accounts,
0395: DB.nu_bank_acc + "," + DB.dt_created + ","
0396: + DB.bo_active + "," + DB.tp_account + ","
0397: + DB.nm_bank + "," + DB.tx_addr + ","
0398: + DB.nm_cardholder + "," + DB.nu_card + ","
0399: + DB.tp_card + "," + DB.tx_expire + ","
0400: + DB.nu_pin + "," + DB.nu_cvv2 + ","
0401: + DB.im_credit_limit + "," + DB.de_bank_acc,
0402: DB.gu_workarea + "=? AND " + DB.nu_bank_acc
0403: + " IN (SELECT " + DB.nu_bank_acc + " FROM "
0404: + DB.k_x_contact_bank + " WHERE "
0405: + DB.gu_workarea + "=? AND " + DB.gu_contact
0406: + "=?)", 10);
0407:
0408: oAccs.load(oConn, new Object[] { get(DB.gu_workarea),
0409: get(DB.gu_workarea), get(DB.gu_contact) });
0410: return oAccs;
0411: } // getAllBankAccounts
0412:
0413: // ----------------------------------------------------------
0414:
0415: /**
0416: * Get active bank accounts for this Contact
0417: * @param oConn JDCConnection
0418: * @return DBSubset nu_bank_acc,dt_created,tp_account,nm_bank,tx_addr,nm_cardholder,nu_card,tp_card,tx_expire,nu_pin,nu_cvv2,im_credit_limit,de_bank_acc
0419: * @throws SQLException
0420: * @throws IllegalStateException if gu_contact or gu_workarea are not set
0421: * @since 3.0
0422: */
0423: public DBSubset getActiveBankAccounts(JDCConnection oConn)
0424: throws SQLException, IllegalStateException {
0425: if (isNull(DB.gu_contact))
0426: throw new IllegalStateException(
0427: "Contact.getActiveBankAccounts() gu_contact property is not set");
0428: if (isNull(DB.gu_workarea))
0429: throw new IllegalStateException(
0430: "Contact.getActiveBankAccounts() gu_workarea property is not set");
0431:
0432: DBSubset oAccs = new DBSubset(DB.k_bank_accounts,
0433: DB.nu_bank_acc + "," + DB.dt_created + ","
0434: + DB.tp_account + "," + DB.nm_bank + ","
0435: + DB.tx_addr + "," + DB.nm_cardholder + ","
0436: + DB.nu_card + "," + DB.tp_card + ","
0437: + DB.tx_expire + "," + DB.nu_pin + ","
0438: + DB.nu_cvv2 + "," + DB.im_credit_limit + ","
0439: + DB.de_bank_acc, DB.gu_workarea + "=? AND "
0440: + DB.bo_active + "<>0 AND " + DB.nu_bank_acc
0441: + " IN (SELECT " + DB.nu_bank_acc + " FROM "
0442: + DB.k_x_contact_bank + " WHERE "
0443: + DB.gu_workarea + "=? AND " + DB.gu_contact
0444: + "=?)", 10);
0445:
0446: oAccs.load(oConn, new Object[] { get(DB.gu_workarea),
0447: get(DB.gu_workarea), get(DB.gu_contact) });
0448: return oAccs;
0449: } // getActiveBankAccounts
0450:
0451: // ----------------------------------------------------------
0452:
0453: /**
0454: * Get unactive bank accounts for this Contact
0455: * @param oConn JDCConnection
0456: * @return DBSubset nu_bank_acc,dt_created,tp_account,nm_bank,tx_addr,nm_cardholder,nu_card,tp_card,tx_expire,nu_pin,nu_cvv2,im_credit_limit,de_bank_acc
0457: * @throws SQLException
0458: * @throws IllegalStateException if gu_contact or gu_workarea are not set
0459: * @since 3.0
0460: */
0461: public DBSubset getUnactiveBankAccounts(JDCConnection oConn)
0462: throws SQLException, IllegalStateException {
0463: if (isNull(DB.gu_company))
0464: throw new IllegalStateException(
0465: "Contact.getUnactiveBankAccounts() gu_company property is not set");
0466: if (isNull(DB.gu_workarea))
0467: throw new IllegalStateException(
0468: "Contact.getUnactiveBankAccounts() gu_contact property is not set");
0469:
0470: DBSubset oAccs = new DBSubset(DB.k_bank_accounts,
0471: DB.nu_bank_acc + "," + DB.dt_created + ","
0472: + DB.tp_account + "," + DB.nm_bank + ","
0473: + DB.tx_addr + "," + DB.nm_cardholder + ","
0474: + DB.nu_card + "," + DB.tp_card + ","
0475: + DB.tx_expire + "," + DB.nu_pin + ","
0476: + DB.nu_cvv2 + "," + DB.im_credit_limit + ","
0477: + DB.de_bank_acc, DB.gu_workarea + "=? AND "
0478: + DB.bo_active + "=0 AND " + DB.nu_bank_acc
0479: + " IN (SELECT " + DB.nu_bank_acc + " FROM "
0480: + DB.k_x_contact_bank + " WHERE "
0481: + DB.gu_workarea + "=? AND " + DB.gu_company
0482: + "=?)", 10);
0483:
0484: oAccs.load(oConn, new Object[] { get(DB.gu_workarea),
0485: get(DB.gu_workarea), get(DB.gu_contact) });
0486: return oAccs;
0487: } // getUnactiveBankAccounts
0488:
0489: // ----------------------------------------------------------
0490:
0491: /**
0492: * Add an Attachment to a Contact
0493: * @param oConn JDCConnection
0494: * @param sGuWriter String GUID of user (from k_users table) who is uploading the attachment
0495: * @param sDirPath String Physical path (directory) where file to be attached ir located
0496: * @param sFileName String Name of file to be attached
0497: * @param bDeleteOriginalFile boolean <b>true</b> if original file must be deleted after being attached
0498: * @return Attachment
0499: * @throws SQLException
0500: * @throws NullPointerException
0501: * @throws FileNotFoundException
0502: * @throws Exception
0503: * @since 3.0
0504: */
0505: public Attachment addAttachment(JDCConnection oConn,
0506: String sGuWriter, String sDirPath, String sFileName,
0507: boolean bDeleteOriginalFile) throws SQLException,
0508: NullPointerException, FileNotFoundException, Exception {
0509:
0510: if (DebugFile.trace) {
0511: DebugFile
0512: .writeln("Begin Contact.addAttachment([Connection],"
0513: + sGuWriter
0514: + ","
0515: + sDirPath
0516: + ","
0517: + sFileName
0518: + ","
0519: + String.valueOf(bDeleteOriginalFile) + ")");
0520: DebugFile.incIdent();
0521: }
0522:
0523: Date dtNow = new Date();
0524: PreparedStatement oStmt;
0525: ResultSet oRSet;
0526: String sNmLegal;
0527: String sProfile;
0528:
0529: // Check that Contact is loaded
0530: if (isNull(DB.gu_contact) || isNull(DB.gu_workarea))
0531: throw new NullPointerException(
0532: "Contact.addAttachment() Contact not loaded");
0533:
0534: if (null == sDirPath)
0535: throw new NullPointerException(
0536: "Contact.addAttachment() File path may not be null");
0537:
0538: if (null == sFileName)
0539: throw new NullPointerException(
0540: "Contact.addAttachment() File name may not be null");
0541:
0542: File oDir = new File(sDirPath);
0543: if (!oDir.isDirectory())
0544: throw new FileNotFoundException("Contact.addAttachment() "
0545: + sDirPath + " is not a directory");
0546:
0547: if (!oDir.exists())
0548: throw new FileNotFoundException(
0549: "Contact.addAttachment() Directory " + sDirPath
0550: + " not found");
0551:
0552: File oFile = new File(Gadgets.chomp(sDirPath,
0553: File.separatorChar)
0554: + sFileName);
0555: if (!oFile.exists())
0556: throw new FileNotFoundException(
0557: "Contact.addAttachment() File "
0558: + Gadgets.chomp(sDirPath,
0559: File.separatorChar) + sFileName
0560: + " not found");
0561:
0562: // Get Id. of Domain to which Contact belongs
0563: Integer iDom = ACLDomain.forWorkArea(oConn,
0564: getString(DB.gu_workarea));
0565:
0566: if (DebugFile.trace)
0567: DebugFile.writeln("id_domain=" + iDom);
0568:
0569: switch (oConn.getDataBaseProduct()) {
0570: case JDCConnection.DBMS_ORACLE:
0571: oStmt = oConn.prepareStatement("SELECT k." + DB.nm_legal
0572: + " FROM " + DB.k_contacts + " c, "
0573: + DB.k_companies + " k WHERE c." + DB.gu_company
0574: + "=k." + DB.gu_company + "(+) AND c."
0575: + DB.gu_contact + "=? AND c." + DB.gu_company
0576: + " IS NOT NULL", ResultSet.TYPE_FORWARD_ONLY,
0577: ResultSet.CONCUR_READ_ONLY);
0578: break;
0579: default:
0580: oStmt = oConn.prepareStatement("SELECT k." + DB.nm_legal
0581: + " FROM " + DB.k_contacts + " c LEFT OUTER JOIN "
0582: + DB.k_companies + " k ON c." + DB.gu_company
0583: + "=k." + DB.gu_company + " WHERE c."
0584: + DB.gu_contact + "=? AND c." + DB.gu_company
0585: + " IS NOT NULL", ResultSet.TYPE_FORWARD_ONLY,
0586: ResultSet.CONCUR_READ_ONLY);
0587: }
0588: oStmt.setString(1, getString(DB.gu_contact));
0589: oRSet = oStmt.executeQuery();
0590: if (oRSet.next())
0591: sNmLegal = Gadgets.ASCIIEncode(oRSet.getString(1));
0592: else
0593: sNmLegal = "_NOCOMPANY";
0594: oRSet.close();
0595: oStmt.close();
0596:
0597: String sCatPath = "apps/Sales/" + sNmLegal + "/"
0598: + getString(DB.gu_contact) + "/";
0599:
0600: if (DebugFile.trace)
0601: DebugFile.writeln("category path = " + sCatPath);
0602:
0603: if (null == oConn.getPool())
0604: sProfile = "hipergate";
0605: else
0606: sProfile = ((DBBind) oConn.getPool().getDatabaseBinding())
0607: .getProfileName();
0608:
0609: if (DebugFile.trace)
0610: DebugFile.writeln("profile = " + sProfile);
0611:
0612: FileSystemWorkArea oFileSys = new FileSystemWorkArea(
0613: Environment.getProfile(sProfile));
0614: oFileSys.mkstorpath(iDom.intValue(), getString(DB.gu_workarea),
0615: sCatPath);
0616:
0617: String sStorage = Environment.getProfilePath(sProfile,
0618: "storage");
0619: String sFileProtocol = Environment.getProfileVar(sProfile,
0620: "fileprotocol", "file://");
0621: String sFileServer = Environment.getProfileVar(sProfile,
0622: "fileserver", "localhost");
0623:
0624: String sWrkAHome = sStorage + "domains" + File.separator
0625: + iDom.toString() + File.separator + "workareas"
0626: + File.separator + getString(DB.gu_workarea)
0627: + File.separator;
0628: if (DebugFile.trace)
0629: DebugFile.writeln("workarea home = " + sWrkAHome);
0630:
0631: Product oProd = new Product();
0632: oProd.put(DB.nm_product, Gadgets.left(sFileName, 128));
0633: oProd.put(DB.gu_owner, sGuWriter);
0634: oProd.put(DB.dt_uploaded, dtNow);
0635: oProd.store(oConn);
0636:
0637: ProductLocation oLoca = new ProductLocation();
0638: oLoca.put(DB.gu_owner, sGuWriter);
0639: oLoca.put(DB.gu_product, oProd.get(DB.gu_product));
0640: oLoca.put(DB.dt_uploaded, dtNow);
0641: oLoca.setPath(sFileProtocol, sFileServer, sWrkAHome + sCatPath,
0642: sFileName, sFileName);
0643: oLoca.setLength(oFile.length());
0644: oLoca.replace(DB.id_cont_type, oLoca.getContainerType());
0645: oLoca.store(oConn);
0646:
0647: if (sFileProtocol.equalsIgnoreCase("ftp://"))
0648: oLoca.upload(oConn, oFileSys, "file://" + sDirPath,
0649: sFileName, "ftp://" + sFileServer + sWrkAHome
0650: + sCatPath, sFileName);
0651: else
0652: oLoca.upload(oConn, oFileSys, "file://" + sDirPath,
0653: sFileName, sFileProtocol + sWrkAHome + sCatPath,
0654: sFileName);
0655:
0656: Attachment oAttach = new Attachment();
0657: oAttach.put(DB.gu_contact, getString(DB.gu_contact));
0658: oAttach.put(DB.gu_product, oProd.getString(DB.gu_product));
0659: oAttach.put(DB.gu_location, oLoca.getString(DB.gu_location));
0660: oAttach.put(DB.gu_writer, sGuWriter);
0661: oAttach.store(oConn);
0662:
0663: if (bDeleteOriginalFile) {
0664: if (DebugFile.trace)
0665: DebugFile.writeln("deleting file "
0666: + oFile.getAbsolutePath());
0667: oFile.delete();
0668: if (DebugFile.trace)
0669: DebugFile.writeln("deleting file " + sFileName
0670: + " deleted");
0671: }
0672:
0673: if (DebugFile.trace) {
0674: DebugFile.decIdent();
0675: DebugFile.writeln("End Contact.addAttachment() : "
0676: + String.valueOf(oAttach.getInt(DB.pg_product)));
0677: }
0678:
0679: return oAttach;
0680: } // addAttachment
0681:
0682: // ----------------------------------------------------------
0683:
0684: /**
0685: * Attach all files from a given directory
0686: * @param oConn JDCConnection
0687: * @param sGuWriter String GUID of user attaching the files
0688: * @param sDirPath String Directory Path
0689: * @param bDeleteOriginalFiles boolean <b>true</b> if original files must be deleted after being attached
0690: * @throws SQLException
0691: * @throws NullPointerException
0692: * @throws FileNotFoundException
0693: * @throws Exception
0694: * @since 3.0
0695: */
0696: public void addAttachments(JDCConnection oConn, String sGuWriter,
0697: String sDirPath, boolean bDeleteOriginalFiles)
0698: throws SQLException, NullPointerException,
0699: FileNotFoundException, Exception {
0700:
0701: File oDir = new File(sDirPath);
0702:
0703: if (!oDir.exists())
0704: throw new FileNotFoundException(
0705: "Contact.addAttachment() Directory " + sDirPath
0706: + " not found");
0707:
0708: if (!oDir.isDirectory())
0709: throw new FileNotFoundException("Contact.addAttachment() "
0710: + sDirPath + " is not a directory");
0711:
0712: if (!oDir.exists())
0713: throw new FileNotFoundException(
0714: "Contact.addAttachment() Directory " + sDirPath
0715: + " not found");
0716:
0717: File[] aFiles = oDir.listFiles();
0718: if (null != aFiles) {
0719: int nFiles = aFiles.length;
0720: for (int f = 0; f < nFiles; f++)
0721: addAttachment(oConn, sGuWriter, sDirPath, aFiles[f]
0722: .getName(), false);
0723: if (bDeleteOriginalFiles) {
0724: for (int f = 0; f < nFiles; f++)
0725: aFiles[f].delete();
0726: } // fi (bDeleteOriginalFiles)
0727: } // fi
0728: } // addAttachments
0729:
0730: // ----------------------------------------------------------
0731:
0732: /**
0733: * Remove attachment
0734: * @param oConn JDCConnection
0735: * @param iPgAttachment int
0736: * @return boolean
0737: * @throws SQLException
0738: * @throws NullPointerException
0739: * @since 3.0
0740: */
0741: public boolean removeAttachment(JDCConnection oConn,
0742: int iPgAttachment) throws SQLException {
0743: Attachment oAttach = new Attachment();
0744: if (oAttach.load(oConn, new Object[] { get(DB.gu_contact),
0745: new Integer(iPgAttachment) }))
0746: return oAttach.delete(oConn);
0747: else
0748: return false;
0749: } // removeAttachment
0750:
0751: // ----------------------------------------------------------
0752:
0753: /**
0754: * Get array of products attached to this Contact
0755: * @param oConn JDCConnection
0756: * @return Attachment[] array or <b>null</b> if no products are attached to this contact
0757: * @throws SQLException
0758: * @throws NullPointerException if gu_contact is <b>null</b>
0759: * @since 3.0
0760: */
0761: public Attachment[] getAttachments(JDCConnection oConn)
0762: throws SQLException, NullPointerException {
0763:
0764: if (isNull(DB.gu_contact))
0765: throw new NullPointerException(
0766: "Contact.getAttachments() Contact not loaded");
0767:
0768: Attachment oAttach = new Attachment();
0769: Attachment[] aAttachs;
0770: DBSubset oAttachs = new DBSubset(DB.k_contact_attachs, oAttach
0771: .getTable(oConn).getColumnsStr(), DB.gu_contact + "=?",
0772: 10);
0773: int iAttachs = oAttachs.load(oConn,
0774: new Object[] { get(DB.gu_contact) });
0775: if (0 == iAttachs) {
0776: aAttachs = null;
0777: } else {
0778: aAttachs = new Attachment[iAttachs];
0779: ListIterator oCols = oAttach.getTable(oConn).getColumns()
0780: .listIterator();
0781: while (oCols.hasNext()) {
0782: String sKey = (String) oCols.next();
0783: if (!sKey.equalsIgnoreCase(DB.dt_created)) {
0784: for (int a = 0; a < iAttachs; a++) {
0785: aAttachs[a].put(sKey, oAttachs.get(sKey, a));
0786: } // next (a)
0787: } // fi (sKey!="dt_created")
0788: } // wend
0789: } // fi (iAttachs)
0790: return aAttachs;
0791: } // getAttachments
0792:
0793: // ----------------------------------------------------------
0794:
0795: /**
0796: * Get most recent WelCome Pack associated to this contact
0797: * @param oConn JDCConnection
0798: * @return WelcomePack
0799: * @throws SQLException
0800: * @since 3.0
0801: */
0802: public WelcomePack getWelcomePack(JDCConnection oConn)
0803: throws SQLException {
0804: return WelcomePack.forContact(oConn, getString(DB.gu_contact));
0805: }
0806:
0807: // **********************************************************
0808: // Static Methods
0809:
0810: /**
0811: * Delete Contact.
0812: * The delete step by step is as follws:<br>
0813: * If k_x_meeting_contact table exists, then Contact is deleted from Meetings.<br>
0814: * If k_orders table exists, then Orders for this Contact are deleted.<br>
0815: * All Contact Attachments are deleted.<br>
0816: * Stored Procedure k_sp_del_contact is called
0817: * @param oConn Database Connection
0818: * @param sContactGUID GUID of Contact to be deleted
0819: * @throws SQLException
0820: */
0821:
0822: public static boolean delete(JDCConnection oConn,
0823: String sContactGUID) throws SQLException {
0824: boolean bRetVal;
0825: Statement oUpdt;
0826: PreparedStatement oDlte;
0827:
0828: if (DebugFile.trace) {
0829: DebugFile.writeln("Begin Contact.delete([Connection], "
0830: + sContactGUID + ")");
0831: DebugFile.incIdent();
0832: }
0833:
0834: /* Desasociar los e-mails */
0835: if (DBBind.exists(oConn, DB.k_inet_addrs, "U")) {
0836: oUpdt = oConn.createStatement();
0837:
0838: if (DebugFile.trace)
0839: DebugFile.writeln("Statement.executeUpdate(UPDATE "
0840: + DB.k_inet_addrs + " SET " + DB.gu_contact
0841: + "=NULL WHERE " + DB.gu_contact + "='"
0842: + sContactGUID + "')");
0843:
0844: oUpdt.executeUpdate("UPDATE " + DB.k_inet_addrs + " SET "
0845: + DB.gu_contact + "=NULL WHERE " + DB.gu_contact
0846: + "='" + sContactGUID + "'");
0847:
0848: oUpdt.close();
0849: }
0850:
0851: /* Desasociar los proyectos */
0852: if (DBBind.exists(oConn, DB.k_projects, "U")) {
0853: oUpdt = oConn.createStatement();
0854:
0855: if (DebugFile.trace)
0856: DebugFile.writeln("Statement.executeUpdate(UPDATE "
0857: + DB.k_projects + " SET " + DB.gu_contact
0858: + "=NULL WHERE " + DB.gu_contact + "='"
0859: + sContactGUID + "')");
0860:
0861: oUpdt.executeUpdate("UPDATE " + DB.k_projects + " SET "
0862: + DB.gu_contact + "=NULL WHERE " + DB.gu_contact
0863: + "='" + sContactGUID + "'");
0864: oUpdt.close();
0865: oUpdt = null;
0866: }
0867:
0868: /* Borrar los pedidos, si existen */
0869: if (DBBind.exists(oConn, DB.k_orders, "U")) {
0870: DBSubset oOrders = new DBSubset(DB.k_orders, DB.gu_order,
0871: DB.gu_contact + "='" + sContactGUID + "'", 1000);
0872:
0873: int iOrders = oOrders.load(oConn);
0874:
0875: for (int o = 0; o < iOrders; o++)
0876: com.knowgate.hipergate.Order.delete(oConn, oOrders
0877: .getString(0, o));
0878: } // fi (exists(DB.k_orders))
0879:
0880: /* Borrar las convocatorias a actividades, si existen */
0881: if (DBBind.exists(oConn, DB.k_x_meeting_contact, "U")) {
0882: if (DebugFile.trace)
0883: DebugFile
0884: .writeln("Connection.prepareStatement(DELETE FROM "
0885: + DB.k_x_meeting_contact
0886: + " WHERE "
0887: + DB.gu_contact
0888: + "='"
0889: + sContactGUID
0890: + "')");
0891:
0892: oDlte = oConn.prepareStatement("DELETE FROM "
0893: + DB.k_x_meeting_contact + " WHERE "
0894: + DB.gu_contact + "=?");
0895:
0896: oDlte.setString(1, sContactGUID);
0897:
0898: oDlte.executeUpdate();
0899:
0900: oDlte.close();
0901:
0902: oDlte = null;
0903: } // fi (exists(oConn, DB.k_x_meeting_contact))
0904:
0905: /* Borrar las llamadas telefónicas */
0906: if (DBBind.exists(oConn, DB.k_phone_calls, "U")) {
0907: if (DebugFile.trace)
0908: DebugFile
0909: .writeln("Connection.prepareStatement(DELETE FROM "
0910: + DB.k_phone_calls
0911: + " WHERE "
0912: + DB.gu_contact
0913: + "='"
0914: + sContactGUID
0915: + "')");
0916:
0917: oDlte = oConn.prepareStatement("DELETE FROM "
0918: + DB.k_phone_calls + " WHERE " + DB.gu_contact
0919: + "=?");
0920:
0921: oDlte.setString(1, sContactGUID);
0922:
0923: oDlte.executeUpdate();
0924:
0925: oDlte.close();
0926: } // fi (exists(oConn, DB.k_phone_calls))
0927:
0928: /* Borrar las reservas en cursos */
0929: if (DBBind.exists(oConn, DB.k_x_course_bookings, "U")) {
0930: if (DebugFile.trace)
0931: DebugFile
0932: .writeln("Connection.prepareStatement(DELETE FROM "
0933: + DB.k_x_course_bookings
0934: + " WHERE "
0935: + DB.gu_contact
0936: + "='"
0937: + sContactGUID
0938: + "')");
0939:
0940: oDlte = oConn.prepareStatement("DELETE FROM "
0941: + DB.k_x_course_bookings + " WHERE "
0942: + DB.gu_contact + "=?");
0943:
0944: oDlte.setString(1, sContactGUID);
0945:
0946: oDlte.executeUpdate();
0947:
0948: oDlte.close();
0949: } // fi (exists(oConn, DB.k_x_course_bookings))
0950:
0951: DBSubset oAttachs = new DBSubset(DB.k_contact_attachs,
0952: DB.gu_product, DB.gu_contact + "='" + sContactGUID
0953: + "'", 64);
0954: int iAttachs = oAttachs.load(oConn);
0955:
0956: if (DebugFile.trace)
0957: DebugFile.writeln("new Product()");
0958:
0959: Product oProd = new Product();
0960:
0961: for (int a = 0; a < iAttachs; a++) {
0962: oProd.replace(DB.gu_product, oAttachs.getString(0, a));
0963: oProd.delete(oConn);
0964: } // next (a)
0965:
0966: oProd = null;
0967:
0968: oAttachs = null;
0969:
0970: Statement oStmt = oConn.createStatement();
0971:
0972: if (DebugFile.trace)
0973: DebugFile.writeln("Statement.executeUpdate(DELETE FROM "
0974: + DB.k_contact_attachs + " WHERE " + DB.gu_contact
0975: + "='" + sContactGUID + "')");
0976:
0977: oStmt
0978: .executeUpdate("DELETE FROM " + DB.k_contact_attachs
0979: + " WHERE " + DB.gu_contact + "='"
0980: + sContactGUID + "'");
0981: oStmt.close();
0982:
0983: if (oConn.getDataBaseProduct() == JDCConnection.DBMS_POSTGRESQL) {
0984: oStmt = oConn.createStatement();
0985: if (DebugFile.trace)
0986: DebugFile
0987: .writeln("Statement.executeQuery(SELECT k_sp_del_contact ('"
0988: + sContactGUID + "')");
0989: oStmt.executeQuery("SELECT k_sp_del_contact ('"
0990: + sContactGUID + "')");
0991: oStmt.close();
0992: bRetVal = true;
0993: } else {
0994: if (DebugFile.trace)
0995: DebugFile
0996: .writeln("Connection.prepareCall({ call k_sp_del_contact ('"
0997: + sContactGUID + "') }");
0998:
0999: CallableStatement oCall = oConn
1000: .prepareCall("{ call k_sp_del_contact ('"
1001: + sContactGUID + "') }");
1002: bRetVal = oCall.execute();
1003: oCall.close();
1004: }
1005:
1006: if (DebugFile.trace) {
1007: DebugFile.decIdent();
1008: DebugFile.writeln("End Contact.delete() : "
1009: + String.valueOf(bRetVal));
1010: }
1011:
1012: return bRetVal;
1013: } // delete
1014:
1015: /**
1016: * <p>Add a Street Type lookup value</a>
1017: * @param oConn Connection
1018: * @param sGuWorkArea String GUID of WorkArea
1019: * @param sTpPassport String Passport Type Internal Value
1020: * @param oTranslations HashMap with one entry for each language
1021: * @return boolean <b>true</b> if new passport type was added, <b>false</b> if it already existed
1022: * @throws SQLException
1023: * @since 3.0
1024: */
1025: public static boolean addLookupPassportType(Connection oConn,
1026: String sGuWorkArea, String sTpPassport,
1027: HashMap oTranslations) throws SQLException {
1028: return DBLanguages
1029: .addLookup(oConn, DB.k_contacts_lookup, sGuWorkArea,
1030: DB.tp_passport, sTpPassport, oTranslations);
1031: }
1032:
1033: /**
1034: * <p>Add a Job Title lookup value</a>
1035: * @param oConn Connection
1036: * @param sGuWorkArea String GUID of WorkArea
1037: * @param sDeTitle String Passport Type Internal Value
1038: * @param oTranslations HashMap with one entry for each language
1039: * @return boolean <b>true</b> if new job title was added, <b>false</b> if it already existed
1040: * @throws SQLException
1041: * @since 3.0
1042: */
1043: public static boolean addLookupJobTitle(Connection oConn,
1044: String sGuWorkArea, String sDeTitle, HashMap oTranslations)
1045: throws SQLException {
1046: return DBLanguages.addLookup(oConn, DB.k_contacts_lookup,
1047: sGuWorkArea, DB.de_title, sDeTitle, oTranslations);
1048: }
1049:
1050: // **********************************************************
1051: // Constantes Publicas
1052:
1053: public static final short ClassId = 90;
1054: }
|