/*
         * This routine is to add SQL WHERE clause to limit records to be loaded.
         * Be sure to copy this routine to SeriesCatalogTableAdapter class when
         * hiscentralDataSet.Designer.cs is regenerated to make GetRow() work.
         * Copied from http://www.codeproject.com/Articles/17324/Extending-TableAdapters-for-Dynamic-SQL.
         * public int FillWhere(hiscentralDataSet.SeriesCatalogDataTable dataTable, string whereExpression)
         * {
         *  string text1 = this._commandCollection[0].CommandText;
         *  try
         *  {
         *      this._commandCollection[0].CommandText += " WHERE " + whereExpression;
         *      return this.Fill(dataTable);
         *  }
         *  finally { this._commandCollection[0].CommandText = text1; }
         * }
         */


        public hiscentralDataSet.SeriesCatalogRow GetRow(string siteCode, string varCode,
                                                         WaterML11.seriesCatalogTypeSeries scts)
        {
            string cond = "SiteCode = '" + siteCode + "' and ";

            cond += "VariableCode = '" + varCode + "'";

            Adapter.FillWhere(Table, cond);
            if (Table.Rows.Count == 0)
            {
                return(null);
            }
            else
            {
                if (Table.Rows.Count > 1)
                {
                    string filter = string.Format("SeriesCode like '%||{0}||{1}||{2}'",
                                                  scts.method.methodID, scts.source.sourceID,
                                                  scts.qualityControlLevel.qualityControlLevelID);
                    DataRow[] rows = Table.Select(filter);
                    if (rows.Length == 1)
                    {
                        return((hiscentralDataSet.SeriesCatalogRow)rows[0]);
                    }
                    else
                    {
                        Console.WriteLine(
                            "SeriesCatalog table contains {0} records for site {1} var {2}, got {3} records with filter '{4}'.",
                            Table.Rows.Count, siteCode, varCode, rows.Length, filter);
                    }
                }
                return((hiscentralDataSet.SeriesCatalogRow)Table.Rows[0]);
            }
        }
        /*
         * This routine is to add SQL WHERE clause to limit records to be loaded.
         * Be sure to copy this routine to SeriesCatalogTableAdapter class when
         * OD_1_1_DataSet.Designer.cs is regenerated to make GetRow() work.
         * Copied from http://www.codeproject.com/Articles/17324/Extending-TableAdapters-for-Dynamic-SQL.
         * public int FillWhere(OD_1_1_DataSet.SeriesCatalogDataTable dataTable, string whereExpression)
         * {
         *  string text1 = this._commandCollection[0].CommandText;
         *  try
         *  {
         *      this._commandCollection[0].CommandText += " WHERE " + whereExpression;
         *      return this.Fill(dataTable);
         *  }
         *  finally { this._commandCollection[0].CommandText = text1; }
         * }
         *
         * public OD_1_1DataSet.SeriesCatalogRow GetRow(string siteCode, string varCode)
         * {
         *  string cond = "SiteCode = '" + siteCode + "' and ";
         *  cond += "VariableCode = '" + varCode + "'";
         *
         *  Adapter.FillWhere(Table, cond);
         *  if (Table.Rows.Count == 0)
         *      return null;
         *  else {
         *      if (Table.Rows.Count > 1)
         *          Console.WriteLine(
         *              "SeriesCatalog table contains {0} records.",
         *              Table.Rows.Count);
         *      return (OD_1_1DataSet.SeriesCatalogRow)Table.Rows[0];
         *  }
         * }
         */

        public OD_1_1_1DataSet.SeriesCatalogRow GetOrCreateSeriesCatalog(
            string siteCode, string varCode,
            WaterML11.seriesCatalogTypeSeries scts)
        {
            string query;
            int    undefined = -99;

            //            Console.WriteLine(@"....No SeriesCatalog from hiscentral for site {0} and var {1},
            //                dataType '{2}' methodID {3} sourcdID {4} qualityID {5}.",
            //                siteCode, varCode, scts.dataType, scts.method.methodID,
            //                scts.source.sourceID,
            //                scts.qualityControlLevel.qualityControlLevelID);

            if (scts.method.methodID < 0)
            {
                scts.method.methodID = undefined;
            }
            if (scts.qualityControlLevel.qualityControlLevelID < 0)
            {
                scts.qualityControlLevel.qualityControlLevelID = undefined;
            }
            if (scts.source.sourceID < 0)
            {
                scts.source.sourceID = undefined;
            }

            query = string.Format(@"SiteCode='{0}' and VariableCode='{1}' and 
                                MethodID = {2} and QualityControlLevelID = {3} and
                                SourceID = {4}", siteCode, varCode,
                                  scts.method.methodID,
                                  scts.qualityControlLevel.qualityControlLevelID,
                                  scts.source.sourceID);
            Adapter.FillWhere(Table, query);
            if (Table.Rows.Count > 0)
            {
                if (Table.Rows.Count > 1)
                {
                    Console.WriteLine(
                        "SeriesCatalog table contains {0} records.", Table.Rows.Count);
                }
                return((OD_1_1_1DataSet.SeriesCatalogRow)Table.Rows[0]);
            }

            Console.WriteLine(">>>Parsing and inserting a new SeriesCatalog");
            OD_1_1_1DataSet.SeriesCatalogRow row = CreateNewSeriesCatalog(siteCode, varCode, scts);
            Table.AddSeriesCatalogRow(row);
            Adapter.Update(Table);

            return(row);
        }