Wednesday, December 16, 2009

ClearQuest Hook (Perl) to Limit the Size of Attachement

The VB version explains the logic and can be found here: http://www.ibm.com/developerworks/rational/library/3883.html

In ALM, three types of records have the attachement, ALMRequest, ALMTask and ALMActivity. A base action validation hook needs to be created for each of these record types.

sub ALMRequest_Validation {
    my($actionname, $actiontype) = @_;
    my $result;
    # $actionname as string scalar
    # $actiontype as long scalar
    # $result as string scalar
    # action is AttachmentzSizeLimit
    # record type name is ALMRequest
    # Start User Code
    # Return a non-empty string explaining why the action cannot commit
    # with its current values. If it is valid, return an empty string.
    # Example:
    #    my $value_info = $entity->GetFieldValue("some field");
    #    if (length($value_info->GetValue()) < 10) {
    #        $result = "Must be at least 10 chars long";
    #    }

    my ($attachmentFields, $attachmentField, $attachments, $attachment, $numfield, $numattach);
    my ($af, $a, $filename, $filesize);

eval {
    $attachmentFields = $entity->GetAttachmentFields();
    $numfields = $attachmentFields->Count();
    $session->OutputDebugString("numfields= $numfields\n");
}; if ($@) {  $session->OutputDebugString("GetAttachmentFields failed: $@\n");}

eval {
    for ($af = 0; $af < $numfields; $af++) {
     $attachmentField = $attachmentFields->Item($af);
     $attachments = $attachmentField->GetAttachments();
     $numattach = $attachments->Count();
     $session->OutputDebugString("numattach is: $numattach\n");

     for ($a = 0; $a < $numattach; $a++ ) {
     $attachment = $attachments->Item($a);
     $filename = $attachment->GetFileName();
     $filesize = $attachment->GetFileSize();
     # file size is always 0 before a first commit
     if ($filesize == 0) {
     #If it returns 0, we know that the file has been added during the current action, and is still on the local drive, so we check the FileSystemObject's filesize
     $filesize = -s $filename;
     }
     $session->OutputDebugString("filename=" . $filename . "filesize=" . $filesize . "\n");
     if ($filesize > 15000000) {
     $result = "$filename is too big, the limited size is 15M."
     }
     }
    }
}; if ($@) {  $session->OutputDebugString("GetAttachment failed: $@\n");}

    # End User Code
    return $result;
}




No comments:

Post a Comment