home *** CD-ROM | disk | FTP | other *** search
- // EmployeeView.cpp : implementation of the CEmployeeView class
- //
-
- #include "stdafx.h"
- #include "Employee.h"
-
- #include "EmployeePaySet.h"
- #include "EmployeePersonalSet.h"
- #include "EmployeeDoc.h"
- #include "EmployeeView.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // CEmployeeView
-
- IMPLEMENT_DYNCREATE(CEmployeeView, CDaoRecordView)
-
- BEGIN_MESSAGE_MAP(CEmployeeView, CDaoRecordView)
- //{{AFX_MSG_MAP(CEmployeeView)
- ON_COMMAND(ID_RECORD_ADD, OnRecordAdd)
- ON_COMMAND(ID_RECORD_DELETE, OnRecordDelete)
- ON_UPDATE_COMMAND_UI(ID_RECORD_DELETE, OnUpdateRecordDelete)
- ON_COMMAND(ID_RECORD_CLEAR, OnRecordClear)
- ON_UPDATE_COMMAND_UI(ID_RECORD_ADD, OnUpdateRecordAdd)
- //}}AFX_MSG_MAP
- // Standard printing commands
- ON_COMMAND(ID_FILE_PRINT, CDaoRecordView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_DIRECT, CDaoRecordView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, CDaoRecordView::OnFilePrintPreview)
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CEmployeeView construction/destruction
-
- CEmployeeView::CEmployeeView()
- : CDaoRecordView(CEmployeeView::IDD)
- {
- //{{AFX_DATA_INIT(CEmployeeView)
- m_pSet = NULL;
- //}}AFX_DATA_INIT
- // TODO: add construction code here
- m_bAddMode = FALSE;
- }
-
- CEmployeeView::~CEmployeeView()
- {
- }
-
- void CEmployeeView::DoDataExchange(CDataExchange* pDX)
- {
- CDaoRecordView::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CEmployeeView)
- DDX_FieldText(pDX, IDC_FNAME, m_pSet->m_First_Name, m_pSet);
- DDX_FieldText(pDX, IDC_LNAME, m_pSet->m_Last_Name, m_pSet);
- DDX_FieldText(pDX, IDC_EMP_NO, m_pSet->m_Employee_Number, m_pSet);
- DDX_FieldText(pDX, IDC_DEPT, m_pSet->m_Department__, m_pSet);
- DDX_FieldText(pDX, IDC_PAY_TYPE, m_pSet->m_Employee_Pay_Type, m_pSet);
- DDX_FieldText(pDX, IDC_HOUR_RATE, m_pSet->m_Hourly_Rate, m_pSet);
- //}}AFX_DATA_MAP
-
- // Make sure this dataset pointer has been setup before trying
- // to access it.
- if(m_pEmplInfoSet != NULL)
- {
- DDX_FieldText(pDX, IDC_BIRTH, m_pEmplInfoSet->m_Birthdate, m_pEmplInfoSet);
- DDX_FieldText(pDX, IDC_MARITAL_STATUS, m_pEmplInfoSet->m_Sex___Marital_Status, m_pEmplInfoSet);
- DDX_FieldText(pDX, IDC_HEIGHT, m_pEmplInfoSet->m_Height, m_pEmplInfoSet);
- DDX_FieldText(pDX, IDC_WEIGHT, m_pEmplInfoSet->m_Weight, m_pEmplInfoSet);
- }
-
- }
-
- BOOL CEmployeeView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
-
- return CDaoRecordView::PreCreateWindow(cs);
- }
-
- void CEmployeeView::OnInitialUpdate()
- {
- m_pSet = GetDocument()->GetEmployeePaySet();
- m_pEmplInfoSet = GetDocument()->GetEmployeeInfoSet();
-
- CDaoRecordView::OnInitialUpdate();
-
- //resize the window
- GetParentFrame()->RecalcLayout();
- ResizeParentToFit(FALSE);
-
- //set the parameter for the info query
- m_pEmplInfoSet->m_EmployeeNumberParam = m_pSet->m_Employee_Number;
-
- //open the Info record set
- try
- {
- m_pEmplInfoSet->Open();
- }
- catch (CDaoException* e)
- {
- AfxMessageBox(e->m_pErrorInfo->m_strDescription);
- e->Delete();
- return;
- }
-
- UpdateData(FALSE);
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CEmployeeView printing
-
- BOOL CEmployeeView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
-
- void CEmployeeView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
-
- void CEmployeeView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CEmployeeView diagnostics
-
- #ifdef _DEBUG
- void CEmployeeView::AssertValid() const
- {
- CDaoRecordView::AssertValid();
- }
-
- void CEmployeeView::Dump(CDumpContext& dc) const
- {
- CDaoRecordView::Dump(dc);
- }
-
- CEmployeeDoc* CEmployeeView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEmployeeDoc)));
- return (CEmployeeDoc*)m_pDocument;
- }
- #endif //_DEBUG
-
- /////////////////////////////////////////////////////////////////////////////
- // CEmployeeView database support
- CDaoRecordset* CEmployeeView::OnGetRecordset()
- {
- return m_pSet;
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CEmployeeView message handlers
- BOOL CEmployeeView::OnMove(UINT nIDMoveCommand)
- {
- if(m_bAddMode)
- {
- AddRecordCancel();
- }
-
- //navigate, and update while you are navigating....
- COleVariant varRecordToReturnTo;
- varRecordToReturnTo = m_pSet->GetBookmark();
- try
- {
- // Begin update transaction
- m_pSet->m_pDatabase->m_pWorkspace->BeginTrans();
-
- // Put the EmployeeInfoSet into Edit mode before
- // calling CDaoRecordSet::OnMove()
- m_pEmplInfoSet->Edit();
-
- // CDaoRecordView::OnMove() will call UpdateData
- CDaoRecordView::OnMove(nIDMoveCommand);
-
- // Finish the update of the EmployeeInfoSet
- m_pEmplInfoSet->Update();
-
- // Commit update transaction
- m_pSet->m_pDatabase->m_pWorkspace->CommitTrans();
- }
- catch (CDaoException* e)
- {
- // Rollback changes
- m_pSet->m_pDatabase->m_pWorkspace->Rollback();
-
- // Make sure PayInfo record set is returned
- // to the proper position
- m_pSet->SetBookmark( varRecordToReturnTo );
-
- AfxMessageBox(e->m_pErrorInfo->m_strDescription);
- e->Delete();
- }
-
- // Update EmployeeNumber parameter and requery
- m_pEmplInfoSet->m_EmployeeNumberParam = m_pSet->m_Employee_Number;
- m_pEmplInfoSet->Requery();
-
- // Show results of Employee Info requery
- UpdateData(FALSE);
-
- return TRUE;
- }
-
- void CEmployeeView::OnRecordAdd()
- {
- UpdateData();
-
- try
- {
- m_pEmplInfoSet->m_Employee_Number = m_pSet->m_Employee_Number;
- m_pSet->Update();
- m_pEmplInfoSet->Update();
-
- //if we didn't except out, commit it to the database
- m_pSet->m_pDatabase->m_pWorkspace->CommitTrans();
- }
- catch (CDaoException* e)
- {
- //something happened, so restore the db
- m_pSet->m_pDatabase->m_pWorkspace->Rollback();
-
- //tell why
- AfxMessageBox(e->m_pErrorInfo->m_strDescription);
- e->Delete();
-
- //restore the add mode
- m_pSet->m_pDatabase->m_pWorkspace->BeginTrans();
- m_pSet->AddNew();
- m_pEmplInfoSet->AddNew();
- return;
- }
-
- //load it all back in, reset current record
- m_pEmplInfoSet->m_EmployeeNumberParam = m_pSet->m_Employee_Number;
- m_pSet->Requery();
- m_pEmplInfoSet->Requery();
-
- //copy back to the form
- UpdateData(FALSE);
-
- SetAddMode(FALSE);
- }
-
- void CEmployeeView::OnRecordDelete()
- {
- try
- {
- //begin the transaction
- m_pSet->m_pDatabase->m_pWorkspace->BeginTrans();
-
- //delete both records
- m_pSet->Delete();
- m_pEmplInfoSet->Delete();
-
- //end the transaction
- m_pSet->m_pDatabase->m_pWorkspace->CommitTrans();
- }
- catch (CDaoException* e)
- {
- //something happened, so restore the db
- m_pSet->m_pDatabase->m_pWorkspace->Rollback();
-
- //tell why
- AfxMessageBox(e->m_pErrorInfo->m_strDescription);
- e->Delete();
-
- //and bail out
- return;
- }
-
- m_pSet->MoveNext();
- m_pSet->Requery();
-
- if(m_pSet->IsEOF() && !m_pSet->IsBOF())
- m_pSet->MoveLast();
-
- if(m_pSet->IsBOF())
- m_pSet->SetFieldNull(NULL);
-
- m_pEmplInfoSet->m_EmployeeNumberParam = m_pSet->m_Employee_Number;
- m_pEmplInfoSet->Requery();
-
- if(m_pEmplInfoSet->IsEOF() && m_pEmplInfoSet->IsBOF())
- m_pEmplInfoSet->SetFieldNull(NULL);
-
- UpdateData(FALSE);
- }
-
- void CEmployeeView::OnUpdateRecordDelete(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(!m_bAddMode);
- }
-
- void CEmployeeView::OnUpdateRecordAdd(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(m_bAddMode);
- }
-
- void CEmployeeView::OnRecordClear()
- {
- //Create a temporary record filled with (database) NULLs
- //No record is added to the database until Update() is called
- //start the transaction
- m_pSet->m_pDatabase->m_pWorkspace->BeginTrans();
- m_pSet->AddNew();
- m_pEmplInfoSet->AddNew();
-
- UpdateData(FALSE);
- SetAddMode(TRUE);
- }
-
- void CEmployeeView::SetAddMode(BOOL bAddMode)
- {
- m_bAddMode = bAddMode;
-
- CEdit* pField = (CEdit* )GetDlgItem(IDC_EMP_NO);
-
- pField->SetReadOnly(!m_bAddMode);
-
- if(m_bAddMode)
- {
- pField = (CEdit* )GetDlgItem(IDC_FNAME);
- pField->SetFocus();
- }
- }
-
- void CEmployeeView::AddRecordCancel()
- {
- SetAddMode(FALSE);
-
- m_pSet->CancelUpdate();
- m_pEmplInfoSet->CancelUpdate();
- m_pSet->m_pDatabase->m_pWorkspace->Rollback();
-
- m_pSet->Requery();
- m_pEmplInfoSet->m_EmployeeNumberParam = m_pSet->m_Employee_Number;
- m_pEmplInfoSet->Requery();
-
- UpdateData(FALSE);
- }
-
-