Exemple #1
0
        public ClearQuestAttachmentItem(
            OAdEntity aHostRecord,
            OAdAttachmentField aHostField,
            OAdAttachment aAttachment,
            ClearQuestConnectionConfig connectionConfiguration)
        {
            // gather info to query for the record
            EntityDefName  = CQWrapper.GetEntityDefName(aHostRecord);
            EntityDispName = CQWrapper.GetEntityDisplayName(aHostRecord);

            // gather info to query for attachment field
            FieldName = CQWrapper.GetAttachmentFieldName(aHostField);

            string name;
            string comment;
            string dispName;
            int    fileSize;

            CQWrapper.GetAttachmentMetadata(aAttachment,
                                            out name,
                                            out comment,
                                            out dispName,
                                            out fileSize);

            Name                    = name;
            Comment                 = comment;
            DisplayName             = dispName;
            Length                  = (long)fileSize; // fileSize returned by CQ API is in bytes
            ConnectionConfiguration = connectionConfiguration;
        }
        private bool AttachmentExists(
            OAdEntity entity,
            string attName,
            string attComment,
            string lengthStr)
        {
            OAdAttachmentFields aAttachmentFields = CQWrapper.GetAttachmentFields(entity);

            for (int aAttachmentFieldsIndex = 0;
                 aAttachmentFieldsIndex < CQWrapper.AttachmentsFieldsCount(aAttachmentFields);
                 aAttachmentFieldsIndex++)
            {
                object             ob = (object)aAttachmentFieldsIndex;
                OAdAttachmentField aAttachmentField = CQWrapper.AttachmentsFieldsItem(aAttachmentFields, ref ob);

                // process all attachments
                OAdAttachments attachments = CQWrapper.GetAttachments(aAttachmentField);
                for (int attachmentIndex = 0;
                     attachmentIndex < CQWrapper.AttachmentsCount(attachments);
                     attachmentIndex++)
                {
                    object        obIndex     = (object)attachmentIndex;
                    OAdAttachment aAttachment = CQWrapper.AttachmentsItem(attachments, ref obIndex);

                    string name;
                    string comment;
                    string dispName;
                    int    fileSize;
                    CQWrapper.GetAttachmentMetadata(aAttachment,
                                                    out name,
                                                    out comment,
                                                    out dispName,
                                                    out fileSize);

                    if (attName.Equals(name) && attComment.Equals(comment) && long.Parse(lengthStr).Equals((long)fileSize))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Exemple #3
0
        public void Download(string localPath)
        {
            byte[] metadataHash = HashAttachmentMetadata(Name, Comment, DisplayName, Length);

            if (null == CQSession)
            {
                throw new InvalidOperationException("CQSession == NULL");
            }

            // [teyang] TODO: validation on localPath
            if (File.Exists(localPath))
            {
                File.Delete(localPath);
            }

            OAdEntity aRecord = CQWrapper.GetEntity(CQSession, EntityDefName, EntityDispName);

            OAdAttachmentFields aAllAttFields = CQWrapper.GetAttachmentFields(aRecord);

            bool attachmentFound = false;

            for (int attFieldsIndex = 0;
                 attFieldsIndex < CQWrapper.AttachmentsFieldsCount(aAllAttFields);
                 attFieldsIndex++)
            {
                object             ob        = (object)attFieldsIndex;
                OAdAttachmentField aAttField = CQWrapper.AttachmentsFieldsItem(aAllAttFields, ref ob);

                string fieldName = CQWrapper.GetAttachmentFieldName(aAttField);

                if (!CQStringComparer.FieldName.Equals(fieldName, this.FieldName))
                {
                    // not the hosting attachment field, try next one
                    continue;
                }

                // attachment field is found, now look for the attachment
                OAdAttachments aAttachments = CQWrapper.GetAttachments(aAttField);
                OAdAttachment  aAttachment  = null;
                for (int attachmentIndex = 0;
                     attachmentIndex < CQWrapper.AttachmentsCount(aAttachments);
                     attachmentIndex++)
                {
                    object obIndex = (object)attachmentIndex;
                    aAttachment = CQWrapper.AttachmentsItem(aAttachments, ref obIndex);

                    string name;
                    string comment;
                    string dispName;
                    int    fileSize;
                    CQWrapper.GetAttachmentMetadata(aAttachment, out name, out comment, out dispName, out fileSize);

                    byte[] hash = HashAttachmentMetadata(name, comment, dispName, (long)fileSize);
                    if (HashProducer.CompareMD5(metadataHash, hash) == 0)
                    {
                        attachmentFound = true;
                        break;
                    }
                }

                if (attachmentFound)
                {
                    Debug.Assert(null != aAttachment, "null == aAttachment");
                    CQWrapper.LoadAttachment(aAttachment, localPath);
                }

                // we've checked the host att field already, no need to proceed
                break;
            }

            if (!attachmentFound)
            {
                // [teyang] TODO: typed exception, AttachmentNotFound conflict handling
                throw new Exception(string.Format("attachment '{0}' is not found", Name));
            }
        }