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.util.Date;
036:
037: import java.sql.SQLException;
038: import java.sql.PreparedStatement;
039: import java.sql.ResultSet;
040: import java.sql.Timestamp;
041: import java.sql.Types;
042:
043: import com.knowgate.jdc.JDCConnection;
044: import com.knowgate.dataobjs.DB;
045: import com.knowgate.dataobjs.DBBind;
046: import com.knowgate.dataobjs.DBPersist;
047: import com.knowgate.dataobjs.DBSubset;
048: import com.knowgate.hipergate.Address;
049: import com.knowgate.misc.Gadgets;
050:
051: /**
052: * <p>Welcome Packs</p>
053: * <p>Copyright: Copyright (c) KnowGate 2006</p>
054: * @author Sergio Montoro Ten
055: * @version 3.0
056: */
057:
058: public class WelcomePack extends DBPersist {
059:
060: /**
061: * Default constructor
062: */
063: public WelcomePack() {
064: super (DB.k_welcome_packs, "WelcomePack");
065: }
066:
067: /**
068: * Create Welcome Pack and load data
069: * @param oConn JDCConnection
070: * @param sGuPack String Welcome Pack GUID
071: * @throws SQLException
072: */
073: public WelcomePack(JDCConnection oConn, String sGuPack)
074: throws SQLException {
075: super (DB.k_welcome_packs, "WelcomePack");
076: load(oConn, new Object[] { sGuPack });
077: }
078:
079: /**
080: * Get Company to which this Welcome Pack belongs
081: * @param oConn JDCConnection
082: * @return Company or <b>null</b> if this Welcome Pack is not associated to a Company
083: * @throws SQLException
084: */
085: public Company getCompany(JDCConnection oConn) throws SQLException {
086: if (isNull(DB.gu_company))
087: return null;
088: else
089: return new Company(oConn, getString(DB.gu_company));
090: }
091:
092: /**
093: * Get Contact to which this Welcome Pack belongs
094: * @param oConn JDCConnection
095: * @return Company or <b>null</b> if this Welcome Pack is not associated to a Contact
096: * @throws SQLException
097: */
098: public Contact getContact(JDCConnection oConn) throws SQLException {
099: if (isNull(DB.gu_contact))
100: return null;
101: else
102: return new Contact(oConn, getString(DB.gu_contact));
103: }
104:
105: /**
106: * Get Address to which this Welcome Pack belongs
107: * @param oConn JDCConnection
108: * @return com.knowgate.hipergate.Address or <b>null</b> if this Welcome Pack is not associated to an Address
109: * @throws SQLException
110: */
111: public Address getAddress(JDCConnection oConn) throws SQLException {
112: if (isNull(DB.gu_address))
113: return null;
114: else
115: return new Address(oConn, getString(DB.gu_address));
116: }
117:
118: /**
119: * <p>Store Welcome Pack</p>
120: * Automatically assign values to ix_pack and dt_modified fields if they are not set
121: * @param oConn JDCConnection
122: * @return boolean
123: * @throws SQLException
124: */
125: public boolean store(JDCConnection oConn) throws SQLException {
126: boolean bRetVal;
127: String sOldStatus;
128: PreparedStatement oStmt;
129:
130: boolean bNew = isNull(DB.gu_pack);
131:
132: if (bNew) {
133: sOldStatus = "";
134: put(DB.gu_pack, Gadgets.generateUUID());
135: } else {
136: oStmt = oConn.prepareStatement("SELECT " + DB.id_status
137: + "," + DB.gu_writer + " FROM "
138: + DB.k_welcome_packs + " WHERE " + DB.gu_pack
139: + "=?", ResultSet.TYPE_FORWARD_ONLY,
140: ResultSet.CONCUR_READ_ONLY);
141: oStmt.setString(1, getString(DB.gu_pack));
142: ResultSet oRSet = oStmt.executeQuery();
143: bNew = !oRSet.next();
144: if (bNew) {
145: sOldStatus = "";
146: } else {
147: sOldStatus = oRSet.getString(1);
148: if (oRSet.wasNull())
149: sOldStatus = "";
150: replace(DB.gu_writer, oRSet.getString(2));
151: }
152: oRSet.close();
153: oStmt.close();
154: } // fi (bNew)
155:
156: replace(DB.dt_modified, new Date());
157:
158: if (isNull(DB.dt_cancel)
159: && getStringNull(DB.id_status, "").equalsIgnoreCase(
160: CANCELLED)
161: && !sOldStatus.equalsIgnoreCase(CANCELLED))
162: put(DB.dt_cancel, new Date());
163:
164: if (isNull(DB.dt_sent)
165: && getStringNull(DB.id_status, "").equalsIgnoreCase(
166: SENT) && !sOldStatus.equalsIgnoreCase(SENT))
167: put(DB.dt_sent, new Date());
168:
169: if (isNull(DB.dt_delivered)
170: && getStringNull(DB.id_status, "").equalsIgnoreCase(
171: DELIVERED)
172: && !sOldStatus.equalsIgnoreCase(DELIVERED))
173: put(DB.dt_delivered, new Date());
174:
175: if (isNull(DB.dt_returned)
176: && getStringNull(DB.id_status, "").equalsIgnoreCase(
177: RETURNED)
178: && !sOldStatus.equalsIgnoreCase(RETURNED))
179: put(DB.dt_returned, new Date());
180:
181: if (bNew) {
182: put(DB.ix_pack, DBBind.nextVal(oConn, "seq_k_welcme_pak"));
183: bRetVal = super .store(oConn);
184: } else {
185: bRetVal = super .store(oConn);
186: if (!sOldStatus.equalsIgnoreCase(getStringNull(
187: DB.id_status, ""))) {
188: oStmt = oConn.prepareStatement("INSERT INTO "
189: + DB.k_welcome_packs_changelog + " ("
190: + DB.gu_pack + "," + DB.gu_writer + ","
191: + DB.dt_last_update + "," + DB.id_old_status
192: + "," + DB.id_new_status + ") VALUES (?,?,"
193: + DBBind.Functions.GETDATE + ",?,?)");
194: oStmt.setString(1, getString(DB.gu_pack));
195: oStmt.setString(2, getString(DB.gu_writer));
196: if (sOldStatus.length() == 0)
197: oStmt.setNull(3, Types.VARCHAR);
198: else
199: oStmt.setString(3, sOldStatus);
200: if (getStringNull(DB.id_status, "").length() == 0)
201: oStmt.setNull(4, Types.VARCHAR);
202: else
203: oStmt.setString(4, getString(DB.id_status));
204: oStmt.executeUpdate();
205: oStmt.close();
206: }
207: } // fi (bNew)
208:
209: return bRetVal;
210: }
211:
212: /**
213: * Delete Welcome Pack incluing its change log
214: * @param oConn JDCConnection
215: * @return boolean <b>true</b> if Welcome Pack actually existed, <b>false</b> otherwise.
216: * @throws SQLException
217: */
218: public boolean delete(JDCConnection oConn) throws SQLException {
219: PreparedStatement oStmt;
220: int iAffected;
221: oStmt = oConn.prepareStatement("DELETE FROM "
222: + DB.k_welcome_packs_changelog + " WHERE " + DB.gu_pack
223: + "=?");
224: oStmt.setObject(1, get(DB.gu_pack), Types.CHAR);
225: oStmt.executeUpdate();
226: oStmt.close();
227: oStmt = oConn.prepareStatement("DELETE FROM "
228: + DB.k_welcome_packs + " WHERE " + DB.gu_pack + "=?");
229: oStmt.setObject(1, get(DB.gu_pack), Types.CHAR);
230: iAffected = oStmt.executeUpdate();
231: oStmt.close();
232: return (iAffected > 0);
233: } // delete
234:
235: /**
236: * Get log for modifications of status of this Welcome Pack
237: * @param oConn JDCConnection
238: * @return WelcomePackChangeLog[]
239: * @throws SQLException
240: */
241: public WelcomePackChangeLog[] changeLog(JDCConnection oConn)
242: throws SQLException {
243: WelcomePackChangeLog[] aWcl;
244: DBSubset oLog = new DBSubset(DB.k_welcome_packs_changelog,
245: DB.gu_pack + "," + DB.dt_last_update + ","
246: + DB.gu_writer + "," + DB.id_old_status + ","
247: + DB.id_new_status, DB.gu_pack
248: + "=? ORDER BY 2", 10);
249: int iLog = oLog.load(oConn,
250: new Object[] { getString(DB.gu_pack) });
251: if (0 == iLog) {
252: aWcl = null;
253: } else {
254: aWcl = new WelcomePackChangeLog[iLog];
255: for (int l = 0; l < iLog; l++) {
256: aWcl[l] = new WelcomePackChangeLog();
257: aWcl[l].putAll(oLog.getRowAsMap(l));
258: aWcl[l]
259: .setWriter(oConn, oLog
260: .getStringNull(4, l, null));
261: } // next
262: } // fi
263: return aWcl;
264: } // changeLog
265:
266: /**
267: * Get the most recent Welcome Pack associated to a Contact
268: * @param oConn JDCConnection
269: * @param sGuContact String Contact GUID
270: * @return WelcomePack
271: * @throws SQLException
272: */
273: public static WelcomePack forContact(JDCConnection oConn,
274: String sGuContact) throws SQLException {
275: WelcomePack oRetObj;
276: if (null == sGuContact) {
277: oRetObj = null;
278: } else {
279: PreparedStatement oStmt = oConn.prepareStatement("SELECT "
280: + DB.gu_pack + " FROM " + DB.k_welcome_packs
281: + " WHERE " + DB.gu_contact + "=? ORDER BY "
282: + DB.dt_created + " DESC",
283: ResultSet.TYPE_FORWARD_ONLY,
284: ResultSet.CONCUR_READ_ONLY);
285: oStmt.setString(1, sGuContact);
286: ResultSet oRSet = oStmt.executeQuery();
287: if (oRSet.next()) {
288: oRetObj = new WelcomePack(oConn, oRSet.getString(1));
289: oRSet.close();
290: } else {
291: oRSet.close();
292: oRetObj = null;
293: }
294: oStmt.close();
295: }
296: return oRetObj;
297: } // forContact
298:
299: /**
300: * Get the most recent Welcome Pack associated to a Company
301: * @param oConn JDCConnection
302: * @param sGuCompany String Company GUID
303: * @return WelcomePack
304: * @throws SQLException
305: */
306: public static WelcomePack forCompany(JDCConnection oConn,
307: String sGuCompany) throws SQLException {
308: WelcomePack oRetObj;
309: if (null == sGuCompany) {
310: oRetObj = null;
311: } else {
312: PreparedStatement oStmt = oConn.prepareStatement("SELECT "
313: + DB.gu_pack + " FROM " + DB.k_welcome_packs
314: + " WHERE " + DB.gu_company + "=? ORDER BY "
315: + DB.dt_created + " DESC",
316: ResultSet.TYPE_FORWARD_ONLY,
317: ResultSet.CONCUR_READ_ONLY);
318: oStmt.setString(1, sGuCompany);
319: ResultSet oRSet = oStmt.executeQuery();
320: if (oRSet.next()) {
321: oRetObj = new WelcomePack(oConn, oRSet.getString(1));
322: oRSet.close();
323: } else {
324: oRSet.close();
325: oRetObj = null;
326: }
327: oStmt.close();
328: }
329: return oRetObj;
330: } // forCompany
331:
332: /**
333: * Get the most recent Welcome Pack associated to an Address
334: * @param oConn JDCConnection
335: * @param sGuAddres String Address GUID
336: * @return WelcomePack
337: * @throws SQLException
338: */
339: public static WelcomePack forAddress(JDCConnection oConn,
340: String sGuAddres) throws SQLException {
341: WelcomePack oRetObj;
342: if (null == sGuAddres) {
343: oRetObj = null;
344: } else {
345: PreparedStatement oStmt = oConn.prepareStatement("SELECT "
346: + DB.gu_pack + " FROM " + DB.k_welcome_packs
347: + " WHERE " + DB.gu_address + "=? ORDER BY "
348: + DB.dt_created + " DESC",
349: ResultSet.TYPE_FORWARD_ONLY,
350: ResultSet.CONCUR_READ_ONLY);
351: oStmt.setString(1, sGuAddres);
352: ResultSet oRSet = oStmt.executeQuery();
353: if (oRSet.next()) {
354: oRSet.close();
355: oRetObj = new WelcomePack(oConn, oRSet.getString(1));
356: } else {
357: oRSet.close();
358: oRetObj = null;
359: }
360: oStmt.close();
361: }
362: return oRetObj;
363: } // forAdress
364:
365: // **********************************************************
366: // Constantes Publicas
367:
368: public static final short ClassId = 99;
369:
370: public static final String PENDING = "PENDING";
371: public static final String CANCELLED = "CANCELLED";
372: public static final String SENT = "SENT";
373: public static final String DELIVERED = "DELIVERED";
374: public static final String RETURNED = "RETURNED";
375:
376: }
|