// KBDBDlg.cpp : implementation file // #include "stdafx.h" #include "kbeditors.h" #include "KBEditorsDoc.h" #include "KBDBDlg.h" #include "general.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CKBDBDlg dialog void CKBDBDlg::SeeDoc(CKBEditorsDoc*Doc) { m_Doc = Doc; } CKBDBDlg::CKBDBDlg(CWnd* pParent /*=NULL*/) : CDialog(CKBDBDlg::IDD, pParent) { //{{AFX_DATA_INIT(CKBDBDlg) //}}AFX_DATA_INIT } void CKBDBDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CKBDBDlg) DDX_Control(pDX, IDC_Fields, m_Fields); DDX_Control(pDX, IDC_LIST2, m_Tables); DDX_Control(pDX, IDC_TREE1, m_CptTree); DDX_Control(pDX, IDC_MSFLEXGRID1, m_FlexGrid); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CKBDBDlg, CDialog) //{{AFX_MSG_MAP(CKBDBDlg) ON_BN_CLICKED(IDC_BUTTON1, OnButton1) ON_LBN_SELCHANGE(IDC_LIST2, OnSelchangeList2) ON_BN_CLICKED(IDC_DelAll, OnDelAll) ON_BN_CLICKED(IDC_CLOSE, OnClose) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CKBDBDlg message handlers void CKBDBDlg::ClearGrid() { CString ss; for(int i = m_FlexGrid.GetRows();i >2;i--){ m_FlexGrid.RemoveItem(i); m_FlexGrid.Refresh(); } ss.Empty(); m_FlexGrid.SetTextMatrix(1,0,(LPCTSTR) ss); m_FlexGrid.SetTextMatrix(1,1,(LPCTSTR) ss); m_FlexGrid.SetTextMatrix(1,2,(LPCTSTR) ss); m_FlexGrid.SetTextMatrix(1,3,(LPCTSTR) ss); } void CKBDBDlg::FillGrid() { POSITION pos = m_Doc->m_CKBDBList.GetStartPosition(); while( pos != NULL ){ CKBDB * ckbdb; CString string; m_Doc->m_CKBDBList.GetNextAssoc( pos, string, ckbdb ); CString strCpt,strProp,strTbl,strFld; ckbdb->GetCptPropTblFld(strCpt,strProp,strTbl,strFld); m_FlexGrid.SetTextMatrix(m_FlexGrid.GetRows()-1,0,(LPCTSTR) strCpt); m_FlexGrid.SetTextMatrix(m_FlexGrid.GetRows()-1,1,(LPCTSTR) strProp); m_FlexGrid.SetTextMatrix(m_FlexGrid.GetRows()-1,2,(LPCTSTR) strTbl); m_FlexGrid.SetTextMatrix(m_FlexGrid.GetRows()-1,3,(LPCTSTR) strFld); m_FlexGrid.SetRows(m_FlexGrid.GetRows()+1); } } BOOL CKBDBDlg::OnInitDialog() { CDialog::OnInitDialog(); CenterWindow(GetDesktopWindow()); DrawCptTree(); if(!m_Doc->DBName.IsEmpty()){ GetDlgItem(IDC_BUTTON1)->EnableWindow(FALSE); GetDlgItem(IDC_DBName)->EnableWindow(FALSE); SetDlgItemText(IDC_DBName,m_Doc->DBPath+m_Doc->DBName); DB.OpenDBCatalog(m_Doc->DBPath+m_Doc->DBName); CStringArray tt; DB.GetTables(tt); for(int i=0;iEnableWindow(TRUE); GetDlgItem(IDC_DBName)->EnableWindow(TRUE); } ClearGrid(); FillGrid(); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } BOOL CKBDBDlg::CptHasDBProp(CConcept* Cpt) { CProperty* Prop; POSITION pos = Cpt->m_PropList.GetStartPosition(); while (pos !=NULL){ CString strProp; Cpt->m_PropList.GetNextAssoc(pos,strProp,Prop); if (m_Doc->IsSourceValue("Database",Cpt->NameL,strProp)) return TRUE; } return FALSE; } void CKBDBDlg::DrawCptTree() { m_CptTree.DeleteAllItems(); CString str ; POSITION pos = m_Doc->m_CptList.GetStartPosition(); while (pos !=NULL){ m_Doc->m_CptList.GetNextAssoc(pos,str,m_Doc->Cpt); if (m_Doc->Cpt->m_PropList.GetCount() > 0) { if(CptHasDBProp(m_Doc->Cpt)){ HTREEITEM TreeItem = m_CptTree.InsertItem(str,TVI_ROOT,TVI_SORT); POSITION pos1 =m_Doc->Cpt->m_PropList.GetStartPosition(); while (pos1 !=NULL){ CString str1; m_Doc->Cpt->m_PropList.GetNextAssoc(pos1,str1,m_Doc->Prop); if (m_Doc->IsSourceValue("Database",str,str1)) HTREEITEM TreeItem1 = m_CptTree.InsertItem(str1,TreeItem); } } } } } void CKBDBDlg::OnButton1() { CFileDialog cfdlg(TRUE,"*.mdb","*.mdb",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"MDB Files (*.mdb)"); cfdlg.DoModal(); FilePath = cfdlg.GetPathName(); if(FilePath.IsEmpty()){ AfxMessageBox("Select Database Name First"); } FileName = cfdlg.GetFileName(); strPath =FilePath; strPath.Delete(FilePath.GetLength()-FileName.GetLength(),FileName.GetLength()); //AfxMessageBox(strPath); SetDlgItemText(IDC_DBName,FilePath); DB.OpenDBCatalog(FilePath); CStringArray tt; DB.GetTables(tt); for(int i=0;iDBName = FileName; m_Doc->DBPath = strPath; m_Doc->OnSaveDocument(m_Doc->AppName); GetDlgItem(IDC_BUTTON1)->EnableWindow(FALSE); GetDlgItem(IDC_DBName)->EnableWindow(FALSE); } void CKBDBDlg::OnSelchangeList2() { m_Fields.ResetContent(); int Sel = m_Tables.GetCurSel(); if (Sel < 0 ) return; CString strTbl; m_Tables.GetText(Sel,strTbl); CStringArray ff; DB.GetFields(strTbl,ff); for(int j=0;jGetType(strCpt,strProp,&m_Doc->m_CptList); if(t1=="nominal") t1="string"; delete R; CString t2 = DB.GetFieldType(strField); if( t1 != t2){ AfxMessageBox("The Type of "+ strProp + " is " + t1 + " \n And the type of " + strField + " is " + t2 +" Does not matched"); return; } //Fill Table keys and type CStringArray tblKeys,tblType; DB.GetTableKeys(strTable,tblKeys); for(int i=0;im_CTblKeysList.Lookup(strTable,tblKey)){ //2- add(m_CTblKeysList, table_name, table_keys, keysType); tblKey = new CTblKeys(tblKeys,tblType); m_Doc->m_CTblKeysList.SetAt(strTable,tblKey); } //3- add(m_CDBKeysList, table_keys) if it is not exist; BOOL flg = FALSE; for(i=0;im_CBDKeysList.GetSize() && !flg;j++){ CString s1,s2; s1 = tblKeys[i]; s2 = m_Doc->m_CBDKeysList[j]; if( s1 == s2 ) flg = TRUE; } if(! flg) m_Doc->m_CBDKeysList.Add(tblKeys[i]); } //4- create new CKBDB node(cpt,prop,table,field) and add it to m_CKBDBList; CKBDB* ckbdb; if(!m_Doc->m_CKBDBList.Lookup(strCpt+strProp+strTable+strField, ckbdb)){ ckbdb = new CKBDB(strCpt,strProp,strTable,strField); m_Doc->m_CKBDBList.SetAt(strCpt+strProp+strTable+strField, ckbdb); } m_Doc->OnSaveDocument(m_Doc->AppName); ClearGrid(); FillGrid(); } void CKBDBDlg::OnCancel() { int R = m_FlexGrid.GetRow(); CString strCpt = m_FlexGrid.GetTextMatrix(R,0); CString strProp = m_FlexGrid.GetTextMatrix(R,1); CString strTable = m_FlexGrid.GetTextMatrix(R,2); CString strField = m_FlexGrid.GetTextMatrix(R,2); if(strCpt.IsEmpty()) return; if(strProp.IsEmpty()) return; if(strTable.IsEmpty()) return; if(strField.IsEmpty()) return; m_FlexGrid.RemoveItem(R); m_FlexGrid.Refresh(); CKBDB* ckbdb; if(m_Doc->m_CKBDBList.Lookup(strCpt+strProp+strTable+strField, ckbdb)){ m_Doc->m_CKBDBList.RemoveKey(strCpt+strProp+strTable+strField); delete ckbdb; } CString str; POSITION pos = m_Doc->m_CKBDBList.GetStartPosition(); BOOL found=FALSE; while (pos !=NULL && !found){ CString c,p,t,f; m_Doc->m_CKBDBList.GetNextAssoc(pos,str,ckbdb); ckbdb->GetCptPropTblFld(c,p,t,f); if(t==strTable) found=TRUE; } if(found) return; /*Removing flat keys*/ CStringArray tblKeys; DB.GetTableKeys(strTable,tblKeys); for(int i=0;im_CBDKeysList.GetSize();j++){ if(tblKeys[i] == m_Doc->m_CBDKeysList[j]){ m_Doc->m_CBDKeysList.RemoveAt(j); break; } } } if(m_Doc->m_CBDKeysList.GetSize()==0)m_Doc->m_CBDKeysList.RemoveAll(); CTblKeys* tblKey; if(m_Doc->m_CTblKeysList.Lookup(strTable,tblKey)){ tblKey->RemoveAllKeys(); m_Doc->m_CTblKeysList.RemoveKey(strTable); delete tblKey; } if(m_Doc->m_CTblKeysList.GetCount()==0)m_Doc->m_CTblKeysList.RemoveAll(); m_Doc->OnSaveDocument(m_Doc->AppName); } void CKBDBDlg::OnDelAll() { CKBDB* ckbdb; CString str; POSITION pos = m_Doc->m_CKBDBList.GetStartPosition(); while (pos !=NULL ){ m_Doc->m_CKBDBList.GetNextAssoc(pos,str,ckbdb); m_Doc->m_CKBDBList.RemoveKey(str); delete ckbdb; } m_Doc->m_CKBDBList.RemoveAll(); m_Doc->m_CBDKeysList.RemoveAll(); CTblKeys* tblKey; if(m_Doc->m_CTblKeysList.Lookup(str,tblKey)){ tblKey->RemoveAllKeys(); m_Doc->m_CTblKeysList.RemoveKey(str); delete tblKey; } m_Doc->m_CTblKeysList.RemoveAll(); ClearGrid(); m_Doc->DBName.Empty(); m_Doc->DBPath.Empty(); GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE); GetDlgItem(IDC_DBName)->EnableWindow(TRUE); FilePath.Empty();; FileName.Empty();; strPath.Empty();; m_Tables.ResetContent(); m_Fields.ResetContent(); SetDlgItemText(IDC_DBName,""); m_Doc->OnSaveDocument(m_Doc->AppName); } void CKBDBDlg::OnClose() { CDialog::EndDialog(0); }