1 32 33 package com.knowgate.crm; 34 35 import java.util.HashMap ; 36 37 import java.sql.Connection ; 38 import java.sql.SQLException ; 39 import java.sql.CallableStatement ; 40 import java.sql.Statement ; 41 import java.sql.PreparedStatement ; 42 import java.sql.ResultSet ; 43 44 import com.knowgate.debug.DebugFile; 45 import com.knowgate.misc.Gadgets; 46 import com.knowgate.jdc.JDCConnection; 47 import com.knowgate.dataobjs.DB; 48 import com.knowgate.dataobjs.DBBind; 49 import com.knowgate.dataobjs.DBSubset; 50 import com.knowgate.dataobjs.DBPersist; 51 import com.knowgate.hipergate.Address; 52 import com.knowgate.hipergate.DBLanguages; 53 54 60 61 public class Company extends DBPersist { 62 63 66 public Company() { 67 super(DB.k_companies, "Company"); 68 } 69 70 75 public Company(String sCompanyId) { 76 super(DB.k_companies, "Company"); 77 78 put (DB.gu_company, sCompanyId); 79 } 80 81 83 89 public Company(JDCConnection oConn, String sCompanyId) 90 throws SQLException { 91 super(DB.k_companies, "Company"); 92 load (oConn, sCompanyId); 93 } 94 95 97 104 public boolean addBankAccount(JDCConnection oConn, String sFullBankAccount) 105 throws SQLException { 106 PreparedStatement oStmt = null; 107 boolean bRetVal; 108 109 try { 110 oStmt = oConn.prepareStatement("INSERT INTO " + DB.k_x_company_bank + " (" + DB.gu_company + "," + DB.nu_bank_acc + "," + DB.gu_workarea + ") VALUES (?,?,?)"); 111 oStmt.setString(1, getStringNull(DB.gu_company, null)); 112 oStmt.setString(2, sFullBankAccount); 113 oStmt.setString(3, getStringNull(DB.gu_workarea, null)); 114 int iAffected = oStmt.executeUpdate(); 115 oStmt.close(); 116 oStmt = null; 117 bRetVal = (iAffected > 0); 118 } catch (SQLException sqle) { 119 bRetVal = false; 120 try { if (oStmt!=null) oStmt.close(); } catch (Exception ignore) {} 121 } 122 return bRetVal; 123 } 125 127 135 public DBSubset getAllBankAccounts(JDCConnection oConn) 136 throws SQLException ,IllegalStateException { 137 if (isNull(DB.gu_company)) 138 throw new IllegalStateException ("Company.getAllBankAccounts() gu_company property is not set"); 139 if (isNull(DB.gu_workarea)) 140 throw new IllegalStateException ("Company.getAllBankAccounts() gu_workarea property is not set"); 141 142 DBSubset oAccs = new DBSubset (DB.k_bank_accounts, 143 DB.nu_bank_acc+","+DB.dt_created+","+DB.bo_active+","+DB.tp_account+","+DB.nm_bank+","+DB.tx_addr+","+DB.nm_cardholder+","+DB.nu_card+","+DB.tp_card+","+DB.tx_expire+","+DB.nu_pin+","+DB.nu_cvv2+","+DB.im_credit_limit+","+DB.de_bank_acc, 144 DB.gu_workarea+"=? AND "+DB.nu_bank_acc+" IN (SELECT "+DB.nu_bank_acc+" FROM "+DB.k_x_company_bank+" WHERE "+DB.gu_workarea+"=? AND "+DB.gu_company+"=?)",10); 145 146 oAccs.load(oConn, new Object []{get(DB.gu_workarea),get(DB.gu_workarea),get(DB.gu_company)}); 147 return oAccs; 148 } 150 152 160 public DBSubset getActiveBankAccounts(JDCConnection oConn) 161 throws SQLException ,IllegalStateException { 162 if (isNull(DB.gu_company)) 163 throw new IllegalStateException ("Company.getActiveBankAccounts() gu_company property is not set"); 164 if (isNull(DB.gu_workarea)) 165 throw new IllegalStateException ("Company.getActiveBankAccounts() gu_workarea property is not set"); 166 167 DBSubset oAccs = new DBSubset (DB.k_bank_accounts, 168 DB.nu_bank_acc+","+DB.dt_created+","+DB.tp_account+","+DB.nm_bank+","+DB.tx_addr+","+DB.nm_cardholder+","+DB.nu_card+","+DB.tp_card+","+DB.tx_expire+","+DB.nu_pin+","+DB.nu_cvv2+","+DB.im_credit_limit+","+DB.de_bank_acc, 169 DB.gu_workarea+"=? AND "+DB.bo_active+"<>0 AND "+DB.nu_bank_acc+" IN (SELECT "+DB.nu_bank_acc+" FROM "+DB.k_x_company_bank+" WHERE "+DB.gu_workarea+"=? AND "+DB.gu_company+"=?)",10); 170 171 oAccs.load(oConn, new Object []{get(DB.gu_workarea),get(DB.gu_workarea),get(DB.gu_company)}); 172 return oAccs; 173 } 175 177 185 public DBSubset getUnactiveBankAccounts(JDCConnection oConn) 186 throws SQLException ,IllegalStateException { 187 if (isNull(DB.gu_company)) 188 throw new IllegalStateException ("Company.getUnactiveBankAccounts() gu_company property is not set"); 189 if (isNull(DB.gu_workarea)) 190 throw new IllegalStateException ("Company.getUnactiveBankAccounts() gu_workarea property is not set"); 191 192 DBSubset oAccs = new DBSubset (DB.k_bank_accounts, 193 DB.nu_bank_acc+","+DB.dt_created+","+DB.tp_account+","+DB.nm_bank+","+DB.tx_addr+","+DB.nm_cardholder+","+DB.nu_card+","+DB.tp_card+","+DB.tx_expire+","+DB.nu_pin+","+DB.nu_cvv2+","+DB.im_credit_limit+","+DB.de_bank_acc, 194 DB.gu_workarea+"=? AND "+DB.bo_active+"=0 AND "+DB.nu_bank_acc+" IN (SELECT "+DB.nu_bank_acc+" FROM "+DB.k_x_company_bank+" WHERE "+DB.gu_workarea+"=? AND "+DB.gu_company+"=?)",10); 195 196 oAccs.load(oConn, new Object []{get(DB.gu_workarea),get(DB.gu_workarea),get(DB.gu_company)}); 197 return oAccs; 198 } 200 202 208 public boolean store(JDCConnection oConn) throws SQLException { 209 java.sql.Timestamp dtNow = new java.sql.Timestamp (DBBind.getTime()); 210 211 if (!AllVals.containsKey(DB.gu_company)) 212 put(DB.gu_company, Gadgets.generateUUID()); 213 214 replace(DB.dt_modified, dtNow); 215 216 return super.store(oConn); 217 } 219 221 226 public boolean delete(JDCConnection oConn) throws SQLException { 227 return Company.delete(oConn, getString(DB.gu_company)); 228 } 229 230 232 239 240 public boolean exists(JDCConnection oConn) throws SQLException { 241 PreparedStatement oStmt = oConn.prepareStatement("SELECT NULL FROM "+DB.k_companies+" WHERE "+DB.gu_company+"=? OR ("+DB.nm_legal+"=? AND "+DB.gu_workarea+"=?)", 242 ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 243 oStmt.setString(1, getStringNull(DB.gu_company,null)); 244 oStmt.setString(2, getStringNull(DB.nm_legal,null)); 245 oStmt.setString(3, getStringNull(DB.gu_workarea,null)); 246 ResultSet oRSet = oStmt.executeQuery(); 247 boolean bExists = oRSet.next(); 248 oRSet.close(); 249 oStmt.close(); 250 return bExists; 251 } 253 255 262 public boolean addAddress(JDCConnection oConn, String sAddrGUID) throws SQLException { 263 PreparedStatement oStmt = null; 264 boolean bRetVal; 265 266 try { 267 oStmt = oConn.prepareStatement("INSERT INTO " + DB.k_x_company_addr + " (" + DB.gu_company + "," + DB.gu_address + ") VALUES (?,?)"); 268 oStmt.setString(1, getStringNull(DB.gu_company, null)); 269 oStmt.setString(2, sAddrGUID); 270 int iAffected = oStmt.executeUpdate(); 271 oStmt.close(); 272 oStmt = null; 273 bRetVal = (iAffected > 0); 274 } catch (SQLException sqle) { 275 bRetVal = false; 276 try { if (oStmt!=null) oStmt.close(); } catch (Exception ignore) {} 277 } 278 return bRetVal; 279 } 281 283 292 public Address getAddress(JDCConnection oConn, String sTpLocation) 293 throws SQLException ,IllegalStateException { 294 295 Address oRetAdr; 296 297 if (isNull(DB.gu_company)) 298 throw new IllegalStateException ("Company.getAddress([Connection],"+sTpLocation+") gu_company property is not set"); 299 300 if (DebugFile.trace) { 301 DebugFile.writeln("Begin Company.getAddress([Connection],"+sTpLocation+")" ); 302 DebugFile.incIdent(); 303 } 304 305 PreparedStatement oStmt = oConn.prepareStatement("SELECT x." + DB.gu_address + 306 " FROM " + DB.k_x_company_addr + " x," + 307 DB.k_addresses + " a WHERE " + 308 "x." + DB.gu_address + "=a." + DB.gu_address + 309 " AND x." + DB.gu_company+"=?" + 310 " AND a." + DB.tp_location+"=?", 311 ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 312 oStmt.setString(1, getString(DB.gu_company)); 313 oStmt.setString(2, sTpLocation); 314 ResultSet oRSet = oStmt.executeQuery(); 315 if (oRSet.next()) 316 oRetAdr = new Address(oConn, oRSet.getString(1)); 317 else 318 oRetAdr = null; 319 oRSet.close(); 320 oStmt.close(); 321 322 if (DebugFile.trace) { 323 DebugFile.decIdent(); 324 DebugFile.writeln("End Company.getAddress()"); 325 } 326 return oRetAdr; 327 } 329 331 338 public DBSubset getAddresses(JDCConnection oConn) 339 throws SQLException ,IllegalStateException { 340 341 if (isNull(DB.gu_company)) 342 throw new IllegalStateException ("Company.getAddresses() gu_company property is not set"); 343 344 if (DebugFile.trace) { 345 DebugFile.writeln("Begin Company.getAddresses([Connection])" ); 346 DebugFile.incIdent(); 347 } 348 349 Address oAddr = new Address(); 350 351 DBSubset oAddrs = new DBSubset (DB.k_addresses, 352 oAddr.getTable(oConn).getColumnsStr(), 353 DB.gu_address + " IN (SELECT " + DB.gu_address + " FROM " + DB.k_x_company_addr + " WHERE " + DB.gu_company + "=?)", 10); 354 int iAddrs = oAddrs.load(oConn, new Object []{getString(DB.gu_company)}); 355 356 oAddr = null; 357 358 if (DebugFile.trace) { 359 DebugFile.decIdent(); 360 DebugFile.writeln("End Company.getAddresses() : " + String.valueOf(iAddrs)); 361 } 362 363 return oAddrs; 364 } 366 368 371 380 public static boolean delete(JDCConnection oConn, String sCompanyGUID) throws SQLException { 381 boolean bRetVal; 382 Statement oStmt; 383 384 if (DebugFile.trace) { 385 DebugFile.writeln("Begin Company.delete([Connection], " + sCompanyGUID + ")"); 386 DebugFile.incIdent(); 387 } 388 389 390 if (DBBind.exists(oConn, DB.k_inet_addrs, "U")) { 391 oStmt = oConn.createStatement(); 392 393 if (DebugFile.trace) 394 DebugFile.writeln("UPDATE " + DB.k_inet_addrs + " SET " + DB.gu_company + "=NULL WHERE " + DB.gu_company + "='" + sCompanyGUID + "'"); 395 396 oStmt.executeUpdate("UPDATE " + DB.k_inet_addrs + " SET " + DB.gu_company + "=NULL WHERE " + DB.gu_company + "='" + sCompanyGUID + "'"); 397 398 oStmt.close(); 399 } 400 401 if (DBBind.exists(oConn, DB.k_projects, "U")) { 402 DBSubset oProjs = new DBSubset(DB.k_projects, DB.gu_project, DB.gu_company + "='" + sCompanyGUID + "'", 10); 403 404 int iProjs = oProjs.load(oConn); 405 406 for (int p=0; p<iProjs; p++) 407 com.knowgate.projtrack.Project.delete (oConn, oProjs.getString(0,p)); 408 } 409 410 if (DBBind.exists(oConn, DB.k_orders, "U")) { 411 DBSubset oOrders = new DBSubset(DB.k_orders, DB.gu_order, DB.gu_company + "='" + sCompanyGUID + "'", 1000); 412 413 int iOrders = oOrders.load(oConn); 414 415 for (int o=0; o<iOrders; o++) 416 com.knowgate.hipergate.Order.delete (oConn, oOrders.getString(0,o)); 417 } 419 DBSubset oContacts = new DBSubset(DB.k_contacts, DB.gu_contact, DB.gu_company + "='" + sCompanyGUID + "'", 1000); 420 int iContacts = oContacts.load(oConn); 421 422 for (int c=0; c<iContacts; c++) 423 Contact.delete(oConn, oContacts.getString(0,c)); 424 425 oContacts = null; 426 427 if (oConn.getDataBaseProduct()==JDCConnection.DBMS_POSTGRESQL) { 428 oStmt = oConn.createStatement(); 429 if (DebugFile.trace) DebugFile.writeln("Statement.executeQuery(SELECT k_sp_del_company ('" + sCompanyGUID + "')"); 430 oStmt.executeQuery("SELECT k_sp_del_company ('" + sCompanyGUID + "')"); 431 oStmt.close(); 432 bRetVal = true; 433 } 434 else { 435 436 if (DebugFile.trace) 437 DebugFile.writeln ("Conenction.prepareCall({call k_sp_del_company ('" + sCompanyGUID + "')}"); 438 439 CallableStatement oCall = oConn.prepareCall("{call k_sp_del_company ('" + sCompanyGUID + "')}"); 440 bRetVal = oCall.execute(); 441 oCall.close(); 442 } 443 444 if (DebugFile.trace) { 445 DebugFile.decIdent(); 446 DebugFile.writeln("End Company.delete() : " + String.valueOf(bRetVal)); 447 } 448 449 return bRetVal; 450 } 452 462 public static boolean addLookupSector (Connection oConn, String sGuWorkArea, String sIdSector, HashMap oTranslations) 463 throws SQLException { 464 return DBLanguages.addLookup(oConn, DB.k_companies_lookup, sGuWorkArea, DB.id_sector, sIdSector, oTranslations); 465 } 466 467 477 public static boolean addLookupCompanyType (Connection oConn, String sGuWorkArea, String sIdType, HashMap oTranslations) 478 throws SQLException { 479 return DBLanguages.addLookup(oConn, DB.k_companies_lookup, sGuWorkArea, DB.tp_company, sIdType, oTranslations); 480 } 481 482 491 public static String getIdFromName(Connection oConn, String sLegalName, String sWorkArea) 492 throws SQLException { 493 String sRetVal; 494 PreparedStatement oStmt = oConn.prepareStatement("SELECT "+DB.gu_company+" FROM "+DB.k_companies+" WHERE "+DB.nm_legal+"=? AND "+DB.gu_workarea+"=?", 495 ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 496 oStmt.setString(1, sLegalName); 497 oStmt.setString(2, sWorkArea); 498 ResultSet oRSet = oStmt.executeQuery(); 499 if (oRSet.next()) 500 sRetVal = oRSet.getString(1); 501 else 502 sRetVal = null; 503 oRSet.close(); 504 oStmt.close(); 505 return sRetVal; 506 } 508 517 public static String getIdFromRef(Connection oConn, String sReference, String sWorkArea) 518 throws SQLException { 519 String sRetVal; 520 PreparedStatement oStmt = oConn.prepareStatement("SELECT "+DB.gu_company+" FROM "+DB.k_companies+" WHERE "+DB.id_ref+"=? AND "+DB.gu_workarea+"=?", 521 ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 522 oStmt.setString(1, sReference); 523 oStmt.setString(2, sWorkArea); 524 ResultSet oRSet = oStmt.executeQuery(); 525 if (oRSet.next()) 526 sRetVal = oRSet.getString(1); 527 else 528 sRetVal = null; 529 oRSet.close(); 530 oStmt.close(); 531 return sRetVal; 532 } 534 543 public static String getIdFromLegalNum(Connection oConn, String sLegalId, String sWorkArea) 544 throws SQLException { 545 String sRetVal; 546 PreparedStatement oStmt = oConn.prepareStatement("SELECT "+DB.gu_company+" FROM "+DB.k_companies+" WHERE "+DB.id_legal+"=? AND "+DB.gu_workarea+"=?", 547 ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 548 oStmt.setString(1, sLegalId); 549 oStmt.setString(2, sWorkArea); 550 ResultSet oRSet = oStmt.executeQuery(); 551 if (oRSet.next()) 552 sRetVal = oRSet.getString(1); 553 else 554 sRetVal = null; 555 oRSet.close(); 556 oStmt.close(); 557 return sRetVal; 558 } 560 563 public static final short ClassId = 91; 564 } 565 | Popular Tags |