KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Date JavaDoc;
36
37 import java.sql.SQLException JavaDoc;
38 import java.sql.PreparedStatement JavaDoc;
39 import java.sql.ResultSet JavaDoc;
40 import java.sql.Timestamp JavaDoc;
41 import java.sql.Types JavaDoc;
42
43 import com.knowgate.jdc.JDCConnection;
44 import com.knowgate.dataobjs.DB;
45 import com.knowgate.dataobjs.DBBind;
46 import com.knowgate.dataobjs.DBPersist;
47 import com.knowgate.dataobjs.DBSubset;
48 import com.knowgate.hipergate.Address;
49 import com.knowgate.misc.Gadgets;
50
51 /**
52  * <p>Welcome Packs</p>
53  * <p>Copyright: Copyright (c) KnowGate 2006</p>
54  * @author Sergio Montoro Ten
55  * @version 3.0
56  */

57
58 public class WelcomePack extends DBPersist {
59
60     /**
61      * Default constructor
62      */

63     public WelcomePack() {
64     super (DB.k_welcome_packs, "WelcomePack");
65   }
66
67   /**
68    * Create Welcome Pack and load data
69    * @param oConn JDCConnection
70    * @param sGuPack String Welcome Pack GUID
71    * @throws SQLException
72    */

73   public WelcomePack(JDCConnection oConn, String JavaDoc sGuPack)
74     throws SQLException JavaDoc {
75     super (DB.k_welcome_packs, "WelcomePack");
76     load(oConn, new Object JavaDoc[]{sGuPack});
77   }
78
79   /**
80    * Get Company to which this Welcome Pack belongs
81    * @param oConn JDCConnection
82    * @return Company or <b>null</b> if this Welcome Pack is not associated to a Company
83    * @throws SQLException
84    */

85   public Company getCompany(JDCConnection oConn)
86       throws SQLException JavaDoc {
87     if (isNull(DB.gu_company))
88       return null;
89     else
90       return new Company(oConn, getString(DB.gu_company));
91   }
92
93   /**
94    * Get Contact to which this Welcome Pack belongs
95    * @param oConn JDCConnection
96    * @return Company or <b>null</b> if this Welcome Pack is not associated to a Contact
97    * @throws SQLException
98    */

99   public Contact getContact(JDCConnection oConn)
100       throws SQLException JavaDoc {
101     if (isNull(DB.gu_contact))
102       return null;
103     else
104       return new Contact(oConn, getString(DB.gu_contact));
105   }
106
107   /**
108    * Get Address to which this Welcome Pack belongs
109    * @param oConn JDCConnection
110    * @return com.knowgate.hipergate.Address or <b>null</b> if this Welcome Pack is not associated to an Address
111    * @throws SQLException
112    */

113   public Address getAddress(JDCConnection oConn)
114       throws SQLException JavaDoc {
115     if (isNull(DB.gu_address))
116       return null;
117     else
118       return new Address(oConn, getString(DB.gu_address));
119   }
120
121   /**
122    * <p>Store Welcome Pack</p>
123    * Automatically assign values to ix_pack and dt_modified fields if they are not set
124    * @param oConn JDCConnection
125    * @return boolean
126    * @throws SQLException
127    */

128   public boolean store (JDCConnection oConn) throws SQLException JavaDoc {
129     boolean bRetVal;
130     String JavaDoc sOldStatus;
131     PreparedStatement JavaDoc oStmt;
132
133     boolean bNew = isNull(DB.gu_pack);
134
135     if (bNew) {
136       sOldStatus = "";
137       put (DB.gu_pack, Gadgets.generateUUID());
138     } else {
139       oStmt = oConn.prepareStatement("SELECT "+DB.id_status+","+DB.gu_writer+" FROM "+DB.k_welcome_packs+" WHERE "+DB.gu_pack+"=?",
140                                      ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
141       oStmt.setString(1, getString(DB.gu_pack));
142       ResultSet JavaDoc oRSet = oStmt.executeQuery();
143       bNew = !oRSet.next();
144       if (bNew) {
145         sOldStatus = "";
146       } else {
147         sOldStatus = oRSet.getString(1);
148         if (oRSet.wasNull()) sOldStatus = "";
149         replace(DB.gu_writer, oRSet.getString(2));
150       }
151       oRSet.close();
152       oStmt.close();
153     } // fi (bNew)
154

155     replace(DB.dt_modified, new Date JavaDoc());
156
157     if (isNull(DB.dt_cancel) && getStringNull(DB.id_status,"").equalsIgnoreCase(CANCELLED) && !sOldStatus.equalsIgnoreCase(CANCELLED))
158       put(DB.dt_cancel, new Date JavaDoc());
159
160     if (isNull(DB.dt_sent) && getStringNull(DB.id_status,"").equalsIgnoreCase(SENT) && !sOldStatus.equalsIgnoreCase(SENT))
161       put(DB.dt_sent, new Date JavaDoc());
162
163     if (isNull(DB.dt_delivered) && getStringNull(DB.id_status,"").equalsIgnoreCase(DELIVERED) && !sOldStatus.equalsIgnoreCase(DELIVERED))
164       put(DB.dt_delivered, new Date JavaDoc());
165
166     if (isNull(DB.dt_returned) && getStringNull(DB.id_status,"").equalsIgnoreCase(RETURNED) && !sOldStatus.equalsIgnoreCase(RETURNED))
167       put(DB.dt_returned, new Date JavaDoc());
168
169     if (bNew) {
170       put (DB.ix_pack, DBBind.nextVal(oConn,"seq_k_welcme_pak"));
171       bRetVal = super.store(oConn);
172     } else {
173       bRetVal = super.store(oConn);
174       if (!sOldStatus.equalsIgnoreCase(getStringNull(DB.id_status, ""))) {
175         oStmt = oConn.prepareStatement("INSERT INTO " +
176                                        DB.k_welcome_packs_changelog + " (" +
177                                        DB.gu_pack + "," + DB.gu_writer+","+
178                                        DB.dt_last_update+","+DB.id_old_status+","+
179                                        DB.id_new_status+") VALUES (?,?,"+DBBind.Functions.GETDATE+",?,?)");
180         oStmt.setString(1, getString(DB.gu_pack));
181         oStmt.setString(2, getString(DB.gu_writer));
182         if (sOldStatus.length()==0)
183           oStmt.setNull(3, Types.VARCHAR);
184         else
185           oStmt.setString(3, sOldStatus);
186         if (getStringNull(DB.id_status, "").length()==0)
187           oStmt.setNull(4, Types.VARCHAR);
188         else
189           oStmt.setString(4, getString(DB.id_status));
190         oStmt.executeUpdate();
191         oStmt.close();
192       }
193     } // fi (bNew)
194

195     return bRetVal;
196   }
197
198   /**
199    * Delete Welcome Pack incluing its change log
200    * @param oConn JDCConnection
201    * @return boolean <b>true</b> if Welcome Pack actually existed, <b>false</b> otherwise.
202    * @throws SQLException
203    */

204   public boolean delete(JDCConnection oConn) throws SQLException JavaDoc {
205     PreparedStatement JavaDoc oStmt;
206     int iAffected;
207     oStmt = oConn.prepareStatement("DELETE FROM "+DB.k_welcome_packs_changelog+" WHERE "+DB.gu_pack+"=?");
208     oStmt.setObject(1, get(DB.gu_pack), Types.CHAR);
209     oStmt.executeUpdate();
210     oStmt.close();
211     oStmt = oConn.prepareStatement("DELETE FROM "+DB.k_welcome_packs+" WHERE "+DB.gu_pack+"=?");
212     oStmt.setObject(1, get(DB.gu_pack), Types.CHAR);
213     iAffected = oStmt.executeUpdate();
214     oStmt.close();
215     return (iAffected>0);
216   } // delete
217

218   /**
219    * Get log for modifications of status of this Welcome Pack
220    * @param oConn JDCConnection
221    * @return WelcomePackChangeLog[]
222    * @throws SQLException
223    */

224   public WelcomePackChangeLog[] changeLog(JDCConnection oConn)
225     throws SQLException JavaDoc {
226     WelcomePackChangeLog[] aWcl;
227     DBSubset oLog = new DBSubset(DB.k_welcome_packs_changelog,
228                                  DB.gu_pack+","+DB.dt_last_update+","+
229                                  DB.gu_writer+","+
230                                  DB.id_old_status+","+DB.id_new_status,
231                                  DB.gu_pack+"=? ORDER BY 2", 10);
232     int iLog = oLog.load(oConn, new Object JavaDoc[]{getString(DB.gu_pack)});
233     if (0==iLog) {
234       aWcl = null;
235     } else {
236       aWcl = new WelcomePackChangeLog[iLog];
237       for (int l=0; l<iLog; l++) {
238         aWcl[l] = new WelcomePackChangeLog();
239         aWcl[l].putAll(oLog.getRowAsMap(l));
240         aWcl[l].setWriter(oConn, oLog.getStringNull(4,l,null));
241       } // next
242
} // fi
243
return aWcl;
244   } // changeLog
245

246   /**
247    * Get the most recent Welcome Pack associated to a Contact
248    * @param oConn JDCConnection
249    * @param sGuContact String Contact GUID
250    * @return WelcomePack
251    * @throws SQLException
252    */

253   public static WelcomePack forContact(JDCConnection oConn, String JavaDoc sGuContact)
254     throws SQLException JavaDoc {
255     WelcomePack oRetObj;
256     if (null==sGuContact) {
257       oRetObj = null;
258     } else {
259       PreparedStatement JavaDoc oStmt = oConn.prepareStatement("SELECT "+DB.gu_pack+" FROM "+
260                                                        DB.k_welcome_packs+" WHERE "+DB.gu_contact+"=? ORDER BY "+DB.dt_created+" DESC",
261                                                        ResultSet.TYPE_FORWARD_ONLY,
262                                                        ResultSet.CONCUR_READ_ONLY);
263       oStmt.setString(1, sGuContact);
264       ResultSet JavaDoc oRSet = oStmt.executeQuery();
265       if (oRSet.next()) {
266         oRetObj = new WelcomePack(oConn, oRSet.getString(1));
267         oRSet.close();
268       } else {
269         oRSet.close();
270         oRetObj = null;
271       }
272       oStmt.close();
273     }
274     return oRetObj;
275   } // forContact
276

277   /**
278    * Get the most recent Welcome Pack associated to a Company
279    * @param oConn JDCConnection
280    * @param sGuCompany String Company GUID
281    * @return WelcomePack
282    * @throws SQLException
283    */

284   public static WelcomePack forCompany(JDCConnection oConn, String JavaDoc sGuCompany)
285     throws SQLException JavaDoc {
286     WelcomePack oRetObj;
287     if (null==sGuCompany) {
288       oRetObj = null;
289     } else {
290       PreparedStatement JavaDoc oStmt = oConn.prepareStatement("SELECT "+DB.gu_pack+" FROM "+
291                                                        DB.k_welcome_packs+" WHERE "+DB.gu_company+"=? ORDER BY "+DB.dt_created+" DESC",
292                                                        ResultSet.TYPE_FORWARD_ONLY,
293                                                        ResultSet.CONCUR_READ_ONLY);
294       oStmt.setString(1, sGuCompany);
295       ResultSet JavaDoc oRSet = oStmt.executeQuery();
296       if (oRSet.next()) {
297         oRetObj = new WelcomePack(oConn, oRSet.getString(1));
298         oRSet.close();
299       } else {
300         oRSet.close();
301         oRetObj = null;
302       }
303       oStmt.close();
304     }
305     return oRetObj;
306   } // forCompany
307

308   /**
309    * Get the most recent Welcome Pack associated to an Address
310    * @param oConn JDCConnection
311    * @param sGuAddres String Address GUID
312    * @return WelcomePack
313    * @throws SQLException
314    */

315   public static WelcomePack forAddress(JDCConnection oConn, String JavaDoc sGuAddres)
316     throws SQLException JavaDoc {
317     WelcomePack oRetObj;
318     if (null==sGuAddres) {
319       oRetObj = null;
320     } else {
321       PreparedStatement JavaDoc oStmt = oConn.prepareStatement("SELECT "+DB.gu_pack+" FROM "+
322                                                        DB.k_welcome_packs+" WHERE "+DB.gu_address+"=? ORDER BY "+DB.dt_created+" DESC",
323                                                        ResultSet.TYPE_FORWARD_ONLY,
324                                                        ResultSet.CONCUR_READ_ONLY);
325       oStmt.setString(1, sGuAddres);
326       ResultSet JavaDoc oRSet = oStmt.executeQuery();
327       if (oRSet.next()) {
328         oRSet.close();
329         oRetObj = new WelcomePack(oConn, oRSet.getString(1));
330       } else {
331         oRSet.close();
332         oRetObj = null;
333       }
334       oStmt.close();
335     }
336     return oRetObj;
337   } // forAdress
338

339   // **********************************************************
340
// Constantes Publicas
341

342   public static final short ClassId = 99;
343
344   public static final String JavaDoc PENDING = "PENDING";
345   public static final String JavaDoc CANCELLED = "CANCELLED";
346   public static final String JavaDoc SENT = "SENT";
347   public static final String JavaDoc DELIVERED = "DELIVERED";
348   public static final String JavaDoc RETURNED = "RETURNED";
349
350 }
351
Popular Tags