GME  13
MgaContext.cpp
Go to the documentation of this file.
00001 // MgaContext.cpp: implementation of the CMgaContext class.
00002 //
00004 
00005 #include "stdafx.h"
00006 #include "gmeactivebrowser.h"
00007 #include "MgaContext.h"
00008 
00009 #ifdef _DEBUG
00010 #undef THIS_FILE
00011 static char THIS_FILE[]=__FILE__;
00012 #define new DEBUG_NEW
00013 #endif
00014 
00016 // Construction/Destruction
00018 
00019 CMgaContext::CMgaContext()
00020 {
00021         // transactions will be enabled only after opening a project
00022         m_nPendingTransactions=0; 
00023         m_bEventTransactionMode=FALSE;
00024 }
00025 
00026 CMgaContext::~CMgaContext()
00027 {
00028 
00029 }
00030 
00031 bool CMgaContext::IsInTransaction()
00032 {
00033         if ( m_bEventTransactionMode || (m_nPendingTransactions > 0) ) {
00034                 return true;
00035         }
00036         else {
00037                 return false;
00038         }
00039 }
00040 
00041 bool CMgaContext::BeginTransaction(bool bIsReadOnly)
00042 {
00043         return BeginTransaction(bIsReadOnly ? TRANSACTION_READ_ONLY : TRANSACTION_GENERAL);
00044 }
00045 
00046 bool CMgaContext::BeginTransaction(transactiontype_enum type)
00047 {
00048         // In the event handlers we are already in transaction
00049         if(m_bEventTransactionMode)
00050                 return true;
00051 
00052         
00053         if(m_nPendingTransactions==0) // Not in transactions
00054         {               
00055                 try
00056                 {
00057                         COMTHROW(m_ccpProject->BeginTransaction(m_ccpTerritory, type));
00058                 }
00059                 catch (hresult_exception)
00060                 {
00061                         ASSERT(false);
00062                         return false;
00063                 }               
00064         }
00065         m_nPendingTransactions++;               
00066         return true;
00067 
00068 }
00069 
00070 bool CMgaContext::CommitTransaction()
00071 {
00072         // In the event handlers we are already in transaction
00073         if(m_bEventTransactionMode)return true;
00074 
00075         if(m_nPendingTransactions==0) // We are not in transaction. What to commit?
00076         {
00077                 return false;
00078         }
00079         
00080         if(m_nPendingTransactions==1)
00081         {                               
00082                 COMTHROW(m_ccpProject->CommitTransaction());                            
00083         }
00084         m_nPendingTransactions--;
00085         return true;
00086 
00087 }
00088 
00089 bool CMgaContext::AbortTransaction()
00090 {
00091         if(m_bEventTransactionMode)
00092                 return true;
00093 
00094         if(m_nPendingTransactions==0)
00095                 return false;
00096 
00097         m_ccpProject->AbortTransaction();
00098         m_nPendingTransactions=0;
00099         m_bEventTransactionMode=false;
00100 
00101         return true;
00102 }
00103 
00104 void CMgaContext::SetTransactionState(int nValue)
00105 {
00106         m_nPendingTransactions=nValue;
00107 }
00108 
00109 void CMgaContext::CloseContext()
00110 {
00111         ASSERT(("Not commited transaction found. Check BeginTransaction - Commit Transaction pairs.",m_nPendingTransactions==0));
00112         // Disabling Transactions       
00113         SetTransactionState(0);
00114         
00115         // Deleting Territory
00116         if(m_ccpTerritory.p!=NULL)
00117         {
00118                 COMTHROW(m_ccpTerritory->Destroy() );
00119         }
00120         m_ccpTerritory.Release();
00121         
00122         m_ccpProject.Release();
00123 
00124 }
00125 
00126 void CMgaContext::CreateContext(IMgaEventSink &rEventSink,LPUNKNOWN pMgaProject)
00127 {
00128         // Creating Project
00129         CComQIPtr<IMgaProject> _project( pMgaProject);
00130         m_ccpProject = _project;
00131         //m_ccpProject.Attach((IMgaProject *) pMgaProject);
00132         // previously was used Attach but this does not call AddRef
00133         // on m_ccpProject, although in CloseContext Release is used
00134         // this will fix the JIRA-
00135         
00136         // Creating Territory
00137         COMTHROW( m_ccpProject->CreateTerritory(&rEventSink,&m_ccpTerritory,NULL) );
00138 
00139         SetTransactionState(0);
00140 }
00141 
00142 CComPtr<IMgaComponentEx> CMgaContext::FindConstraintManager()
00143 {
00144         CComPtr<IMgaComponentEx> constrMgr;
00145         // Finding constraint manager among the addons
00146         CComPtr<IMgaComponents> comps;
00147         COMTHROW( m_ccpProject->get_AddOnComponents(&comps));
00148         MGACOLL_ITERATE(IMgaComponent, comps) 
00149         {
00150                 CComBSTR name;
00151                 COMTHROW(MGACOLL_ITER->get_ComponentName(&name));
00152                 if(name == L"ConstraintManager") 
00153                 {
00154                         constrMgr = CComQIPtr<IMgaComponentEx>(MGACOLL_ITER); 
00155                         break;
00156                 }
00157         } MGACOLL_ITERATE_END;
00158 
00159         return constrMgr;
00160 }
00161 
00162 void CMgaContext::SetEventTransactionMode(bool bOnOff)
00163 {
00164         m_bEventTransactionMode=bOnOff;
00165 }