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

  1. /******************************************************************************
  2. * .FILE:        querydb.cpp                                                   *
  3. *                                                                             *
  4. * .DESCRIPTION: Source file used to query for the suspect data                *
  5. *                                                                             *
  6. * .CLASSES:                                                                   *
  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 <stdio.h>
  25. #include <stdlib.h>
  26. #include <itrace.hpp>
  27. #include <fstream.h>
  28. #include <iostream.h>
  29. #include <iprofile.hpp>
  30. #include "irsltrec.hpp"
  31. #include "isusrec.hpp"
  32. #include "iarstrec.hpp"
  33. #include "ialsrec.hpp"
  34. #include "querydb.hpp"
  35. #include "rap.h"
  36.  
  37. IString delimeter(",");
  38.  
  39. /*****************************************************************/
  40. /*                                                               */
  41. /* getAllSuspects                                                */
  42. /*                                                               */
  43. /*Function: Gets the list of suspects from the database          */
  44. /*                                                               */
  45. /*****************************************************************/
  46.  
  47. IResultRecord getAllSuspects()
  48. {
  49.  
  50.   IBoolean firstSuspectRecord=true;
  51.  
  52.   IString   lastName, firstName, gender, hairColor, eyeColor, weight, height,
  53.             enableHasScar, enableHasMole, enableHasTattoo, birthDate,
  54.             bookNumber, mugFront, mugRight, mugLeft, street,
  55.             city, county, state, zip, description, MO,
  56.             scarDescription, moleDescription, tattooDescription,
  57.             tempRecords, suspectData;
  58.  
  59.   ISuspectRecord  suspectRecord;
  60.   IResultRecord   resultRecord;
  61.   unsigned long   numberOfSuspects;
  62.   long            numberOfResults;
  63.   unsigned short  delimeterPos=0,
  64.                   searchPos=1;
  65.  
  66. //-------------------------------------------------------------------------------
  67. // Setup the IProfile
  68. //-------------------------------------------------------------------------------
  69.  
  70.    IFUNCTRACE_DEVELOP();
  71.    ITRACE_DEVELOP("Creating and associating application name with suspect profile");
  72.    IString appName(SUSPECT_APPNAME);
  73.    IProfile suspectProfile(IString(SUSPECT_INI));
  74.    suspectProfile.setDefaultApplicationName(SUSPECT_APPNAME); // set default app name
  75.    ITRACE_DEVELOP("Creating and associating application name with suspect profile done.");
  76.  
  77. //-------------------------------------------------------------------------------
  78. // Iterate through the suspect profile and process each suspect data
  79. //-------------------------------------------------------------------------------
  80.  
  81.    IProfile::Cursor cursor(suspectProfile, appName);
  82.    for (cursor.setToFirst();cursor.isValid();cursor.setToNext())
  83.    {
  84.  
  85.       // retrieve one suspect's data
  86.       // NOTE: the order of the retrieves must be as following;
  87.       // it matches how the IProfile is build in susprof.cpp
  88.  
  89.       IString key = suspectProfile.applicationOrKeyAt(cursor);
  90.       suspectData = suspectProfile.elementWithKey(key);
  91.  
  92.       // get LastName from suspectData and set in the suspectRecord
  93.  
  94.       delimeterPos = suspectData.indexOf (delimeter,searchPos);
  95.       lastName = suspectData.subString(searchPos,delimeterPos-searchPos);
  96.       searchPos = delimeterPos + 1;         // set next search position
  97.       suspectRecord.setLastName(lastName);
  98.  
  99.       // get FirstName from suspectData and set in the suspectRecord
  100.  
  101.       delimeterPos = suspectData.indexOf (delimeter,searchPos);
  102.       firstName = suspectData.subString(searchPos,delimeterPos-searchPos);
  103.       searchPos =  delimeterPos + 1;         // set next search position
  104.       suspectRecord.setFirstName(firstName);
  105.  
  106.       // get Gender from suspectData and set in the suspectRecord
  107.  
  108.       delimeterPos = suspectData.indexOf (delimeter,searchPos);
  109.       gender = suspectData.subString(searchPos,delimeterPos-searchPos);
  110.       searchPos =  delimeterPos +1;         // set next search position
  111.       suspectRecord.setGender((unsigned short)gender.asUnsigned());
  112.  
  113.  
  114.       // get HairColor from suspectData and set in the suspectRecord
  115.  
  116.       delimeterPos = suspectData.indexOf (delimeter,searchPos);
  117.       hairColor = suspectData.subString(searchPos,delimeterPos-searchPos);
  118.       searchPos =  delimeterPos+1;         // set next search position
  119.       suspectRecord.setHairColor((unsigned short)hairColor.asUnsigned());
  120.  
  121.  
  122.       // get EyeColor from suspectData and set in the suspectRecord
  123.  
  124.       delimeterPos = suspectData.indexOf (delimeter,searchPos);
  125.       eyeColor = suspectData.subString(searchPos,delimeterPos-searchPos);
  126.       searchPos =   delimeterPos+1;         // set next search position
  127.       suspectRecord.setEyeColor((unsigned short)eyeColor.asUnsigned());
  128.  
  129.       // get Weight from suspectData and set in the suspectRecord
  130.  
  131.       delimeterPos = suspectData.indexOf (delimeter,searchPos);
  132.       weight = suspectData.subString(searchPos,delimeterPos-searchPos);
  133.       searchPos =  delimeterPos+1;         // set next search position
  134.       suspectRecord.setWeight((unsigned short)weight.asUnsigned());
  135.  
  136.       // get Height from suspectData and set in the suspectRecord
  137.  
  138.       delimeterPos = suspectData.indexOf (delimeter,searchPos);
  139.       height = suspectData.subString(searchPos,delimeterPos-searchPos);
  140.       searchPos =   delimeterPos+1;         // set next search position
  141.       suspectRecord.setHeight((unsigned short)height.asUnsigned());
  142.  
  143.  
  144.       // get enableHasScar from suspectData and set in the suspectRecord
  145.  
  146.       delimeterPos = suspectData.indexOf (delimeter,searchPos);
  147.       enableHasScar = suspectData.subString(searchPos,delimeterPos-searchPos);
  148.       searchPos =   delimeterPos+1;         // set next search position
  149.       suspectRecord.enableHasScar((unsigned short)enableHasScar.asUnsigned());
  150.  
  151.       // get enableHasMole from suspectData and set in the suspectRecord
  152.  
  153.       delimeterPos = suspectData.indexOf (delimeter,searchPos);
  154.       enableHasMole = suspectData.subString(searchPos,delimeterPos-searchPos);
  155.       searchPos =   delimeterPos+1;         // set next search position
  156.       suspectRecord.enableHasMole((unsigned short)enableHasMole.asUnsigned());
  157.  
  158.  
  159.       // get enableHasTattoo from suspectData and set in the suspectRecord
  160.  
  161.       delimeterPos = suspectData.indexOf (delimeter,searchPos);
  162.       enableHasTattoo = suspectData.subString(searchPos,delimeterPos-searchPos);
  163.       searchPos =   delimeterPos+1;         // set next search position
  164.       suspectRecord.enableHasTattoo((unsigned short)enableHasTattoo.asUnsigned());
  165.  
  166.       // get BirthDate from suspectData and set in the suspectRecord
  167.  
  168.       delimeterPos = suspectData.indexOf (delimeter,searchPos);
  169.       birthDate = suspectData.subString(searchPos,delimeterPos-searchPos);
  170.       searchPos =   delimeterPos+1;         // set next search position
  171.       suspectRecord.setBirthDate(birthDate);
  172.  
  173.       // get BookNumber from suspectData and set in the suspectRecord
  174.  
  175.       delimeterPos = suspectData.indexOf (delimeter,searchPos);
  176.       bookNumber = suspectData.subString(searchPos,delimeterPos-searchPos);
  177.       searchPos =   delimeterPos+1;         // set next search position
  178.       suspectRecord.setBookNumber(bookNumber.asUnsigned());
  179.  
  180.       // example: unsigned long count = strCount.asUnsigned(); strCount is IString
  181.  
  182.  
  183.       // get MugFront from suspectData and set in the suspectRecord
  184.  
  185.       delimeterPos = suspectData.indexOf (delimeter,searchPos);
  186.       mugFront = suspectData.subString(searchPos,delimeterPos-searchPos);
  187.       searchPos =   delimeterPos+1;         // set next search position
  188.       suspectRecord.setMugFront((unsigned short)mugFront.asUnsigned());
  189.  
  190.       // get MugRight from suspectData and set in the suspectRecord
  191.  
  192.       delimeterPos = suspectData.indexOf (delimeter,searchPos);
  193.       mugRight = suspectData.subString(searchPos,delimeterPos-searchPos);
  194.       searchPos =   delimeterPos+1;         // set next search position
  195.       suspectRecord.setMugRight((unsigned short)mugRight.asUnsigned());
  196.  
  197.       // get MugLeft from suspectData and set in the suspectRecord
  198.  
  199.       delimeterPos = suspectData.indexOf (delimeter,searchPos);
  200.       mugLeft = suspectData.subString(searchPos,delimeterPos-searchPos);
  201.       searchPos = delimeterPos+1;         // set next search position
  202.       suspectRecord.setMugLeft((unsigned short)mugLeft.asUnsigned());
  203.  
  204.       // get Street from suspectData and set in the suspectRecord
  205.  
  206.       delimeterPos = suspectData.indexOf (delimeter,searchPos);
  207.       street  = suspectData.subString(searchPos,delimeterPos-searchPos);
  208.       searchPos = delimeterPos+1;         // set next search position
  209.       suspectRecord.setStreet(street);
  210.  
  211.       // get City from suspectData and set in the suspectRecord
  212.  
  213.       delimeterPos = suspectData.indexOf (delimeter,searchPos);
  214.       city = suspectData.subString(searchPos,delimeterPos-searchPos);
  215.       searchPos = delimeterPos+1;         // set next search position
  216.       suspectRecord.setCity(city);
  217.  
  218.       // get County from suspectData and set in the suspectRecord
  219.  
  220.       delimeterPos = suspectData.indexOf (delimeter,searchPos);
  221.       county = suspectData.subString(searchPos,delimeterPos-searchPos);
  222.       searchPos = delimeterPos+1;         // set next search position
  223.       suspectRecord.setCounty(county);
  224.  
  225.       // get State from suspectData and set in the suspectRecord
  226.  
  227.       delimeterPos = suspectData.indexOf (delimeter,searchPos);
  228.       state = suspectData.subString(searchPos,delimeterPos-searchPos);
  229.       searchPos = delimeterPos+1;         // set next search position
  230.       suspectRecord.setState(state);
  231.  
  232.       // get zip from suspectData and set in the suspectRecord
  233.  
  234.       delimeterPos = suspectData.indexOf (delimeter,searchPos);
  235.       zip = suspectData.subString(searchPos,delimeterPos-searchPos);
  236.       searchPos = delimeterPos+1;         // set next search position
  237.       suspectRecord.setZip(zip);
  238.  
  239.       // get Description from suspectData and set in the suspectRecord
  240.  
  241.       delimeterPos = suspectData.indexOf (delimeter,searchPos);
  242.       description = suspectData.subString(searchPos,delimeterPos-searchPos);
  243.       searchPos =   delimeterPos+1;         // set next search position
  244.       suspectRecord.setDescription(description);
  245.  
  246.       // get MO from suspectData and set in the suspectRecord
  247.  
  248.       delimeterPos = suspectData.indexOf (delimeter,searchPos);
  249.       MO  = suspectData.subString(searchPos,delimeterPos-searchPos);
  250.       searchPos = delimeterPos+1;         // set next search position
  251.       suspectRecord.setMO(MO);
  252.  
  253.       // get ScarDescription from suspectData and set in the suspectRecord
  254.  
  255.       delimeterPos = suspectData.indexOf (delimeter,searchPos);
  256.       scarDescription = suspectData.subString(searchPos,delimeterPos-searchPos);
  257.       searchPos =   delimeterPos+1;         // set next search position
  258.       suspectRecord.setScarDescription(scarDescription);
  259.  
  260.       // get MoleDescription from suspectData and set in the suspectRecord
  261.  
  262.       delimeterPos = suspectData.indexOf (delimeter,searchPos);
  263.       moleDescription = suspectData.subString(searchPos,delimeterPos-searchPos);
  264.       searchPos =   delimeterPos+1;         // set next search position
  265.       suspectRecord.setMoleDescription(moleDescription);
  266.  
  267.       // get TattooDescription from suspectData and set in the suspectRecord
  268.  
  269.       delimeterPos = suspectData.indexOf (delimeter,searchPos);
  270.       tattooDescription = suspectData.subString(searchPos,delimeterPos-searchPos);
  271.       searchPos =   delimeterPos+1;         // set next search position
  272.       suspectRecord.setTattooDescription(tattooDescription);
  273.  
  274.       // at this time, one suspect record has been built
  275.  
  276.       //-----------------------------------------------------------------
  277.       // If this is the first suspect record then copy it to a temporary
  278.       // area. Otherwise, append it to the temporary area
  279.       // When all records are copied/appended, copy the temporary
  280.       // suspect record to the result record.
  281.       //-----------------------------------------------------------------
  282.  
  283.       if (firstSuspectRecord) {
  284.        ITRACE_DEVELOP("This is the first suspect record.");
  285.        firstSuspectRecord = false;
  286.        tempRecords = suspectRecord.asString();
  287.        }
  288.       else {
  289.        ITRACE_DEVELOP("This is not the first suspect record.");
  290.        tempRecords += suspectRecord.asString();
  291.        }
  292.  
  293.       delimeterPos = 0;              // reset for each suspect data
  294.       searchPos = 1;
  295.  
  296.    }  // end of for loop for processing each suspect
  297.  
  298.    //------------------------------------------------------
  299.    // Finished building suspect records. Build the result record
  300.    //------------------------------------------------------
  301.  
  302.  
  303.    ITRACE_DEVELOP("Setting up result record.");
  304.    numberOfSuspects = suspectProfile.numberOfKeys();
  305.    resultRecord.setNumResults(numberOfSuspects); // #of suspects
  306.    resultRecord.setResultData(tempRecords);
  307.  
  308.    return resultRecord;
  309. } // getAllSuspects()
  310.  
  311.  
  312. /*****************************************************************/
  313. /*                                                               */
  314. /* getAllAliases                                                 */
  315. /*                                                               */
  316. /*Function: Gets the list of aliases from  the database          */
  317. /*          using the book number from the suspect object        */
  318. /*                                                               */
  319. /*****************************************************************/
  320.  
  321. IResultRecord getAllAliases(unsigned long bookNumber)
  322. {
  323.   IBoolean       firstAliasRecord = true;
  324.   unsigned short delimeterPos=0,
  325.                  searchPos=1;
  326.   IString        tempRecords, name,
  327.                  key, data, profBookNumber;             // used with IProfile
  328.   IAliasRecord   aliasRecord;
  329.   IResultRecord  aliasResultRecord;
  330.   long           numberOfAliases=0;
  331.  
  332.  
  333.   //-----------------------
  334.   // Setup the aliasProfile
  335.   //-----------------------
  336.  
  337.   IFUNCTRACE_DEVELOP();
  338.   ITRACE_DEVELOP("Creating and associating application name with alias profile");
  339.   IString appName(ALIAS_APPNAME);
  340.   IProfile aliasProfile(IString(ALIAS_INI));
  341.   aliasProfile.setDefaultApplicationName(ALIAS_APPNAME);       // set default app name
  342.   ITRACE_DEVELOP("Creating and associating app. name with aliases profile done.");
  343.  
  344.   //------------------------------------------------------------------
  345.   // Iterate through the alias profile and build the aliasResultRecord
  346.   //------------------------------------------------------------------
  347.  
  348.   IProfile::Cursor cursor(aliasProfile, ALIAS_APPNAME);
  349.   for (cursor.setToFirst();cursor.isValid();cursor.setToNext())
  350.   {
  351.      key = aliasProfile.applicationOrKeyAt(cursor);
  352.      data = aliasProfile.elementWithKey(key);
  353.  
  354.      delimeterPos = data.indexOf (delimeter,searchPos);
  355.      profBookNumber = data.subString(searchPos,delimeterPos-searchPos);
  356.      if (bookNumber == profBookNumber.asUnsigned())           // process the alias
  357.         {
  358.          numberOfAliases = numberOfAliases +1;
  359.          searchPos = delimeterPos +1;
  360.  
  361.          // extract the name from the alias data from the profile
  362.          // and add to the aliasRecord
  363.  
  364.          delimeterPos = data.indexOf (delimeter,searchPos);
  365.          name = data.subString(searchPos,delimeterPos-searchPos);
  366.  
  367.          aliasRecord.setName(name);
  368.          //-----------------------------------------------------------------
  369.          // If this is the first alias record then COPY it to a temporary
  370.          // area. Otherwise, APPEND it to the temporary area
  371.          // When all records are copied/appended, copy the temporary
  372.          // alias record to the result record.
  373.          //-----------------------------------------------------------------
  374.          if (firstAliasRecord)
  375.          {
  376.           ITRACE_DEVELOP("this is the first alias");
  377.           firstAliasRecord = false;
  378.           tempRecords = aliasRecord.asString();
  379.           ITRACE_DEVELOP("tempRecords in first record code: " + tempRecords + ".");
  380.          }
  381.          else
  382.          {
  383.           tempRecords += aliasRecord.asString();
  384.           ITRACE_DEVELOP("tempRecords in subsequent record code: " + tempRecords + ".");
  385.          }
  386.         }
  387.      delimeterPos = 0;                     // reset for next alias data read in
  388.      searchPos = 1;
  389.   }  // end of for iteration of the aliasProfile
  390.  
  391.   //----------------------------------------------------------
  392.   // Finished building alias records. Add to the result record
  393.   //----------------------------------------------------------
  394.   ITRACE_DEVELOP("setting the result data");
  395.   aliasResultRecord.setResultData(tempRecords);
  396.   aliasResultRecord.setNumResults(numberOfAliases);
  397.   ITRACE_DEVELOP("set the result data");
  398.  
  399.   return aliasResultRecord;
  400. }
  401.  
  402.  
  403. /*****************************************************************/
  404. /*                                                               */
  405. /* getAllArrests                                                 */
  406. /*                                                               */
  407. /*Function: Gets the list of arrests from  the database          */
  408. /*          using the book number from the suspect object        */
  409. /*                                                               */
  410. /*****************************************************************/
  411. IResultRecord getAllArrests(unsigned long bookNumber)
  412. {
  413.   IBoolean              firstArrestRecord = true;
  414.   IString               tempRecords,
  415.                         key,arrestData, profBookNumber, // used with IProfile
  416.                         arrestCharge,arrestStatus,arrestDate;
  417.   IArrestRecord         arrestRecord;
  418.   IResultRecord         arrestResultRecord;
  419.   long                  numberOfArrests=0;
  420.   unsigned short        searchPos=1, delimeterPos=0;
  421.  
  422.   //-------------------------
  423.   // Setup the arrestsProfile
  424.   //-------------------------
  425.  
  426.   IFUNCTRACE_DEVELOP();
  427.   ITRACE_DEVELOP("Creating and associating application name with arrest profile");
  428.   IString appName(ARREST_APPNAME);
  429.   IProfile arrestsProfile(IString(ARREST_INI));
  430.   arrestsProfile.setDefaultApplicationName(ARREST_APPNAME);       // set default app name
  431.   ITRACE_DEVELOP("Creating and associating app. name with arrests profile done.");
  432.  
  433.   //-----------------------------------------------------------------------
  434.   // Iterate through the arrests profile and build the arrest result record
  435.   //-----------------------------------------------------------------------
  436.  
  437.   IProfile::Cursor cursor(arrestsProfile, ARREST_APPNAME);
  438.   for (cursor.setToFirst();cursor.isValid();cursor.setToNext())
  439.   {
  440.      key = arrestsProfile.applicationOrKeyAt(cursor);
  441.      arrestData = arrestsProfile.elementWithKey(key);
  442.  
  443.      delimeterPos = arrestData.indexOf (delimeter,searchPos);
  444.      profBookNumber = arrestData.subString(searchPos,delimeterPos-searchPos);
  445.      if (bookNumber == profBookNumber.asUnsigned())           // process the alias
  446.         {
  447.          numberOfArrests = numberOfArrests + 1;
  448.  
  449.          // extract the arrestCharge, arrestStatus, and arrestDate from
  450.          // the aliasData from the profile and add to the arrestRecord
  451.  
  452.          searchPos = delimeterPos+1;         // set next search position
  453.          delimeterPos = arrestData.indexOf (delimeter,searchPos);
  454.          arrestCharge = arrestData.subString(searchPos,delimeterPos-searchPos);
  455.          arrestRecord.setCharge(arrestCharge.asUnsigned());
  456.  
  457.          searchPos = delimeterPos +1;
  458.          delimeterPos = arrestData.indexOf (delimeter,searchPos);
  459.          arrestStatus = arrestData.subString(searchPos,delimeterPos-searchPos);
  460.          arrestRecord.setStatus(arrestStatus.asUnsigned());
  461.  
  462.          searchPos = delimeterPos +1;
  463.          delimeterPos = arrestData.indexOf (delimeter,searchPos);
  464.          arrestDate = arrestData.subString(searchPos,delimeterPos-searchPos);
  465.          arrestRecord.setStatusDate(arrestDate);
  466.  
  467.          //-----------------------------------------------------------------
  468.          // If this is the first alias record then COPY it to a temporary
  469.          // area. Otherwise, APPEND it to the temporary area
  470.          // When all records are copied/appended, copy the temporary
  471.          // alias record to the result record.
  472.          //-----------------------------------------------------------------
  473.          if (firstArrestRecord)
  474.          {
  475.           ITRACE_DEVELOP("this is the first alias");
  476.           firstArrestRecord = false;
  477.           tempRecords = arrestRecord.asString();
  478.           ITRACE_DEVELOP("tempRecords in first record code: " + tempRecords + ".");
  479.          }
  480.          else
  481.          {
  482.           tempRecords += arrestRecord.asString();
  483.           ITRACE_DEVELOP("tempRecords in subsequent record code: " + tempRecords + ".");
  484.          }
  485.         }
  486.      delimeterPos = 0;                     // reset for next alias data read in
  487.      searchPos = 1;
  488.   }  // end of for iteration of the arrestProfile
  489.  
  490.   //------------------------------------------------------------------
  491.   // Finished building arrest records. Add to the arrest result record
  492.   //------------------------------------------------------------------
  493.   ITRACE_DEVELOP("setting the result data");
  494.   arrestResultRecord.setResultData(tempRecords);
  495.   arrestResultRecord.setNumResults(numberOfArrests);
  496.   ITRACE_DEVELOP("set the result data");
  497.  
  498.   return arrestResultRecord;
  499.  
  500. }
  501.  
  502.  
  503. /*****************************************************************************
  504.  ! error(const char *message)
  505.  !
  506.  !   Display error message and exit.
  507.  *****************************************************************************/
  508. void error(const char *message)
  509. {
  510.    cout << "\n\nERROR: %s\n\n", message;
  511.    exit(1);
  512. }
  513.  
  514.