Creating, Reading and Modifying Objects and Collections
This section shows example code to create applications that create, read, and modify objects and collections of your model in the MDS repository.
The following examples use the CMetaObject class and create new objects using CMetaObject::WriteObject.
//////////////////////////////////////////////////////
// LoadAutoData functions
//
// Writes new AutoMaker and Auto objects
//////////////////////////////////////////////////////
HRESULT LoadAutoData(CMetaRepository &repos)
{
HRESULT result = S_OK; // return value
OBJECTID_t
ClassIDAutoCorp = NULLLOID,
ClassIDAutoDiv = NULLLOID,
ClassIDAuto = NULLLOID,
loidBigGuy = NULLLOID,
loidAmericanAutos = NULLLOID,
loidTortoise = NULLLOID;
CMetaObject obj;
CMetaProperty PropObj;
// Get the ClassIDs of the AutoManuf and Auto Classes
result = repos.GetObjectID(CLSGUID_AutoCorp, &ClassIDAutoCorp);
if (SUCCEEDED(result))
result = repos.GetObjectID(CLSGUID_AutoDiv, &ClassIDAutoDiv);
if (SUCCEEDED(result))
result = repos.GetObjectID(CLSGUID_Auto, &ClassIDAuto);
if (SUCCEEDED(result))
{
result = WriteNewObject(ClassIDAutoCorp,
_T("BigGuy Motors"), OBJGUID_BigGuyMotors, _T("A large American manufacturer"),
0, loidBigGuy);
}
if (SUCCEEDED(result))
{
result = WriteNewObject(ClassIDAutoDiv, _T("AmericanAutos"),
OBJGUID_AmericanAutos,
_T("A fictitous automaker based in the United States. This automaker likes naming their cars after animals."),
0, loidAmericanAutos);
}
if (SUCCEEDED(result))
{
unsigned long carId = 0x1004abef;
result = WriteNewAuto(ClassIDAuto,_T("Tortoise"),
OBJGUID_Tortoise,
_T("American Autos' Tortoise. A reliable sedan."), carId, 18545,
_T("1997-07-01"), 16.0, 55.1, 3350,
_T("Mid-priced Cars"), _T("AA's best-selling auto for those over 80."),
loidTortoise);
}
return(result)
}
////////////////////////////////////////////////////////////////////
// WriteNewObject function
//
// Writes a new object for a class that has only common properties.
////////////////////////////////////////////////////////////////////
HRESULT WriteNewObject(OBJECTID_t lclassid, TCHAR *name,
const GUID &goid,
TCHAR *description,
const OBJECTID_t lOwnerid,
OBJECTID_t &assignedLoid)
{
CMetaObject obj; // new object to write
HRESULT result = S_OK; // return value
// Set the object common properties
obj.SetClassID(lclassid);
obj.SetObjectName(name);
obj.SetObjectGUID(goid);
obj.SetDescription(description);
if (lOwnerid)
{
// If OwnerID is not set, the owner will be set to the
// initialized user
obj.SetOwnerID(lOwnerid);
}
// Write the new object
if (SUCCEEDED(result = obj.WriteObject()))
{
// Get the ObjectID of the new object
assignedLoid = obj.GetObjectID();
}
return (result);
} // WriteNewObject
///////////////////////////////////////////////////////////////
// WriteNewAuto function
//
// Write an Auto class object. This class has eight
// unique properties.
///////////////////////////////////////////////////////////////
HRESULT WriteNewAuto(OBJECTID_t lclassid, TCHAR *name,
const GUID &goid, TCHAR *description,
unsigned long carId, long basePrice,
TCHAR *designDate, double fuelTank,
double height, double weight,
TCHAR *vehicleType, TCHAR *review,
OBJECTID_t &assignedLoid)
{
CMetaObject obj;
HRESULT result = S_OK;
CMetaProperty PropObj;
// Set common properties
obj.SetClassID(lclassid);
obj.SetObjectName(name);
obj.SetObjectGUID(goid);
obj.SetDescription(description);
// Set Unique Class Properties
// set CarId
result = PropObj.SetBinary(carId);
obj.SetPropertyValue(PID_AUTO_CARID, PropObj);
// set base price
PropObj.SetShort(basePrice);
obj.SetPropertyValue(PID_AUTO_BASEPRICE, PropObj);
// set design date
PropObj.SetString(designDate);
obj.SetPropertyValue(PID_AUTO_DESIGNDATE, PropObj);
// set fuel tank
PropObj.SetDbl(fuelTank);
obj.SetPropertyValue(PID_AUTO_FUELTANK, PropObj);
// set height
PropObj.SetDbl(height);
obj.SetPropertyValue(PID_AUTO_HEIGHT, PropObj);
// set weight
PropObj.SetInt(weight);
obj.SetPropertyValue(PID_AUTO_WEIGHT, PropObj);
// set vehicletype
PropObj.SetString(vehicleType);
obj.SetPropertyValue(PID_AUTO_VEHICLETYPE, PropObj);
// set review
PropObj.SetString(review);
obj.SetPropertyValue(PID_AUTO_REVIEW, PropObj);
// Call WriteObject
if (SUCCEEDED(result = obj.WriteObject()))
{
// Get the new Object ID
assignedLoid = obj.GetObjectID();
}
return (result);
} // WriteNewAuto