home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / database / stocksom / client.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  16.3 KB  |  424 lines

  1. //******************************************************************************
  2. //                                                                             *
  3. //COPYRIGHT: Copyright (C) International Business Machines Corp., 1992,1994.   *
  4. //                                                                             *
  5. //DISCLAIMER OF WARRANTIES:                                                    *
  6. //  The following [enclosed] code is sample code created by IBM                *
  7. //  Corporation.  This sample code is not part of any standard IBM product     *
  8. //  and is provided to you solely for the purpose of assisting you in the      *
  9. //  development of your applications.  The code is provided "AS IS",           *
  10. //  without warranty of any kind.  IBM shall not be liable for any damages     *
  11. //  arising out of your use of the sample code, even if they have been         *
  12. //  advised of the possibility of such damages.                                *
  13. //                                                                             *
  14. //******************************************************************************
  15. //******************************************************************************
  16. // File Name: client.cpp                                                       *
  17. //                                                                             *
  18. // Note: This file keeps the SOM program which directly uses the methods from  *
  19. //       SOM IDL CLass Library to connect and disconnect a datastore in DB2/2  *
  20. //       and uses the generated methods to add, update, and retrieve.          *
  21. //                                                                             *
  22. //******************************************************************************
  23. #include <iostream.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <IString.hpp>
  27. #include <iomanip.h>
  28. #include "sdsmcon.xh"           // For SOM IDL Datastore Class
  29. #include "sdsexc.xh"            // For SOM Exception Classes
  30. #include "prclist.xh"          // For SOM IDL generated PriceList Class
  31. #include "invento.xh"          // For SOM IDL generated Inventory Class
  32. #include "receive.xh"          // For SOM IDL generated Receivedorder Class
  33.  
  34. static int exceptionCheck(void); // Routine to check if an exception has occurred.
  35. static int generatePriceList();  // Generate a price list
  36. static int postingInventory();  // update the inventory data with received order
  37.  
  38. static int addReceivedOrder(     // Add a received order to the database
  39.     short  receivedNumberArg,    // received order number
  40.     string prodNumberArg,        // product number
  41.     long    receivedQuantityArg, // recieved quantity
  42.     double  totalCostArg,        // total cost of the order
  43.     string receivedDateArg);     // date of the received order
  44.  
  45. static int addNewItem(           // add a new item to the inventory table
  46.     string prodNumberArg,        // product number
  47.     string prodDescriptionArg,   // product description
  48.     long    onHandQuantityArg,   // on hand quantity
  49.     double  avarageCostArg,      // average cost per unit
  50.     double  listingPriceArg);    // listing price per unit
  51.  
  52. //******************************************************************************
  53. // Static variables                                                            *
  54. //******************************************************************************
  55. enum postedflagValue {NOT_POSTED, POSTED};
  56. Environment                *ev                  = somGetGlobalEnvironment();
  57. DatastoreFactory           *pidFactDS           = DatastoreNewClass(0,0);
  58. PrclistFactory             *prclistFacPtr       = PrclistNewClass(0,0);
  59. ReceiveFactory             *receiveFacPtr       = ReceiveNewClass(0,0);
  60. InventoFactory             *inventoFacPtr       = InventoNewClass(0,0);
  61.  
  62. void main(int argc, char * argv[]) {
  63.   Datastore            *dsm;
  64.   PrclistDatastore     *dsm1;
  65.   InventoDatastore     *dsm2;
  66.   ReceiveDatastore     *dsm3;
  67.  
  68.   char *dstoreId;
  69.   double listPrice, aveCost, totalCost;
  70.   int rc;
  71.  
  72.   IString  ISESQL;
  73.  
  74.   // Establish connection to the datastore.
  75.  
  76.   ISESQL = getenv("ISESQL");
  77.   if (ISESQL == "TRUE")  {
  78.  
  79.      dsm = pidFactDS->create_object(ev);
  80.  
  81.      if (argc < 3) {
  82.        dsm->_set_datastore_name(ev, "DAXSAMP");
  83.        dsm->connect_defaults(ev);
  84.      }
  85.      else {
  86.        dsm->connect(ev,"DAXSAMP",argv[1],argv[2]);
  87.      }
  88.     rc = exceptionCheck();
  89.   }
  90.   else {
  91.  
  92.      dsm1 = prclistFacPtr->create_datastore(ev);
  93.      dsm2 = inventoFacPtr->create_datastore(ev);
  94.      dsm3 = receiveFacPtr->create_datastore(ev);
  95.  
  96.     if (argc == 3) {
  97.       dsm1->_set_user_name(ev,argv[1]);
  98.       dsm1->_set_authentication(ev,argv[2]);
  99.       dsm2->_set_user_name(ev,argv[1]);
  100.       dsm2->_set_authentication(ev,argv[2]);
  101.       dsm3->_set_user_name(ev,argv[1]);
  102.       dsm3->_set_authentication(ev,argv[2]);
  103.     }
  104.     dsm1->connect_defaults(ev);
  105.     rc = exceptionCheck();
  106.     dsm2->connect_defaults(ev);
  107.     rc+= exceptionCheck();
  108.     dsm3->connect_defaults(ev);
  109.     rc+= exceptionCheck();
  110.  
  111.   }
  112.  
  113.   if (rc == 0) {
  114.  
  115.     // Generate a price list from the Price List object class.
  116.     cout << "The following is a price list before update:" << endl << endl;
  117.     rc = generatePriceList();
  118.     if (rc == 0) {
  119.  
  120.       // Add new items to the inventory table.
  121.       rc = addNewItem("HDR0025","Hard Drive 250MD ID", 12, 200.00, 239.45);
  122.       rc += addNewItem("HDR0034","Hard Drive 340MD ID", 40, 220.00, 270.89);
  123.       rc += addNewItem("CPU486a","486SLC-33 TI CPU,AM", 50, 105.45, 139.99);
  124.       rc += addNewItem("CPUPTMa","Pentium 60/66 PCI25", 23, 350.85, 459.50);
  125.  
  126.       // continue if adding new inventory items is OK.
  127.       if (rc == 0) {
  128.  
  129.         // Add new records to the receivedorder table.
  130.         rc = addReceivedOrder(123,"HDR0025", 10, 1800.00, "01/01/1994");
  131.         rc += addReceivedOrder(124,"RAM4-72", 30, 6001.35, "03/01/1994");
  132.         rc += addReceivedOrder(125,"CPUPTMa",  5, 1904.75, "02/01/1994");
  133.         rc += addReceivedOrder(126,"MONAD14", 40, 1538.00, "04/01/1994");
  134.  
  135.         // continue if adding new received orders is OK.
  136.         if (rc == 0) {
  137.  
  138.           // The inventory has to be posted with the new received orders
  139.           rc = postingInventory();
  140.           if (rc == 0) {
  141.  
  142.             // Commit transaction activities after successful posting
  143.             if (!strcmp(ISESQL,"TRUE"))  {
  144.                dsm->commit(ev);
  145.                rc+= exceptionCheck();
  146.             }
  147.             else {
  148.                dsm1->commit(ev);
  149.                rc+= exceptionCheck();
  150.                dsm2->commit(ev);
  151.                rc+= exceptionCheck();
  152.                dsm3->commit(ev);
  153.                rc+= exceptionCheck();
  154.             }
  155.  
  156.             rc = exceptionCheck();
  157.             if (rc == 0) {
  158.  
  159.               cout << endl << endl;
  160.               cout << "The following is an updated price list:" << endl << endl;
  161.  
  162.               // Generate an updated report after posting.
  163.               generatePriceList();
  164.  
  165.             }
  166.  
  167.           } else {
  168.  
  169.             // Rollback transaction activities because of error in posting
  170.             if (!strcmp(ISESQL,"TRUE"))  {
  171.                dsm->rollback(ev);
  172.                rc+= exceptionCheck();
  173.             }
  174.             else {
  175.                dsm1->rollback(ev);
  176.                rc+= exceptionCheck();
  177.                dsm2->rollback(ev);
  178.                rc+= exceptionCheck();
  179.                dsm3->rollback(ev);
  180.                rc+= exceptionCheck();
  181.             }
  182.           }
  183.  
  184.         } else {
  185.  
  186.           // Rollback transaction activities because of error in adding receive orders
  187.           if (!strcmp(ISESQL,"TRUE"))  {
  188.              dsm->rollback(ev);
  189.              rc+= exceptionCheck();
  190.           }
  191.           else {
  192.              dsm1->rollback(ev);
  193.              rc+= exceptionCheck();
  194.              dsm2->rollback(ev);
  195.              rc+= exceptionCheck();
  196.              dsm3->rollback(ev);
  197.              rc+= exceptionCheck();
  198.           }
  199.         }
  200.       } else {
  201.  
  202.         // Rollback transaction activities because of error in adding new items
  203.         if (!strcmp(ISESQL,"TRUE"))  {
  204.            dsm->rollback(ev);
  205.            rc+= exceptionCheck();
  206.         }
  207.         else {
  208.            dsm1->rollback(ev);
  209.            rc+= exceptionCheck();
  210.            dsm2->rollback(ev);
  211.            rc+= exceptionCheck();
  212.            dsm3->rollback(ev);
  213.            rc+= exceptionCheck();
  214.         }
  215.       }
  216.     }
  217.  
  218.     // disconnect from the datastore.
  219.     if (!strcmp(ISESQL,"TRUE"))  {
  220.        dsm->disconnect(ev);
  221.     }
  222.     else {
  223.        dsm1->commit(ev);
  224.        dsm1->disconnect(ev);
  225.        rc+= exceptionCheck();
  226.        dsm2->commit(ev);
  227.        dsm2->disconnect(ev);
  228.        rc+= exceptionCheck();
  229.        dsm3->commit(ev);
  230.        dsm3->disconnect(ev);
  231.        rc+= exceptionCheck();
  232.     }
  233.   }
  234.  
  235.   cout << endl;
  236.   if (rc == 0) {
  237.      cout << "Program completed successfully" << endl;
  238.   } else {
  239.      cout << "Error occured during program excution." << endl;
  240.   } /* endif */
  241.  
  242. }
  243.  
  244. //******************************************************************************
  245. // Function Name: generatePriceList                                            *
  246. // Note : This member function generates a price list for the store.           *
  247. //        The data printed in the list is a subset of the inventory            *
  248. //        database.                                                            *
  249. //******************************************************************************
  250. int generatePriceList() {
  251.   Prclist                      *priceListPtr;
  252.   _IDL_SEQUENCE_PersistentObject itemList;
  253.   int rc;
  254.  
  255.   itemList = prclistFacPtr->retrieveAll(ev);
  256.   rc = exceptionCheck();
  257.   if (rc != 0) {
  258.     cout << "Failed: Exception occurs..." << endl;
  259.   } else {
  260.     // loop until the end of the list.
  261.     cout << "Prod. ID   Description            Price " << endl;
  262.     cout << "========   =====================  ===== " << endl;
  263.     for (int j=0; j < sequenceLength(itemList); j++) {
  264.       priceListPtr = (Prclist *) sequenceElement(itemList,j);
  265.       cout << priceListPtr->_get_pnumber(ev) << ' ';
  266.       cout << priceListPtr->_get_pdes(ev) << "   $";
  267.       cout << setiosflags(ios::fixed) <<
  268.               setprecision(2) << priceListPtr->_get_lstprice(ev) << endl;
  269.     }
  270.   }
  271.  
  272.   return rc;
  273. }
  274.  
  275. //******************************************************************************
  276. // Function Name: postingInventory                                             *
  277. // Note: This member function updates the inventory table with the received    *
  278. //       received order table.                                                 *
  279. //******************************************************************************
  280. int postingInventory() {
  281.   Receive                  *receivedOrderPtr;
  282.   Invento                  *inventoryPtr     = inventoFacPtr->create_object(ev);
  283.   _IDL_SEQUENCE_PersistentObject itemList;
  284.   double newAveCost;                      // new average cost after update
  285.   double newListingPrice;                 // new listing price after update
  286.   long   newQuantity;                     // new quantity after update
  287.   char   selectArg[] = "pFlag = 0";  // selection condition
  288.   char * currentProdNumber;
  289.   int icnt, rc;
  290.  
  291.   itemList = receiveFacPtr->select(ev, selectArg);
  292.   rc = exceptionCheck();
  293.   if (rc != 0) {
  294.     cout << "Error: Exception occurs..." << endl;
  295.   } else {
  296.     for (icnt = 0, rc = 0; icnt < sequenceLength(itemList) && rc ==0; icnt ++) {
  297.       receivedOrderPtr = (Receive *) sequenceElement(itemList,icnt);
  298.       currentProdNumber = receivedOrderPtr->_get_pnum(ev);
  299.       inventoryPtr->_set_pnumber(ev,currentProdNumber);
  300.       inventoryPtr->retrieve(ev);
  301.  
  302.       // calculate the new quantity, the new average cost, and new listing price
  303.       newQuantity = inventoryPtr->_get_onhquan(ev) +
  304.                     receivedOrderPtr->_get_rquan(ev);
  305.       newAveCost  = (inventoryPtr->_get_avgcost(ev) * inventoryPtr->_get_onhquan(ev) +
  306.                     receivedOrderPtr->_get_tcost(ev)) / newQuantity;
  307.       newListingPrice = newAveCost * 1.5;
  308.  
  309.       // update the row with the new data
  310.       inventoryPtr->_set_onhquan(ev, newQuantity);
  311.       inventoryPtr->_set_avgcost(ev, newAveCost);
  312.       inventoryPtr->_set_lstprice(ev, newListingPrice);
  313.       inventoryPtr->update(ev);
  314.       rc = exceptionCheck();
  315.  
  316.       // reset the posted flag so that it will not be used again.
  317.       receivedOrderPtr->_set_pflag(ev, POSTED);
  318.       receivedOrderPtr->update(ev);
  319.       rc = exceptionCheck();
  320.     }
  321.   } /* endif */
  322.  
  323.   delete inventoryPtr;
  324.   return rc;
  325.  
  326. }
  327.  
  328. //******************************************************************************
  329. // Function: addReceivedOrder                                                  *
  330. // Note: Whenever an order is received, a new entry is added to the received   *
  331. //       order table                                                           *
  332. //******************************************************************************
  333. int addReceivedOrder(
  334.        short   receivedNumberArg,     //received order number
  335.        char *  prodNumberArg,         // product number
  336.        long    receivedQuantityArg,   // received order quantity
  337.        double  totalCostArg,          // total cost of this order
  338.        char *  receivedDateArg) {     // date of the received order
  339.  
  340.   Receive              *receivedOrderPtr = receiveFacPtr->create_object(ev);
  341.   int rc;
  342.   // Set the attributes
  343.   receivedOrderPtr->_set_pflag           (ev,NOT_POSTED);
  344.   receivedOrderPtr->_set_pnum         (ev,prodNumberArg);
  345.   receivedOrderPtr->_set_rnumber         (ev,receivedNumberArg);
  346. //  receivedOrderPtr->_set_receiveddate    (ev,receivedDateArg);
  347.   receivedOrderPtr->_set_rquan           (ev,receivedQuantityArg);
  348.   receivedOrderPtr->_set_tcost           (ev,totalCostArg);
  349.  
  350.   // add a new row to the table
  351.   receivedOrderPtr->add(ev);
  352.  
  353.   // Check for exception and return a value accordingly.
  354.   rc =exceptionCheck();
  355.  
  356.   delete receivedOrderPtr;
  357.   return rc;
  358. }
  359.  
  360. //******************************************************************************
  361. // Function: addNewItem                                                        *
  362. // Note: This function add a new row to the inventory table                    *
  363. //******************************************************************************
  364. int addNewItem(
  365.        char * prodNumberArg,       // product number
  366.        char * prodDescriptionArg,  // product description
  367.        long   onHandQuantityArg,   // on hand quantity
  368.        double averageCostArg,      // average cost per unit
  369.        double listingPriceArg) {   // listing price per unit
  370.  
  371.   Invento                  *inventoryPtr     = inventoFacPtr->create_object(ev);
  372.   int rc;
  373.  
  374.   // Set the attributes
  375.   inventoryPtr->_set_pnumber         (ev,prodNumberArg);
  376.   inventoryPtr->_set_pdes            (ev,prodDescriptionArg);
  377.   inventoryPtr->_set_onhquan         (ev,onHandQuantityArg);
  378.   inventoryPtr->_set_avgcost     (ev,averageCostArg);
  379.   inventoryPtr->_set_lstprice        (ev,listingPriceArg);
  380.  
  381.   // add a new row to the table
  382.   inventoryPtr->add(ev);
  383.  
  384.   // Check for exception and return a value accordingly.
  385.   rc = exceptionCheck();
  386.  
  387.   delete inventoryPtr;
  388.   return rc;
  389. }
  390.  
  391. //******************************************************************************
  392. // Function: exceptionCheck                                                    *
  393. // Note: This function add a new row to the inventory table                    *
  394. //******************************************************************************
  395. int exceptionCheck(void) {
  396.    int rc = 0;
  397.    char *exId;
  398.    DA_DatastoreAccessError * er;
  399.  
  400.    switch (ev->_major) {
  401.    case SYSTEM_EXCEPTION:
  402.       cout << "system exception" << endl;
  403.       rc ++;
  404.       break;
  405.    case USER_EXCEPTION:
  406.       cout << "User Exception" << endl;
  407.       char * exId;
  408.       exId = somExceptionId(ev);
  409.       cout << "Exception ID: " << somExceptionId(ev) << endl;
  410.       er = (DA_DatastoreAccessError *) somExceptionValue(ev);
  411.       cout << "error code:" << er->error_code << endl;
  412.       cout << "error text:" << er->error_text << endl;
  413.       cout << "error state:" << er->error_state << endl;
  414.       cout << "error number:" << er->error_number << endl;
  415.       rc ++;
  416.       somExceptionFree(ev);
  417.       break;
  418.    case NO_EXCEPTION:
  419.       break;
  420.    }
  421.    return rc;
  422. }
  423.  
  424.