Los archivos de datos adjuntos son archivos que se guardan en una base de datos. Se puede añadir cualquier tipo de archivo a una base de datos utilizando Rational ClearQuest. Por ejemplo, puede guardar archivos de texto, proceso de texto, hojas de cálculo, imágenes y diagramas.
Cuando se guarda un archivo como parte de una entidad de solicitud de cambio (registro), también se puede añadir una descripción del archivo, a fin de simplificar posteriormente la identificación del archivo. También se almacenan otras informaciones como, por ejemplo, el nombre de archivo original, y la base de datos crea de modo automático un identificador exclusivo para el archivo.
Los archivos de datos adjuntos, como otros tipos de valores, se mantienen en un campo de la base de datos. El tipo de datos de un campo que mantiene archivos de datos adjuntos es AttachmentField. Puesto que, por lo general, los archivos de datos adjuntos se almacenan en grupos lógicos, como cuando se trata un defecto, un campo de archivo de datos adjuntos es una recopilación. Las instancias de la recopilación, cada una de las cuales mantiene un único archivo, son del tipo de datos Attachment.
Los métodos para buscar y acceder a los campos de archivo de datos adjuntos están disponibles en un objeto especial de tipo AttachmentFields. Cada entidad tiene siempre un objeto AttachmentFields, aunque no haya ningún archivo de datos adjuntos almacenado en la misma. Por tanto, en cada campo de archivo de datos adjuntos hay un objeto Attachments que permite gestionar objetos Attachment individuales.
Para otros tipos de campos, los valores de campo se obtienen por medio de un objeto FieldInfo y, a continuación, invocando GetValue() o GetValueAsList(). Para GetValue(), se devuelve una única serie. Si se invoca en un campo de archivo de datos adjuntos, GetValue() puede producir alguna cosa significativa si, por ejemplo, el archivo de datos adjuntos es un archivo de texto de una línea. No obstante, por lo general, la utilización de GetValue() en un campo de archivo de datos adjuntos no produce un resultado útil. En lugar de esto, para los archivos de datos adjuntos los valores se obtienen, generalmente, cruzando primero a un objeto Attachment y, a continuación escribiendo el archivo en disco y abriéndolo con un programa de aplicación.
Suponga que ha definido una entidad con dos campos de archivo de datos adjuntos. Al cruzar su estructura, itera en dos recopilaciones de AttachmentField. Para distinguir las recopilaciones, puede utilizar los nombres de campo de cada AttachmentField. Para identificar archivos de datos adjuntos individuales, puede utilizar las descripciones de cada instancia de Attachment.
El fragmento de código siguiente itera en todos los campos de archivo de datos adjuntos de un registro. Para cada uno de los campos de archivo de datos adjuntos, este código:
Para ilustrar que la descripción del archivo de datos adjuntos es una propiedad de lectura/escritura, el código también:
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 = "foo.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 ' una Session set MySession = GetSession() MySession.OutputDebugString & Msg & VbCrlf end sub REM End of Global Script DBGOUT
# 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 "foo.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