1 32 33 package com.knowgate.crm; 34 35 import java.util.Date ; 36 37 import java.sql.SQLException ; 38 import java.sql.PreparedStatement ; 39 import java.sql.ResultSet ; 40 import java.sql.Timestamp ; 41 import java.sql.Types ; 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 57 58 public class WelcomePack extends DBPersist { 59 60 63 public WelcomePack() { 64 super (DB.k_welcome_packs, "WelcomePack"); 65 } 66 67 73 public WelcomePack(JDCConnection oConn, String sGuPack) 74 throws SQLException { 75 super (DB.k_welcome_packs, "WelcomePack"); 76 load(oConn, new Object []{sGuPack}); 77 } 78 79 85 public Company getCompany(JDCConnection oConn) 86 throws SQLException { 87 if (isNull(DB.gu_company)) 88 return null; 89 else 90 return new Company(oConn, getString(DB.gu_company)); 91 } 92 93 99 public Contact getContact(JDCConnection oConn) 100 throws SQLException { 101 if (isNull(DB.gu_contact)) 102 return null; 103 else 104 return new Contact(oConn, getString(DB.gu_contact)); 105 } 106 107 113 public Address getAddress(JDCConnection oConn) 114 throws SQLException { 115 if (isNull(DB.gu_address)) 116 return null; 117 else 118 return new Address(oConn, getString(DB.gu_address)); 119 } 120 121 128 public boolean store (JDCConnection oConn) throws SQLException { 129 boolean bRetVal; 130 String sOldStatus; 131 PreparedStatement 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 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 } 155 replace(DB.dt_modified, new Date ()); 156 157 if (isNull(DB.dt_cancel) && getStringNull(DB.id_status,"").equalsIgnoreCase(CANCELLED) && !sOldStatus.equalsIgnoreCase(CANCELLED)) 158 put(DB.dt_cancel, new Date ()); 159 160 if (isNull(DB.dt_sent) && getStringNull(DB.id_status,"").equalsIgnoreCase(SENT) && !sOldStatus.equalsIgnoreCase(SENT)) 161 put(DB.dt_sent, new Date ()); 162 163 if (isNull(DB.dt_delivered) && getStringNull(DB.id_status,"").equalsIgnoreCase(DELIVERED) && !sOldStatus.equalsIgnoreCase(DELIVERED)) 164 put(DB.dt_delivered, new Date ()); 165 166 if (isNull(DB.dt_returned) && getStringNull(DB.id_status,"").equalsIgnoreCase(RETURNED) && !sOldStatus.equalsIgnoreCase(RETURNED)) 167 put(DB.dt_returned, new Date ()); 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 } 195 return bRetVal; 196 } 197 198 204 public boolean delete(JDCConnection oConn) throws SQLException { 205 PreparedStatement 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 } 218 224 public WelcomePackChangeLog[] changeLog(JDCConnection oConn) 225 throws SQLException { 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 []{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 } } return aWcl; 244 } 246 253 public static WelcomePack forContact(JDCConnection oConn, String sGuContact) 254 throws SQLException { 255 WelcomePack oRetObj; 256 if (null==sGuContact) { 257 oRetObj = null; 258 } else { 259 PreparedStatement 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 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 } 277 284 public static WelcomePack forCompany(JDCConnection oConn, String sGuCompany) 285 throws SQLException { 286 WelcomePack oRetObj; 287 if (null==sGuCompany) { 288 oRetObj = null; 289 } else { 290 PreparedStatement 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 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 } 308 315 public static WelcomePack forAddress(JDCConnection oConn, String sGuAddres) 316 throws SQLException { 317 WelcomePack oRetObj; 318 if (null==sGuAddres) { 319 oRetObj = null; 320 } else { 321 PreparedStatement 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 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 } 339 342 public static final short ClassId = 99; 343 344 public static final String PENDING = "PENDING"; 345 public static final String CANCELLED = "CANCELLED"; 346 public static final String SENT = "SENT"; 347 public static final String DELIVERED = "DELIVERED"; 348 public static final String RETURNED = "RETURNED"; 349 350 } 351 | Popular Tags |