public void DeleteFromClientDocumentation(long documentId)
        {
            try
            {
                _impersonator.ImpersonateValidUser(_username, _domain, _encryptedPassword);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                _impersonator.Dispose();
            }

            using (var securityService = new SecurityServiceClient())
            using (new OperationContextScope(securityService.InnerChannel))
            {
                OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("Username", "Peninsula.Common", "SafeCheckUser"));
                securityService.EnsureUserExists(null);
            }

            using (var clientDocumentService = new ClientDocumentServiceClient())
            using (new OperationContextScope(clientDocumentService.InnerChannel))
            {
                OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("Username", "Peninsula.Common", "SafeCheckUser"));

                var doc = clientDocumentService.GetById(documentId);

                if (doc.Deleted == false)
                {
                    long[] documentIds = { documentId };
                    clientDocumentService.DeleteByIds(documentIds);
                }
            }
        }
        public long WriteToClientDocumentation(string fileName, byte[] pdfBytes, int clientId, long? siteId)
        {
            long documentID = 0;

            var fullFileName = _holdingPath + Guid.NewGuid();
            try
            {
                _impersonator.ImpersonateValidUser(_username, _domain, _encryptedPassword);
                File.WriteAllBytes(fullFileName, pdfBytes);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                _impersonator.Dispose();
            }

            using(var securityService = new SecurityServiceClient())
            using (new OperationContextScope(securityService.InnerChannel))
            {
                OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("Username", "Peninsula.Common", "SafeCheckUser"));
                securityService.EnsureUserExists(null);
            }

            using(var clientDocumentService = new ClientDocumentServiceClient())
            using (new OperationContextScope(clientDocumentService.InnerChannel))
            {
                OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("Username", "Peninsula.Common", "SafeCheckUser"));

                var createClientDocumentRequest = new CreateClientDocumentRequest()
                                                      {
                                                          ClientId = clientId,
                                                          DocumentTypeId = 131, //MAGIC NUMBER: refactor out
                                                          OriginalFilename = fileName,
                                                          PhysicalFilePath = fullFileName,
                                                          Title = fileName,
                                                          SiteId = siteId
                                                      };

                documentID = clientDocumentService.CreateDocument(createClientDocumentRequest);
            }

            File.Delete(fullFileName);

            return documentID;
        }
        public HttpResponse GetExecutiveSummaryForChecklist(Guid checklistId)
        {
            var checklist = _checklistRepository.GetById(checklistId);

            ClientDocumentDto clientDocumentDto = null;
            if (checklist != null && checklist.ActionPlan != null
                && checklist.ActionPlan.ExecutiveSummaryDocumentLibraryId.HasValue
                && checklist.ActionPlan.ExecutiveSummaryDocumentLibraryId.Value != 0)
            {
                using (var securityService = new SecurityServiceClient())
                using (new OperationContextScope(securityService.InnerChannel))
                {
                    OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("Username",
                        "Peninsula.Common",
                        "SafeCheckUser"));
                    securityService.EnsureUserExists(null);
                }

                using (var clientDocumentService = new ClientDocumentServiceClient())
                {
                    using (new OperationContextScope(clientDocumentService.InnerChannel))
                    {
                        OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("Username",
                            "Peninsula.Common",
                            "SafeCheckUser"));
                        clientDocumentDto =
                            clientDocumentService.GetByIdWithContent(
                                checklist.ActionPlan.ExecutiveSummaryDocumentLibraryId.Value);
                    }
                }
            }

            // get the object representing the HTTP response to browser
            HttpResponse httpResponse = System.Web.HttpContext.Current.Response;
            if (clientDocumentDto == null)
            {
                httpResponse.Status = "204 No Content";
                httpResponse.StatusCode = 204;

                httpResponse.ContentType = "text/html";
                httpResponse.Write("Error downloading file - Please contact with us");

                httpResponse.End();

            }
            else
            {
                httpResponse.AddHeader("Content-Type", "application/pdf");
                httpResponse.AddHeader("Content-Disposition", String.Format("attachment; filename={1}; size={0}",
                    clientDocumentDto.FileBytes.Length.ToString(), clientDocumentDto.OriginalFilename));

                // write the PDF document bytes as attachment to HTTP response
                httpResponse.BinaryWrite(clientDocumentDto.FileBytes);

                // Note: it is important to end the response, otherwise the ASP.NET
                // web page will render its content to PDF document stream
                httpResponse.End();
            }

            return httpResponse;
        }