/// <summary>
 /// Calling the proxy client to fetch a SurveyResponseResponse object
 /// </summary>
 /// <param name="surveyid"></param>
 /// <returns></returns>
 public SurveyAnswerResponse GetSurveyAnswer(SurveyAnswerRequest pRequest)
 {
     try
     {
         //SurveyResponseResponse result = Client.GetSurveyResponse(pRequest);
         SurveyAnswerResponse result = new  Epi.Web.Common.Message.SurveyAnswerResponse();
         if (!pRequest.Criteria.IsDownLoadFromApp)
         {
             result = _iDataService.GetSurveyAnswer(pRequest);
         }
         else
         {
             result = _iManagerService.GetSurveyAnswer(pRequest);
         }
         return(result);
     }
     catch (FaultException <CustomFaultException> cfe)
     {
         throw cfe;
     }
     catch (FaultException fe)
     {
         throw fe;
     }
     catch (CommunicationException ce)
     {
         throw ce;
     }
     catch (TimeoutException te)
     {
         throw te;
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
        /// <summary>
        /// Initiates an export of the data to the specified file.
        /// </summary>
        public override void Export()
        {
            OnSetStatusMessage(ImportExportSharedStrings.WEB_CSV_EXPORT_CONNECTING);

            rowsExported = 0;
            WordBuilder  wb = new WordBuilder(SEPARATOR);
            StreamWriter sw = null;

            Epi.Web.Common.Message.SurveyAnswerRequest Request = new Epi.Web.Common.Message.SurveyAnswerRequest();
            Request.Criteria.SurveyId           = surveyKey.ToString();
            Request.Criteria.UserPublishKey     = secToken;
            Request.Criteria.OrganizationKey    = orgKey;
            Request.Criteria.ReturnSizeInfoOnly = true;
            Epi.Web.Common.Message.SurveyAnswerResponse Result = client.GetSurveyAnswer(Request);
            Pages    = Result.NumberOfPages;
            PageSize = Result.PageSize;

            Request.Criteria.ReturnSizeInfoOnly = false;

            int count = 0;

            List <SurveyAnswerResponse> Results = new List <SurveyAnswerResponse>();

            OnSetStatusMessage(ImportExportSharedStrings.WEB_CSV_EXPORT_BUILDING_COLUMN_HEADINGS);

            for (int i = 1; i <= Pages; i++)
            {
                Request.Criteria.PageNumber = i;
                Request.Criteria.PageSize   = PageSize;

                Result = client.GetSurveyAnswer(Request);
                Results.Add(Result);

                foreach (Epi.Web.Common.DTO.SurveyAnswerDTO surveyAnswer in Result.SurveyResponseList)
                {
                    if (surveyAnswer.Status == 3)
                    {
                        count++;
                    }
                }
            }


            if (SetMaxProgressBarValue != null)
            {
                SetMaxProgressBarValue((double)count);
            }

            List <string> columnHeaders = new List <string>();

            foreach (SurveyAnswerResponse R in Results)
            {
                wfList = ParseXML(R);

                foreach (WebFieldData wfData in wfList)
                {
                    if (!columnHeaders.Contains(wfData.FieldName))
                    {
                        columnHeaders.Add(wfData.FieldName);
                    }
                    else
                    {
                        break;
                    }
                }

                break;
            }

            try
            {
                OnSetStatusMessage(ImportExportSharedStrings.WEB_CSV_EXPORTING);

                sw = File.CreateText(fileName);

                foreach (string s in columnHeaders)
                {
                    wb.Add(s);
                }

                sw.WriteLine(wb.ToString());
                rowsExported = 0;


                foreach (SurveyAnswerResponse R in Results)
                {
                    string currentGUID = string.Empty;
                    string lastGUID    = string.Empty;

                    wfList = ParseXML(R);

                    wb = new WordBuilder(SEPARATOR);

                    foreach (WebFieldData wfData in wfList)
                    {
                        currentGUID = wfData.RecordGUID;

                        if (!string.IsNullOrEmpty(currentGUID) && !string.IsNullOrEmpty(lastGUID) && currentGUID != lastGUID)
                        {
                            sw.WriteLine(wb.ToString());
                            wb = new WordBuilder(SEPARATOR);

                            OnSetStatusMessageAndProgressCount("", 1);
                        }

                        string rowValue = wfData.FieldValue.ToString().Replace("\r\n", " ");
                        if (rowValue.Contains(",") || rowValue.Contains("\""))
                        {
                            rowValue = rowValue.Replace("\"", "\"\"");
                            rowValue = Util.InsertIn(rowValue, "\"");
                        }
                        wb.Add(rowValue);

                        lastGUID = wfData.RecordGUID;
                    }

                    sw.WriteLine(wb.ToString());
                }

                OnSetStatusMessage(ImportExportSharedStrings.WEB_CSV_EXPORT_COMPLETE);

                if (FinishExport != null)
                {
                    FinishExport();
                }
            }

            catch (Exception ex)
            {
                OnSetStatusMessage(ex.Message);

                if (ExportFailed != null)
                {
                    ExportFailed(ex.Message);
                }
            }
            finally
            {
                // Clean up
                if (sw != null)
                {
                    sw.Close();
                    sw.Dispose();
                    sw = null;
                }
            }
        }