Azavea.NijPredictivePolicing.ACSAlchemistLibrary.FileFormats.DesiredColumnsReader.ImportDesiredVariables C# (CSharp) Method

ImportDesiredVariables() public method

public ImportDesiredVariables ( DbConnection conn, IDataClient client, string filename, string tablename ) : bool
conn System.Data.Common.DbConnection
client IDataClient
filename string
tablename string
return bool
        public bool ImportDesiredVariables(DbConnection conn, IDataClient client, string filename, string tablename)
        {
            if ((string.IsNullOrEmpty(filename)) || (!File.Exists(filename)))
            {
                _log.DebugFormat("ImportDesiredVariables failed: provided filename was empty or did not exist \"{0}\" ", filename);
                return false;
            }

            try
            {

                //empty/create our temporary table
                DataTable dt = SetupTable(conn, client, tablename);

                //get a list of the columns they wanted
                dt = ReadVariablesFile(filename, dt);

                //check all our error scenarios
                TableChecker[] fnList = new TableChecker[] {
                    CheckForMaxColumns,
                    CheckForMinColumns,
                    CheckForDuplicates,
                    CheckForMOEDuplicates,
                    CheckForReserved
                };

                bool noErrors = true;
                foreach (var errCheckFn in fnList)
                {
                    string msg = errCheckFn(dt);
                    if (!string.IsNullOrEmpty(msg))
                    {
                        noErrors = false;
                        _log.Error(msg);
                    }
                }

                if (!noErrors)
                {
                    return false;
                }

                SaveTable(conn, client, dt);

                return true;
            }
            catch (Exception ex)
            {
                _log.Error("Variable Import Failed", ex);
                RemoveTemporaryTable(conn, client);
            }

            return false;
        }

Usage Example

        /// <summary>
        /// Currently rebuilds the requested variables table every time
        /// </summary>
        /// <param name="conn"></param>
        /// <returns></returns>
        public DataTable GetRequestedVariables(DbConnection conn)
        {
            _log.Debug("Processing requested variables... ");

            DataTable dt = null;
            if (!string.IsNullOrEmpty(DesiredVariablesFilename))
            {
                DesiredColumnsReader fileReader = new DesiredColumnsReader();
                bool importSucceeded = fileReader.ImportDesiredVariables(conn, DbClient,
                    this.DesiredVariablesFilename, DbConstants.TABLE_DesiredColumns);

                if (importSucceeded)
                {
                    string getRequestedCount = string.Format(
                      @"SELECT    * FROM ""{0}"" ;",
                      DbConstants.TABLE_DesiredColumns);
                    DataTable desiredVars = DataClient.GetMagicTable(conn, DbClient, getRequestedCount);
                    int numDesired = desiredVars.Rows.Count;

                    _log.DebugFormat("Variable file {0} imported successfully", this.DesiredVariablesFilename);
                    string getRequestedVariablesSQL = string.Format(
                      @"SELECT    columnMappings.CENSUS_TABLE_ID,
                                  columnMappings.COLNO,
                                  columnMappings.SEQNO,
                                  {0}.CUSTOM_COLUMN_NAME AS COLNAME
                        FROM      columnMappings, ""{0}""
                        WHERE     columnMappings.CENSUS_TABLE_ID = {0}.CENSUS_TABLE_ID;",
                        DbConstants.TABLE_DesiredColumns);

                    dt = DataClient.GetMagicTable(conn, DbClient, getRequestedVariablesSQL);

                    if ((dt == null) || (numDesired != dt.Rows.Count))
                    {
                        _log.Warn("I couldn't find one or more of the variables you requested.");
                        HashSet<string> foundVarsSet = new HashSet<string>();
                        if (dt != null)
                        {
                            foreach (DataRow dr in dt.Rows)
                            {
                                foundVarsSet.Add(dr["CENSUS_TABLE_ID"] as string);
                            }
                        }
                        foreach (DataRow dr in desiredVars.Rows)
                        {
                            var varName = dr["CENSUS_TABLE_ID"] as string;
                            if (!foundVarsSet.Contains(varName))
                            {
                                _log.ErrorFormat("Could not find requested variable \"{0}\"", varName);
                            }
                        }

                        _log.Debug("Processing requested variables... Done -- with errors");

                        //make tests pass
                        if (dt.Rows.Count == 0)
                            return null;

                        return dt;
                    }
                }
                else
                {
                    _log.Warn("Processing requested variables... Failed!");
                    return null;
                }
            }
            else
            {
                _log.Debug("No variables file specified");
            }

            _log.Debug("Processing requested variables... Done!");
            return dt;
        }