GME  13
MgaResolver.cpp
Go to the documentation of this file.
00001 // MgaResolver.cpp : Implementation of CMgaResolver
00002 #include "stdafx.h"
00003 
00004 #include "CommonCollection.h"
00005 #include "MgaResolver.h"
00006 #include "DialogList.h"
00007 #include "AlterNmspDlg.h"
00008 #include "CommonError.h"
00009 
00011 
00012 CString helper_ObjTypeStr(objtype_enum objtype) {
00013 
00014         switch(objtype) {
00015 
00016                 case OBJTYPE_MODEL : {
00017                         return CString("model");
00018                 }
00019                 case OBJTYPE_ATOM : {
00020                         return CString("atom");
00021                 }
00022                 case OBJTYPE_REFERENCE : {
00023                         return CString("reference");
00024                 }
00025                 case OBJTYPE_CONNECTION : {
00026                         return CString("connection");
00027                 }
00028                 case OBJTYPE_SET : {
00029                         return CString("set");
00030                 }
00031                 case OBJTYPE_FOLDER : {
00032                         return CString("folder");
00033                 }
00034                 case OBJTYPE_ASPECT : {
00035                         return CString("aspect");
00036                 }
00037                 case OBJTYPE_ROLE : {
00038                         return CString("role");
00039                 }
00040                 case OBJTYPE_ATTRIBUTE : {
00041                         return CString("attribute");
00042                 }
00043                 default : {
00044                         return CString("unknown object type");
00045                 }
00046         }
00047 } // helper_ObjTypeStr()
00048 
00050 // CMgaResolver
00051 CComBSTR CMgaResolver::prefixIt(CComBSTR pIn)
00052 {
00053         CComBSTR rval;
00054         COMTHROW(rval.AppendBSTR( m_prefix));
00055         COMTHROW(rval.AppendBSTR( pIn));
00056 
00057         return rval;
00058 }
00059 
00060 CComBSTR CMgaResolver::truncIt(CComBSTR pIn)
00061 {
00062         CComBSTR rval;
00063 
00064         CString crole( pIn);
00065         CString trunc( m_trunc);
00066 
00067         if( crole.Left( trunc.GetLength()) == trunc)
00068         {
00069                 COMTHROW(rval.Append( crole.Right( crole.GetLength() - trunc.GetLength())));
00070         }
00071 
00072         return rval;
00073 }
00074 
00075 bool CMgaResolver::isAlterationSet() const
00076 {
00077         return m_alter_option == CH_PREFIX 
00078                 || m_alter_option == CH_TRUNCATE
00079                 || m_alter_option == CH_MIGRATE;
00080 }
00081 
00082 void CMgaResolver::setAlteration( alter_mode_enum v)
00083 {
00084         if( v >= CH_NONE && v <= CH_MIGRATE)
00085                 m_alter_option = v;
00086         else
00087                 m_alter_option = CH_NONE;
00088 }
00089 
00090 STDMETHODIMP CMgaResolver::Clear() {
00091 
00092         this->clear_mappings();
00093 
00094         return S_OK;
00095 }
00096 
00097 void CMgaResolver::clear_mappings() {
00098 
00099         while (! map_FolderByStr.IsEmpty() )
00100                 delete map_FolderByStr.RemoveHead();
00101 
00102         while (! map_KindByStr.IsEmpty() )
00103                 delete map_KindByStr.RemoveHead();
00104 
00105         while (! map_RoleByStr.IsEmpty() )
00106                 delete map_RoleByStr.RemoveHead();
00107 
00108         while (! map_AttrByStr.IsEmpty() )
00109                 delete map_AttrByStr.RemoveHead();
00110 
00111         while (! map_RoleByPathStr.IsEmpty() )
00112                 delete map_RoleByPathStr.RemoveHead();
00113 
00114         mb_use_sticky = false;
00115 
00116         // namespace related
00117         m_alter_option = CH_NONE;
00118         m_prefix = "";
00119         m_trunc  = "";
00120         mb_name_alteration_sticky = false;
00121 }
00122 
00123 STDMETHODIMP CMgaResolver::put_IsInteractive(VARIANT_BOOL p) {
00124         mb_is_interactive = (p != VARIANT_FALSE);
00125         return S_OK;
00126 }
00127 
00128 STDMETHODIMP CMgaResolver::get_IsInteractive(VARIANT_BOOL *p) {
00129         *p = (this->mb_is_interactive ? VARIANT_TRUE : VARIANT_FALSE );
00130         return S_OK;
00131 }
00132 
00133 STDMETHODIMP CMgaResolver::put_IsStickyEnabled(VARIANT_BOOL p) {
00134         mb_use_sticky = (p != VARIANT_FALSE);
00135         return S_OK;
00136 }
00137 
00138 STDMETHODIMP CMgaResolver::get_IsStickyEnabled(VARIANT_BOOL *p) {
00139         *p = (this->mb_use_sticky ? VARIANT_TRUE : VARIANT_FALSE );
00140         return S_OK;
00141 }
00142 
00143 STDMETHODIMP CMgaResolver::getUserOptions()
00144 {
00145         AFX_MANAGE_STATE(AfxGetStaticModuleState());
00146 
00147         if( !this->mb_is_interactive) return S_OK;
00148 
00149         COMTRY {
00150                 AlterNmspDlg cdl;
00151                 if( cdl.DoModal() == IDOK)
00152                 {
00153                         this->m_prefix = cdl.m_strPrefix;
00154                         this->m_trunc  = cdl.m_strTrunc;
00155                         this->setAlteration( (alter_mode_enum) cdl.m_vRadAlter);
00156                         this->mb_name_alteration_sticky = cdl.m_bAlterSticky == TRUE;
00157                 }
00158         } COMCATCH(;)
00159 }
00160 
00161 
00162 STDMETHODIMP CMgaResolver::get_FolderByStr(IMgaFolder *parent, 
00163                                                                                    BSTR kind, 
00164                                                                                    IMgaMetaFolder **p) {
00165 
00166         AFX_MANAGE_STATE(AfxGetStaticModuleState());
00167 
00168         CHECK_OUT(p);
00169 
00170         ASSERT(parent!=NULL);
00171 
00172         HRESULT hr;
00173 
00174         COMTRY {
00175 
00176                 CComPtr<IMgaMetaFolder> parent_mfolder;
00177                 CComPtr<IMgaMetaFolder> mfolder;
00178                 CComPtr<IMgaMetaFolder> mfolder_ret;
00179                 CComPtr<IMgaMetaFolders> mfolders;
00180                 long mfs_count = -1;
00181 
00182                 CString sz_debug(kind);
00183 
00184                 COMTHROW( parent->get_MetaFolder(&parent_mfolder) );
00185                 ASSERT(parent_mfolder != NULL);
00186 
00187                 COMTHROW( 
00188                         (hr = parent_mfolder->get_LegalChildFolderByName(kind, &mfolder)) == E_NOTFOUND ?
00189                         S_OK : hr
00190                 );
00191 
00192                 // if not found and namespace settings are available
00193                 if (mfolder.p == NULL && isAlterationSet())
00194                 {
00195                         // (exclusive or: either truncate or prefix or migrate)
00196                         if( m_alter_option == CH_PREFIX)
00197                         {
00198                                 CComBSTR kind2 = prefixIt( kind);
00199                                 // try with prefixed name
00200                                 COMTHROW( 
00201                                         (hr = parent_mfolder->get_LegalChildFolderByName(kind2, &mfolder)) == E_NOTFOUND ?
00202                                         S_OK : hr
00203                                 );
00204                         }
00205                         else if( m_alter_option == CH_TRUNCATE)
00206                         {
00207                                 CComBSTR kind2 = truncIt( kind);
00208                                 // try with truncated name
00209                                 COMTHROW( 
00210                                         (hr = parent_mfolder->get_LegalChildFolderByName(kind2, &mfolder)) == E_NOTFOUND ?
00211                                         S_OK : hr
00212                                 );
00213                         }
00214                         else if( m_alter_option == CH_MIGRATE)
00215                         {
00216                                 CComBSTR kind1 = truncIt( kind);
00217                                 CComBSTR kind2 = prefixIt( kind1);
00218                                 // try with migrated name from one namespace to another
00219                                 COMTHROW( 
00220                                         (hr = parent_mfolder->get_LegalChildFolderByName(kind2, &mfolder)) == E_NOTFOUND ?
00221                                         S_OK : hr
00222                                 );
00223                         }
00224                 }
00225 
00226                 if (mfolder.p != NULL) {
00227                         COMTHROW( mfolder.CopyTo(p) );
00228                         return S_OK;
00229                 }
00230 
00231                 // no explicit match: now check mapping
00232 
00233                 if (this->map_get_FolderByStr(parent_mfolder.p, kind, p)) {
00234                         return S_OK;
00235                 }
00236 
00237                 // no match in mapping: allow for selection of possible folders
00238 
00239                 COMTHROW( parent_mfolder->get_LegalChildFolders(&mfolders) );
00240 
00241                 if (mfolders.p != NULL) {
00242 
00243                         COMTHROW( mfolders->get_Count(&mfs_count) );
00244 
00245                         if (mfs_count > 0) {
00246 
00247                                 // Get the first defined folder, but do not automatically resolve 
00248                                 // root folder (otherwise, library import does not work)
00249                                 if (mfs_count == 1 && CString(kind) != "RootFolder") {
00250 
00251                                         COMTHROW( mfolders->get_Item(1, p) );
00252 
00253                                         //    ' update the mapping: (parent.kind, kindname)->metafolder
00254 
00255                                         this->map_put_FolderByStr(parent_mfolder.p, kind, *p);
00256 
00257                                         return S_OK;
00258                                 }
00259 
00260                                 // no unique match in mapping and no obvious pick: 
00261                                 //              since not interactive, exit
00262 
00263                                 if (! this->mb_is_interactive) {
00264                                         SetErrorInfo(CString(L"Could not resolve folder for kind '") + kind + "'");
00265                                         return RESOLV_ERR_get_FolderByStr;
00266                                 }
00267 
00268                                 // else create a dialog to pick from
00269 
00270                                 CComPtr<IMgaMetaFolder> immf_ix;
00271                                 CDialogList cdl("Resolve Folder Kind", CDialogList::CHKTEXT_ONETIMER);
00272 
00273                                 MGACOLL_ITERATE(IMgaMetaFolder, mfolders) {
00274 
00275                                         immf_ix = MGACOLL_ITER;
00276                                         ASSERT(immf_ix != NULL);
00277 
00278                                         CComBSTR name;
00279 
00280                                         COMTHROW( immf_ix->get_Name(&name) );
00281 
00282                                         cdl.m_sz_prelist.AddTail(CString(name)); 
00283 
00284                                 } // eo for()
00285                                 MGACOLL_ITERATE_END;
00286 
00287                                 if ((cdl.DoModal() != IDOK) || (cdl.mn_selection_index == LB_ERR)) {
00288                                         SetErrorInfo(L"No item chosen from dialog");
00289                                         return RESOLV_ERR_get_FolderByStr;
00290                                 }
00291 
00292                                 // else, find item from dialog, copy pointer, and update map
00293 
00294                                 // copy pointer
00295                                 COMTHROW( mfolders->get_Item(cdl.mn_selection_index+1,p) );
00296                                 ASSERT(*p != NULL);
00297 
00298                                 // update map
00299 
00300                                 if (cdl.mb_check_once == TRUE)
00301                                         this->map_put_FolderByStr(parent_mfolder.p, kind, *p);
00302 
00303                                 return S_OK;
00304 
00305                         }
00306                 }
00307 
00308 //              #pragma bookmark (note: insert helpful message box here get_FolderByStr[] )
00309                 CComBSTR parfolder_name;
00310                 COMTHROW( parent->get_Name(&parfolder_name) );
00311 
00312                 CComBSTR parmfolder_name;
00313                 COMTHROW( parent_mfolder->get_Name(&parmfolder_name) );
00314 
00315                 CString sz_format;
00316                 sz_format.Format(       _T("Paradigm violation: Cannot create folder.\n")
00317                                                         _T("Parent name: %s\n")
00318                                                         _T("Parent type: %s\n")
00319                                                         _T("Child type: %s"),
00320                                                         (BSTR) parfolder_name, (BSTR) parmfolder_name,
00321                                                         kind
00322                 );
00323 
00324                 CComPtr<IMgaProject> mgaProject;
00325                 COMTHROW(parent->get_Project(&mgaProject));
00326                 SetErrorInfo(sz_format);
00327 
00328                 return RESOLV_ERR_get_FolderByStr;
00329         
00330         }
00331 
00332         COMCATCH(;)
00333         
00334 
00335 }
00336 
00337 
00338 STDMETHODIMP CMgaResolver::get_KindByStr(IMgaFolder *parent, BSTR kind, 
00339                                                                                  objtype_enum objtype, 
00340                                                                                  IMgaMetaFCO **p) {
00341 
00342         AFX_MANAGE_STATE(AfxGetStaticModuleState());
00343 
00344 #ifdef DEBUG
00345         CComBSTR sz_debug_kind(kind);
00346 #endif
00347 
00348         CHECK_OUT(p);
00349         ASSERT(parent != NULL);
00350 
00351         HRESULT hr;
00352 
00353         COMTRY {
00354 
00355                 CComPtr<IMgaMetaFCO> mfco;
00356                 objtype_enum mfco_objtype;
00357 
00358                 CComPtr<IMgaMetaFolder> parent_mf;
00359 
00360                 COMTHROW( parent->get_MetaFolder(&parent_mf) );
00361                 ASSERT(parent_mf!=NULL);
00362 
00363                 CString sentence;
00364                 if(CComBSTR(kind).Length()) {
00365                         sentence = _T("Specified metaobject '") + CString(kind) + _T("'");
00366                         COMTHROW( 
00367                                 ((hr = parent_mf->get_LegalRootObjectByName(kind, &mfco)) == E_NOTFOUND) ?
00368                                 S_OK : hr
00369                         );
00370 
00371                         // if not found and namespace settings are available
00372                         if( hr == E_NOTFOUND && isAlterationSet())
00373                         {
00374                                 // exclusive or
00375                                 if( m_alter_option == CH_PREFIX)
00376                                 {
00377                                         CComBSTR kind2 = prefixIt( kind);
00378                                         // try with prefixed name
00379                                         COMTHROW( 
00380                                                 ((hr = parent_mf->get_LegalRootObjectByName(kind2, &mfco)) == E_NOTFOUND) ?
00381                                                 S_OK : hr
00382                                         );
00383                                 }
00384                                 else if( m_alter_option == CH_TRUNCATE)
00385                                 {
00386                                         CComBSTR kind2 = truncIt( kind);
00387                                         // try with truncated name
00388                                         if( kind2.Length() > 0)
00389                                                 COMTHROW( 
00390                                                         ((hr = parent_mf->get_LegalRootObjectByName(kind2, &mfco)) == E_NOTFOUND) ?
00391                                                         S_OK : hr
00392                                                 );
00393                                 }
00394                                 else if( m_alter_option == CH_MIGRATE)
00395                                 {
00396                                         CComBSTR kind1 = truncIt( kind);
00397                                         CComBSTR kind2 = prefixIt( kind1);
00398                                         // try with migrated name from one namespace to another
00399                                         if( kind2.Length() > 0) 
00400                                                 COMTHROW( 
00401                                                         ((hr = parent_mf->get_LegalRootObjectByName(kind2, &mfco)) == E_NOTFOUND) ?
00402                                                         S_OK : hr
00403                                                 );
00404                                 }
00405                         }
00406 
00407                         if (mfco != NULL) {
00408 
00409                                 COMTHROW( mfco->get_ObjType(&mfco_objtype) );
00410 
00411                                 if ((objtype == OBJTYPE_NULL) || (mfco_objtype == objtype)) {
00412                                         COMTHROW( mfco.CopyTo(p) );
00413                                         // insert mapping
00414 
00415                                         this->map_put_KindByStr(parent_mf.p, kind, objtype, *p);
00416 
00417                                         return S_OK;
00418                                 }
00419 
00420                                 // match on name object occurs, but incompatible user spec'd objtype
00421 
00422 
00423                                 sentence += _T(" is of invalid object type. ");
00424                         }
00425 
00426                         // either a fail on name match && proper objtype,
00427                         //  or no name match (so look up on objtype)
00428 
00429                         // check mapping for non-match
00430 
00431                         else {
00432 
00433                                 sentence += _T(" is not found. ");
00434 
00435                         }
00436                 }
00437                 // check mapping
00438                 if (this->map_get_KindByStr(parent_mf.p, kind, objtype, p)) {
00439                         return S_OK;
00440                 }
00441                 //  enumerate list of children with proper objtypes
00442 
00443                 // perhaps a call to: this->get_KindByObjtype()
00444 
00445                 CComPtr<IMgaMetaFCOs> _immfcos;
00446 
00447                 COMTHROW( parent_mf->get_LegalRootObjects(&_immfcos) );
00448                 ASSERT(_immfcos!=NULL);
00449 
00450                 long _fs_ct = 0;
00451 
00452                 COMTHROW( _immfcos->get_Count(&_fs_ct) );
00453 
00454                 if (_fs_ct == 0) {
00455 //                      #pragma bookmark (note: insert helpful message box here get_KindByStr[1] )
00456                         CComBSTR parent_name;
00457                         COMTHROW( parent->get_Name(&parent_name) );
00458 
00459                         CComBSTR mparent_name;
00460                         COMTHROW( parent_mf->get_Name(&mparent_name) );
00461 
00462                         CString sz_format;
00463 
00464                         sz_format.Format(       _T("%sParent folder %s\n")
00465                                                                 _T("of type %s\n")
00466                                                                 _T("cannot have any children."),
00467                                                                 sentence,
00468                                                                 (BSTR) parent_name,
00469                                                                 (BSTR) mparent_name
00470                         );
00471                         SetErrorInfo(sz_format);
00472 
00473                         return RESOLV_ERR_get_KindByStr;
00474                 }
00475 
00476                 
00477                 CDialogList cdl(sentence + _T("Resolve Object Kind of ObjType ")+helper_ObjTypeStr(objtype),
00478                         CDialogList::CHKTEXT_ONETIMER);
00479                 CComPtr<IMgaMetaFCO> fco_ix;
00480                 CList<int,int> valid_obj_map;
00481                 objtype_enum objt_ix;
00482 
00483                 for (long z = 1; z <= _fs_ct; z++) {
00484 
00485                         fco_ix = NULL;
00486                         COMTHROW( _immfcos->get_Item(z, &fco_ix) );
00487 
00488                         ASSERT(fco_ix != NULL);
00489 
00490                         COMTHROW( fco_ix->get_ObjType(&objt_ix) );
00491 
00492                         if ((objt_ix == objtype) || (objtype == OBJTYPE_NULL)) {
00493 
00494                                 CComBSTR name;
00495 
00496                                 COMTHROW( fco_ix->get_Name(&name) );
00497 
00498                                 cdl.m_sz_prelist.AddTail(CString(name)); 
00499                                 valid_obj_map.AddTail(z);
00500                         }
00501                 }
00502 
00503                 if (valid_obj_map.GetCount() == 0) {
00504 //                      #pragma bookmark (note: insert helpful message box here get_KindByStr[1] )
00505                         CComBSTR parent_name;
00506                         COMTHROW( parent->get_Name(&parent_name) );
00507 
00508                         CComBSTR mparent_name;
00509                         COMTHROW( parent_mf->get_Name(&mparent_name) );
00510 
00511                         CString sz_format;
00512                         CString sz_format_tmp;
00513 
00514                         sz_format_tmp.Format(   _T("Paradigm Violation: parent folder %s ")
00515                                                                         _T("of type %s ")
00516                                                                         _T("cannot have any children"),
00517                                                                         (BSTR) parent_name,
00518                                                                         (BSTR) mparent_name
00519                         );
00520                         sz_format += sz_format_tmp;
00521 
00522                         if (objtype != OBJTYPE_NULL) {
00523                                 sz_format_tmp.Format(   _T(" of object type %s\n"),
00524                                                                                 helper_ObjTypeStr(objtype)
00525                                                                                 );
00526                                 sz_format += sz_format_tmp;
00527                         }
00528 
00529                         sz_format += sentence;
00530 
00531                         SetErrorInfo(sz_format);
00532 
00533                         return RESOLV_ERR_get_KindByStr;
00534                 }
00535 
00536                 if (valid_obj_map.GetCount() == 1) {
00537                         // only one object of that type can exist-- grab it and update map
00538                         int index = valid_obj_map.GetHead();
00539 
00540                         fco_ix = NULL;
00541                         COMTHROW( _immfcos->get_Item(index, &fco_ix) );
00542                         ASSERT(fco_ix != NULL);
00543 
00544                         COMTHROW( fco_ix.CopyTo(p) );
00545 
00546                         if(this->mb_is_interactive && sentence.GetLength()) {
00547                                 CComBSTR newname;
00548                                 COMTHROW(fco_ix->get_Name(&newname));
00549                                 sentence += _T("\nUsing the only valid choice '") + CString(newname) + _T("' instead.");
00550 
00551                                 CComPtr<IMgaProject> mgaProject;
00552                                 COMTHROW(parent->get_Project(&mgaProject));
00553                                 CComPtr<IGMEOLEApp> pGME = get_GME(mgaProject);
00554                                 if (pGME) {
00555                                         COMTHROW(pGME->ConsoleMessage(CComBSTR(sentence), MSG_ERROR));
00556                                 }
00557                                 else {
00558                                         AfxMessageBox(sentence, MB_ICONSTOP | MB_OK);
00559                                 }
00560 
00561                         }
00562                         if (!this->mb_is_interactive)
00563                         {
00564                                 SetErrorInfo(CString(L"Could not resolve kind '") + kind + L"'");
00565                                 return RESOLV_ERR_get_KindByStr;
00566                         }
00567                         // update mapping
00568                         this->map_put_KindByStr(parent_mf.p, kind, objtype, *p);
00569 
00570                         return S_OK;
00571                 }
00572 
00573                 // > 1 valid objects: show enumeration if possible
00574 
00575                 // if not interactive, we have to quit-- no more choices!
00576 
00577                 if (!this->mb_is_interactive) {
00578                         SetErrorInfo(CString(L"Could not resolve kind '") + kind + L"'");
00579                         return RESOLV_ERR_get_KindByStr;
00580                 }
00581 
00582                 if ((cdl.DoModal() != IDOK) || (cdl.mn_selection_index == LB_ERR)) {
00583 //                      #pragma bookmark (note: insert helpful message box here get_KindByStr[1] )
00584                         SetErrorInfo(_T("No item chosen from dialog"));
00585                         return RESOLV_ERR_get_KindByStr;
00586                 }
00587 
00588                 int coll_ix = valid_obj_map.GetAt(valid_obj_map.FindIndex(
00589                         cdl.mn_selection_index));
00590 
00591 
00592                 fco_ix = NULL;
00593                 COMTHROW( _immfcos->get_Item(coll_ix, &fco_ix) );
00594                 ASSERT(fco_ix != NULL);
00595 
00596                 COMTHROW( fco_ix.CopyTo(p) );
00597 
00598                 // update mapping
00599                 if (cdl.mb_check_once == TRUE)
00600                         this->map_put_KindByStr(parent_mf.p, kind, objtype, *p);
00601 
00602                 return S_OK;
00603 
00604         }
00605 
00606 
00607         COMCATCH(;)
00608 
00609 }
00610 
00611 
00612 STDMETHODIMP CMgaResolver::get_RoleByStr(IMgaModel *parent, BSTR kind, 
00613                                                                                  objtype_enum objtype,
00614                                                                                  BSTR role, BSTR aspect, 
00615                                                                                  IMgaMetaRole **p) {
00616 
00617         AFX_MANAGE_STATE(AfxGetStaticModuleState());
00618 
00619         HRESULT hr;
00620 
00621         CHECK_OUT(p);
00622 
00623         CComPtr<IMgaMetaFCO> parent_meta_fco;
00624         CComPtr<IMgaMetaModel> parent_meta;
00625         CComPtr<IMgaMetaRole> targ_role;
00626         CComPtr<IMgaMetaRoles> targ_roles;
00627         CComPtr<IMgaMetaFCO> targ_fco;
00628         CComPtr<IMgaMetaAspect> targ_aspect;
00629         CComPtr<IMgaMetaParts> targ_parts;
00630         CComPtr<IMgaMetaPart> targ_part_ix;
00631         objtype_enum targ_objtype;
00632 
00633         ASSERT(parent != NULL);
00634 
00635         COMTRY 
00636         {
00637 
00638                 COMTHROW( parent->get_Meta(&parent_meta_fco) );
00639                 ASSERT(parent_meta_fco != NULL);
00640                 COMTHROW( parent_meta_fco.QueryInterface(&parent_meta) );
00641                 ASSERT(parent_meta!=NULL);
00642 
00643                 CComBSTR meta_parent_model_debug;
00644                 COMTHROW( parent_meta->get_Name(&meta_parent_model_debug) );
00645                 CComBSTR kind_debug(kind);
00646 
00647                 // let's try mapping first since resolution is expensive
00648 
00649                 if (this->map_get_RoleByStr(parent_meta.p,kind, objtype, role, aspect, p)) {
00650                         return S_OK;
00651                 }
00652 
00653                 // now resort to starting with the role
00654 
00655                 COMTHROW( 
00656                         ((hr = parent_meta->get_RoleByName(role, &targ_role)) == E_NOTFOUND) ? 
00657                         S_OK : hr
00658                 );
00659 
00660                 // if not found and namespace settings are available
00661                 if( hr == E_NOTFOUND && isAlterationSet())
00662                 {
00663                         // (exclusive or: either truncate or prefix)
00664                         if( m_alter_option == CH_PREFIX)
00665                         {
00666                                 CComBSTR role2 = prefixIt( role);
00667                                 // try with prefixed name
00668                                 COMTHROW( 
00669                                         ((hr = parent_meta->get_RoleByName(role2, &targ_role)) == E_NOTFOUND) ? 
00670                                         S_OK : hr
00671                                 );
00672                         }
00673                         else if( m_alter_option == CH_TRUNCATE)
00674                         {
00675                                 CComBSTR role2 = truncIt( role);
00676                                 // try with truncated name
00677                                 if( role2.Length() > 0) 
00678                                         COMTHROW( 
00679                                                 ((hr = parent_meta->get_RoleByName(role2, &targ_role)) == E_NOTFOUND) ? 
00680                                                 S_OK : hr
00681                                         );
00682                         }
00683                         else if( m_alter_option == CH_MIGRATE)
00684                         {
00685                                 CComBSTR role1 = truncIt( role);
00686                                 CComBSTR role2 = prefixIt( role1);
00687                                 // try with migrated name from one namespace to another
00688                                 if( role2.Length() > 0)
00689                                         COMTHROW( 
00690                                                 ((hr = parent_meta->get_RoleByName(role2, &targ_role)) == E_NOTFOUND) ? 
00691                                                 S_OK : hr
00692                                         );
00693                         }
00694 
00695                 }
00696 
00697                 
00698 
00699                 if ((hr == S_OK) && (targ_role != NULL ) ) {
00700 
00701                         CComBSTR targ_kind_name;
00702 
00703                         COMTHROW( targ_role->get_Kind(&targ_fco) );
00704                         ASSERT(targ_fco != NULL);
00705 
00706                         COMTHROW( targ_fco->get_Name(&targ_kind_name) );
00707 
00708                         CString c1((BSTR) targ_kind_name);
00709                         CString c2(kind);
00710                         CString c3;
00711 
00712                         if (isAlterationSet() && (m_alter_option == CH_PREFIX || m_alter_option == CH_MIGRATE))
00713                         {
00714                                 CComBSTR role2 = prefixIt(role);
00715                                 CopyTo(role2, c3);
00716                         }
00717                         else if (isAlterationSet() && m_alter_option == CH_TRUNCATE)
00718                         {
00719                                 CComBSTR role2 = truncIt(role);
00720                                 CopyTo(role2, c3);
00721                         }
00722 
00723                         if ((c2 == _T("")) || (c2 == c1) || (c3 != _T("") && c1 == c3)) {
00724 
00725                                 COMTHROW(targ_fco->get_ObjType(&targ_objtype));
00726 
00727                                 if (
00728                                         (objtype == OBJTYPE_NULL) || 
00729                                         (targ_objtype == objtype)
00730                                         ) {
00731 
00732                                         COMTHROW(targ_role->get_Parts(& targ_parts));
00733                                         ASSERT(targ_parts!=NULL);
00734 
00735                                         bool found = false;
00736                                         MGACOLL_ITERATE(IMgaMetaPart, targ_parts) {
00737 
00738                                                 if( !found) {
00739                                                         targ_part_ix = MGACOLL_ITER;
00740                                                         ASSERT(targ_part_ix != NULL);
00741 
00742                                                         CComBSTR name;
00743 
00744                                                         VARIANT_BOOL vb_primary;
00745 
00746                                                         COMTHROW(targ_part_ix->get_IsPrimary(&vb_primary) );
00747 
00748                                                         if (vb_primary != VARIANT_FALSE) {
00749 
00750                                                                 targ_aspect = NULL;
00751                                                                 COMTHROW( targ_part_ix->get_ParentAspect(&targ_aspect) );
00752                                                                 ASSERT(targ_aspect != NULL);
00753                                                                 COMTHROW( targ_aspect->get_Name(&name) );
00754                                                                 
00755                                                                 CString cb1((BSTR) name);
00756                                                                 CString cb2(aspect);
00757 
00758                                                                 if ((cb2 == _T("")) || (cb2 == cb1 )) {
00759 
00760                                                                         // finally-- success!
00761 
00762                                                                         // ??? PETER: Why do we increase the refcount here ?
00763                                                                         COMTHROW( targ_role.CopyTo(p) );
00764                                                                         
00765                                                                         
00766 
00767                                                                         // map entry
00768 
00769                                                                         this->map_put_RoleByStr(
00770                                                                                 parent_meta.p,
00771                                                                                 kind,
00772                                                                                 objtype,
00773                                                                                 role,
00774                                                                                 aspect,
00775                                                                                 *p);
00776 
00777                                                                         found = true;  // do not return from the middle of an iteration
00778                                                                         //return S_OK; // hopefully p will not change
00779                                                                 }
00780                                                         }
00781 
00782                                                 }
00783 
00784 
00785 //                                                      cdl.m_sz_prelist.AddTail(CString(name)); 
00786 
00787                                         } // eo for()
00788                                         MGACOLL_ITERATE_END;
00789                                         if( found) return S_OK;
00790                                 }
00791                         }
00792                 } // eo if(exact match from MetaModel->get_RoleByName[] )
00793 
00794                 // now resort to starting with the kind
00795 
00796                 targ_fco = NULL;
00797                 COMTHROW( 
00798                         ((hr = parent_meta->get_DefinedFCOByName(kind, VARIANT_TRUE, &targ_fco)) == E_NOTFOUND) ? 
00799                         S_OK : hr
00800                 );
00801 
00802                 // if not found and namespace settings are available
00803                 if( hr == E_NOTFOUND && isAlterationSet())
00804                 {
00805                         // (exclusive or: either truncate or prefix)
00806                         if( m_alter_option == CH_PREFIX)
00807                         {
00808                                 CComBSTR kind2 = prefixIt( kind);
00809                                 // try with prefixed name
00810                                 COMTHROW( 
00811                                         ((hr = parent_meta->get_DefinedFCOByName(kind2, VARIANT_TRUE, &targ_fco)) == E_NOTFOUND) ? 
00812                                         S_OK : hr
00813                                 );
00814                         }
00815                         else if( m_alter_option == CH_TRUNCATE)
00816                         {
00817                                 CComBSTR kind2 = truncIt( kind);
00818                                 // try with truncated name
00819                                 if( kind2.Length() > 0)
00820                                         COMTHROW( 
00821                                                 ((hr = parent_meta->get_DefinedFCOByName(kind2, VARIANT_TRUE, &targ_fco)) == E_NOTFOUND) ? 
00822                                                 S_OK : hr
00823                                         );
00824                         }
00825                         else if( m_alter_option == CH_MIGRATE)
00826                         {
00827                                 CComBSTR kind1 = truncIt( kind);
00828                                 CComBSTR kind2 = prefixIt( kind1);
00829                                 // try with migrated name from one namespace to another
00830                                 if( kind2.Length() > 0) 
00831                                         COMTHROW( 
00832                                                 ((hr = parent_meta->get_DefinedFCOByName(kind2, VARIANT_TRUE, &targ_fco)) == E_NOTFOUND) ? 
00833                                                 S_OK : hr
00834                                         );
00835                         }
00836 
00837                 }
00838 
00839                 if ((hr == S_OK) && (targ_fco != NULL)) {
00840 
00841                         COMTHROW( targ_fco->get_ObjType(&targ_objtype) );
00842 
00843                         if ((objtype == OBJTYPE_NULL) || (objtype == targ_objtype)) {
00844 
00845                                 targ_roles = NULL;
00846                                 COMTHROW( parent_meta->LegalRoles(targ_fco, &targ_roles) );
00847                                 ASSERT(targ_roles != NULL);
00848 
00849                                 long role_count = 0;
00850 
00851                                 COMTHROW( targ_roles->get_Count(&role_count));
00852 
00853                                 if (role_count == 1) {
00854 
00855                                         // ensure aspect ok
00856 
00857                                         targ_role = NULL;
00858                                         COMTHROW( targ_roles->get_Item(1, &targ_role) );
00859                                         ASSERT(targ_role != NULL);
00860 
00861                                         if (CString(aspect) == _T("")) { // done
00862 
00863                                                 COMTHROW( targ_role.CopyTo(p) );
00864 
00865                                                 // update map
00866 
00867                                                 this->map_put_RoleByStr(parent_meta.p, 
00868                                                         kind, objtype, role, aspect, *p);
00869 
00870                                                 return S_OK;
00871                                         }
00872                                         if (!this->mb_is_interactive) {
00873                                                 SetErrorInfo(CString(L"Could not resolve kind '") + kind + L"'");
00874                                                 return RESOLV_ERR_get_RoleByStr;
00875                                         }
00876 
00877                                         // else, check each possible aspect
00878 
00879                                         targ_parts = NULL;
00880                                         COMTHROW( targ_role->get_Parts(&targ_parts) );
00881                                         ASSERT(targ_parts != NULL);
00882 
00883                                         bool found = false;
00884                                         MGACOLL_ITERATE(IMgaMetaPart, targ_parts) {
00885 
00886                                                 if( !found) {
00887                                                         targ_part_ix = MGACOLL_ITER;
00888                                                         ASSERT( targ_part_ix != NULL );
00889 
00890                                                         VARIANT_BOOL vb_primary;
00891 
00892                                                         COMTHROW(targ_part_ix->get_IsPrimary(&vb_primary) );
00893 
00894                                                         if (vb_primary != VARIANT_FALSE) {
00895 
00896                                                                 targ_aspect = NULL;
00897                                                                 COMTHROW( targ_part_ix->get_ParentAspect(&targ_aspect) );
00898                                                                 ASSERT( targ_aspect != NULL);
00899 
00900                                                                 CComBSTR asp_name;
00901 
00902                                                                 COMTHROW( targ_aspect->get_Name(&asp_name) );
00903 
00904                                                                 CString cz1(aspect);
00905                                                                 CString cz2(asp_name);
00906 
00907                                                                 if (cz1 == cz2) {
00908 
00909                                                                         COMTHROW( targ_role.CopyTo(p) );
00910 
00911                                                                         // update map
00912 
00913                                                                         this->map_put_RoleByStr(parent_meta.p, 
00914                                                                                 kind, objtype, role, aspect, *p);
00915 
00916                                                                         found = true;  // do not return from the middle of an iteration
00917                                                                         //return S_OK; // hopefully p will not change after this
00918 
00919                                                                 }
00920                                                         }
00921 
00922                                                 }
00923 
00924                                         }
00925                                         MGACOLL_ITERATE_END;
00926                                         if( found) return S_OK;
00927 
00928                                 } // eo if (count == 1)
00929 
00930                                 CList<int, int> valid_role_map;
00931                                 CDialogList cdl(_T("Resolve Role Type"), CDialogList::CHKTEXT_ONETIMER); // TODO support ignore button
00932 
00933                                 for (int z = 1; z <= role_count; z++) {
00934 
00935                                         targ_role = NULL;
00936                                         COMTHROW( targ_roles->get_Item(z, &targ_role) );
00937                                         ASSERT(targ_role != NULL);
00938 
00939                                         if (CString(aspect) == _T("")) { // done
00940 
00941                                                 CComBSTR role_name;
00942 
00943                                                 COMTHROW( targ_role->get_Name(&role_name) );
00944 
00945                                                 cdl.m_sz_prelist.AddTail(CString((BSTR) role_name));
00946                                                 valid_role_map.AddTail(z);
00947 
00948                                         } else { // else, check each possible aspect
00949 
00950                                                 targ_parts = NULL;
00951                                                 COMTHROW( targ_role->get_Parts(&targ_parts) );
00952 
00953                                                 MGACOLL_ITERATE(IMgaMetaPart, targ_parts) {
00954 
00955                                                         targ_part_ix = MGACOLL_ITER;
00956                                                         ASSERT( targ_part_ix != NULL );
00957 
00958 
00959                                                         VARIANT_BOOL vb_primary;
00960 
00961                                                         COMTHROW(targ_part_ix->get_IsPrimary(&vb_primary) );
00962 
00963                                                         if (vb_primary != VARIANT_FALSE) {
00964 
00965                                                                 targ_aspect = NULL;     // Akos
00966                                                                 COMTHROW( targ_part_ix->get_ParentAspect(&targ_aspect) );
00967                                                                 ASSERT( targ_aspect != NULL);
00968 
00969                                                                 CComBSTR asp_name;
00970 
00971                                                                 COMTHROW( targ_aspect->get_Name(&asp_name) );
00972 
00973                                                                 CString cz1(aspect);
00974                                                                 CString cz2(asp_name);
00975 
00976                                                                 if (cz1 == cz2) {
00977 
00978                                                                         CComBSTR role_name;
00979 
00980                                                                         COMTHROW( targ_role->get_Name(&role_name) );
00981 
00982                                                                         cdl.m_sz_prelist.AddTail(CString((BSTR) role_name) );
00983                                                                         valid_role_map.AddTail(z);
00984 
00985                                                                         break; // back to for(z) loop
00986 
00987                                                                 }
00988                                                         }
00989 
00990                                                 } // iterate over aspects
00991                                         MGACOLL_ITERATE_END;
00992 
00993                                         } // check if aspect names important
00994                                 } // eo for(all roles of this kind name)
00995 
00996 
00997                                 // we have a fork in the road-- if no user input, we can't resolve
00998 
00999                                 if (!this->mb_is_interactive) {
01000                                         SetErrorInfo(CString(L"Could not resolve kind '") + kind + L"'");
01001                                         return RESOLV_ERR_get_RoleByStr;
01002                                 }
01003 
01004                                 INT_PTR dlgResult;
01005                                 if ((cdl.m_sz_prelist.GetCount() != 0) 
01006                                         && 
01007                                         ((dlgResult = cdl.DoModal()) == IDOK) 
01008                                         && 
01009                                         (cdl.mn_selection_index != LB_ERR)) {
01010 
01011                                         int index_into_roles_coll = 
01012                                                 valid_role_map.GetAt(valid_role_map.FindIndex(
01013                                                         cdl.mn_selection_index));
01014 
01015                                         targ_role = NULL;
01016                                         COMTHROW( targ_roles->get_Item(index_into_roles_coll, 
01017                                                 &targ_role) );
01018                                         ASSERT(targ_role != NULL);
01019 
01020                                         // we now have our damn role
01021 
01022                                         COMTHROW( targ_role.CopyTo(p) );
01023 
01024                                         // update map
01025 
01026                                         if (cdl.mb_check_once == TRUE)
01027                                                 this->map_put_RoleByStr(parent_meta.p, 
01028                                                         kind, objtype, role, aspect, *p);
01029 
01030                                         return S_OK;
01031                                                                                 
01032                                 }
01033                                 if (dlgResult == IDCANCEL)
01034                                 {
01035                                         SetErrorInfo(CString(L"Parsing cancelled"));
01036                                         return RESOLV_ERR_get_RoleByStr;
01037                                 }
01038 
01039 
01040                         } // if objtype match
01041 
01042                 } // eo if(match from FCO kind as source)
01043 
01044 
01045 //              #pragma bookmark (note: insert helpful message box here get_AttrByStr[] )
01046 
01047                 CComBSTR parent_name;
01048                 COMTHROW( parent->get_Name(&parent_name) );
01049 
01050                 CComBSTR mparent_name;
01051                 COMTHROW( parent_meta_fco->get_Name(&mparent_name) );
01052 
01053                 CString sz_format;
01054                 CString sz_format_tmp;
01055 
01056                 sz_format_tmp.Format(   _T("Paradigm Violation: Cannot create child.\n")
01057                                                                 _T("Parent name: %s\n")
01058                                                                 _T("Parent type: %s\n"),
01059                                                                 (BSTR) parent_name,
01060                                                                 (BSTR) mparent_name);
01061                 sz_format+=sz_format_tmp;
01062 
01063                 if (CString(kind) != _T("")) {
01064                         sz_format_tmp.Format(_T("Child kind %s\n"), kind);
01065                         sz_format+=sz_format_tmp;
01066                 }
01067 
01068                 if (objtype != OBJTYPE_NULL) {
01069                         sz_format_tmp.Format(_T("Child type %s\n"), helper_ObjTypeStr(objtype));
01070                         sz_format+=sz_format_tmp;
01071                 }
01072 
01073                 if (CString(aspect) != _T("")) {
01074                         sz_format_tmp.Format(_T("Aspect: %s"), aspect);
01075                         sz_format+=sz_format_tmp;
01076                 }
01077                 SetErrorInfo(sz_format);
01078                 return RESOLV_ERR_get_RoleByStr;
01079         
01080         }
01081         
01082         COMCATCH(;)
01083 
01084 
01085 } // eo CMgaResolver::get_RoleByStr()
01086 
01087 
01088 STDMETHODIMP CMgaResolver::get_AttrByStr(IMgaFCO *parent, 
01089                                                                                  BSTR kind, 
01090                                                                                  IMgaMetaAttribute **p) {
01091 
01092         AFX_MANAGE_STATE(AfxGetStaticModuleState());
01093 
01094         CHECK_OUT(p);
01095 
01096         HRESULT hr;
01097 
01098         COMTRY
01099         {
01100 
01101                 CComPtr<IMgaMetaFCO> parent_metafco;
01102                 CComPtr<IMgaMetaAttribute> imma;
01103                 CComPtr<IMgaMetaAttributes> mattrs;
01104         //      objtype_enum objtype;
01105                 long ma_count = 0;
01106 
01107 
01108                 CComBSTR sz_debug(kind);
01109 
01110                 COMTHROW( parent->get_Meta(&parent_metafco) );
01111                 ASSERT( parent_metafco != NULL );
01112                 _bstr_t kindName;
01113                 COMTHROW(parent_metafco->get_Name(kindName.GetAddress()));
01114 
01115                 COMTHROW( 
01116                         ((hr = parent_metafco->get_AttributeByName(kind, &imma)) == E_NOTFOUND) ?
01117                         S_OK : hr
01118                 );
01119 
01120                 if (imma.p != NULL) {
01121                         imma.CopyTo(p);
01122                         return S_OK;
01123                 }
01124 
01125                 // no explicit match: now check mapping
01126 
01127                 if (this->map_get_AttrByStr(parent_metafco.p, kind, p)) {
01128                         return S_OK;
01129                 }
01130 
01131                 // no match in mapping: allow for selection of possible attrs
01132 
01133                 COMTHROW( parent_metafco->get_Attributes(&mattrs) );
01134 
01135                 if (mattrs.p != NULL) {
01136 
01137                         COMTHROW( mattrs->get_Count(&ma_count) );
01138 
01139                         if (ma_count > 0) {
01140 
01141                                 if (ma_count == 1) {
01142                                         // GME-414: Can't do anything here for xme importing:
01143                                         //  Consider the case where the new meta has 1 attribute, and the xme has 2 (and maybe we've only parsed 1 so far): we may want to ignore
01144                                 }
01145 
01146                                 // no unique match in mapping and no obvious pick: 
01147                                 //              since not interactive, exit
01148 
01149                                 if (! this->mb_is_interactive) {
01150                                         SetErrorInfo(CString(L"Could not resolve attribute '") + kind + L"'");
01151                                         return RESOLV_ERR_get_AttrByStr;
01152                                 }
01153 
01154                                 // else create a dialog to pick from
01155 
01156                                 CComPtr<IMgaMetaAttribute> imma_ix;
01157                                 CString sz_dialog_name;
01158                                 sz_dialog_name.Format(_T("Resolve Attribute '%s' for Kind '%s':"), kind, static_cast<BSTR>(kindName));
01159                                 CDialogList cdl( sz_dialog_name, CDialogList::CHKTEXT_ONETIMER, true );
01160 
01161                                 MGACOLL_ITERATE(IMgaMetaAttribute, mattrs) {
01162 
01163                                         imma_ix = MGACOLL_ITER;
01164 
01165                                         CComBSTR name;
01166 
01167                                         COMTHROW( imma_ix->get_Name(&name) );
01168 
01169                                         cdl.m_sz_prelist.AddTail(CString(name)); 
01170 
01171                                 } // eo for()
01172                                 MGACOLL_ITERATE_END;
01173 
01174                 int dlgres = cdl.DoModal();
01175                 if( dlgres == IDIGNORE )
01176                 {
01177                     *p = NULL;
01178                                         if (cdl.mb_check_once == TRUE)
01179                                                 map_put_AttrByStr(parent_metafco.p, kind, NULL);
01180                     return S_OK;
01181                 }
01182                 else if( dlgres == IDCANCEL )
01183                 {
01184                                         SetErrorInfo(CString(L"Could not resolve attribute '") + kind + L"'");
01185                     return RESOLV_ERR_get_AttrByStr;
01186                 }                               
01187 
01188                                 // else, find item from dialog, copy pointer, and update map
01189 
01190                                 if (cdl.mn_selection_index != LB_ERR) { // LB_ERR == init_val
01191 
01192                                         // copy pointer
01193                                         COMTHROW( mattrs->get_Item(cdl.mn_selection_index+1, p) );
01194 
01195                                         // update map
01196 
01197                                         if (cdl.mb_check_once == TRUE)
01198                                                 this->map_put_AttrByStr(parent_metafco.p, kind, *p);
01199 
01200                                         return S_OK;
01201                                 }
01202 
01203                         }
01204                 }
01205 
01206 //              #pragma bookmark (note: insert helpful message box here get_AttrByStr[] )
01207                 CString sz_format;
01208                 CComBSTR name;
01209                 CComBSTR meta_name;
01210                 objtype_enum objtype;
01211 
01212                 COMTHROW( parent->get_ObjType(&objtype) );
01213                 COMTHROW( parent->get_Name(&name) );
01214                 COMTHROW( parent_metafco->get_Name(&meta_name) );
01215   
01216                         
01217                 sz_format.Format(       _T("No such attribute %s\n")
01218                                                         _T("can be found in %s %s\n")
01219                                                         _T("of kind %s"),
01220                                 kind,
01221                                 helper_ObjTypeStr(objtype),
01222                                 name.m_str,
01223                                 meta_name.m_str
01224                 );
01225                 SetErrorInfo(sz_format);
01226 
01227                 return RESOLV_ERR_get_AttrByStr;
01228 
01229         }
01230 
01231 
01232         COMCATCH(;)
01233 
01234 }
01235 
01236 STDMETHODIMP CMgaResolver::get_RoleByMeta(IMgaModel *parent, 
01237                                                                                   IMgaMetaFCO *kind, 
01238                                                                                   objtype_enum objtype,
01239                                                                                   IMgaMetaRole *role, 
01240                                                                                   IMgaMetaAspect *aspect, 
01241                                                                                   IMgaMetaRole **p) {
01242 
01243         AFX_MANAGE_STATE(AfxGetStaticModuleState());
01244 
01245         CHECK_OUT(p);
01246 
01247         CComBSTR sz_kind;
01248         CComBSTR sz_role;
01249         CComBSTR sz_aspect;
01250 
01251         COMTRY {
01252 
01253 
01254                 if (kind != NULL) {
01255                         COMTHROW( kind->get_Name(&sz_kind) );
01256                 }
01257 
01258                 if (role != NULL) {
01259                         COMTHROW( role->get_Name(&sz_role) );
01260                 }
01261 
01262                 if (aspect != NULL) {
01263                         COMTHROW( aspect->get_Name(&sz_aspect) );
01264                 }
01265 
01266 
01267         HRESULT hr;
01268 
01269                 hr = this->get_RoleByStr(       parent, 
01270                                                                         sz_kind, 
01271                                                                         objtype, 
01272                                                                         sz_role, 
01273                                                                         sz_aspect, 
01274                                                                         p
01275                                                                 );
01276 
01277                 if (SUCCEEDED(hr)) {
01278                         return hr;
01279                 }
01280                 else {
01281                         SetErrorInfo(CString(L"Could not resolve role for kind '") + sz_kind + L"'");
01282                         return ((hr == RESOLV_ERR_get_RoleByStr) ? RESOLV_ERR_get_RoleByMeta : hr);
01283                 }
01284 
01285         }
01286 
01287         COMCATCH(;)
01288 
01289 }
01290 
01291 STDMETHODIMP CMgaResolver::get_ConnRoleByMeta(IMgaModel *parent, 
01292                                                                                   IMgaMetaAspect *aspect, 
01293                                                                                   IMgaFCO *src, IMgaFCO *src_port,
01294                                                                                   IMgaFCO *dst, IMgaFCO *dst_port,
01295                                                                                   IMgaMetaRole **p) {
01296 
01297         AFX_MANAGE_STATE(AfxGetStaticModuleState());
01298 
01299         CHECK_OUT(p);
01300 
01301         ASSERT(src!=NULL);
01302         ASSERT(dst!=NULL);
01303         ASSERT(parent!=NULL);
01304 
01305         CComPtr<IMgaMetaRole> meta_src_role;
01306         CComPtr<IMgaMetaRole> meta_dst_role;
01307 
01308         CComPtr<IMgaMetaRole> meta_src_port_role;
01309         CComPtr<IMgaMetaRole> meta_dst_port_role;
01310 
01311         COMTRY {
01312 
01313                 COMTHROW( src->get_MetaRole(& meta_src_role) );
01314                 COMTHROW( dst->get_MetaRole(& meta_dst_role) );
01315 
01316                 CString path = _T("src ");
01317                 CString tmp;
01318                 metaref_type mr;
01319                 COMTHROW( meta_src_role->get_MetaRef(&mr) );
01320                 tmp.Format(_T("%d "),mr);
01321                 path += tmp;
01322                 if(src_port!=NULL) {
01323                         COMTHROW( src_port->get_MetaRole( & meta_src_port_role ) );
01324                         ASSERT(meta_src_port_role!=NULL);
01325 
01326                         COMTHROW( meta_src_port_role->get_MetaRef(&mr) );
01327                         tmp.Format(_T("%d "),mr);
01328                         path += tmp;
01329                 }
01330                 path += _T(", dst ");
01331 
01332                 COMTHROW( meta_dst_role->get_MetaRef(&mr) );
01333                 tmp.Format(_T("%d "),mr);
01334                 path += tmp;
01335 
01336                 if(dst_port != NULL) {
01337                         COMTHROW( dst_port->get_MetaRole( & meta_dst_port_role ) );
01338                         ASSERT(meta_dst_port_role!=NULL);
01339 
01340                         COMTHROW( meta_dst_port_role->get_MetaRef(&mr) );
01341                         tmp.Format(_T("%d "),mr);
01342                         path += tmp;
01343                 }
01344 
01345                 CComBSTR bstr(path);
01346 
01347 
01348                 CComPtr<IMgaMetaRoles> roles;
01349                 CComPtr<IMgaMetaFCO> metaFco;
01350 
01351                 COMTHROW(parent->get_Meta(&metaFco));
01352 
01353                 CComPtr<IMgaMetaModel> metaModel;
01354 
01355                 COMTHROW(metaFco.QueryInterface(&metaModel));
01356 
01357                 COMTHROW(metaModel->LegalConnectionRoles(bstr,&roles));
01358 
01359                 // now get valid roles for input aspect (if any)
01360 
01361                 CDialogList cdl(_T("Select Connection Role Type"), CDialogList::CHKTEXT_STICKY);
01362                 CList<int, int> valid_role_map;
01363                 CComPtr<IMgaMetaRole> immr_ix;
01364 
01365                 long role_count = 0;
01366 
01367                 COMTHROW( roles->get_Count(&role_count) );
01368 
01369                 for (int z = 1; z <= role_count; z++) {
01370 
01371                         immr_ix = NULL;
01372                         COMTHROW( roles->get_Item(z, &immr_ix) );
01373                         ASSERT(immr_ix != NULL);
01374 
01375                         if (aspect == NULL) {
01376 
01377                                 CComBSTR role_name;
01378 
01379                                 COMTHROW( immr_ix->get_Name(&role_name ) );
01380 
01381                                 cdl.m_sz_prelist.AddTail(CString(role_name)); 
01382 
01383                                 valid_role_map.AddTail(z);
01384                                 
01385                         } else {
01386 
01387                                 CComPtr<IMgaMetaParts> parts;
01388                                 COMTHROW( immr_ix->get_Parts(&parts) );
01389                                 ASSERT(parts!=NULL);
01390 
01391                                 CComPtr<IMgaMetaPart> part_ix;
01392 
01393                                 MGACOLL_ITERATE(IMgaMetaPart,parts) {
01394 
01395                                         part_ix = MGACOLL_ITER;
01396                                         ASSERT(part_ix != NULL);
01397 
01398                                         VARIANT_BOOL vb_primary;
01399 
01400                                         COMTHROW(part_ix->get_IsPrimary(&vb_primary) );
01401 
01402                                         if (vb_primary != VARIANT_FALSE) {
01403                                         
01404                                         
01405                                                 CComPtr<IMgaMetaAspect> part_aspect;
01406 
01407                                                 COMTHROW( part_ix->get_ParentAspect(&part_aspect) );
01408 
01409                                                 if (part_aspect == aspect) {
01410 
01411                                                         CComBSTR role_name;
01412 
01413                                                         COMTHROW( immr_ix->get_Name(&role_name ) );
01414 
01415                                                         cdl.m_sz_prelist.AddTail(CString(role_name)); 
01416 
01417                                                         valid_role_map.AddTail(z);
01418 
01419                                                         break;
01420                                                 }
01421 
01422                                         }
01423                                 }
01424                                 MGACOLL_ITERATE_END;
01425 
01426                         } // else aspect not null
01427 
01428                 } // iterate over roles
01429 
01430                 int valid_role_count = valid_role_map.GetCount();
01431 
01432                 if (valid_role_count == 1) {
01433 
01434                         COMTHROW( roles->get_Item(valid_role_map.GetHead(), p) );
01435                         return S_OK;
01436 
01437                 }
01438 
01439                 // before popping up the dialog, see if it is in the knowledge map
01440                 if(map_get_RoleByPathStr(bstr, metaModel,OBJTYPE_CONNECTION,p)) {
01441                         if(mb_use_sticky) { // if sticky is enabled, return happily
01442                                 return S_OK;
01443                         }
01444                         else {          // if sticky is disabled, remove it from the map
01445                                 (*p)->Release(); *p = NULL;
01446                                 map_put_RoleByPathStr(bstr, metaModel,OBJTYPE_CONNECTION,*p);
01447                         }
01448                 }
01449 
01450                 if (!this->mb_is_interactive) {
01451 
01452 //                      if (valid_role_count == 0)
01453 //                              return RESOLV_ERR_get_ConnRoleByMeta;
01454 
01455                         SetErrorInfo(CString(L"Could not resolve connection role"));
01456                         return RESOLV_ERR_get_ConnRoleByMeta;
01457                 }
01458 
01459                 if (valid_role_count > 0) {
01460 
01461                         if ((cdl.DoModal() != IDOK) || (cdl.mn_selection_index == LB_ERR)) {
01462                                 SetErrorInfo(CString(L"Cancelled by user"));
01463                                 return E_ABORT;
01464 
01465                         } else {
01466 
01467                                 // get the entry in the list
01468 
01469                                 immr_ix = NULL;
01470                                 COMTHROW( roles->get_Item(
01471                                         valid_role_map.GetAt(valid_role_map.FindIndex(cdl.mn_selection_index)), p) );
01472 
01473                                 if(cdl.mb_check_once == TRUE) {
01474                                         map_put_RoleByPathStr(bstr, metaModel,OBJTYPE_CONNECTION,*p);
01475                                 }
01476                                 return S_OK;
01477 
01478                         }
01479                 
01480                 } else {
01481 
01482                         CString sz_aspect_name;
01483 
01484                         CComBSTR name;
01485 
01486                         CComPtr<IMgaMetaRole> parent_role;
01487                         COMTHROW( parent->get_MetaRole(&parent_role) );
01488 
01489                         CString sz_parent_role(name);
01490 
01491                         if (parent_role != NULL) {
01492                                 name.Empty();
01493                                 COMTHROW( parent_role->get_Name(&name) );
01494                                 sz_parent_role = CString(name);
01495                         } else {
01496 
01497                                 CComPtr<IMgaMetaFCO> meta_parent;
01498                                 COMTHROW( parent->get_Meta(&meta_parent) );
01499 
01500                                 name.Empty();
01501                                 COMTHROW( meta_parent->get_Name(&name) );
01502 
01503                                 sz_parent_role = CString(name);
01504                         }
01505 
01506                         if (aspect != NULL) {
01507                                 name.Empty();
01508                                 COMTHROW( aspect->get_Name(&name) );
01509                                 sz_aspect_name = CString(name);
01510                         }
01511 
01512                         name.Empty();
01513                         COMTHROW( parent->get_Name(&name) );
01514                         CString sz_parent_name(name);
01515 
01516                         name.Empty();
01517                         COMTHROW( src->get_Name(&name) );
01518                         CString sz_src_name(name);
01519 
01520                         name.Empty();
01521                         COMTHROW( meta_src_role->get_Name(&name) );
01522                         CString sz_src_role_name(name);
01523 
01524                         name.Empty();
01525                         COMTHROW( dst->get_Name(&name) );
01526                         CString sz_dst_name(name);
01527 
01528                         name.Empty();
01529                         COMTHROW( meta_dst_role->get_Name(&name) );
01530                         CString sz_dst_role_name(name);
01531 
01532                         CString sz_src_port(_T("<no port>"));
01533                         CString sz_src_port_role(_T("<no role>"));
01534                         if (src_port != NULL) {
01535                                 name.Empty();
01536                                 COMTHROW( src_port->get_Name(&name) );
01537                                 sz_src_port = CString(name);
01538 
01539                                 name.Empty();
01540                                 COMTHROW( meta_src_port_role->get_Name(&name) );
01541                                 sz_src_port_role = CString(name);
01542                         }
01543 
01544                         CString sz_dst_port(_T("<no port>"));
01545                         CString sz_dst_port_role(_T("<no role>"));
01546                         if (dst_port != NULL) {
01547                                 name.Empty();
01548                                 COMTHROW( dst_port->get_Name(&name) );
01549                                 sz_dst_port = CString(name);
01550 
01551                                 name.Empty();
01552                                 COMTHROW( meta_dst_port_role->get_Name(&name) );
01553                                 sz_dst_port_role = CString(name);
01554                         }
01555 
01556                         CString sz_format;
01557                         CString sz_format_tmp;
01558 
01559                         sz_format_tmp.Format(
01560                                 _T("Paradigm violation: cannot make a connection.\n")
01561                                 _T("Source: %s (%s)\n"),
01562                                 sz_src_name, sz_src_role_name
01563                         );
01564 
01565                         sz_format += sz_format_tmp;
01566                                 
01567 
01568                         if (src_port != NULL) {
01569 
01570                                 sz_format_tmp.Format(
01571                                         _T("Source port: %s (%s)\n"),
01572                                         sz_src_port, sz_src_port_role                           
01573                                 );
01574 
01575                                 sz_format += sz_format_tmp;
01576                         }
01577                                 
01578                         sz_format_tmp.Format(
01579                                 _T("Destination: %s (%s)\n"),
01580                                 sz_dst_name, sz_dst_role_name
01581                         );
01582                         sz_format += sz_format_tmp;
01583 
01584                         if (dst_port != NULL) {
01585                                 sz_format_tmp.Format(
01586                                         _T("Destination port: %s (%s)\n"),
01587                                         sz_dst_port, sz_dst_port_role                           
01588                                 );
01589 
01590                                 sz_format += sz_format_tmp;
01591                         }
01592 
01593                         if (sz_aspect_name != _T("")) {
01594                                 sz_format_tmp.Format(_T("Aspect: %s\n"),
01595                                         sz_aspect_name);
01596                                 sz_format += sz_format_tmp;
01597                         }
01598 
01599                         sz_format_tmp.Format(_T("Parent: %s (%s)"),
01600                                 sz_parent_name, sz_parent_role
01601                         );
01602                         sz_format += sz_format_tmp;
01603 
01604                         SetErrorInfo(sz_format);
01605                         return RESOLV_ERR_get_ConnRoleByMeta;
01606 
01607                 }
01608         }
01609 
01610         COMCATCH(;)
01611 
01612 }
01613 
01614 
01615 STDMETHODIMP CMgaResolver::get_RefRoleByMeta(IMgaModel *parent, 
01616                                                                                          IMgaMetaAspect *aspect, 
01617                                                                                          IMgaFCO *src, IMgaMetaRole **p) {
01618 
01619         AFX_MANAGE_STATE(AfxGetStaticModuleState());
01620 
01621         CHECK_OUT(p);
01622 
01623         ASSERT(parent != NULL);
01624         ASSERT(src!=NULL);
01625 
01626         COMTRY {
01627 
01628                 CComBSTR path;
01629 
01630                 CComPtr<IMgaMetaFCO> metafco;
01631                 CComPtr<IMgaMetaModel> metamodel;
01632 
01633                 COMTHROW( parent->get_Meta(&metafco) );
01634                 ASSERT(metafco != NULL);
01635 
01636                 COMTHROW( metafco.QueryInterface(&metamodel) );
01637 
01638                 COMTHROW(src->GetAbsMetaPath(&path));
01639                 CComPtr<IMgaMetaRoles> roles;
01640                 COMTHROW(metamodel->LegalReferenceRoles(path,&roles));
01641 
01642                 // copy roles into the dialog
01643 
01644                 CDialogList cdl(_T("Select Reference Role Type"), CDialogList::CHKTEXT_ONETIMER);
01645                 CList<int, int> valid_role_map;
01646                 CComPtr<IMgaMetaRole> immr_ix;
01647 
01648                 long role_count = 0;
01649 
01650                 COMTHROW( roles->get_Count(&role_count) );
01651 
01652                 for (int z = 1; z <= role_count; z++) {
01653 
01654                         immr_ix = NULL;
01655                         COMTHROW( roles->get_Item(z, &immr_ix) );
01656                         ASSERT(immr_ix != NULL);
01657 
01658                         if (aspect == NULL) {
01659 
01660                                 CComBSTR role_name;
01661 
01662                                 COMTHROW( immr_ix->get_Name(&role_name ) );
01663 
01664                                 cdl.m_sz_prelist.AddTail(CString(role_name)); 
01665 
01666                                 valid_role_map.AddTail(z);
01667                                 
01668                         } else {
01669 
01670                                 CComPtr<IMgaMetaParts> parts;
01671                                 COMTHROW( immr_ix->get_Parts(&parts) );
01672                                 ASSERT(parts!=NULL);
01673 
01674                                 CComPtr<IMgaMetaPart> part_ix;
01675 
01676                                 MGACOLL_ITERATE(IMgaMetaPart,parts) {
01677 
01678                                         part_ix = MGACOLL_ITER;
01679                                         ASSERT(part_ix != NULL);
01680 
01681                                         VARIANT_BOOL vb_primary;
01682 
01683                                         COMTHROW(part_ix->get_IsPrimary(&vb_primary) );
01684 
01685                                         if (vb_primary != VARIANT_FALSE) {
01686 
01687                                                 CComPtr<IMgaMetaAspect> part_aspect;
01688 
01689                                                 COMTHROW( part_ix->get_ParentAspect(&part_aspect) );
01690                                                 ASSERT(part_aspect != NULL);
01691 
01692                                                 if (part_aspect == aspect) {
01693 
01694                                                         CComBSTR role_name;
01695 
01696                                                         COMTHROW( immr_ix->get_Name(&role_name ) );
01697 
01698                                                         cdl.m_sz_prelist.AddTail(CString(role_name)); 
01699 
01700                                                         valid_role_map.AddTail(z);
01701 
01702                                                         break;
01703                                                 }
01704 
01705                                         }
01706                                 }
01707                                 MGACOLL_ITERATE_END;
01708 
01709                         } // else aspect not null
01710 
01711                 } // iterate over roles
01712 
01713                 int valid_role_count = valid_role_map.GetCount();
01714 
01715                 if (valid_role_count == 1) {
01716 
01717                         COMTHROW( roles->get_Item(valid_role_map.GetHead(), p) );
01718                         return S_OK;
01719 
01720                 }
01721 
01722                 // before popping up the dialog, see if it is in the knowledge map
01723                 if(map_get_RoleByPathStr(path, metamodel,OBJTYPE_REFERENCE,p)) {
01724                         if(mb_use_sticky) { // if sticky is enabled, return happily
01725                                 return S_OK;
01726                         }
01727                         else {          // if sticky is disabled, remove it from the map
01728                                 (*p)->Release(); *p = NULL;
01729                                 map_put_RoleByPathStr(path, metamodel,OBJTYPE_REFERENCE,*p);
01730                         }
01731                 }
01732 
01733                 if (!this->mb_is_interactive) {
01734 
01735                         //if (valid_role_count == 0) {
01736                         //      return RESOLV_ERR_get_ConnRoleByMeta;
01737                         //}
01738 
01739                         SetErrorInfo(CString(L"Could not resolve reference role'"));
01740                         return RESOLV_ERR_get_ConnRoleByMeta;
01741 
01742                 }
01743 
01744                 if (valid_role_count > 0) {
01745 
01746                         if (cdl.DoModal() != IDOK || (cdl.mn_selection_index == LB_ERR)) {
01747                                 SetErrorInfo(L"No item chosen from dialog");
01748                                 return E_ABORT;
01749                         } else {
01750 
01751                                 // get the entry in the list
01752 
01753                                 immr_ix = NULL;
01754                                 COMTHROW( roles->get_Item(
01755                                         valid_role_map.GetAt(valid_role_map.FindIndex(cdl.mn_selection_index)), p) );
01756 
01757                                 if(cdl.mb_check_once == TRUE) {
01758                                         map_put_RoleByPathStr(path, metamodel,OBJTYPE_REFERENCE,*p);
01759                                 }
01760                                 return S_OK;
01761 
01762                         }
01763                 
01764                 } else {
01765 
01766                         CString sz_aspect_name;
01767 
01768                         CComBSTR name;
01769 
01770                         CComPtr<IMgaMetaRole> parent_role;
01771                         COMTHROW( parent->get_MetaRole(&parent_role) );
01772 
01773                         CComPtr<IMgaMetaRole> src_role;
01774                         COMTHROW( src->get_MetaRole(&src_role) );
01775                         // note: it is ok to have a null src_role
01776 
01777                         CString sz_parent_role;
01778 
01779                         if (parent_role != NULL) {
01780                                 name.Empty();
01781                                 COMTHROW( parent_role->get_Name(&name) );
01782                                 sz_parent_role = CString(name);
01783                         } else {
01784                                 CComPtr<IMgaMetaFCO> meta_parent;
01785                                 COMTHROW( parent->get_Meta(&meta_parent) );
01786 
01787                                 name.Empty();
01788                                 COMTHROW( meta_parent->get_Name(&name) );
01789 
01790                                 sz_parent_role = CString(name);
01791                         }
01792 
01793                         if (aspect != NULL) {
01794                                 name.Empty();
01795                                 COMTHROW( aspect->get_Name(&name) );
01796                                 sz_aspect_name = CString(name);
01797                         }
01798 
01799                         name.Empty();
01800                         COMTHROW( parent->get_Name(&name) );
01801                         CString sz_parent_name(name);
01802 
01803                         name.Empty();
01804                         COMTHROW( src->get_Name(&name) );
01805                         CString sz_src_name(name);
01806 
01807                         CString sz_src_role_name;
01808                         name.Empty();
01809                         if (src_role != NULL) {
01810                                 COMTHROW( src_role->get_Name(&name) );
01811                         } else {
01812                                 CComPtr<IMgaMetaFCO> meta_src;
01813                                 COMTHROW( src->get_Meta(&meta_src) );
01814 
01815                                 name.Empty();
01816                                 COMTHROW( meta_src->get_Name(&name) );
01817                         }
01818                         sz_src_role_name = CString(name);
01819 
01820                         CString sz_format;
01821                         CString sz_format_tmp;
01822 
01823                         sz_format_tmp.Format(
01824                                 _T("Cannot insert reference.\n")
01825                                 _T("Source: %s (%s)\n"),
01826                                 sz_src_name, sz_src_role_name
01827                                 );
01828                         sz_format += sz_format_tmp;
01829 
01830                         if (sz_aspect_name != _T("")) {
01831 
01832                                 sz_format_tmp.Format(
01833                                         _T("Aspect: %s\n"),
01834                                         sz_aspect_name
01835                                         );
01836                                 sz_format += sz_format_tmp;
01837 
01838 
01839                         }
01840 
01841                         sz_format_tmp.Format(
01842                                 _T("Parent: %s (%s)"),
01843                                 sz_parent_name, sz_parent_role
01844                         );
01845                         sz_format += sz_format_tmp;
01846 
01847                         SetErrorInfo(sz_format);
01848 
01849                         return RESOLV_ERR_get_ConnRoleByMeta;
01850 
01851                 }
01852 
01853         }
01854 
01855         COMCATCH(;)
01856 
01857 }
01858 
01859 STDMETHODIMP CMgaResolver::get_RolesByMetas(IMgaModel *dst_parent, 
01860                                                                                         IMgaFCOs *src, 
01861                                                                                         IMgaMetaAspect * aspect, 
01862                                                                                         BSTR sz_aspect,
01863                                                                                         IMgaMetaRoles ** p) {
01864 
01865 
01866         AFX_MANAGE_STATE(AfxGetStaticModuleState());
01867 
01868         HRESULT hr;
01869 
01870         CComPtr<IMgaFCO> fco_ix;
01871         CComPtr<IMgaMetaFCO> mfco_ix;
01872         CComPtr<IMgaMetaRole> role_ix;
01873 
01874         CHECK_OUT(p);
01875 
01876         ASSERT(dst_parent != NULL);
01877         ASSERT(src != NULL);
01878 
01879         COMTRY {
01880 
01881                 long fco_count = 0;
01882 
01883                 CComBSTR sz_aspect_found;
01884 
01885                 if (aspect != NULL) {
01886                         COMTHROW( aspect->get_Name(&sz_aspect_found) );
01887                 }
01888 
01889                 COMTHROW( src->get_Count(&fco_count) );
01890 
01891                 CComPtr<IMgaMetaRoles> roles;
01892 
01893                 COMTHROW(roles.CoCreateInstance(L"Mga.MgaMetaRoles"));
01894 
01895                 for (int ctr_z = fco_count; ctr_z > 0; ctr_z--) {
01896 
01897                         fco_ix = NULL;
01898                         COMTHROW( src->get_Item(ctr_z, &fco_ix) );
01899                         ASSERT(fco_ix != NULL);
01900 
01901                         mfco_ix = NULL;
01902                         COMTHROW( fco_ix->get_Meta(&mfco_ix) );
01903                         ASSERT(mfco_ix != NULL);
01904 
01905                         CComBSTR mfco_name_ix;
01906 
01907                         COMTHROW( mfco_ix->get_Name(&mfco_name_ix) );
01908 
01909                         CComPtr<IMgaMetaRole> mfco_role_ix;
01910                         CComBSTR mfco_role_ix_sz;
01911 
01912                         COMTHROW( fco_ix->get_MetaRole(&mfco_role_ix) );
01913 
01914                         if (mfco_role_ix != NULL) {
01915                                 COMTHROW( mfco_role_ix->get_Name(&mfco_role_ix_sz) );
01916                         }
01917 
01918                         role_ix = NULL;
01919                         COMTHROW( 
01920                                 ((hr = this->get_RoleByStr(dst_parent, 
01921                                         mfco_name_ix, 
01922                                         OBJTYPE_NULL, 
01923                                         (BSTR) mfco_role_ix_sz, // role
01924                                         ((aspect == NULL) ? sz_aspect : (BSTR) sz_aspect_found), // aspect
01925                                         &role_ix)) == RESOLV_ERR_get_RoleByStr) ?
01926                                         S_OK : hr
01927                                         );
01928 
01929                         // add pointer to collection
01930 
01931                         roles->Append(role_ix);
01932                 }
01933 
01934                 // copy collection to p
01935 
01936                 *p = roles.Detach();
01937 
01938 //              return RESOLV_ERR_get_RolesByMetas;
01939 
01940         }
01941 
01942         COMCATCH(;)
01943 
01944 }
01945 
01946 
01947 bool CMgaResolver::map_get_FolderByStr(IMgaMetaFolder *&parent, 
01948                                                                            BSTR kind, 
01949                                                                            IMgaMetaFolder **p) {
01950 
01951         Cmap_elmt_type_FolderByStr * map_find = NULL;
01952 
01953         // get a key instance
01954 
01955         Cmap_elmt_type_FolderByStr * map_key = 
01956                 new Cmap_elmt_type_FolderByStr(parent, kind);
01957 
01958         // now do "lookup" function
01959 
01960         bool found = false;
01961         POSITION pos = this->map_FolderByStr.GetHeadPosition();
01962 
01963         while (pos != NULL) {
01964 
01965                 map_find = this->map_FolderByStr.GetNext(pos);
01966 
01967                 if (*map_find == *map_key) {
01968                         found = true;
01969                         break;
01970                 }
01971         }
01972 
01973         if (!found) {
01974                 delete map_key;
01975                 return false;
01976         }
01977 
01978         map_find->p.CopyTo(p);
01979 
01980         delete map_key;
01981         return true;
01982 
01983 } // eo CMgaResolver::map_get_FolderByStr()
01984 
01985 bool CMgaResolver::map_put_FolderByStr(IMgaMetaFolder *&parent , 
01986                                                                            BSTR kind, 
01987                                                                            IMgaMetaFolder *p) {
01988 
01989         // ASSERT: this thing had better not be in here already!!
01990 
01991         // get a key instance
01992 
01993         Cmap_elmt_type_FolderByStr * map_key = 
01994                 new Cmap_elmt_type_FolderByStr(parent, kind);
01995 
01996         // set map_to element
01997 
01998         map_key->p = p;
01999 
02000         this->map_FolderByStr.AddTail(map_key);
02001 
02002         return true;
02003 
02004 } // eo CMgaResolver::map_put_FolderByStr()
02005 
02006 
02007 bool CMgaResolver::map_get_AttrByStr(IMgaMetaFCO *&parent, 
02008                                                                          BSTR kind, 
02009                                                                          IMgaMetaAttribute **p) {
02010 
02011         Cmap_elmt_type_AttrByStr * map_find = NULL;
02012 
02013         // get a key instance
02014 
02015         Cmap_elmt_type_AttrByStr * map_key = 
02016                 new Cmap_elmt_type_AttrByStr(parent, kind);
02017 
02018         // now do "lookup" function
02019 
02020         bool found = false;
02021         POSITION pos = this->map_AttrByStr.GetHeadPosition();
02022 
02023         while (pos != NULL) {
02024 
02025                 map_find = this->map_AttrByStr.GetNext(pos);
02026 
02027                 if (*map_find == *map_key) {
02028                         found = true;
02029                         break;
02030                 }
02031         }
02032 
02033         if (!found) {
02034                 delete map_key;
02035                 return false;
02036         }
02037 
02038         map_find->p.CopyTo(p);
02039 
02040         delete map_key;
02041         return true;
02042 
02043 }
02044 
02045 bool CMgaResolver::map_put_AttrByStr(IMgaMetaFCO *&parent, 
02046                                                                          BSTR kind, 
02047                                                                          IMgaMetaAttribute *p) {
02048 
02049         // ASSERT: this thing had better not be in here already!!
02050 
02051         // get a key instance
02052 
02053         Cmap_elmt_type_AttrByStr * map_key = 
02054                 new Cmap_elmt_type_AttrByStr(parent, kind);
02055 
02056         // set map_to element
02057 
02058         map_key->p = p;
02059 
02060         this->map_AttrByStr.AddTail(map_key);
02061 
02062         return true;
02063 
02064 }
02065 
02066 bool CMgaResolver::map_get_KindByStr(IMgaMetaFolder *&parent, 
02067                                                                          BSTR kind, 
02068                                                                          objtype_enum objtype, 
02069                                                                          IMgaMetaFCO **p) {
02070 
02071         ASSERT(p != 0 && *p == 0);
02072 
02073         Cmap_elmt_type_KindByStr * map_find = NULL;
02074 
02075         // get a key instance
02076 
02077         Cmap_elmt_type_KindByStr * map_key = 
02078                 new Cmap_elmt_type_KindByStr(parent, kind, objtype);
02079 
02080         // now do "lookup" function
02081 
02082         bool found = false;
02083         POSITION pos = this->map_KindByStr.GetHeadPosition();
02084 
02085         while (pos != NULL) {
02086 
02087                 map_find = this->map_KindByStr.GetNext(pos);
02088 
02089                 if (*map_find == *map_key) {
02090                         found = true;
02091                         break;
02092                 }
02093         }
02094 
02095         if (!found) {
02096                 delete map_key;
02097                 return false;
02098         }
02099 
02100         map_find->p.CopyTo(p);
02101 
02102         delete map_key;
02103         return true;
02104 
02105 }
02106  
02107 
02108 bool CMgaResolver::map_put_KindByStr(IMgaMetaFolder *&parent, 
02109                                                                          BSTR kind, 
02110                                                                          objtype_enum objtype, 
02111                                                                          IMgaMetaFCO *p) {
02112 
02113         // ASSERT: this thing had better not be in here already!!
02114 
02115         // get a key instance
02116 
02117         Cmap_elmt_type_KindByStr * map_key = 
02118                 new Cmap_elmt_type_KindByStr(parent, kind, objtype);
02119 
02120         // set map_to element
02121 
02122         map_key->p = p;
02123 
02124         this->map_KindByStr.AddTail(map_key);
02125 
02126         return true;
02127 
02128 }
02129 
02130 bool CMgaResolver::map_get_RoleByStr(IMgaMetaModel *&parent, 
02131                                                                          BSTR kind, 
02132                                                                          objtype_enum objtype,
02133                                                                          BSTR &role, 
02134                                                                          BSTR &aspect, 
02135                                                                          IMgaMetaRole **p) {
02136 
02137         Cmap_elmt_type_RoleByStr * map_find = NULL;
02138 
02139         // get a key instance
02140 
02141         Cmap_elmt_type_RoleByStr * map_key = 
02142                 new Cmap_elmt_type_RoleByStr(parent, kind, objtype, role, aspect);
02143 
02144         // now do "lookup" function
02145 
02146         bool found = false;
02147         POSITION pos = this->map_RoleByStr.GetHeadPosition();
02148 
02149         while (pos != NULL) {
02150 
02151                 map_find = this->map_RoleByStr.GetNext(pos);
02152 
02153                 if (*map_find == *map_key) {
02154                         found = true;
02155                         break;
02156                 }
02157         }
02158 
02159         if (!found) {
02160                 delete map_key;
02161                 return false;
02162         }
02163 
02164         map_find->p.CopyTo(p);
02165 
02166         delete map_key;
02167         return true;
02168 
02169 }
02170 
02171 bool CMgaResolver::map_put_RoleByStr(IMgaMetaModel *&parent, 
02172                                                                          BSTR kind, 
02173                                                                          objtype_enum objtype,
02174                                                                          BSTR &role, 
02175                                                                          BSTR &aspect, 
02176                                                                          IMgaMetaRole *p) {
02177 
02178 
02179         // ASSERT: this thing had better not be in here already!!
02180 
02181         // get a key instance
02182 
02183         Cmap_elmt_type_RoleByStr * map_key = 
02184                 new Cmap_elmt_type_RoleByStr(parent, kind, objtype, role, aspect);
02185 
02186         // set map_to element
02187 
02188         map_key->p = p;
02189 
02190         this->map_RoleByStr.AddTail(map_key);
02191 
02192         return true;
02193 
02194 }
02195 
02196 
02197 bool CMgaResolver::map_get_RoleByPathStr(BSTR path, IMgaMetaBase *parent, objtype_enum objtype, IMgaMetaRole **p) {
02198 
02199         Cmap_elmt_type_RoleByPathStr * map_find = NULL;
02200 
02201         // get a key instance
02202 
02203         Cmap_elmt_type_RoleByPathStr * map_key = 
02204                 new Cmap_elmt_type_RoleByPathStr(path, parent, objtype);
02205 
02206         // now do "lookup" function
02207 
02208         bool found = false;
02209         POSITION pos = this->map_RoleByPathStr.GetHeadPosition();
02210 
02211         while (pos != NULL) {
02212 
02213                 map_find = this->map_RoleByPathStr.GetNext(pos);
02214 
02215                 if (*map_find == *map_key) {
02216                         found = true;
02217                         break;
02218                 }
02219         }
02220 
02221         delete map_key;
02222         if (found)      map_find->p.CopyTo(p);
02223 
02224         return found;
02225 
02226 }
02227 
02228 bool CMgaResolver::map_put_RoleByPathStr(BSTR path, IMgaMetaBase *parent, objtype_enum objtype, IMgaMetaRole *p) {
02229 
02230         // get a key instance
02231 
02232         Cmap_elmt_type_RoleByPathStr * map_key = 
02233                 new Cmap_elmt_type_RoleByPathStr(path, parent, objtype);
02234 
02235         POSITION pos = this->map_RoleByPathStr.GetHeadPosition();
02236         while (pos != NULL) {
02237 
02238                 Cmap_elmt_type_RoleByPathStr *map_find = this->map_RoleByPathStr.GetAt(pos);
02239                 if (*map_find == *map_key) {
02240                         if(p) {
02241                                 map_find->p = p;
02242                         }
02243                         else {
02244                                 delete map_find;
02245                                 map_RoleByPathStr.RemoveAt(pos);
02246                         }
02247                         return true;
02248                 }
02249                 map_RoleByPathStr.GetNext(pos);
02250         }
02251 
02252         // not found in list, set map_to element
02253         
02254         if(p) {
02255                 map_key->p = p;
02256                 this->map_RoleByPathStr.AddTail(map_key);
02257         }
02258 
02259         return true;
02260 
02261 }
02262 
02263 CComPtr<IGMEOLEApp>     CMgaResolver::get_GME(CComPtr<IMgaProject> project)
02264 {
02265         CComPtr<IGMEOLEApp> gme;
02266         if (mb_is_interactive && (project != NULL)) {           
02267                 CComBSTR bstrName(L"GME.Application");
02268                 CComPtr<IMgaClient> pClient;
02269                 HRESULT hr = project->GetClientByName(bstrName, &pClient);
02270                 if (SUCCEEDED(hr) && pClient) {
02271                         CComPtr<IDispatch> pDispatch;
02272                         hr = pClient->get_OLEServer(&pDispatch);
02273                         if (SUCCEEDED(hr) && pDispatch) {
02274                                 hr = pDispatch.QueryInterface(&gme);
02275                                 if (FAILED(hr)) {
02276                                         gme = NULL;
02277                                 }
02278                         }
02279                 }
02280         }
02281         return gme;
02282 }
02283 
02284 
02285 // eof MgaResolver.cpp;