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

  1. /******************************************************************************
  2. * .FILE:        ialsrmgr.cpp                                                  *
  3. *                                                                             *
  4. * .DESCRIPTION: Implementation for the class, IAliasClientMgr                 *
  5. *                                                                             *
  6. * .CLASSES:     IAliasClientMgr                                               *
  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 "ialsrmgr.hpp"               //IAliasClientMgr header
  25. #include <inotifev.hpp>
  26. #include <itrace.hpp>
  27. #include "irsltrec.hpp"               //IResultRecord header
  28. #include "ialsrec.hpp"                //IAliasRecord header
  29. #include "querydb.hpp"
  30.  
  31.  
  32. /*******************************************************************
  33.  * Events
  34.  *******************************************************************/
  35. INotificationId IAliasClientMgr :: parsedResultId = "IAliasClientMgr::parsedResult";
  36. INotificationId IAliasClientMgr :: requestBufferId = "IAliasClientMgr::requestBuffer";
  37. INotificationId IAliasClientMgr :: resultBufferId = "IAliasClientMgr::resultBuffer";
  38. INotificationId IAliasClientMgr :: resultListId = "IAliasClientMgr::resultList";
  39. INotificationId IAliasClientMgr :: resultListWrapperId = "IAliasClientMgr::resultListWrapper";
  40. INotificationId IAliasClientMgr :: resultObjectId = "IAliasClientMgr::resultObject";
  41. INotificationId IAliasClientMgr :: resultWrapperId = "IAliasClientMgr::resultWrapper";
  42. INotificationId IAliasClientMgr :: sameRequestId = "IAliasClientMgr::sameRequest";
  43. INotificationId IAliasClientMgr :: noObjectsFoundId = "IAliasClientMgr::noObjectsFound";
  44. INotificationId IAliasClientMgr :: oneObjectFoundId = "IAliasClientMgr::oneObjectFound";
  45. INotificationId IAliasClientMgr :: manyObjectsFoundId = "IAliasClientMgr::manyObjectsFound";
  46.  
  47.  
  48. /*******************************************************************
  49.  * Constructors
  50.  *******************************************************************/
  51. IAliasClientMgr :: IAliasClientMgr() :
  52.         dRequestBuffer(""),
  53.         dResultBuffer(""),
  54.         dResultWrapper(0),
  55.         dResultListWrapper(new IVSequence<IAlias *>)
  56. {
  57.    enableNotification();
  58.    dResultListWrapper->enableNotification();
  59. }
  60.  
  61.  
  62. /*******************************************************************
  63.  * Destructor
  64.  *******************************************************************/
  65. IAliasClientMgr :: ~IAliasClientMgr()
  66. {
  67.   ISequence<IAlias *> tempList;
  68.   IAlias *            alias;
  69.  
  70. // if (dResultWrapper)
  71. //    delete dResultWrapper;
  72.   if (dResultListWrapper)
  73.   {
  74.      IVSequence<IAlias*>::Cursor cursor(*dResultListWrapper);
  75.      forCursor( cursor )
  76.        tempList.addAsFirst( dResultListWrapper->elementAt( cursor ) );
  77.  
  78.      dResultListWrapper->removeAll( );
  79.  
  80.      ISequence<IAlias*>::Cursor tempCursor( tempList );
  81.      forCursor( cursor )
  82.      {
  83.        alias = tempList.elementAt ( tempCursor );
  84.        delete alias;
  85.      }
  86.      tempList.removeAll( );
  87.  
  88.      delete dResultListWrapper;
  89.   }
  90. }
  91.  
  92.  
  93. /*******************************************************************
  94.  * Attribute Access Member Functions
  95.  *******************************************************************/
  96. /*------------------------------------------------------------------
  97.  * requestBuffer
  98.  *-----------------------------------------------------------------*/
  99. IString IAliasClientMgr :: requestBuffer () const
  100. {
  101.   return dRequestBuffer;
  102. }
  103.  
  104. IAliasClientMgr & IAliasClientMgr ::
  105.   setRequestBuffer (const IString & iRequestBuffer)
  106. {
  107.   if (dRequestBuffer != iRequestBuffer)
  108.   {
  109.     dRequestBuffer = iRequestBuffer;
  110.     notifyObservers(INotificationEvent(requestBufferId, *this, true,
  111.                                        IEventData((char *)iRequestBuffer)));
  112.   }
  113.   else
  114.     notifyObservers(INotificationEvent(sameRequestId, *this, false));
  115.   return *this;
  116. }
  117.  
  118. /*------------------------------------------------------------------
  119.  * resultBuffer
  120.  *-----------------------------------------------------------------*/
  121. IString IAliasClientMgr :: resultBuffer () const
  122. {
  123.   return dResultBuffer;
  124. }
  125.  
  126. IAliasClientMgr & IAliasClientMgr ::
  127.   setResultBuffer (const IString & iResultBuffer)
  128. {
  129.   if (dResultBuffer != iResultBuffer)
  130.   {
  131.     dResultBuffer = iResultBuffer;
  132.     notifyObservers(INotificationEvent(resultBufferId, *this, true,
  133.                                IEventData((char *)iResultBuffer)));
  134.   } /* endif */
  135.   return *this;
  136. }
  137.  
  138. /*------------------------------------------------------------------
  139.  * resultList (read-only) and resultListWrapper
  140.  *-----------------------------------------------------------------*/
  141. IVSequence <IAlias *> IAliasClientMgr ::
  142.   resultList () const
  143. {
  144.   return *dResultListWrapper;
  145. }
  146.  
  147. IVSequence <IAlias *> * IAliasClientMgr ::
  148.   resultListWrapper () const
  149. {
  150.   return dResultListWrapper;
  151. }
  152.  
  153. /*------------------------------------------------------------------
  154.  * resultObject and resultWrapper (read-only)
  155.  *-----------------------------------------------------------------*/
  156. IAlias * IAliasClientMgr :: resultWrapper () const
  157. {
  158.   return dResultWrapper;
  159. }
  160.  
  161. IAlias IAliasClientMgr :: resultObject () const
  162. {
  163.   return *dResultWrapper;
  164. }
  165.  
  166.  
  167. /*******************************************************************
  168.  * Actions
  169.  *******************************************************************/
  170. /*------------------------------------------------------------------
  171.  * allbyBookNumber
  172.  *-----------------------------------------------------------------*/
  173. IString IAliasClientMgr :: allByBookNumber (unsigned long aBookNumber)
  174. {
  175.   IFUNCTRACE_DEVELOP();
  176.  
  177.   IResultRecord rsltRec;
  178.   IString suspectKey = IString(aBookNumber);
  179.  
  180.   ITRACE_DEVELOP("suspectKey: " + suspectKey + ".");
  181.  
  182.   if (dRequestBuffer != suspectKey)
  183.   {
  184.      setRequestBuffer(suspectKey);
  185.      ITRACE_DEVELOP("Calling the DB server.");
  186.      rsltRec = getAllAliases(aBookNumber);
  187.      ITRACE_DEVELOP("Number aliases found: " + IString(rsltRec.numResults()));
  188.   }
  189.   else
  190.      setRequestBuffer(suspectKey);
  191.  
  192.   return rsltRec.asString();
  193. }
  194.  
  195. /*------------------------------------------------------------------
  196.  * processResult()
  197.  *    The user controls when dResultBuffer should be processed.
  198.  *-----------------------------------------------------------------*/
  199. IAliasClientMgr & IAliasClientMgr :: processResult ()
  200. {
  201.    IAlias *           aliasObj;
  202.    IAliasRecord       aliasRec;
  203.    unsigned short     recOffset, i;
  204.    IString            buffer;
  205.  
  206.   IFUNCTRACE_DEVELOP();
  207.  
  208.   if (!dResultBuffer.isWhiteSpace())
  209.   {
  210.      ITRACE_DEVELOP("resultBuffer received: " + dResultBuffer + ".");
  211.      IResultRecord resultRec(dResultBuffer);
  212.      ITRACE_DEVELOP(resultRec.asDebugInfo());
  213.  
  214.      //clear the result list if it's not empty (i.e., if it was set in a previous invocation)
  215.      if (dResultListWrapper->numberOfElements())
  216.      {
  217.         ITRACE_DEVELOP("had to empty the resultList");
  218.         dResultListWrapper->removeAll();
  219.      }
  220.  
  221.      recOffset = 1;
  222.      for (i=0 ; i < resultRec.numResults(); i++)
  223.      {
  224.         aliasObj = new IAlias;
  225.  
  226.         buffer = resultRec.resultData().subString(recOffset,
  227.                                                   aliasRec.size());
  228.         aliasRec = buffer;
  229.  
  230.         ITRACE_DEVELOP("alias name pulled from the record: " + aliasRec.name() + ".");
  231.         aliasObj->setAlias(aliasRec.name());
  232.         ITRACE_DEVELOP("alias name set in alias object: " + aliasObj->alias() + ".");
  233.  
  234.         addResult(aliasObj);      //adds alias to
  235.                                   //the result list
  236.  
  237.         recOffset += aliasRec.size();  //get the next aliasRec
  238.      }
  239.  
  240.      notifyObservers(INotificationEvent(parsedResultId, *this, false));
  241.      if (resultRec.numResults() == 1)
  242.         notifyObservers(INotificationEvent(oneObjectFoundId, *this, false));
  243.      else if (resultRec.numResults() == 0)
  244.      {
  245.         notifyObservers(INotificationEvent(noObjectsFoundId, *this, false));
  246.         throw IException("No aliases exist for the specified suspect."); }
  247.      else
  248.         notifyObservers(INotificationEvent(manyObjectsFoundId, *this, false));
  249.   }
  250.  
  251.    return *this;
  252. }
  253.  
  254. /*******************************************************************
  255.  * Class Member Functions
  256.  *******************************************************************/
  257. /*------------------------------------------------------------------
  258.  * addResult()
  259.  *-----------------------------------------------------------------*/
  260. IAliasClientMgr & IAliasClientMgr ::
  261.   addResult (IAlias * iAliasWrapper)
  262. {
  263.    IFUNCTRACE_DEVELOP();
  264.    ITRACE_DEVELOP("addResult(*) of ialsrmgr.");
  265.  
  266.    if (dResultListWrapper->isEmpty())
  267.    {
  268.       setResultWrapper(iAliasWrapper);
  269.    }
  270.  
  271.    dResultListWrapper->add(iAliasWrapper);
  272.    ITRACE_DEVELOP("added an alias to the resultList");
  273.    return *this;
  274. }
  275.  
  276. /*------------------------------------------------------------------
  277.  * addResult()
  278.  *-----------------------------------------------------------------*/
  279. IAliasClientMgr & IAliasClientMgr ::
  280.   addResult (IAlias & iAlias)
  281. {
  282.    IFUNCTRACE_DEVELOP();
  283.    ITRACE_DEVELOP("addResult(&) of ialsrmgr.");
  284.  
  285.    if (dResultListWrapper->isEmpty())
  286.    {
  287.       setResultWrapper(&iAlias);
  288.    }
  289.  
  290.    dResultListWrapper->add(&iAlias);
  291.    ITRACE_DEVELOP("added an alias to the resultList");
  292.    return *this;
  293. }
  294.  
  295. /*------------------------------------------------------------------
  296.  * setResultWrapper()
  297.  *-----------------------------------------------------------------*/
  298. IAliasClientMgr & IAliasClientMgr ::
  299.   setResultWrapper (IAlias * iResultWrapper)
  300. {
  301.   if (dResultWrapper != iResultWrapper)
  302.   {
  303.      if (dResultWrapper)
  304.         delete dResultWrapper;
  305.      dResultWrapper = iResultWrapper;
  306.      notifyObservers(INotificationEvent(resultObjectId, *this, false));
  307.      notifyObservers(INotificationEvent(resultWrapperId, *this, false));
  308.   }
  309.   return *this;
  310. }
  311.  
  312. /*------------------------------------------------------------------
  313.  * setResultWrapper()
  314.  *-----------------------------------------------------------------*/
  315. IAliasClientMgr & IAliasClientMgr ::
  316.   setResultWrapper (const IAlias & iResult)
  317. {
  318.   /*****************************************************************
  319.    * Have to make a copy of the IAlias object passed in since it's
  320.    * identified as being a const.  That is, the compiler won't
  321.    * allow dResultWrapper = &iResultWrapper, since it would allow
  322.    * us, via dResultWrapper, to update the IAlias object.
  323.    *****************************************************************/
  324.   return setResultWrapper(new IAlias(iResult));
  325. }
  326.