home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / labs / c08 / lab02 / ex02 / employeeview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  8.6 KB  |  353 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. #include "EmployeeDoc.h"
  10. #include "EmployeeView.h"
  11.  
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18. /////////////////////////////////////////////////////////////////////////////
  19. // CEmployeeView
  20.  
  21. IMPLEMENT_DYNCREATE(CEmployeeView, CDaoRecordView)
  22.  
  23. BEGIN_MESSAGE_MAP(CEmployeeView, CDaoRecordView)
  24.     //{{AFX_MSG_MAP(CEmployeeView)
  25.     ON_COMMAND(ID_RECORD_ADD, OnRecordAdd)
  26.     ON_COMMAND(ID_RECORD_DELETE, OnRecordDelete)
  27.     ON_UPDATE_COMMAND_UI(ID_RECORD_DELETE, OnUpdateRecordDelete)
  28.     ON_COMMAND(ID_RECORD_CLEAR, OnRecordClear)
  29.     ON_UPDATE_COMMAND_UI(ID_RECORD_ADD, OnUpdateRecordAdd)
  30.     //}}AFX_MSG_MAP
  31.     // Standard printing commands
  32.     ON_COMMAND(ID_FILE_PRINT, CDaoRecordView::OnFilePrint)
  33.     ON_COMMAND(ID_FILE_PRINT_DIRECT, CDaoRecordView::OnFilePrint)
  34.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CDaoRecordView::OnFilePrintPreview)
  35. END_MESSAGE_MAP()
  36.  
  37. /////////////////////////////////////////////////////////////////////////////
  38. // CEmployeeView construction/destruction
  39.  
  40. CEmployeeView::CEmployeeView()
  41.     : CDaoRecordView(CEmployeeView::IDD)
  42. {
  43.     //{{AFX_DATA_INIT(CEmployeeView)
  44.     m_pSet = NULL;
  45.     //}}AFX_DATA_INIT
  46.     // TODO: add construction code here
  47.     m_bAddMode = FALSE;
  48. }
  49.  
  50. CEmployeeView::~CEmployeeView()
  51. {
  52. }
  53.  
  54. void CEmployeeView::DoDataExchange(CDataExchange* pDX)
  55. {
  56.     CDaoRecordView::DoDataExchange(pDX);
  57.     //{{AFX_DATA_MAP(CEmployeeView)
  58.     DDX_FieldText(pDX, IDC_FNAME, m_pSet->m_First_Name, m_pSet);
  59.     DDX_FieldText(pDX, IDC_LNAME, m_pSet->m_Last_Name, m_pSet);
  60.     DDX_FieldText(pDX, IDC_EMP_NO, m_pSet->m_Employee_Number, m_pSet);
  61.     DDX_FieldText(pDX, IDC_DEPT, m_pSet->m_Department__, m_pSet);
  62.     DDX_FieldText(pDX, IDC_PAY_TYPE, m_pSet->m_Employee_Pay_Type, m_pSet);
  63.     DDX_FieldText(pDX, IDC_HOUR_RATE, m_pSet->m_Hourly_Rate, m_pSet);
  64.     //}}AFX_DATA_MAP
  65.  
  66.     //    Make sure this dataset pointer has been setup before trying
  67.     //    to access it.
  68.     if(m_pEmplInfoSet != NULL)
  69.     {
  70.         DDX_FieldText(pDX, IDC_BIRTH, m_pEmplInfoSet->m_Birthdate, m_pEmplInfoSet);
  71.         DDX_FieldText(pDX, IDC_MARITAL_STATUS, m_pEmplInfoSet->m_Sex___Marital_Status, m_pEmplInfoSet);
  72.         DDX_FieldText(pDX, IDC_HEIGHT, m_pEmplInfoSet->m_Height, m_pEmplInfoSet);
  73.         DDX_FieldText(pDX, IDC_WEIGHT, m_pEmplInfoSet->m_Weight, m_pEmplInfoSet);
  74.     }
  75.  
  76. }
  77.  
  78. BOOL CEmployeeView::PreCreateWindow(CREATESTRUCT& cs)
  79. {
  80.     // TODO: Modify the Window class or styles here by modifying
  81.     //  the CREATESTRUCT cs
  82.  
  83.     return CDaoRecordView::PreCreateWindow(cs);
  84. }
  85.  
  86. void CEmployeeView::OnInitialUpdate()
  87. {
  88.     m_pSet            = GetDocument()->GetEmployeePaySet();
  89.     m_pEmplInfoSet    = GetDocument()->GetEmployeeInfoSet();
  90.  
  91.     CDaoRecordView::OnInitialUpdate();
  92.  
  93.     //resize the window
  94.     GetParentFrame()->RecalcLayout();
  95.     ResizeParentToFit(FALSE);
  96.  
  97.     //set the parameter for the info query
  98.     m_pEmplInfoSet->m_EmployeeNumberParam = m_pSet->m_Employee_Number;
  99.  
  100.     //open the Info record set
  101.     try
  102.     {
  103.         m_pEmplInfoSet->Open();
  104.     }
  105.     catch (CDaoException* e)
  106.     {
  107.         AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  108.         e->Delete();
  109.         return;
  110.     }
  111.  
  112.     UpdateData(FALSE);
  113. }
  114.  
  115. /////////////////////////////////////////////////////////////////////////////
  116. // CEmployeeView printing
  117.  
  118. BOOL CEmployeeView::OnPreparePrinting(CPrintInfo* pInfo)
  119. {
  120.     // default preparation
  121.     return DoPreparePrinting(pInfo);
  122. }
  123.  
  124. void CEmployeeView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  125. {
  126.     // TODO: add extra initialization before printing
  127. }
  128.  
  129. void CEmployeeView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  130. {
  131.     // TODO: add cleanup after printing
  132. }
  133.  
  134. /////////////////////////////////////////////////////////////////////////////
  135. // CEmployeeView diagnostics
  136.  
  137. #ifdef _DEBUG
  138. void CEmployeeView::AssertValid() const
  139. {
  140.     CDaoRecordView::AssertValid();
  141. }
  142.  
  143. void CEmployeeView::Dump(CDumpContext& dc) const
  144. {
  145.     CDaoRecordView::Dump(dc);
  146. }
  147.  
  148. CEmployeeDoc* CEmployeeView::GetDocument() // non-debug version is inline
  149. {
  150.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEmployeeDoc)));
  151.     return (CEmployeeDoc*)m_pDocument;
  152. }
  153. #endif //_DEBUG
  154.  
  155. /////////////////////////////////////////////////////////////////////////////
  156. // CEmployeeView database support
  157. CDaoRecordset* CEmployeeView::OnGetRecordset()
  158. {
  159.     return m_pSet;
  160. }
  161.  
  162. /////////////////////////////////////////////////////////////////////////////
  163. // CEmployeeView message handlers
  164. BOOL CEmployeeView::OnMove(UINT nIDMoveCommand) 
  165. {
  166.     if(m_bAddMode)
  167.     {
  168.         AddRecordCancel();
  169.     }
  170.  
  171.     //navigate, and update while you are navigating....
  172.     COleVariant varRecordToReturnTo;
  173.     varRecordToReturnTo = m_pSet->GetBookmark();
  174.     try
  175.     {
  176.         //    Begin update transaction
  177.         m_pSet->m_pDatabase->m_pWorkspace->BeginTrans();
  178.  
  179.         //    Put the EmployeeInfoSet into Edit mode before
  180.         //    calling CDaoRecordSet::OnMove()
  181.         m_pEmplInfoSet->Edit();
  182.  
  183.         //    CDaoRecordView::OnMove() will call UpdateData
  184.         CDaoRecordView::OnMove(nIDMoveCommand);
  185.  
  186.         //    Finish the update of the EmployeeInfoSet
  187.         m_pEmplInfoSet->Update();
  188.         
  189.         //    Commit update transaction
  190.         m_pSet->m_pDatabase->m_pWorkspace->CommitTrans();
  191.     }
  192.     catch (CDaoException* e)
  193.     {
  194.         //    Rollback changes
  195.         m_pSet->m_pDatabase->m_pWorkspace->Rollback();
  196.  
  197.         //    Make sure PayInfo record set is returned
  198.         //    to the proper position
  199.         m_pSet->SetBookmark( varRecordToReturnTo );
  200.  
  201.         AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  202.         e->Delete();
  203.     }
  204.  
  205.     //    Update EmployeeNumber parameter and requery
  206.     m_pEmplInfoSet->m_EmployeeNumberParam = m_pSet->m_Employee_Number;
  207.     m_pEmplInfoSet->Requery();
  208.  
  209.     // Show results of Employee Info requery
  210.     UpdateData(FALSE);
  211.  
  212.      return TRUE;
  213. }
  214.  
  215. void CEmployeeView::OnRecordAdd() 
  216. {
  217.     UpdateData();
  218.  
  219.     try
  220.     {
  221.         m_pEmplInfoSet->m_Employee_Number = m_pSet->m_Employee_Number;
  222.         m_pSet->Update();
  223.         m_pEmplInfoSet->Update();
  224.         
  225.         //if we didn't except out, commit it to the database
  226.         m_pSet->m_pDatabase->m_pWorkspace->CommitTrans();    
  227.     }
  228.     catch (CDaoException* e)
  229.     {
  230.         //something happened, so restore the db
  231.         m_pSet->m_pDatabase->m_pWorkspace->Rollback();
  232.  
  233.         //tell why
  234.         AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  235.         e->Delete();
  236.     
  237.         //restore the add mode
  238.         m_pSet->m_pDatabase->m_pWorkspace->BeginTrans();
  239.         m_pSet->AddNew();
  240.         m_pEmplInfoSet->AddNew();
  241.         return;
  242.     }
  243.  
  244.     //load it all back in, reset current record
  245.     m_pEmplInfoSet->m_EmployeeNumberParam = m_pSet->m_Employee_Number;
  246.     m_pSet->Requery();
  247.     m_pEmplInfoSet->Requery();
  248.     
  249.     //copy back to the form
  250.     UpdateData(FALSE);
  251.  
  252.     SetAddMode(FALSE);
  253. }
  254.  
  255. void CEmployeeView::OnRecordDelete() 
  256. {
  257.     try
  258.     {
  259.         //begin the transaction
  260.         m_pSet->m_pDatabase->m_pWorkspace->BeginTrans();
  261.  
  262.         //delete both records
  263.         m_pSet->Delete();
  264.         m_pEmplInfoSet->Delete();
  265.  
  266.         //end the transaction
  267.         m_pSet->m_pDatabase->m_pWorkspace->CommitTrans();
  268.     }
  269.     catch (CDaoException* e)
  270.     {
  271.         //something happened, so restore the db
  272.         m_pSet->m_pDatabase->m_pWorkspace->Rollback();
  273.  
  274.         //tell why
  275.         AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  276.         e->Delete();
  277.  
  278.         //and bail out
  279.         return;
  280.     }
  281.     
  282.     m_pSet->MoveNext();
  283.     m_pSet->Requery();
  284.  
  285.     if(m_pSet->IsEOF() && !m_pSet->IsBOF())
  286.         m_pSet->MoveLast();
  287.  
  288.     if(m_pSet->IsBOF())
  289.         m_pSet->SetFieldNull(NULL);
  290.  
  291.     m_pEmplInfoSet->m_EmployeeNumberParam = m_pSet->m_Employee_Number;
  292.     m_pEmplInfoSet->Requery();
  293.  
  294.     if(m_pEmplInfoSet->IsEOF() && m_pEmplInfoSet->IsBOF())
  295.             m_pEmplInfoSet->SetFieldNull(NULL);
  296.  
  297.     UpdateData(FALSE);
  298. }
  299.  
  300. void CEmployeeView::OnUpdateRecordDelete(CCmdUI* pCmdUI) 
  301. {
  302.     pCmdUI->Enable(!m_bAddMode);
  303. }
  304.  
  305. void CEmployeeView::OnUpdateRecordAdd(CCmdUI* pCmdUI) 
  306. {
  307.     pCmdUI->Enable(m_bAddMode);
  308. }
  309.  
  310. void CEmployeeView::OnRecordClear() 
  311. {
  312.     //Create a temporary record filled with (database) NULLs
  313.     //No record is added to the database until Update() is called
  314.     //start the transaction
  315.     m_pSet->m_pDatabase->m_pWorkspace->BeginTrans();
  316.     m_pSet->AddNew();
  317.     m_pEmplInfoSet->AddNew();
  318.  
  319.     UpdateData(FALSE);
  320.     SetAddMode(TRUE);
  321. }
  322.  
  323. void CEmployeeView::SetAddMode(BOOL bAddMode)
  324. {
  325.     m_bAddMode = bAddMode;
  326.  
  327.     CEdit* pField = (CEdit* )GetDlgItem(IDC_EMP_NO);
  328.  
  329.     pField->SetReadOnly(!m_bAddMode);
  330.  
  331.     if(m_bAddMode)
  332.     {
  333.         pField = (CEdit* )GetDlgItem(IDC_FNAME);
  334.         pField->SetFocus();
  335.     }
  336. }
  337.  
  338. void CEmployeeView::AddRecordCancel()
  339. {
  340.     SetAddMode(FALSE);
  341.  
  342.     m_pSet->CancelUpdate();
  343.     m_pEmplInfoSet->CancelUpdate();
  344.     m_pSet->m_pDatabase->m_pWorkspace->Rollback();
  345.     
  346.     m_pSet->Requery();
  347.     m_pEmplInfoSet->m_EmployeeNumberParam = m_pSet->m_Employee_Number;
  348.     m_pEmplInfoSet->Requery();
  349.  
  350.     UpdateData(FALSE);
  351. }
  352.  
  353.