コード例 #1
0
        // vieno operatoriaus vienos GS lentelės duomenis - fetchina, paverčia WeldingInspection ir updateinaDB bei GS
        private static void fetchConvertUpdateSheet(SheetsService service,
                                                    Spreadsheets.Operator operatorData,                 // lemia, katro operatoriaus spreadsheet
                                                    Spreadsheets.SheetsRanges.SheetRangeData rangeData, // lemia, katrą to operatoriaus lentelę
                                                    ConvertDataToListMethod convertMethod,              // --
                                                    DbUpdateMethod dbUpdateMethod)                      // --
        {
            try
            {
                List <IList <Object> > data = SheetDataFetcher.Fetch(
                    operatorData.SpreadsheetId,
                    rangeData.RangeAddress,
                    rangeData.FilterColumn.Index,
                    service).ToList();
                List <WeldingInspection> inspectionList = convertMethod(data, rangeData.FieldMappings, operatorData.OperatorId);
                dbUpdateMethod(inspectionList);
                results.AppendFormat("{0} {1} db update OK", operatorData.OperatorId, rangeData.SheetName).AppendLine();
                allInspections = allInspections.Concat(inspectionList);
            }
            catch (Exception ex)
            {
                results.AppendFormat("{0} {1} db update FAIL.", operatorData.OperatorId, rangeData.SheetName).AppendLine();
                Exception e;
                if (ex.InnerException != null)
                {
                    e = ex.InnerException;
                }
                else
                {
                    e = ex;
                }
                results.AppendFormat("Exception message: {0}", e.Message).AppendLine();
                results.AppendLine("Google Sheets lentelėje duomenys nepažymimi kaip įvesti į DB ir šitie duomenys nebus įtraukti į ataskaitą.");
                results.AppendLine();
                return;
            }

            // updateinama Google Sheets lentelė.
            // jeigu GS updateinimas nutrūktų, informuoja, kad tokio operatoriaus tokie patikrinimai į dB sukišti, bet lentelė neatnaujinta.
            try
            {
                BatchUpdateValuesResponse response = GSheetsUpdater.BatchUpdateSheet(operatorData.SpreadsheetId, rangeData, service);
                results.AppendFormat("{0} {1} Google Sheets update OK", operatorData.OperatorId, rangeData.SheetName).AppendLine();
                results.AppendLine();
            }
            catch (Exception ex)
            {
                results.AppendFormat("{0} {1} Google Sheets update FAIL.", operatorData.OperatorId, rangeData.SheetName).AppendLine();
                results.AppendFormat("Failure info: {0}", ex.Message).AppendLine();
                results.Append("DĖMESIO! Kadangi duomenys į duomenų bazę sukelti, tie duomenys bus įtraukti į ataskaitą, kaip sutvarkyti. ");
                results.AppendLine("Google Sheets lentelę reikėtų sutvarkyti rankiniu būdu - prisijungti prie Google Sheets, atsidaryti lentelę ir joje pažymėti, kurie duomenys yra sukelti į DB.");
                results.AppendLine();
            }
        }
コード例 #2
0
ファイル: ControllerUpdate.cs プロジェクト: indoub47/weldings
        /// <summary>
        /// Single Google Sheets sheet:
        /// 1. fetches data from the sheet
        /// 2. converts it to WeldingInspection objects
        /// 3. updates database
        /// 4. in GoogleSheets marks those rows which have been put into database
        /// </summary>
        /// <param name="service">Google Sheets service</param>
        /// <param name="operatorData">which operator's spreadsheet</param>
        /// <param name="rangeData">which sheet of the spreadsheet</param>
        /// <param name="convertMethod">data converting method, specific to this sheet type</param>
        /// <param name="dbUpdateMethod">db updating method specific to this sheet type</param>
        /// <param name="sb">reference to the string builder to output workflow information</param>
        /// <returns>success or failure</returns>
        /// <exception cref="Weldings.DbUpdateException">is thrownon on failed db update and failed rollback if Settings.Default.AbortOnFailedRollback is set to true</exception>
        private static bool DoControllSingleSheet(
            SheetsService service,
            Spreadsheets.Operator operatorData,                 // lemia, katro operatoriaus spreadsheet
            Spreadsheets.SheetsRanges.SheetRangeData rangeData, // lemia, katrą to operatoriaus lentelę
            ConvertDataToWIMethod convertMethod,                // tam lentelės tipui specifinis convert metodas
            DbUpdateMethod dbUpdateMethod,                      // tam lentelės tipui specifinis update metodas
            StringBuilder sb)
        {
            string operSheet = string.Format(Messages.Default.OperSheetFrmt,
                                             operatorData.OperatorId, rangeData.SheetName);

            // fetch data from GoogleSheets
            List <IList <Object> > dataRows;

            try
            {
                dataRows = SheetDataFetcher.Fetch(
                    operatorData.SpreadsheetId,
                    rangeData.RangeAddress,
                    rangeData.FilterColumn.Index,
                    service).ToList();
            }
            catch (Exception ex)
            {
                string errorText = Messages.Default.GSDataFetchFail;
                sb.AppendLine(errorText);
                sb.AppendLine(Messages.Default.FailureInfo + ex.Message);
                OnProgressUpdated(0, errorText);
                LogWriter.Log(ex);
                return(false);
            }

            // convert row data to WeldingInspection objects
            List <WeldingInspection> wiList;

            try
            {
                wiList = convertMethod(dataRows, rangeData.FieldMappings, operatorData.OperatorId);
                // sb.AppendLine(operSheet + Messages.Default.GSDataConvertOK);
            }
            catch (BadDataException bdEx)
            {
                string errorText    = operSheet + Messages.Default.BadGSData;
                string bdListString = BadDataReportCreator.CreateString(bdEx.BadDataList.ToList()).ToString();
                sb.AppendLine(errorText);
                sb.AppendLine(bdListString);
                OnProgressUpdated(0, errorText);
                return(false);
            }
            catch (Exception ex)
            {
                string errorText = operSheet + Messages.Default.GSDataConvertFail;
                sb.AppendLine(errorText);
                sb.AppendLine(Messages.Default.FailureInfo + ex.Message);
                OnProgressUpdated(0, errorText);
                LogWriter.Log(ex);
                return(false);
            }

            // update database
            try
            {
                dbUpdateMethod(wiList);
                sb.AppendLine(operSheet + Messages.Default.DBUpdateOK);
            }
            catch (DbUpdateException dbuEx) when(dbuEx.RollbackSuccess)
            {
                string errorText = operSheet + dbuEx.Message;

                sb.AppendLine(errorText);
                sb.AppendLine(Messages.Default.FailureInfo + dbuEx.InnerException.Message);
                OnProgressUpdated(0, errorText);
                LogWriter.Log(dbuEx);
                return(false);
            }
            catch (DbUpdateException dbuEx) when(!dbuEx.RollbackSuccess)
            {
                string errorText = operSheet + dbuEx.Message;

                sb.AppendLine(errorText);
                sb.AppendLine(Messages.Default.FailureInfo + dbuEx.InnerException.Message);
                OnProgressUpdated(0, errorText);
                if (Settings.Default.AbortOnFailedRollback)
                {
                    throw dbuEx;
                }
                return(false);
            }

            allInspections = allInspections.Concat(wiList);

            // update Google Sheets
            try
            {
                GSheetsUpdater.BatchUpdateSheet(operatorData.SpreadsheetId, rangeData, service);
                sb.AppendLine(operSheet + Messages.Default.GSUpdateOK);
            }
            catch (Exception ex)
            {
                string errorText = operSheet + Messages.Default.GSUpdateFail;
                sb.AppendLine(errorText);
                sb.AppendLine(Messages.Default.FailureInfo + ex.Message);
                sb.AppendLine(Messages.Default.GSUpdateFailInstruction);
                OnProgressUpdated(0, errorText);
                LogWriter.Log(ex);
                return(true); // pagrindinis darbas - db updateinimas - pavyko, o sheet updateinimas yra tik detalės
            }

            return(true);
        }