< 이전 | 다음 >

새 레코드 작성

레코드를 새로 작성하려면 Session 오브젝트의 BuildEntity 메소드를 사용합니다.

BuildEntity 메소드는 주어진 사용자 데이터베이스에 대해 고유 ID를 가진 새 Entity 오브젝트를 작성하고 레코드에 대해 제출 조치를 시작합니다. 제출 조치 중에 Entity 오브젝트의 기본값을 편집하기 위해 레코드를 사용할 수 있습니다.

BuildEntity를 사용하여 Entity 오브젝트를 작성하고 아직 데이터베이스에 확약하지 않은 경우에는 오브젝트를 편집할 수 있습니다.

BuildEntity 메소드는 지정된 유형의 새 레코드를 작성하고 제출 조치를 시작하므로 사용자는 레코드 내용 편집을 시작할 수 있습니다. (EditEntity를 호출하지 않아도 레코드를 편집 가능 상태로 만들 수 있습니다.) 리턴된 Entity 오브젝트의 SetFieldValue 메소드를 사용하여 새 레코드의 필드에 값을 지정할 수 있습니다. 레코드 업데이트를 완료한 경우, Entity 오브젝트의 ValidateCommit 메소드를 사용하여 레코드에 대한 변경사항을 각각 유효성 검증하고 확약합니다. entitydef_name 매개변수에 지정하는 이름은 스키마의 적절한 레코드 유형에도 해당되어야 합니다. entitydef_name에 적합한 이름 목록을 얻으려면 GetSubmitEntityDefNames 메소드를 사용하십시오.

BuildEntity의 구문은 다음과 같습니다.
$session->BuildEntity(entity_def_name); 
  • session - 현재 database-access 세션을 나타내는 Session 오브젝트입니다.
  • entity_def_name - 레코드를 작성할 때 템플리트로 사용할 EntityDef 오브젝트의 이름을 포함하는 문자열.
리턴값은 템플리트로 이름 지정된 EntityDef 오브젝트를 사용하여 빌드된 새 Entity 오브젝트입니다.
예를 들어, 다음과 같습니다.
# Build Session object...

# Create a new "defect" record 
$entityobj = $sessionobj->BuildEntity("defect"); 

BuildEntity 메소드를 호출한 후에는 이 학습서의 이전 단원에 설명된 대로 Entity 오브젝트의 메소드를 호출하여 해당 레코드에서 필드 값을 설정할 수 있습니다.

레코드를 새로 작성하려면 다음을 수행하십시오.

  1. Session 오브젝트의 GetSubmitEntityDefNames 메소드를 호출하여 작성할 수 있는 레코드 유형을 판별하십시오.
  2. Session 오브젝트의 BuildEntity 메소드를 호출하십시오.
  3. Entity 오브젝트의 메소드를 사용하여 레코드에서 필드의 값을 설정하십시오.
  4. 레코드 편집을 완료한 경우에는 Entity 오브젝트의 ValidateCommit 메소드를 각각 호출하여 유효성을 검증하고 변경사항을 데이터베이스로 확약하십시오.

예제

다음 예는 Stateful 및 Stateless 레코드 유형 둘 다에 대해 작업하는 방법을 보여줍니다.

스키마에 Project와 같은 Stateless 레코드와, 상태가 변경되는 Defect와 같은 Stateful 레코드가 있습니다. Rational ClearQuest API를 사용하면 두 레코드 종류 모두의 필드 값을 가져오고 설정할 수 있습니다. 다음 예제에는 Stateless 레코드에 대한 No_state와 상태가 있는 레코드에 대한 Has_state의 두 서브루틴이 포함되어 있습니다.

코드 예제는 다음과 같습니다.
  1. Session의 BuildEntity 메소드를 사용하여 Entity 오브젝트를 작성합니다.
  2. 하나 이상의 필드에 값을 설정합니다.
  3. 엔티티를 유효성 검증하고 확약합니다.
  4. 엔티티를 검색하고 수정합니다.
  5. 엔티티를 되돌립니다.
이 두 가지 서브루틴의 코드는 여기에 표시되지 않은 두 개의 외부 루틴을 호출합니다. 하나는 엔티티의 필드를 표준 출력으로 인쇄하는 DumpFields이고, 다른 하나는 먼저 변경사항의 유효성을 검증하고 유효성 검정 성공 후 변경사항을 확약하거나 유효성 검증 실패 시 되돌리는 myValidateCommit입니다. 다음은 myValidateCommit에 사용될 수 있는 코드입니다.
 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";
} 
다음은 Stateful 엔티티에 대한 테스트의 예제입니다.
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";
 }
< 이전 | 다음 >

피드백