Managing records (entities) that are stateless and stateful
Your schema has stateless records, such as the Project, and stated records, such as Defect, which move from state to state. The DevOps Plan Schema API enables you to get and set field values for both kinds of records.
The example shown in this section is
an external application example that contains two subroutines: No_state for
stateless records, and Has_state for records that
have states. The example does the following:
- Uses the Session's
BuildEntitymethod to create an Entity Object. - Set the values in one or more fields.
- Validates and commits the entity.
- Retrieves and modifies the entity.
- Reverts the entity.
The code invokes some external routines that are not shown here:
Perl
sub No_state {
my($session) = @_;
my($entity);
my($failure);
print "Test for stateless entities is starting";
print "submit a stateless entity";
$entity = $session->BuildEntity("project");
# ignore failure
$failure = $entity->SetFieldValue("name", "initial project
name");
DumpFields($entity);
$entity->Validate();
$entity->Commit();
$entity = "";
print "Reload, show values before modification";
$entity = $session->GetEntity("project", "initial project name");
DumpFields($entity);
print "Modify, then show new values";
$session->EditEntity($entity, "modify");
# ignore the failure
$failure = $entity->SetFieldValue("name", "modified project name");
DumpFields($entity);
print "revert, then show restored values";
$entity->Revert();
DumpFields($entity);
print "Modify again, and commit";
$session->EditEntity($entity, "modify");
# ignore failure
$failure = $entity->SetFieldValue("name", "final project name");
$entity->Validate();
$entity->Commit();
$entity = "";
print "Reload, and show final result";
$entity = $session->GetEntity("project", "final project name");
DumpFields($entity);
$entity = "";
print "Test for stateless entities is done";
}
Perl
The following is an example of testing for stateful entities:
sub Has_states {
my($session) = @_;
my($entity); # the entity that is stateful
# failure message from functions that return strings
my($failure);
my($id);
# DevOps Plan defect database ID
print "Test for stateful entities is starting";
print "submit a stateful entity";
$entity = $session->BuildEntity("defect");
# ignore failures
$failure = $entity->SetFieldValue("headline", "man bites dog!");
$failure = $entity->SetFieldValue("project", "final project name");
$failure = $entity->SetFieldValue("submit_date", "03/18/2000 10:09:08");
$id = $entity->GetDbId();
open(FILE, ">>XXStdout");
print FILE, "Entity id is", $id, "\n";
close FILE;
DumpFields($entity);
$entity->Validate();
$entity->Commit();
$entity = "";
print "Reload, show values before modification";
$entity = $session->GetEntityByDbId("defect", $id);
DumpFields($entity);
print "Modify then show new values";
$session->EditEntity($entity, "modify");
# ignore failure
$failure = $entity->SetFieldValue("headline", "man bites tree!");
DumpFields($entity);
print "revert, then show restored values";
$entity->Revert();
DumpFields($entity);
print "Modify again and commit";
$session->EditEntity($entity, "modify");
# ignore failure
$failure = $entity->SetFieldValue("headline", "tree bites man!");
$entity->Validate();
$entity->Commit();
$entity = "";
print "Reload and show before changing state";
$entity = $session->GetEntityByDbId("defect", $id);
DumpFields($entity);
print "Change to new state, then show new values";
$session->EditEntity($entity, "close");
$failure = $entity->SetFieldValue("description",
"looked like an oak tree");
# ignore failure
DumpFields($entity);
print "revert then show restored values";
$entity->Revert();
DumpFields($entity);
print "Change to new state again then commit";
$session->EditEntity($entity, "close");
$failure = $entity->SetFieldValue("description",
"man of steel, tree of maple");
# ignore failure
$entity->Validate();
$entity->Commit();
$entity = "";
print "Reload, show final values";
$entity = $session->GetEntityByDbId("defect", $id);
DumpFields($entity);
$entity = "";
print "Test of stateful entities is done";
}