BuildEntity 메소드는 주어진 사용자 데이터베이스에 대해 고유 ID를 가진 새 Entity 오브젝트를 작성하고 레코드에 대해 제출 조치를 시작합니다. 제출 조치 중에 Entity 오브젝트의 기본값을 편집하기 위해 레코드를 사용할 수 있습니다.
BuildEntity를 사용하여 Entity 오브젝트를 작성하고 아직 데이터베이스에 확약하지 않은 경우에는 오브젝트를 편집할 수 있습니다.
BuildEntity 메소드는 지정된 유형의 새 레코드를 작성하고 제출 조치를 시작하므로 사용자는 레코드 내용 편집을 시작할 수 있습니다. (EditEntity를 호출하지 않아도 레코드를 편집 가능 상태로 만들 수 있습니다.) 리턴된 Entity 오브젝트의 SetFieldValue 메소드를 사용하여 새 레코드의 필드에 값을 지정할 수 있습니다. 레코드 업데이트를 완료한 경우, Entity 오브젝트의 Validate 및 Commit 메소드를 사용하여 레코드에 대한 변경사항을 각각 유효성 검증하고 확약합니다. entitydef_name 매개변수에 지정하는 이름은 스키마의 적절한 레코드 유형에도 해당되어야 합니다. entitydef_name에 적합한 이름 목록을 얻으려면 GetSubmitEntityDefNames 메소드를 사용하십시오.
$session->BuildEntity(entity_def_name);
# Build Session object...
# Create a new "defect" record
$entityobj = $sessionobj->BuildEntity("defect");
BuildEntity 메소드를 호출한 후에는 이 학습서의 이전 단원에 설명된 대로 Entity 오브젝트의 메소드를 호출하여 해당 레코드에서 필드 값을 설정할 수 있습니다.
레코드를 새로 작성하려면 다음을 수행하십시오.
다음 예는 Stateful 및 Stateless 레코드 유형 둘 다에 대해 작업하는 방법을 보여줍니다.
스키마에 Project와 같은 Stateless 레코드와, 상태가 변경되는 Defect와 같은 Stateful 레코드가 있습니다. Rational ClearQuest API를 사용하면 두 레코드 종류 모두의 필드 값을 가져오고 설정할 수 있습니다. 다음 예제에는 Stateless 레코드에 대한 No_state와 상태가 있는 레코드에 대한 Has_state의 두 서브루틴이 포함되어 있습니다.
sub myValidateCommit( $entity ){
$status = $entity->Validate;
if ( $status ) {
$entity->Revert;
# for simplicity, we die on any error, you should do what makes sense in your
# application if recovery is possible.
die "Error validating: $status \n";
}
else {
$entity->Commit;
}
}
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);
myValidateCommit( $entity );
$entity = "";
print "Reload, show values before modification";
# Note that the Entity->Reload method can be used to force a refresh of the entity from the database.
$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");
myValidateCommit( $entity );
$entity = "";
print "Reload, and show final result";
$entity = $session->GetEntity("project", "final project name");
DumpFields($entity);
$entity = "";
print "Test for stateless entities is done";
}
sub Has_states {
my($session) = @_;
my($entity);
# the entity that is stateful
# failure message from functions that return strings
my($failure);
my($id);
# Rational ClearQuest 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);
myValidateCommit( $entity );
$entity = "";
print "Reload, show values before modification";
# Note that the Entity->Reload method can be used to force a refresh of the entity from the database.
$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!");
myValidateCommit( $entity );
$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
myValidateCommit( $entity );
$entity = "";
print "Reload, show final values";
$entity = $session->GetEntityByDbId("defect", $id);
DumpFields($entity);
$entity = "";
print "Test of stateful entities is done";
}