Object Templates
In Service Manager, we have a variety of scenarios where we needed to capture partial object state and apply that state at a later time. The state captured is usually all or part of some IT process. For example, when creating a Change Request from the UI, the user is prompted with a set of templates to choose from, each representing a different kind of change request. These templates are stored as Object Templates in MPs and pre-define a set of values to populate the change request form with.
Object templates can be used to store data about classes as well as projections. They are neither classes, nor objects, but rather blueprints (stored in management packs) for creating or modifying objects. In the attached MP you will find three different object templates.
- <ObjectTemplate ID="NFL.NFCDivision.Template" TypeID="NFL.Division" >
- <Property Path="$Target/Property[Type='NFL.Conference']/Name$">NFC</Property>
- </ObjectTemplate>
The first two set properties of a class. The Path notation is the same as used in projections, with the exception that you specify the particular property you want to set as well. You can specify as many or as few properties as you want, these objects don’t need to be complete, but we do verification to ensure property values make sense.
Projections can also be defined:
- <ObjectTemplate ID="NFL.New.Template" TypeID="NFL.Conference.All" >
- <Property Path="$Target/Property[Type='NFL.Conference']/Name$">New Conference</Property>
- <Object Path="$Target/Path[Relationship='NFL.ConferenceHostsDivision']$">
- <Property Path="$Target/Property[Type='NFL.Conference']/Name$">New Conference</Property>
- <Property Path="$Target/Property[Type='NFL.Division']/Name$">New Division 1</Property>
- <Object Path="$Target/Path[Relationship='NFL.DivisionContainsTeam' TypeConstraint='NFC.Team']$">
- <Property Path="$Target/Property[Type='NFC.Team']/Name$">New Team</Property>
- </Object>
- </Object>
- <Object Path="$Target/Path[Relationship='NFL.ConferenceHostsDivision']$">
- <Property Path="$Target/Property[Type='NFL.Conference']/Name$">New Conference</Property>
- <Property Path="$Target/Property[Type='NFL.Division']/Name$">New Division 2</Property>
- </Object>
- </ObjectTemplate>
These are similar, but allow you to define a hierarchy of objects that conform to the projection definition the object template references. Whenever you want to define a new object, you need to ensure the endpoint of the relationship is concrete, or you need to specify the TypeConstraint to indicate what class you want to create, as in the example above.
In code, you can use these templates to create new objects or update existing objects:
// Connect to the management group
EnterpriseManagementGroup managementGroup =
new EnterpriseManagementGroup("localhost");
// Get the management pack
ManagementPack nflManagementPack =
managementGroup.ManagementPacks.GetManagementPack("NFL", null, new Version("1.0.0.0"));
// Get the object templates
ManagementPackObjectTemplate nfcDivisionTemplate =
nflManagementPack.GetObjectTemplate("NFL.NFCDivision.Template");
ManagementPackObjectTemplate afcDivisionTemplate =
nflManagementPack.GetObjectTemplate("NFL.AFCDivision.Template");
ManagementPackObjectTemplate nflTemplate =
nflManagementPack.GetObjectTemplate("NFL.New.Template");
// Create a new NFC division using the template
CreatableEnterpriseManagementObject newNfcDivision =
new CreatableEnterpriseManagementObject(managementGroup, nfcDivisionTemplate);
// Change it to an AFC division
newNfcDivision.ApplyTemplate(afcDivisionTemplate);
// Create a new projection from a template
EnterpriseManagementObjectProjection newNfl =
new EnterpriseManagementObjectProjection(managementGroup, nflTemplate);
// You can also apply a template to an existing projection
newNfl.ApplyTemplate(nflTemplate);