Exemplo n.º 1
0
        } // end of ProcessHistory()

        /// <summary>
        /// Processes the attachments for current record
        /// </summary>
        private void ProcessAttachments()
        {
            int noOfAttachments = 0;

            try
            {
                OAdAttachmentFields allAttachments = CQWrapper.GetAttachmentFields(m_CQEntity);
                StringCollection    filesAttached  = new StringCollection();

                int attachmentsIndex = 0;
                for (attachmentsIndex = 0;
                     attachmentsIndex < CQWrapper.AttachmentsFieldsCount(allAttachments);
                     attachmentsIndex++)
                {
                    object             ob            = (object)attachmentsIndex;
                    OAdAttachmentField attachmentFld = CQWrapper.AttachmentsFieldsItem(allAttachments, ref ob);

                    // process all attachments
                    OAdAttachments attachments = CQWrapper.GetAttachments(attachmentFld);
                    int            attachmentIndex;
                    for (attachmentIndex = 0;
                         attachmentIndex < CQWrapper.AttachmentsCount(attachments);
                         attachmentIndex++)
                    {
                        // there are some attachments

                        // create the dir for this bug dbid only if
                        string dirName = Path.Combine(CQConstants.AttachmentsDir, m_dbid.ToString());

                        if (attachmentIndex == 0)   // for the first time
                        {
                            // check if dir with curr dbid exist
                            if (Directory.Exists(dirName))
                            {
                                // if the dir exists, clean the contents
                                // as it is the system created dir
                                Logger.Write(LogSource.CQ, TraceLevel.Warning,
                                             "Removing folder {0} recursively for attachments of entity {1}",
                                             m_dbid, m_entityName);

                                try
                                {
                                    Directory.Delete(dirName, true);
                                }
                                catch (IOException ioe)
                                {
                                    Logger.Write(LogSource.CQ, TraceLevel.Warning, "Failed to delete folder {0} containing attachments for work item {1}",
                                                 dirName, SourceId);
                                    Logger.WriteException(LogSource.CQ, ioe);
                                }
                                catch (UnauthorizedAccessException uae)
                                {
                                    Logger.Write(LogSource.CQ, TraceLevel.Warning, "Permission denied for deleting folder {0} containing attachments for work item {1}",
                                                 dirName, SourceId);
                                    Logger.WriteException(LogSource.CQ, uae);
                                }
                            }

                            Logger.Write(LogSource.CQ, TraceLevel.Info,
                                         "Creating folder {0} for attachments of entity {1}",
                                         m_dbid, m_entityName);
                            UtilityMethods.CreateDirectory(dirName);
                        }

                        // for every attachments attachment create a separate
                        // sub dir as (attchmentsIndex+attchmentIndex)
                        string currAttachmentDir = Path.Combine(dirName, attachmentsIndex.ToString(CultureInfo.InvariantCulture));
                        currAttachmentDir = Path.Combine(currAttachmentDir, attachmentIndex.ToString(CultureInfo.InvariantCulture));

                        // there cannot be any dir existing as we already cleaned up
                        Logger.Write(LogSource.CQ, TraceLevel.Info,
                                     "Creating folder {0} for attachment {1} of entity {2}",
                                     currAttachmentDir, attachmentIndex + 1, m_entityName);
                        UtilityMethods.CreateDirectory(currAttachmentDir);

                        object        obIndex     = (object)attachmentIndex;
                        OAdAttachment aAttachment = CQWrapper.AttachmentsItem(attachments, ref obIndex);
                        noOfAttachments++;
                        string attachFileName;
                        string attachDescription;
                        CQWrapper.GetAttachmentFileNameAndDescription(aAttachment, out attachFileName, out attachDescription);
                        Logger.Write(LogSource.CQ, TraceLevel.Verbose, "Adding attachment");
                        Logger.Write(LogSource.CQ, TraceLevel.Verbose, "\tFile Name : {0}", attachFileName);
                        Logger.Write(LogSource.CQ, TraceLevel.Verbose, "\tDescription : {0}", attachDescription);

                        // save the attachment as file on disk
                        string tempfile = Path.Combine(currAttachmentDir, attachFileName);
                        CQWrapper.LoadAttachment(aAttachment, tempfile);
                        m_imWorkItem.Attachments.Add(new InMemoryAttachment(tempfile, attachDescription, false));
                        filesAttached.Add(attachFileName);
#if DEBUG
                        CommonConstants.NoOfAttachments++;
                        CommonConstants.TotalAttachmentSize += CQWrapper.AttachmentFileSize(aAttachment);
#endif
                    } // end of processing attachment
                }     // end of processing attachments
            }
            finally
            {
            }
        } // end of GetAttachments