home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / visbuild / rapsheet / cppwv23 / isusrmgr.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-16  |  17.5 KB  |  452 lines

  1. /******************************************************************************
  2. * .FILE:        isusrmgr.cpp                                                  *
  3. *                                                                             *
  4. * .DESCRIPTION: Implementation for the class, ISuspectClientMgr               *
  5. *                                                                             *
  6. * .CLASSES:     ISuspectClientMgr                                             *
  7. *                                                                             *
  8. * .COPYRIGHT:                                                                 *
  9. *    Licensed Material - Program-Property of IBM                              *
  10. *    (C) Copyright IBM Corp. 1992, 1996 - All Rights Reserved                 *
  11. *                                                                             *
  12. * .DISCLAIMER:                                                                *
  13. *   The following [enclosed] code is sample code created by IBM               *
  14. *   Corporation.  This sample code is not part of any standard IBM product    *
  15. *   and is provided to you solely for the purpose of assisting you in the     *
  16. *   development of your applications.  The code is provided 'AS IS',          *
  17. *   without warranty of any kind.  IBM shall not be liable for any damages    *
  18. *   arising out of your use of the sample code, even if they have been        *
  19. *   advised of the possibility of such damages.                               *
  20. *                                                                             *
  21. * .NOTE: WE RECOMMEND USING A FIXED SPACE FONT TO LOOK AT THE SOURCE          *
  22. *                                                                             *
  23. ******************************************************************************/
  24. #include "isusrmgr.hpp"
  25. #include <inotifev.hpp>
  26. #include <itrace.hpp>
  27. #include <iexcbase.hpp>
  28. #include <iprofile.hpp>
  29. #include <iadd.hpp>
  30. #include "irsltrec.hpp"
  31. #include "isusrec.hpp"
  32. #include "querydb.hpp"
  33.  
  34.  
  35. /*******************************************************************
  36.  * Events
  37.  *******************************************************************/
  38. INotificationId ISuspectClientMgr :: requestBufferId = "ISuspectClientMgr::requestBuffer";
  39. INotificationId ISuspectClientMgr :: resultBufferId = "ISuspectClientMgr::resultBuffer";
  40. INotificationId ISuspectClientMgr :: resultListId = "ISuspectClientMgr::resultList";
  41. INotificationId ISuspectClientMgr :: resultListWrapperId = "ISuspectClientMgr::resultListWrapper";
  42. INotificationId ISuspectClientMgr :: resultObjectId = "ISuspectClientMgr::resultObject";
  43. INotificationId ISuspectClientMgr :: resultWrapperId = "ISuspectClientMgr::resultWrapper";
  44.  
  45. INotificationId ISuspectClientMgr :: parsedResultId = "ISuspectClientMgr::parsedResult";
  46. INotificationId ISuspectClientMgr :: sameRequestId = "ISuspectClientMgr::sameRequest";
  47. INotificationId ISuspectClientMgr :: noObjectsFoundId = "ISuspectClientMgr::noObjectsFound";
  48. INotificationId ISuspectClientMgr :: oneObjectFoundId = "ISuspectClientMgr::oneObjectFound";
  49. INotificationId ISuspectClientMgr :: manyObjectsFoundId = "ISuspectClientMgr::manyObjectsFound";
  50.  
  51.  
  52. /*******************************************************************
  53.  * Constructors
  54.  *******************************************************************/
  55. ISuspectClientMgr :: ISuspectClientMgr() :
  56.         dRequestBuffer(""),
  57.         dResultBuffer(""),
  58.         dResultWrapper(0),
  59.         dResultListWrapper(new IVSequence<ISuspect *>)
  60. {
  61.    enableNotification();
  62.    dResultListWrapper->enableNotification();
  63. }
  64.  
  65.  
  66. /*******************************************************************
  67.  * Destructor
  68.  *******************************************************************/
  69. ISuspectClientMgr :: ~ISuspectClientMgr()
  70. {
  71.   ISequence<ISuspect *> tempList;
  72.   ISuspect *            suspect;
  73.  
  74. // if (dResultWrapper)
  75. //    delete dResultWrapper;
  76.   if (dResultListWrapper)
  77.   {
  78.      IVSequence<ISuspect*>::Cursor cursor(*dResultListWrapper);
  79.      forCursor( cursor )
  80.        tempList.addAsFirst( dResultListWrapper->elementAt( cursor ) );
  81.  
  82.      dResultListWrapper->removeAll( );
  83.  
  84.      ISequence<ISuspect*>::Cursor tempCursor( tempList );
  85.      forCursor( cursor )
  86.      {
  87.        suspect = tempList.elementAt ( tempCursor );
  88.        delete suspect;
  89.      }
  90.      tempList.removeAll( );
  91.  
  92.      delete dResultListWrapper;
  93.   }
  94. }
  95.  
  96.  
  97. /****************************************************************************
  98.  * Attribute Access Member Functions
  99.  ****************************************************************************/
  100. /*------------------------------------------------------------------
  101.  * requestBuffer
  102.  *-----------------------------------------------------------------*/
  103. IString ISuspectClientMgr :: requestBuffer () const
  104. {
  105.   return dRequestBuffer;
  106. }
  107.  
  108. ISuspectClientMgr & ISuspectClientMgr ::
  109.   setRequestBuffer (const IString & iRequestBuffer)
  110. {
  111.   if (dRequestBuffer != iRequestBuffer)
  112.   {
  113.     dRequestBuffer = iRequestBuffer;
  114.     notifyObservers(INotificationEvent(requestBufferId, *this, true,
  115.                                        IEventData((char *)iRequestBuffer)));
  116.   }
  117.   else
  118.     notifyObservers(INotificationEvent(sameRequestId, *this, false));
  119.   return *this;
  120. }
  121.  
  122. /*------------------------------------------------------------------
  123.  * resultBuffer
  124.  *-----------------------------------------------------------------*/
  125. IString ISuspectClientMgr :: resultBuffer () const
  126. {
  127.   return dResultBuffer;
  128. }
  129.  
  130. ISuspectClientMgr & ISuspectClientMgr ::
  131.   setResultBuffer (const IString & iResultBuffer)
  132. {
  133.   if (dResultBuffer != iResultBuffer)
  134.   {
  135.     dResultBuffer = iResultBuffer;                  //Set class.attribute.title%>
  136.     notifyObservers(INotificationEvent(resultBufferId, *this, true,
  137.                                        IEventData((char *)iResultBuffer)));
  138.   }
  139.   else
  140.     notifyObservers(INotificationEvent(resultBufferId, *this, false));
  141.     //moved this outside of the test so would fire each time; found wasn't
  142.     //getting message displayed when did 2 consecutive not-found searches
  143.  
  144.   return *this;
  145. }
  146.  
  147. /*------------------------------------------------------------------
  148.  * resultList (read-only) and resultListWrapper
  149.  *-----------------------------------------------------------------*/
  150. IVSequence <ISuspect *> ISuspectClientMgr ::
  151.   resultList () const
  152. {
  153.   return *dResultListWrapper;
  154. }
  155.  
  156. IVSequence <ISuspect *> * ISuspectClientMgr ::
  157.   resultListWrapper () const
  158. {
  159.   return dResultListWrapper;
  160. }
  161.  
  162. /*------------------------------------------------------------------
  163.  * resultObject and resultWrapper (read-only)
  164.  *-----------------------------------------------------------------*/
  165. ISuspect * ISuspectClientMgr :: resultWrapper () const
  166. {
  167.   return dResultWrapper;
  168. }
  169.  
  170. ISuspect ISuspectClientMgr :: resultObject () const
  171. {
  172.   return *dResultWrapper;
  173. }
  174.  
  175.  
  176. /*******************************************************************
  177.  * Actions
  178.  *******************************************************************/
  179. /*------------------------------------------------------------------
  180.  * getAll  (retrieves all suspects from DB2/2 database)
  181.  *-----------------------------------------------------------------*/
  182. IString ISuspectClientMgr :: getAll()
  183. {
  184.  
  185.   IResultRecord rsltRec;
  186.  
  187.   IFUNCTRACE_DEVELOP();
  188.   ITRACE_DEVELOP("Calling the DB server.");
  189.   rsltRec = getAllSuspects();
  190.   ITRACE_DEVELOP("Number suspects found: " + IString(rsltRec.numResults()));
  191.  
  192.   return rsltRec.asString();
  193. }
  194.  
  195. /*------------------------------------------------------------------
  196.  * primeDBWithSuspects
  197.  *-----------------------------------------------------------------*/
  198. ISuspectClientMgr & ISuspectClientMgr :: primeDBWithSuspects ()
  199. {
  200.    IString profKey,
  201.            profData;
  202.  
  203.    /***************************************************************
  204.     * Prime the DB with some suspects
  205.     ***************************************************************/
  206.    IProfile suspectProfile(SUSPECT_INI);
  207.    suspectProfile.setDefaultApplicationName(SUSPECT_APPNAME);
  208.  
  209.    profKey = "0";
  210.    profData = "Mamo,Lou,1,1,1,145,63,0,0,1,01-01-1951,0,7,7,7,101 Main St,Kalamazoo,Dunn,MI,34534, ,Ring leader of the notorious CLASS mob., , ,Rose on right forearm,";
  211.    suspectProfile.addOrReplaceElementWithKey(profKey, profData);
  212.  
  213.    profKey = "1";
  214.    profData = "Lavin,Dave,1,2,0,155,69,1,0,0,06-18-1956,1,1,3,2,234 Main St,Cary,Wake,NC,27512, ,Hit man for Knuckles Mamo. Works closely with the Hammer.,2 inch scar above right eyebrow, , ,";
  215.    suspectProfile.addOrReplaceElementWithKey(profKey, profData);
  216.  
  217.    profKey = "2";
  218.    profData = "Spung,Peter,1,3,3,165,72,0,0,0,11-28-1958,2,7,7,7,565 Capitol Ave, Raleigh,Wake,NC,27444, ,Juggles the books for Knuckles Mamo. Likes to gamble., , , ,";
  219.    suspectProfile.addOrReplaceElementWithKey(profKey, profData);
  220.  
  221.    profKey = "3";
  222.    profData = "Lebda,Ken,1,0,2,165,70,0,0,0,10-31-1953,3,5,6,4,767 Western Blvd,Raleigh,Wake,NC,27890, ,Main hit man for Knuckles Mamo. Likes to use wooden weapons., , , ,";
  223.    suspectProfile.addOrReplaceElementWithKey(profKey, profData);
  224.  
  225.    profKey = "4";
  226.    profData = "Carpenter,Susan,0,3,3,135,62,0,0,0,07-07-1957,4,7,7,7,777 Orchard Lane,Cary,Wake,NC,27512, ,Prints conterfeit money., , , ,";
  227.    suspectProfile.addOrReplaceElementWithKey(profKey, profData);
  228.  
  229.    /***************************************************************
  230.     * Prime the DB with the suspects' aliases
  231.     ***************************************************************/
  232.    IProfile aliasProfile(ALIAS_INI);
  233.    aliasProfile.setDefaultApplicationName(ALIAS_APPNAME);
  234.  
  235.    profKey = "0";
  236.    profData = "0,Louie Knuckles Mamo,";
  237.    aliasProfile.addOrReplaceElementWithKey(profKey, profData);
  238.  
  239.    profKey = "1";
  240.    profData = "0,The God Father,";
  241.    aliasProfile.addOrReplaceElementWithKey(profKey, profData);
  242.  
  243.    profKey = "2";
  244.    profData = "1,Waves Lavin,";
  245.    aliasProfile.addOrReplaceElementWithKey(profKey, profData);
  246.  
  247.    profKey = "3";
  248.    profData = "2,The Juggler,";
  249.    aliasProfile.addOrReplaceElementWithKey(profKey, profData);
  250.  
  251.    profKey = "4";
  252.    profData = "3,The Hammer,";
  253.    aliasProfile.addOrReplaceElementWithKey(profKey, profData);
  254.  
  255.    /***************************************************************
  256.     * Prime the DB with the suspects' arrests
  257.     ***************************************************************/
  258.    IProfile arrestProfile(ARREST_INI);
  259.    arrestProfile.setDefaultApplicationName(ARREST_APPNAME);
  260.  
  261.    profKey = "0";
  262.    profData = "0,Murder,Acquitted,03-02-1994,";
  263.    arrestProfile.addOrReplaceElementWithKey(profKey, profData);
  264.  
  265.    profKey = "1";
  266.    profData = "1,Accomplice to murder,arrigned,05-30-1994,";
  267.    arrestProfile.addOrReplaceElementWithKey(profKey, profData);
  268.  
  269.    profKey = "2";
  270.    profData = "2,fraud,arrested,02-15-1994,";
  271.    arrestProfile.addOrReplaceElementWithKey(profKey, profData);
  272.  
  273.    profKey = "3";
  274.    profData = "2,embezzlement,arrested,04-15-1994,";
  275.    arrestProfile.addOrReplaceElementWithKey(profKey, profData);
  276.  
  277.    profKey = "4";
  278.    profData = "3,premeditated murder,arraigned,05-30-1994,";
  279.    arrestProfile.addOrReplaceElementWithKey(profKey, profData);
  280.  
  281.    return *this;
  282. }
  283.  
  284. /*------------------------------------------------------------------
  285.  * processResult
  286.  *    The user controls when dResultBuffer should be processed.
  287.  *-----------------------------------------------------------------*/
  288. ISuspectClientMgr & ISuspectClientMgr :: processResult ()
  289. {
  290.    ISuspect           *suspect;
  291.    ISuspectRecord     suspectRec;
  292.    IAddress           address;
  293.    unsigned short     recOffset, i;
  294.    IString            buffer;
  295.  
  296.   IFUNCTRACE_DEVELOP();
  297.  
  298.   if (!dResultBuffer.isWhiteSpace())
  299.   {
  300.      ITRACE_DEVELOP("resultBuffer received.");
  301.      IResultRecord resultRec(dResultBuffer);
  302.      ITRACE_DEVELOP(resultRec.asDebugInfo());
  303.  
  304.      //clear the result list if it's not empty (i.e., if it was set in a previous invocation)
  305.      if (dResultListWrapper->numberOfElements())
  306.      {
  307.         ITRACE_DEVELOP("had to empty the resultList");
  308.         dResultListWrapper->removeAll();
  309.      }
  310.  
  311.      if (resultRec.numResults() != 0)
  312.      {
  313.         recOffset = 1;
  314.         Boolean noError = true;
  315.  
  316.         for (i=0 ; i < resultRec.numResults() && noError; i++)
  317.         {
  318.            suspect = new ISuspect;
  319.            if (recOffset + suspectRec.size() - 1 <= resultRec.size() - 2)
  320.            {
  321.               buffer = resultRec.resultData().subString(recOffset,suspectRec.size());
  322.               suspectRec = buffer;
  323.               ITRACE_DEVELOP("Suspect record" + IString(i) + " ...");
  324.               ITRACE_DEVELOP(suspectRec.asDebugInfo());
  325.  
  326.               suspect->setLastName(suspectRec.lastName().strip());
  327.               suspect->setFirstName(suspectRec.firstName().strip());
  328.               suspect->setInfo(suspectRec.description().strip());
  329.               suspect->enableHasMole(suspectRec.hasMole());
  330.               suspect->enableHasScar(suspectRec.hasScar());
  331.               suspect->enableHasTattoo(suspectRec.hasTattoo());
  332.               suspect->setBirthDate(suspectRec.birthDate().strip());
  333.               suspect->setBookNumber(suspectRec.bookNumber());
  334.               suspect->setMO(suspectRec.MO().strip());
  335.               suspect->setMoleDesc(suspectRec.moleDescription().strip());
  336.               suspect->setScarDesc(suspectRec.scarDescription().strip());
  337.               suspect->setTattooDesc(suspectRec.tattooDescription().strip());
  338.               suspect->setEyeColor(suspectRec.eyeColor());
  339.               suspect->setHairColor(suspectRec.hairColor());
  340.               suspect->setGender(suspectRec.gender());
  341.               suspect->setHeight(suspectRec.height());
  342.               suspect->setWeight(suspectRec.weight());
  343.               suspect->setMugFront(suspectRec.mugFront());
  344.               suspect->setMugLeft(suspectRec.mugLeft());
  345.               suspect->setMugRight(suspectRec.mugRight());
  346.  
  347.               address.setStreet(suspectRec.street().strip());
  348.               address.setCity(suspectRec.city().strip());
  349.               address.setState(suspectRec.state().strip());
  350.               address.setZip(suspectRec.zip().strip());
  351. //            address.setCounty(suspectRec.county().strip());
  352.  
  353.               suspect->setAddress(new IAddress( address ));
  354.  
  355.               ITRACE_DEVELOP(suspect->asDebugInfo());
  356.               addResult(suspect);              //adds suspect to dResultList
  357.               recOffset += suspectRec.size();
  358.            }
  359.            else
  360.            {
  361.               ITRACE_DEVELOP("The capacity of the result record buffer has been exceeded.");
  362.               noError = false;
  363.            }
  364.         }
  365.      }
  366.  
  367.      notifyObservers(INotificationEvent(parsedResultId, *this, false));
  368.      if (resultRec.numResults() == 1)
  369.         notifyObservers(INotificationEvent(oneObjectFoundId, *this, false));
  370.      else if (resultRec.numResults() == 0)
  371.      {
  372.         notifyObservers(INotificationEvent(noObjectsFoundId, *this, false));
  373.         throw IException("No information exists for the specified suspect.");
  374.      }
  375.      else
  376.         notifyObservers(INotificationEvent(manyObjectsFoundId, *this, false));
  377.  
  378.   }
  379.    return *this;
  380. }
  381.  
  382. /*------------------------------------------------------------------
  383.  * addResult()
  384.  *-----------------------------------------------------------------*/
  385. ISuspectClientMgr & ISuspectClientMgr :: addResult
  386.    (ISuspect * iSuspectWrapper)
  387. {
  388.    IFUNCTRACE_DEVELOP();
  389.    ITRACE_DEVELOP("addResult(*)");
  390.    ITRACE_DEVELOP(iSuspectWrapper->asDebugInfo());
  391.  
  392.    if (dResultListWrapper->isEmpty())
  393.    {
  394.       setResultWrapper(iSuspectWrapper);
  395.    }
  396.  
  397.    dResultListWrapper->addAsLast(iSuspectWrapper);
  398.    ITRACE_DEVELOP("added a suspect to the resultList");
  399.    return *this;
  400. }
  401.  
  402. /*------------------------------------------------------------------
  403.  * addResult()
  404.  *-----------------------------------------------------------------*/
  405. ISuspectClientMgr & ISuspectClientMgr :: addResult
  406.    (ISuspect & iSuspect)
  407. {
  408.    IFUNCTRACE_DEVELOP();
  409.    ITRACE_DEVELOP("addResult(&)");
  410.  
  411.    if (dResultListWrapper->isEmpty())
  412.    {
  413.       setResultWrapper(&iSuspect);
  414.    }
  415.  
  416.    dResultListWrapper->addAsLast(&iSuspect);
  417.    ITRACE_DEVELOP("added a suspect to the resultList");
  418.    return *this;
  419. }
  420.  
  421. /*------------------------------------------------------------------
  422.  * setResultWrapper()
  423.  *-----------------------------------------------------------------*/
  424. ISuspectClientMgr & ISuspectClientMgr ::
  425.   setResultWrapper (ISuspect * iResultWrapper)
  426. {
  427.   if (dResultWrapper != iResultWrapper)
  428.   {
  429.      if (dResultWrapper)
  430.         delete dResultWrapper;
  431.      dResultWrapper = iResultWrapper;
  432.      notifyObservers(INotificationEvent(resultObjectId, *this, false));
  433.      notifyObservers(INotificationEvent(resultWrapperId, *this, false));
  434.   }
  435.   return *this;
  436. }
  437.  
  438. /*------------------------------------------------------------------
  439.  * setResultWrapper()
  440.  *-----------------------------------------------------------------*/
  441. ISuspectClientMgr & ISuspectClientMgr ::
  442.   setResultWrapper (const ISuspect & iResult)
  443. {
  444.   /*****************************************************************
  445.    * Have to make a copy of the ISuspect object passed in since it's
  446.    * identified as being a const.  That is, the compiler won't
  447.    * allow dResultWrapper = &iResultWrapper, since it would allow
  448.    * us, via dResultWrapper, to update the ISuspect object.
  449.    *****************************************************************/
  450.   return setResultWrapper(new ISuspect(iResult));
  451. }
  452.