Gli allegati sono file salvati in un database. È possibile aggiungere qualsiasi tipo di file a un database utilizzando Rational ClearQuest. Ad esempio è possibile salvare diversi tipi di file, ad esempio, testo, elaborazione testo, fogli di lavoro, immagini e diagrammi.
Quando un file viene salvato come parte di un'entità di richiesta di modifica (record), è inoltre possibile aggiungere una descrizione del file che ne faciliterà l'identificazione in un secondo momento. Saranno memorizzate anche altre informazioni, come il nome originale del file, e il database creerà automaticamente un identificativo univoco per il file.
Gli allegati, come altri tipi di valori, sono conservati in un campo database. AttachmentField è il tipo di dati di un campo contenente allegati. Poiché di norma gli allegati vengono memorizzati in gruppi logici, come quando si discute un difetto, un campo allegato è una raccolta. Le istanze della raccolta, ognuna delle quali contiene un singolo file, sono dati di tipo Allegato.
I metodi per individuare e accedere ai campi allegato sono disponibili in un oggetto specifico di tipo AttachmentFields. Ogni Entità dispone sempre di un oggetto AttachmentFields, anche se non vi sono allegati memorizzati. In ogni campo allegato è presente un oggetto Attachments che consente di gestire singoli oggetti Attachment.
Per altri tipi di campi, si ottengono valori campo acquisendo un oggetto FieldInfo e richiamando GetValue() o GetValueAsList(). Per GetValue(), sarà restituita una singola stringa. Se richiamato su un campo allegato, GetValue() potrebbe generare un risultato significativo se l'allegato è un file di testo su una sola riga. Tuttavia, di norma, l'utilizzo di GetValue() su un campo allegato non genera un risultato utile. Invece, per gli allegati, di norma si acquisiscono valori passando prima a un oggetto Attachment e poi scrivendo il file nel disco e aprendolo utilizzando un programma applicativo.
Supporre di aver definito un'Entitàcon due campi allegato. Durante lo spostamento in questa struttura, l'utente si ripete in due raccolte AttachmentField. Per distinguere le raccolte, è possibile utilizzare i nomi di campo di ogni AttachmentField. Per identificare file allegato individuali, è possibile utilizzare le descrizioni presenti in ogni istanza di Attachment.
Il seguente frammento di codice si ripete in tutti i campi allegato di un record. Per ogni campo allegato, questo codice:
Per mostrare che la descrizione dell'allegato è una proprietà di lettura/scrittura, il codice inoltre:
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
# 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