Security caveats for a record

Question of the day - Find all security caveats for a record.

HPRM has an expansive SDK and often when I am asked a question I have to do some elementary research to find the answer, that is one excuse although maybe I am just getting forgetful. So I don't forget the process as well as the answer today in addition to answering the question I will step through my thought process.

The thought process

If I do not immediately know the answer I go through a process a little like this:

  1. Everything (almost) in the SDK is a TrimMainObject, TrimChildObject, or property of one of these.  I start typing in Visual Studio, up pops SecurityCaveat, a TrimMainObject.
  2. So, which mechanism relates it to a Record?
    1. Child list? No there is no relevant child list on the record object.
    2. Is there a search clause?  No, you can search for records by caveat but not caveats by record.
    3. Maybe there is some special mechanism?
  3. Time to browse through the record properties, I spot Record.Security, but that is a string property, close but maybe not quite.
  4. Aha... Record.SecurityProfile, looks interesting. Does it contain a list of caveats? No, it has AddCaveat, RemoveCaveat but, wait in, here is something nearly as good...

The code

This will display a comma separated string containing the security level and names of all caveats.

Record rec = new Record(database, 9000000221);
Console.WriteLine(rec.Security);

We could take this list and find all caveats with the corresponding names.

Record rec = new Record(database, 9000000221);
foreach (string caveatName 
    in rec.Security.Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries))
{
    var caveat = database.FindTrimObjectByName(BaseObjectTypes.SecurityCaveat, caveatName) as SecurityCaveat;
    if (caveat != null)
    {
        Console.WriteLine(caveat.Name);
    }
}

Or better still (depending on how many caveats there are) we could loop through all caveats and find the ones we want.

Record rec = new Record(database, 9000000221);
TrimMainObjectSearch caveatSearch = new TrimMainObjectSearch(database, BaseObjectTypes.SecurityCaveat);
caveatSearch.SelectAll();
foreach (SecurityCaveat caveat in caveatSearch)
{
    if (rec.SecurityProfile.IsCaveatOn(caveat))
    {
        Console.WriteLine(caveat.Name);
    }
}