GME  13
MgaSet.cpp
Go to the documentation of this file.
00001 // MgaSet.cpp : Implementation of CMgaSet
00002 #include "stdafx.h"
00003 #include "Mga.h"
00004 #include "MgaSet.h"
00005 
00007 // CMgaSet
00008 
00009 
00010 HRESULT FCO::get_Members(IMgaFCOs **pVal)
00011 {
00012         COMTRY {
00013                 CheckRead();
00014                 CHECK_OUTPTRPAR(pVal);
00015                 CREATEEXCOLLECTION_FOR(MgaFCO, q);
00016                 CoreObjs members = self[ATTRID_SETMEMBER + ATTRID_COLLECTION];
00017                 ITERATE_THROUGH(members) {
00018                         CComPtr<IMgaFCO> s;
00019                         ObjForCore(CoreObj(ITER[ATTRID_XREF]))->getinterface(&s);
00020                         q->Add(s);
00021                 }
00022                 *pVal = q.Detach();
00023         } COMCATCH(;);
00024 }
00025 
00026 HRESULT FCO::get_IsMember(IMgaFCO *obj, VARIANT_BOOL *pVal) {
00027         COMTRY {
00028                 CheckRead();
00029                 CHECK_INPTRPAR(obj);
00030                 CHECK_OUTPAR(pVal);
00031                 CoreObj search(obj);
00032                 CoreObjs members = self[ATTRID_SETMEMBER + ATTRID_COLLECTION];
00033                 *pVal = VARIANT_FALSE;
00034                 ITERATE_THROUGH(members) {
00035                         if(COM_EQUAL(search, ITER)) {
00036                                 *pVal = VARIANT_TRUE;
00037                                 break;
00038                         }       
00039                 }
00040         } COMCATCH(;);
00041 }
00042 
00043 bool addmember::Do(CoreObj self, std::vector<CoreObj> *peers) {
00044         CoreObjs members = self[ATTRID_SETMEMBER + ATTRID_COLLECTION];
00045         auto fco = ObjForCore(self);
00046         ITERATE_THROUGH(members) {
00047                 if(COM_EQUAL((*peers)[0], CoreObj(ITER[ATTRID_XREF]))) { // Already there !!!
00048                         ASSERT(!CoreObj(ITER[ATTRID_MASTEROBJ]));
00049                         ITER[ATTRID_MASTEROBJ] = (*peers)[1];
00050                         (*peers)[1] = ITER;
00051                         return false;
00052                 }   
00053         }
00054         CoreObj nnode;
00055         COMTHROW(fco->mgaproject->dataproject->CreateObject(DTID_SETNODE,&nnode.ComPtr()));
00056         nnode[ATTRID_MASTEROBJ] = (*peers)[1];
00057         nnode[ATTRID_SETMEMBER] = self;
00058         nnode[ATTRID_XREF] = (*peers)[0];
00059         (*peers)[1] = nnode;
00060         ObjForCore((*peers)[0])->SelfMark(OBJEVENT_SETINCLUDED);
00061         fco->SelfMark(OBJEVENT_RELATION);
00062         COMTHROW(fco->Check());
00063         return true;
00064 };
00065 
00066 
00067 // TODO: define semantics for assigning members that already exist for a set.
00068 HRESULT FCO::AddMember(IMgaFCO *member)
00069 {
00070         COMTRY_IN_TRANSACTION { 
00071                 CheckWrite();
00072                 CHECK_MYINPTRPAR(member);
00073                 std::vector<CoreObj> peers(2);
00074                 peers[0] = CoreObj(member);
00075                 peers[1] = NULLCOREOBJ;
00076                 addmember(false).DoWithDeriveds(self, &peers);
00077                 CoreObj d = self[ATTRID_DERIVED];
00078                 CoreObj memd = peers[0][ATTRID_DERIVED];
00079                 if(d && memd) {
00080                         CoreObjs members = d[ATTRID_SETMEMBER + ATTRID_COLLECTION];
00081                         ITERATE_THROUGH(members) {
00082                                 if(COM_EQUAL(memd, CoreObj(ITER[ATTRID_XREF]))) { 
00083                                         peers[1][ATTRID_MASTEROBJ] = ITER;
00084                                         break;
00085                                 }   
00086                         }
00087                 }
00088         } COMCATCH_IN_TRANSACTION(;);
00089 }
00090 
00091 
00092 bool removemember::Do(CoreObj self, std::vector<CoreObj> *peers) {
00093         ObjForCore(CoreObj(self[ATTRID_XREF]))->SelfMark(OBJEVENT_SETEXCLUDED);
00094         ObjForCore(CoreObj(self[ATTRID_SETMEMBER]))->SelfMark(OBJEVENT_RELATION);
00095         ITERATE_THROUGH(self[ATTRID_MASTEROBJ+ATTRID_COLLECTION]) {
00096                 ITER[ATTRID_MASTEROBJ] = NULLCOREOBJ;
00097         }
00098         COMTHROW(self->Delete());
00099         return true;
00100 }
00101 
00102 HRESULT FCO::RemoveMember(IMgaFCO *member) {
00103         COMTRY_IN_TRANSACTION { 
00104                 CheckWrite();
00105                 CHECK_MYINPTRPAR(member);
00106                 bool f = false;
00107                 CoreObj mem(member);
00108                 CoreObjs members = self[ATTRID_SETMEMBER + ATTRID_COLLECTION];
00109                 ITERATE_THROUGH(members) {
00110                         if(COM_EQUAL(mem, CoreObj(ITER[ATTRID_XREF]))) {
00111                                 removemember().DoWithDeriveds(ITER);
00112                                 f = true;
00113                         }
00114                 }
00115                 if(!f) COMTHROW(E_MGA_OBJECT_NOT_MEMBER);
00116         } COMCATCH_IN_TRANSACTION(;);
00117 }
00118 
00119 HRESULT FCO::SetRemoveAll()
00120 {
00121         COMTRY_IN_TRANSACTION { 
00122                 CheckWrite();
00123                 CoreObjs members = self[ATTRID_SETMEMBER + ATTRID_COLLECTION];
00124                 ITERATE_THROUGH(members) {
00125                         removemember().DoWithDeriveds(ITER);
00126                 }
00127         } COMCATCH_IN_TRANSACTION(;);
00128 }
00129 
00130 
00131 HRESULT FCO::SetCompareToBase(IMgaFCO *a, short *status) { 
00132         COMTRY {
00133                 CheckRead();
00134                 CHECK_MYINPTRPARVALIDNULL(a);
00135                 CHECK_OUTPAR(status);
00136                 CoreObj base = self[ATTRID_DERIVED];
00137                 if(!base) COMTHROW(E_MGA_NOT_DERIVED);
00138                 CoreObjs bmembers = base[ATTRID_SETMEMBER + ATTRID_COLLECTION];
00139                 CoreObjs members = self[ATTRID_SETMEMBER + ATTRID_COLLECTION];
00140                 *status = 1;
00141                 //CoreObj ca(a);
00142                 ITERATE_THROUGH(members) {
00143                         if(!CoreObj(ITER[ATTRID_MASTEROBJ])) {
00144                                 /*if(!ca || COM_EQUAL(ca, CoreObj(ITER[ATTRID_XREF])))*/ break; //commented by ZolMol since always true
00145                         }
00146                 }
00147                 if( !ITER_BROKEN && members.Count() == bmembers.Count()) *status = 0; // by ZolMol: equal if & only if the self has only master objects and the # of the members is equal in the derived and the base set
00148         } COMCATCH(;);
00149 }
00150 
00151 HRESULT FCO::SetRevertToBase(IMgaFCO* a)  { 
00152         COMTRY_IN_TRANSACTION { 
00153                 CheckWrite();
00154 //              CHECK_MYINPTRPAR(a);
00155                 CoreObj base = self[ATTRID_DERIVED];
00156                 if(!base) COMTHROW(E_MGA_NOT_DERIVED);
00157                 COMTHROW(SetRemoveAll());       // simple solution: first delete everything, then re-create all.
00158                 CoreObjs bmembers = base[ATTRID_SETMEMBER + ATTRID_COLLECTION];
00159                 CoreObj rb;
00160                 int d;
00161                 GetRootOfDeriv(base, rb, &d);
00162                 CoreObj subt = self.FollowChain(ATTRID_FCOPARENT,d);
00163                 ITERATE_THROUGH(bmembers) {
00164                         CoreObj newmember;
00165                         std::vector<CoreObj> peers(2);
00166 //  if it is external in the most original base, the target must be kept the same
00167                         bool isinternal = IsInternalRelation(ITER);
00168                         if(isinternal)  GetDerivedEquivalent(ITER[ATTRID_XREF],subt,peers[0]);
00169                         else                    peers[0] = ITER[ATTRID_XREF];
00170                         peers[1] = ITER;
00171                         addmember(!isinternal).DoWithDeriveds(self, &peers);
00172                 }
00173                 
00174         } COMCATCH_IN_TRANSACTION(;);
00175 }
00176 
00177 // ----------------------------------------
00178 // get sets this object is member of (available for all FCO-s)
00179 // ----------------------------------------
00180 HRESULT FCO::get_MemberOfSets(IMgaFCOs **pVal) {
00181                 COMTRY {
00182                         CheckRead();
00183                         CHECK_OUTPTRPAR(pVal);
00184                         CoreObjs segs = self[ATTRID_XREF+ATTRID_COLLECTION];
00185                         CREATEEXCOLLECTION_FOR(MgaFCO, q);
00186                         ITERATE_THROUGH(segs) {
00187                                 if(GetMetaID(ITER) != DTID_SETNODE) continue;
00188                                 CoreObj fco = ITER[ATTRID_SETMEMBER];
00189                                 ASSERT(fco);
00190                                 CComPtr<IMgaFCO> ffco;
00191                                 ObjForCore(fco)->getinterface(&ffco);
00192                                 q->Add(ffco); 
00193                         }
00194                         *pVal = q.Detach();
00195                 } COMCATCH(;)
00196 }
00197 
00198