home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / VSCPPv8.zip / VACPP / IBMCPP / samples / IOC / LANCELOT / LPERSWDM.CPP < prev    next >
C/C++ Source or Header  |  1995-04-07  |  14KB  |  331 lines

  1. /****************************************************************************
  2. * LANCELOT SAMPLE PROGRAM - lperswdm.cpp                                    *
  3. *                                                                           *
  4. * Classes : EmployeeItem                                                    *
  5. *                                                                           *
  6. * DISCLAIMER OF WARRANTIES:                                                 *
  7. *   The following [enclosed] code is sample code created by IBM             *
  8. *   Corporation.  This sample code is not part of any standard IBM product  *
  9. *   and is provided to you solely for the purpose of assisting you in the   *
  10. *   development of your applications.  The code is provided "AS IS",        *
  11. *   without warranty of any kind.  IBM shall not be liable for any damages  *
  12. *   arising out of your use of the sample code, even if they have been      *
  13. *   advised of the possibility of such damages.                             *
  14. ****************************************************************************/
  15.  
  16. #ifndef _IBASE_                         //Make sure ibase.hpp is included
  17.   #include <ibase.hpp>                  //  since that is where IC_<environ>
  18. #endif                                  //  is defined.
  19. #include <icnrctl.hpp>
  20. #include <icnrobj.hpp>
  21. #include <imsgbox.hpp>
  22.  
  23. #include "lperswdm.hpp"
  24. #include "lperswin.hpp"
  25. #include "lstatus.hpp"
  26.  
  27. /****************************************************************************
  28. * CLASS EmployeeItem - Constructor                                          *
  29. ****************************************************************************/
  30. EmployeeItem ::  EmployeeItem( const IDMItem::Handle &item )
  31.     : IDMCnrItem( item )
  32. {
  33.     IString
  34.       rmf1 = IDMItem::rmfFrom( IDM::rmDiscard, IDM::rfUnknown );
  35.     // Get pointer to the associated Employee container object...
  36.     LPersonnelCnrObject
  37.      *employee  = (LPersonnelCnrObject*)this->object();
  38.  
  39.     // Build and set contents.  We can only do this on the source
  40.     // side.  Note that since we call this ctor on both source
  41.     // and target sides, we must distinguish them.  That is done
  42.     // here by checking the "object" pointer.  If this ctor was
  43.     // called from within our generateSourceItems, then this value
  44.     // will be non-zero.  If called from with the template provider's
  45.     // provideTargetItemFor, then it will be zero.
  46.     if ( employee ) {
  47.  
  48.        IString
  49.          contents;
  50.        contents = employee -> employeeRecord().department();
  51.        this -> setContents( contents );
  52.  
  53.        // Add RMFs supported by this class (IDMCnrItem will have
  54.        // already specified others)...
  55.        this -> addRMF( rmf1 );
  56.     }
  57.     else
  58.       // On target side, add in <rmLibrary,rfSharedMem> if source concurs
  59.       // (and it's not already in there)...
  60.       if ( item -> supportsRMF( rmf1 )
  61.            &&
  62.            !( this -> supportsRMF( rmf1 ) ) )
  63.          this -> addRMF( rmf1 );
  64.  
  65. } // EmployeeItem constructor
  66.  
  67. /****************************************************************************
  68. * CLASS EmployeeItem - Destructor                                           *
  69. ****************************************************************************/
  70. EmployeeItem ::   ~EmployeeItem( )
  71.     {
  72.     }
  73.  
  74. /****************************************************************************
  75. * CLASS EmployeeItem - generateSourceItem                                   *
  76. ****************************************************************************/
  77. IBase::Boolean EmployeeItem ::
  78.   generateSourceItems ( const IDMSourceOperation::Handle &srcOp )
  79.     {
  80.     // Get generic container items.  Note that we call the inherited
  81.     // function since it already has smarts to deal with multi-selection,
  82.     // etc...
  83.     Boolean
  84.       result = Inherited::generateSourceItems( srcOp );
  85.  
  86.     // Now, replace each IDMCnrItem with a employee item...
  87.     for ( unsigned i = 1; i <= srcOp->numberOfItems(); i++ )
  88.       srcOp -> replaceItem( i, new EmployeeItem( srcOp -> item( i ) ) );
  89.  
  90.     // Set stack3AndFade as the default image style ...
  91.     srcOp -> setImageStyle( IDM::stack3AndFade );
  92.     return result;
  93.     }
  94.  
  95. /****************************************************************************
  96. * CLASS EmployeeItem - sourceDiscard                                        *
  97. ****************************************************************************/
  98. Boolean EmployeeItem ::
  99.   sourceDiscard ( IDMSourceDiscardEvent &event )
  100.     {
  101.  
  102.     IContainerControl
  103.      *cnr = (IContainerControl*)( event.sourceOperation()->sourceWindow() );
  104.  
  105.     LPersonnelCnrObject
  106.       *employee  = (LPersonnelCnrObject*)this->object();
  107.  
  108.     LPersonnelWindow *theWindow = (LPersonnelWindow *)cnr->parent();
  109.  
  110.     deleteActionDM( employee, theWindow );
  111.  
  112.     // Remove the object from the container...
  113.     IContainerObject
  114.      *obj = (IContainerObject*)( this->object() );
  115.     cnr -> removeObject( obj );
  116.     // discard from DB
  117.  
  118.     return true;
  119.     }
  120.  
  121.  
  122. /****************************************************************************
  123. * CLASS EmployeeItem - targetDrop                                           *
  124. ****************************************************************************/
  125. Boolean EmployeeItem ::
  126.   targetDrop ( IDMTargetDropEvent &event )
  127.     {
  128.     Boolean
  129.       result = true;
  130.     // Check if using rfSharedMem...
  131.     IString
  132.       myRMF = IDMItem::rmfFrom( IDM::rmLibrary, IDM::rfSharedMem );
  133.  
  134.     LPersonnelCnrObject
  135.       *employee  = (LPersonnelCnrObject*)this->object();
  136.  
  137.     if ( this->selectedRMF() == myRMF ) {
  138.  
  139.       // Yes, get dept name from passed data...
  140.  
  141.       IString
  142.         dept     = this->contents();
  143.  
  144.       // See if the departments are different, if yes then transfer the employee
  145.       // if it the same or the query is a dept then don't do the drop
  146.       //
  147.  
  148.       IContainerControl
  149.        *cnr = event.container();
  150.  
  151.       LPersonnelWindow *theWindow = (LPersonnelWindow *)cnr->parent();
  152.       if (theWindow->deptQuery() != dept ) {
  153.          // transfer
  154.  
  155.          LPersonnelCnrObject
  156.           *employee  = (LPersonnelCnrObject*)this->object();
  157.  
  158.          transferActionDM( employee, dept, theWindow );
  159.        }
  160.     }
  161.     else {
  162.  
  163.       // Some other RMF, base class must support it...
  164.       // See if the departments are different, if yes then transfer the employee
  165.       // if it the same or the query is a dept then don't do the drop
  166.       //
  167.  
  168.       IString
  169.         dept     = this->contents();
  170.  
  171.       IContainerControl
  172.        *cnr = event.container();
  173.  
  174.       LPersonnelWindow *theWindow = (LPersonnelWindow *)cnr->parent();
  175.       if ((theWindow->deptQuery() != dept ) &&
  176.           (theWindow->deptQuery().length()>0)) {
  177.  
  178.          transferActionDM( employee, theWindow->deptQuery(), theWindow );
  179.          result = Inherited::targetDrop( event );
  180.        }
  181.        else
  182.          if (this->sourceContainer() != cnr) {
  183.             // don't drop it, message box.
  184.             IString aMsg;
  185.             if (theWindow->deptQuery() == dept ) {
  186.  
  187.                aMsg = employee -> employeeRecord().firstName() + " " +
  188.                       employee -> employeeRecord().lastName() + " " +
  189.                          IApplication::current().userResourceLibrary().
  190.                              loadString( STR_TRANSFER_MSG6 ) + " " + dept;
  191.             }
  192.             else {
  193.                aMsg = employee -> employeeRecord().firstName() + " " +
  194.                       employee -> employeeRecord().lastName() + " " +
  195.                          IApplication::current().userResourceLibrary().
  196.                           loadString( STR_TRANSFER_MSG5 );
  197.             }
  198.  
  199.             // no message if source and target is the same
  200.             IMessageBox msg( theWindow );
  201.             msg.show( aMsg,
  202.                       IMessageBox::informationIcon );
  203.          } // not the same container
  204.  
  205.     }
  206.  
  207.     return result;
  208. }
  209.  
  210.  
  211. /****************************************************************************
  212. * CLASS EmployeeItem - supportedOperationsFor                               *
  213. ****************************************************************************/
  214. unsigned long  EmployeeItem ::
  215.   supportedOperationsFor ( const IString &rmf ) const
  216.     {
  217.     if ( rmf == IDMItem::rmfFrom( IDM::rmLibrary, IDM::rfSharedMem ) )
  218.       // If using <rmLibrary,rfSharedMem> then only copy is supported...
  219.       return IDMItem::copyable & this->supportedOperations();
  220.     else
  221.       // Else, whatever base class supports...
  222.       return Inherited::supportedOperationsFor( rmf );
  223.     }
  224.  
  225.  
  226. /****************************************************************************
  227. * CLASS EmployeeItem :: objectCopy()                                             *
  228. ****************************************************************************/
  229. IContainerObject* EmployeeItem :: objectCopy()
  230. {
  231.    EmployeeItem *copy = new EmployeeItem(*this);
  232.    return((IContainerObject *)copy);
  233. }
  234.  
  235.  
  236. /****************************************************************************
  237. * CLASS EmployeeItem :: transferActionDM()                                       *
  238. ****************************************************************************/
  239. EmployeeItem& EmployeeItem::transferActionDM( LPersonnelCnrObject* pObject,
  240.                                               IString xfrDept,
  241.                                               LPersonnelWindow* theWindow)
  242. {
  243. /*----------------------------------------------------------------------------
  244. | Get the employee's record                                                  |
  245. ----------------------------------------------------------------------------*/
  246.   LEmployeeData empData = pObject->employeeRecord();
  247.   IString person = IString("'") + empData.firstName() + " " +
  248.                    empData.lastName() + "'";
  249.  
  250. /*----------------------------------------------------------------------------
  251. | - Change the employee's department                                         |
  252. | - Put the change to the database                                           |
  253. ----------------------------------------------------------------------------*/
  254.         if ( xfrDept != "" )
  255.         {
  256.             empData.setDepartment( xfrDept );
  257.             empData.save( empData.employeeNumber() );
  258.             IString curDept = IString("'") + xfrDept + "'";
  259.  
  260.             IMessageBox msg( theWindow );
  261.             msg.show( person +
  262.                       IApplication::current().userResourceLibrary().
  263.                           loadString( STR_TRANSFER_MSG4 ) +
  264.                       xfrDept + ".",
  265.                       IMessageBox::informationIcon );
  266.         }
  267.     return *this;
  268. }
  269.  
  270. /****************************************************************************
  271. * CLASS EmployeeItem :: deleteActionDM()                                      *
  272. ****************************************************************************/
  273. IBase::Boolean EmployeeItem::deleteActionDM( LPersonnelCnrObject* pObject,
  274.                                               LPersonnelWindow* theWindow)
  275. {
  276.     Boolean retCode = false;
  277.  
  278.     IMessageBox msgBox( theWindow );
  279.  
  280. /*----------------------------------------------------------------------------
  281. | Ask if the user really wants to delete or just set to inactive.            |
  282. ----------------------------------------------------------------------------*/
  283.     IMessageBox::Response
  284.         response = msgBox.show( IResourceId( STR_PERSONNEL_DELETE_MSG ),
  285.                                 IMessageBox::warningIcon |
  286.                                 IMessageBox::yesNoCancelButton,
  287.                                 ID_PERSONNEL_DELETE_HELP );
  288. /*----------------------------------------------------------------------------
  289. | Get the employee's record                                                  |
  290. ----------------------------------------------------------------------------*/
  291.     LEmployeeData empData = pObject->employeeRecord();
  292.     IString person = empData.firstName() + " " +
  293.                      empData.lastName();
  294.  
  295. /*----------------------------------------------------------------------------
  296. | Delete the employee                                                        |
  297. | - Delete the employee from the database                                    |
  298. | - Delete the cnr object                                                    |
  299. ----------------------------------------------------------------------------*/
  300.     if ( response == IMessageBox::no )
  301.     {
  302.         LDeleteEmployee deleteEmp;
  303.         deleteEmp.deleteFromDataBase(
  304.              empData.employeeNumber() );
  305.         msgBox.show( STR_PERSONNEL_DELETED_MSG,
  306.                      IMessageBox::okButton |
  307.                      IMessageBox::informationIcon );
  308.         retCode = true;
  309.     }
  310. /*----------------------------------------------------------------------------
  311. | Set the employee to inactive                                               |
  312. | - Get the employee's status data                                           |
  313. | - Put the status data back with active set to false                        |
  314. ----------------------------------------------------------------------------*/
  315.     else if ( response == IMessageBox::yes )
  316.     {
  317.         LStatusData statData( empData.employeeNumber() );
  318.         statData.save( empData.employeeNumber(),
  319.                       statData.statusRate(),
  320.                       statData.statusStart(),
  321.                       statData.statusEnd(),
  322.                       false );
  323.  
  324.         msgBox.show( STR_PERSONNEL_INACTIVE_MSG,
  325.                   IMessageBox::okButton |
  326.                   IMessageBox::informationIcon );
  327.         retCode = false;
  328.     }
  329.     return retCode;
  330. }
  331.