home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / labs / c09 / lab01 / ex01 / employeeview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  4.2 KB  |  161 lines

  1. // EmployeeView.cpp : implementation of the CEmployeeView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "Employee.h"
  6.  
  7. #include "EmployeePaySet.h"
  8. #include "EmployeePersonalSet.h"
  9.  
  10. #include "EmployeeDoc.h"
  11. #include "EmployeeView.h"
  12. #include "EmployeeDDX.cpp"
  13.  
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19.  
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CEmployeeView
  22.  
  23.  
  24. IMPLEMENT_DYNCREATE(CEmployeeView, CRecordView)
  25.  
  26. BEGIN_MESSAGE_MAP(CEmployeeView, CRecordView)
  27.     //{{AFX_MSG_MAP(CEmployeeView)
  28.     //}}AFX_MSG_MAP
  29. END_MESSAGE_MAP()
  30.  
  31. /////////////////////////////////////////////////////////////////////////////
  32. // CEmployeeView construction/destruction
  33.  
  34. CEmployeeView::CEmployeeView()
  35.     : CRecordView(CEmployeeView::IDD)
  36. {
  37.     //{{AFX_DATA_INIT(CEmployeeView)
  38.     m_pSet = NULL;
  39.     m_pEmplInfoSet = NULL;
  40.     //}}AFX_DATA_INIT
  41.     // TODO: add construction code here
  42. }
  43.  
  44. CEmployeeView::~CEmployeeView()
  45. {
  46. }
  47.  
  48. void CEmployeeView::DoDataExchange(CDataExchange* pDX)
  49. {
  50.     CRecordView::DoDataExchange(pDX);
  51.     //{{AFX_DATA_MAP(CEmployeeView)
  52.     DDX_FieldText(pDX, IDC_FNAME, m_pSet->m_First_Name, m_pSet);
  53.     DDX_FieldText(pDX, IDC_LNAME, m_pSet->m_Last_Name, m_pSet);
  54.     DDX_FieldText(pDX, IDC_EMP_NO, m_pSet->m_Employee_Number, m_pSet);
  55.     DDX_FieldText(pDX, IDC_DEPT, m_pSet->m_Department__, m_pSet);
  56.     DDX_FieldText(pDX, IDC_PAY_TYPE, m_pSet->m_Employee_Pay_Type, m_pSet);
  57.     DDX_FieldText(pDX, IDC_HOUR_RATE, m_pSet->m_Hourly_Rate, m_pSet);
  58.     //}}AFX_DATA_MAP
  59.     
  60.     //Ensure pointer is valid
  61.     if ( NULL != m_pEmplInfoSet )
  62.     {
  63.         DDX_FieldText(pDX, IDC_BIRTH, m_pEmplInfoSet->m_Birthdate, m_pEmplInfoSet);
  64.         DDV_Date( pDX, m_pEmplInfoSet->m_Birthdate, m_pEmplInfoSet );
  65.         DDX_FieldText(pDX, IDC_MARITAL_STATUS, m_pEmplInfoSet->m_Sex___Marital_Status, m_pEmplInfoSet);
  66.         DDX_FieldText(pDX, IDC_HEIGHT, m_pEmplInfoSet->m_Height, m_pEmplInfoSet);
  67.         DDX_FieldText(pDX, IDC_WEIGHT, m_pEmplInfoSet->m_Weight, m_pEmplInfoSet);
  68.     }
  69. }
  70.  
  71. BOOL CEmployeeView::PreCreateWindow(CREATESTRUCT& cs)
  72. {
  73.     return CRecordView::PreCreateWindow(cs);
  74. }
  75.  
  76. void CEmployeeView::OnInitialUpdate()
  77. {
  78.     // AppWizard provided this line. 
  79.     // m_pSet = &GetDocument( )->m_employeePaySet;
  80.  
  81.     // Best to replace it with an accessor function.
  82.     m_pSet = GetDocument( )->GetEmployeePaySet( );
  83.     
  84.     m_pEmplInfoSet = GetDocument( )->GetEmployeeInfoSet( );
  85.     GetParentFrame( )->RecalcLayout( );
  86.     ResizeParentToFit( FALSE );
  87.  
  88.     try
  89.     {
  90.         //Open the primary table
  91.         m_pSet->Open( );    
  92.         m_pEmplInfoSet->m_EmployeeNumberParam 
  93.             = m_pSet->m_Employee_Number;    //Set join param
  94.         //Open foreign table
  95.         m_pEmplInfoSet->Open( );    
  96.     }
  97.     catch( CException * pEx )
  98.     {
  99.         pEx->ReportError( );
  100.         pEx->Delete( );
  101.         return;
  102.     }
  103.  
  104.     UpdateData( FALSE );
  105.  
  106.     CRecordView::OnInitialUpdate( );
  107. }
  108.  
  109. /////////////////////////////////////////////////////////////////////////////
  110. // CEmployeeView diagnostics
  111.  
  112. #ifdef _DEBUG
  113. void CEmployeeView::AssertValid() const
  114. {
  115.     CRecordView::AssertValid();
  116. }
  117.  
  118. void CEmployeeView::Dump(CDumpContext& dc) const
  119. {
  120.     CRecordView::Dump(dc);
  121. }
  122.  
  123. CEmployeeDoc* CEmployeeView::GetDocument() // non-debug version is inline
  124. {
  125.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEmployeeDoc)));
  126.     return (CEmployeeDoc*)m_pDocument;
  127. }
  128. #endif //_DEBUG
  129.  
  130. /////////////////////////////////////////////////////////////////////////////
  131. // CEmployeeView database support
  132. CRecordset* CEmployeeView::OnGetRecordset()
  133. {
  134.     return m_pSet;
  135. }
  136.  
  137.  
  138. /////////////////////////////////////////////////////////////////////////////
  139. // CEmployeeView message handlers
  140.  
  141. BOOL CEmployeeView::OnMove(UINT nIDMoveCommand) 
  142. {
  143.     if ( ! UpdateData( TRUE ) )
  144.         return FALSE;    //Bad data: We don't want to move
  145.     m_pSet->SetFieldDirty( NULL );    //Otherwise DB update won't occur
  146.     
  147.     BOOL bMove = CRecordView::OnMove(nIDMoveCommand);    //Primary table updated
  148.  
  149.     if ( bMove )    //If successful update the foreign info
  150.     {
  151.         m_pEmplInfoSet->Edit( );                //Turn on edit mode
  152.         m_pEmplInfoSet->SetFieldDirty( NULL );    //Mark the fields dirty
  153.         m_pEmplInfoSet->Update( );
  154.         m_pEmplInfoSet->m_EmployeeNumberParam = 
  155.             m_pSet->m_Employee_Number;
  156.         m_pEmplInfoSet->Requery( );        //Find foreign record
  157.         UpdateData( FALSE );            //Display foreign record
  158.     }
  159.     return bMove;
  160. }
  161.