Exemplo n.º 1
0
        public static string RetrieveAndSaveData(string cultureName)
        {
            string strMsg = "";

            #region CEMT Patient Transfers
            //Now get the patients that were transferred from the CEMT tool. These sheets will have a PatNum=0;
            List <Sheet> listSheetsFromCemtTool = Sheets.GetTransferSheets();
            List <long>  listCemtSheetsToDelete = new List <long>();
            foreach (Sheet cemtSheet in listSheetsFromCemtTool)
            {
                try {
                    //continue if the SheetNum is either marked as skipped or is going to be deleted.
                    if (cemtSheet.SheetNum.In(listCemtSheetsToDelete))
                    {
                        continue;
                    }
                    if (!DidImportSheet(null, cemtSheet, null, listSheetsFromCemtTool, System.Globalization.CultureInfo.CurrentCulture.Name,
                                        ref listCemtSheetsToDelete))
                    {
                        //user wants to cancel import.
                        return(strMsg);
                    }
                }
                catch (Exception e) {
                    strMsg += e.Message + "\r\n";
                }
            }
            foreach (long sheetNum in listCemtSheetsToDelete)
            {
                Sheets.Delete(sheetNum);                //Does not delete the sheet but sets the Sheet.IsDeleted=true.
            }
            #endregion
            #region WebForms
            try {
                SheetsSynchProxy.TimeoutOverride = 300000;              //5 minutes.  Default is 100000 (1.66667 minutes).
                if (WebUtils.GetDentalOfficeID() == 0)
                {
                    strMsg = Lan.g("FormWebForms", "Either the registration key provided by the dental office is incorrect or the Host Server Address cannot be found.");
                    return(strMsg);
                }
                List <WebForms_Sheet> listWebFormSheets;
                List <long>           listSheetsSeen = new List <long>();
                int iterations = 0;
                do
                {
                    if (WebForms_Sheets.TryGetSheets(out listWebFormSheets))
                    {
                        //if we are not in HQ, filter out non-HQ sheets that don't match our current clinic
                        listWebFormSheets = listWebFormSheets.FindAll(x => x.ClinicNum == 0 || Clinics.ClinicNum == 0 || x.ClinicNum == Clinics.ClinicNum);
                    }
                    iterations++;
                    List <long> listSheetIdsForDeletion = new List <long>();
                    listWebFormSheets.RemoveAll(x => listSheetsSeen.Contains(x.SheetID));                    //Remove all sheets that we've already seen.
                    if (listWebFormSheets.Count == 0)
                    {
                        if (iterations == 1 && listSheetsFromCemtTool.Count == 0)
                        {
                            strMsg = Lan.g("FormWebForms", "No Patient forms retrieved from server");
                        }
                        else
                        {
                            strMsg = Lan.g("FormWebForms", "All Patient forms retrieved from server");
                        }
                        return(strMsg);
                    }
                    listSheetsSeen.AddRange(listWebFormSheets.Select(x => x.SheetID));
                    //loop through all incoming sheets
                    foreach (WebForms_Sheet webFormsSheet in listWebFormSheets)
                    {
                        try {                         //this try catch is put so that a defective downloaded sheet does not stop other sheets from being downloaded.
                            if (listSheetIdsForDeletion.Contains(webFormsSheet.SheetID))
                            {
                                continue;                                //Marked for deletion already. Next.
                            }
                            if (!DidImportSheet(webFormsSheet, null, listWebFormSheets, null, cultureName, ref listSheetIdsForDeletion))
                            {
                                //user wants to cancel import.
                                return(strMsg);
                            }
                        }
                        catch (Exception e) {
                            strMsg += e.Message + "\r\n";
                        }
                    }                    // end of for loop
                    if (!WebForms_Sheets.DeleteSheetData(listSheetIdsForDeletion.Distinct().ToList()))
                    {
                        break;                        //If any error happens, stop retrieving forms.
                    }
                } while(listWebFormSheets.Count > 0);
            }
            catch (Exception e) {
                strMsg += e.Message;
                return(strMsg);
            }
            #endregion
            return(strMsg);
        }