添付ファイル情報の取得と設定

添付ファイルは、データベースに保管されたファイルです。Rational® ClearQuest® を使用して、任意の種類のファイルをデータベースに追加できます。例えば、テキスト、ワード プロセッサ、スプレッドシート、イメージ、ダイアグラムの各ファイルを保存できます。

変更依頼エンティティ (レコード) の一部としてファイルを保存する場合、ファイルの説明も追加できます。説明により、後でファイルの識別が容易になります。オリジナルのファイル名などのその他の情報も保存され、データベースによってそのファイルの固有の識別子が自動的に作成されます。

添付ファイルは、他のタイプの値のように、データベース フィールドに保持されます。添付ファイルを保持するフィールドのデータ タイプは、AttachmentField です。添付ファイルは通常、論理グループに格納されるので、障害が論議される場合のように、添付ファイル フィールドはコレクションです。コレクションのインスタンス (それぞれに単一ファイルを保持) のデータ タイプは Attachment です。

添付ファイル フィールドを検索してアクセスするメソッドは、AttachmentFields タイプの特殊なオブジェクトで使用できます。各 Entity には常に AttachmentFields オブジェクトがあります。添付ファイルが実際に格納されていなくてもあります。さらに、各添付ファイル フィールドには、個々の Attachment オブジェクトを管理できる Attachments オブジェクトがあります。

他の種類のフィールドの場合、FieldInfo オブジェクトを取得してフィールド値を取得し、その後 GetValue() または GetValueAsList() を起動します。GetValue() の場合は、単一文字列が戻されます。GetValue() は、添付ファイル フィールドで起動される場合に、例えば、添付ファイルが 1 行のテキスト ファイルのときは、何か意味のあるものを作成することがあります。しかし、一般的に、添付ファイル フィールドで GetValue() を使用すると、意味のある結果は作成されません。添付ファイルの場合は代わりに、通常はまず Attachment オブジェクトに移動して値を取得し、その後ファイルをディスクに書き込んでアプリケーション プログラムで開きます。

2 つの添付ファイル フィールドで Entity を定義していると想定します。この構造内をトラバースする場合、2 つの AttachmentField コレクションを繰り返します。コレクションを区別するために、各 AttachmentField のフィールド名を使用できます。個々の添付ファイルを識別するために、Attachment の各インスタンスで説明を使用できます。

次のコードは、レコードのすべての添付ファイル フィールドについて繰り返します。各添付ファイル フィールドについて、次のコードを作成します。

添付ファイルの説明は読み取り/書き込みプロパティであることを示すために、次のコードも作成します。

注: 次のコードはフックであり、したがって Session オブジェクトを取得します。しかし、セッション オブジェクトを手動で作成して、データベースにログオンする場合は、外部アプリケーションにこのコードを組み込むこともできます。

VBScript

REM Start of Global Script ShowAttachmentInfo

Sub ShowAttachmentInfo(actionname, hookname)

  DBGOUT "Entering '" & actionname & "' action's " & hookname & "_
                       script (VB version)"

  DIM MyAttachmentFields ' The list of attachment fields

  DIM MyAttachmentField  ' An attachment field (contains a list of
                          'attachments)

  DIM MyAttachment       ' An Attachment object 



  ' Tell how many attachment fields there are and show their 
  ' names...

  M = "This entity contains " & AttachmentFields.Count & "_
                                  attachment field(s)" & VBCrLf

  For Each MyAttachmentField in AttachmentFields

     M = M & "    " & MyAttachmentField.Fieldname & VBCrLf

   Next

   DBGOUT M



  ' Iterate over the attachment fields; for each one, list the 
  ' attachments it contains in the current record...

  For Each MyAttachmentField in AttachmentFields

     M = "Attachment field '" & MyAttachmentField.Fieldname & "'_
                  contains:" & VBCrLf

     ' Iterate over the attachments in this field...

     AtCount = 0

     For Each MyAttachment in MyAttachmentField.Attachments

       AtCount = AtCount + 1

       ' Demonstrate how to set an attachment's description...

       If (Len(MyAttachment.Description) = 0 or _
                  MyAttachment.Description = " ")

       Then

         ' DBGOUT "Description before: '" & _
            MyAttachment.Description & "'"

            MyAttachment.Description = "Not very descriptive!"

         ' DBGOUT "Description after: '" & _
                  MyAttachment.Description & "'"

       End If



       ' Demonstrate how to write out the attachment's contents
       ' to an external file...

       If (MyAttachment.Filename = "test.doc") Then

          F = "C:¥TEMP¥" & GetDisplayName() & "_" & _
                     MyAttachment.FileName

           MyAttachment.Load F

          DBGOUT "Attachment " & MyAttachment.FileName & " was _
                     written to " & F

       End If



       ' Report info about this attachment...

       M = M & "Filename='" & MyAttachment.FileName & "'"  & _

                " FileSize=" & MyAttachment.FileSize & _
                " Description='" & MyAttachment.Description & "'"_
                 & VBCrLf

     Next

     M = M & "Total attachments: " & AtCount

     DBGOUT M

  Next

  DBGOUT "Exiting '" & actionname & "' action's " & hookname & _
          " script (VB version)"

End Sub

REM End of Global Script ShowAttachmentInfo

REM Start of Global Script DBGOUT

sub DBGOUT(Msg)

   Dim MySession ' a Session

   set MySession = GetSession()

   MySession.OutputDebugString & Msg & VbCrlf

end sub

REM End of Global Script DBGOUT 

Perl

# Start of Global Script ShowAttachmentInfo

# ShowAttachmentInfo() -- Display information about
# attachments...

sub ShowAttachmentInfo {

  # $actionname as string

  # $hookname as string

  my($actionname, $hookname) = @_;

  my($M) = "Entering '".$actionname."' action's ".$hookname."
    script (Perl version)¥n¥n";

  # DBGOUT($M); $M="";



  # Get a list of the attachment fields in this record type...

  my($AttachmentFields) = $entity->GetAttachmentFields();



  # Tell how many attachment fields there are and show their
  # names...

  $M = $M . "This entity contains " . $AttachmentFields->Count() .
      " attachment field(s)¥n";

  for ($A = 0; $A < $AttachmentFields->Count(); $A++) 

  {

    $M = $M . "    " . ($AttachmentFields->Item($A) )->GetFieldName() . "¥n";

  }

  $M .= "¥n";



  # Iterate over the attachment fields; for each one, list the 

  # attachments it contains in the current record...

  for (my($AF) = 0; $AF < $AttachmentFields->Count(); $AF++) {

    my ($AttachmentField) = $AttachmentFields->Item($AF);

    $M = $M ."Attachment field '"
          . $AttachmentField->GetFieldName().
           "' contains:¥n";

       # Iterate over the attachments in this field...

       my($Attachments) = $AttachmentField->GetAttachments();

    for (my($A) = 0; $A < $Attachments->Count(); $A++) {

      my($Attachment) = $Attachments->Item($A);

      # Demonstrate how to set an attachment's description...

      if ($Attachment->GetDescription() eq " ") {

        # DBGOUT("Description before:
                         '".$Attachment->GetDescription()."'");

         $Attachment->SetDescription("Not too descriptive!");

         # DBGOUT("Description after:
                         '".$Attachment->GetDescription()."'");

      }

      # Demonstrate how to write out the attachment's contents

      # to an external file...

      if ($Attachment->GetFileName() eq "test.doc") {

        my($F) = "C:¥¥TEMP¥¥" . $entity->GetDisplayName()

                            . '_' . $Attachment->GetFileName();

        $Attachment->Load($F);

        DBGOUT("Attachment written to $F

      }

      # Report info about this attachment...

      $M = $M .

      "    Filename='" . $Attachment->GetFileName() . "'" .

      " FileSize=" . $Attachment->GetFileSize() . 

      " Description='" . $Attachment->GetDescription() . "'" .

      "¥n";

    }

    $M = $M . "Total attachments: " . $Attachments->Count() . 
               "¥n¥n";

  }

  # Display the results...

  DBGOUT($M); $M="";

}

# End of Global Script ShowAttachmentInfo



# Start of Global Script DBGOUT

sub DBGOUT {

   my($Msg) = shift;

   my($FN) = $ENV{'TEMP'}.'¥STDOUT.txt';

   open(DBG, ">>$FN") || die "Failed to open $FN";

   print DBG ($Msg);

   close(DBG);

   system("notepad $FN");

   system("del $FN");

}

# End of Global Script DBGOUT 

フィードバック