Пример #1
0
 public static Consent ToModel(this ConsentDocument s)
 {
     if (s == null)
     {
         return(null);
     }
     return(Mapper.Map <ConsentDocument, Consent>(s));
 }
        public async Task Download(ConsentDocument consentDocument)
        {
            IsRefreshing = true;
            var connection = await apiService.CheckConnection();

            if (!connection.IsSuccess)
            {
                await dialogService.ShowMessage("Error", connection.Message);

                return;
            }

            var dateNow         = DateTime.Now.ToString("dd-MM-yyyy");
            var cookie          = Settings.Cookie;
            var res             = cookie.Substring(11, 32);
            var cookieContainer = new CookieContainer();
            var handler         = new HttpClientHandler()
            {
                CookieContainer = cookieContainer
            };
            var client = new HttpClient(handler);
            //get Document
            var getUrl = "https://portalesp.smart-path.it/Portalesp/patient/getHeaderDocument?documentId=" + consentDocument.repositoryTemplate.id + "&patientId=" + Patient.id;

            Debug.WriteLine("+++++++++++++++++++++++++getUrl++++++++++++++++++++++++");
            Debug.WriteLine(getUrl);
            client.BaseAddress = new Uri(getUrl);
            cookieContainer.Add(client.BaseAddress, new Cookie("JSESSIONID", res));
            var getResponse = await client.GetAsync(getUrl);

            if (!getResponse.IsSuccessStatusCode)
            {
                await Application.Current.MainPage.DisplayAlert("Error", getResponse.StatusCode.ToString(), "ok");

                return;
            }
            var getResult = await getResponse.Content.ReadAsStringAsync();

            Debug.WriteLine("+++++++++++++++++++++++++getResult++++++++++++++++++++++++");
            Debug.WriteLine(getResult);
            var getDocument = JsonConvert.DeserializeObject <DocumentConsentPatient>(getResult, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            });
            //download word
            var _model = new DocumentConsentPatient
            {
                documentCode       = getDocument.documentCode,
                documentVersion    = getDocument.documentVersion,
                documentProperties = getDocument.documentProperties
            };
            var request = JsonConvert.SerializeObject(_model);

            Debug.WriteLine("********request*************");
            Debug.WriteLine(request);
            var content = new StringContent(request, Encoding.UTF8, "application/json");
            var url     = "https://portalesp.smart-path.it/port-docs/smartmaker/document/fill/base64?code=" + getDocument.documentCode + "&version=" + getDocument.documentVersion;

            Debug.WriteLine("********url*************");
            Debug.WriteLine(url);
            client.BaseAddress = new Uri(url);
            cookieContainer.Add(client.BaseAddress, new Cookie("JSESSIONID", res));
            var response = await client.PostAsync(url, content);

            Debug.WriteLine("********response*************");
            Debug.WriteLine(response);
            if (!response.IsSuccessStatusCode)
            {
                IsRefreshing = false;
                await Application.Current.MainPage.DisplayAlert("Error", response.StatusCode.ToString(), "ok");

                return;
            }

            var result = await response.Content.ReadAsStringAsync();

            Debug.WriteLine("********result*************");
            Debug.WriteLine(result);
            var word = JsonConvert.DeserializeObject <DownloadWordDocument>(result);

            IsRefreshing = false;
            byte[]       bytes  = Convert.FromBase64String(word.content);
            MemoryStream stream = new MemoryStream(bytes);

            if (stream == null)
            {
                await Application.Current.MainPage.DisplayAlert("Warning", "Data is Empty", "ok");

                return;
            }
            await DependencyService.Get <ISave>().SaveAndView(getDocument.documentCode + "-" + dateNow + ".DOCX", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", stream);
        }