/// <summary>
 /// Deletes the specified object from the <c>FlagInfo</c> table.
 /// </summary>
 /// <param name="value">The <see cref="FlagInfoRow"/> object to delete.</param>
 /// <returns>true if the record was deleted; otherwise, false.</returns>
 public bool Delete(FlagInfoRow value)
 {
     return DeleteByPrimaryKey(value.PkId);
 }
 /// <summary>
 /// Converts <see cref="System.Data.DataRow"/> to <see cref="FlagInfoRow"/>.
 /// </summary>
 /// <param name="row">The <see cref="System.Data.DataRow"/> object to be mapped.</param>
 /// <returns>A reference to the <see cref="FlagInfoRow"/> object.</returns>
 protected virtual FlagInfoRow MapRow(DataRow row)
 {
     FlagInfoRow mappedObject = new FlagInfoRow();
     DataTable dataTable = row.Table;
     DataColumn dataColumn;
     // Column "PkId"
     dataColumn = dataTable.Columns["PkId"];
     if(!row.IsNull(dataColumn))
         mappedObject.PkId = (int)row[dataColumn];
     // Column "FlagId"
     dataColumn = dataTable.Columns["FlagId"];
     if(!row.IsNull(dataColumn))
         mappedObject.FlagId = (string)row[dataColumn];
     // Column "FlagName"
     dataColumn = dataTable.Columns["FlagName"];
     if(!row.IsNull(dataColumn))
         mappedObject.FlagName = (string)row[dataColumn];
     // Column "FlagNameFr"
     dataColumn = dataTable.Columns["FlagNameFr"];
     if(!row.IsNull(dataColumn))
         mappedObject.FlagNameFr = (string)row[dataColumn];
     // Column "FlagDescription"
     dataColumn = dataTable.Columns["FlagDescription"];
     if(!row.IsNull(dataColumn))
         mappedObject.FlagDescription = (string)row[dataColumn];
     // Column "FlagDescriptionFr"
     dataColumn = dataTable.Columns["FlagDescriptionFr"];
     if(!row.IsNull(dataColumn))
         mappedObject.FlagDescriptionFr = (string)row[dataColumn];
     // Column "Category"
     dataColumn = dataTable.Columns["Category"];
     if(!row.IsNull(dataColumn))
         mappedObject.Category = (string)row[dataColumn];
     // Column "FileName"
     dataColumn = dataTable.Columns["FileName"];
     if(!row.IsNull(dataColumn))
         mappedObject.FileName = (string)row[dataColumn];
     // Column "SalesOrg"
     dataColumn = dataTable.Columns["SalesOrg"];
     if(!row.IsNull(dataColumn))
         mappedObject.SalesOrg = (string)row[dataColumn];
     // Column "DistributionChannel"
     dataColumn = dataTable.Columns["DistributionChannel"];
     if(!row.IsNull(dataColumn))
         mappedObject.DistributionChannel = (string)row[dataColumn];
     // Column "Sequence"
     dataColumn = dataTable.Columns["Sequence"];
     if(!row.IsNull(dataColumn))
         mappedObject.Sequence = (int)row[dataColumn];
     // Column "CreatedOn"
     dataColumn = dataTable.Columns["CreatedOn"];
     if(!row.IsNull(dataColumn))
         mappedObject.CreatedOn = (System.DateTime)row[dataColumn];
     // Column "UpdatedOn"
     dataColumn = dataTable.Columns["UpdatedOn"];
     if(!row.IsNull(dataColumn))
         mappedObject.UpdatedOn = (System.DateTime)row[dataColumn];
     // Column "Locale"
     dataColumn = dataTable.Columns["Locale"];
     if (!row.IsNull(dataColumn))
         mappedObject.Locale = (string)row[dataColumn];
     return mappedObject;
 }
        /// <summary>
        /// Reads data from the provided data reader and returns 
        /// an array of mapped objects.
        /// </summary>
        /// <param name="reader">The <see cref="System.Data.IDataReader"/> object to read data from the table.</param>
        /// <param name="startIndex">The index of the first record to map.</param>
        /// <param name="length">The number of records to map.</param>
        /// <param name="totalRecordCount">A reference parameter that returns the total number 
        /// of records in the reader object if 0 was passed into the method; otherwise it returns -1.</param>
        /// <returns>An array of <see cref="FlagInfoRow"/> objects.</returns>
        protected virtual FlagInfoRow[] MapRecords(IDataReader reader, 
										int startIndex, int length, ref int totalRecordCount)
        {
            if(0 > startIndex)
                throw new ArgumentOutOfRangeException("startIndex", startIndex, "StartIndex cannot be less than zero.");
            if(0 > length)
                throw new ArgumentOutOfRangeException("length", length, "Length cannot be less than zero.");

            int pkIdColumnIndex = reader.GetOrdinal("pkId");
            int flagIdColumnIndex = reader.GetOrdinal("FlagId");
            int flagNameColumnIndex = reader.GetOrdinal("FlagName");
            int flagNameFrColumnIndex = reader.GetOrdinal("FlagNameFr");
            int flagDescriptionColumnIndex = reader.GetOrdinal("FlagDescription");
            int flagDescriptionFrColumnIndex = reader.GetOrdinal("FlagDescriptionFr");
            int categoryColumnIndex = reader.GetOrdinal("Category");
            int fileNameColumnIndex = reader.GetOrdinal("FileName");
            int salesOrgColumnIndex = reader.GetOrdinal("SalesOrg");
            int distributionChannelColumnIndex = reader.GetOrdinal("DistributionChannel");
            int sequenceColumnIndex = reader.GetOrdinal("Sequence");
            int createdOnColumnIndex = reader.GetOrdinal("CreatedOn");
            int updatedOnColumnIndex = reader.GetOrdinal("UpdatedOn");
            int localeColumnIndex = reader.GetOrdinal("Locale");

            System.Collections.ArrayList recordList = new System.Collections.ArrayList();
            int ri = -startIndex;
            while(reader.Read())
            {
                ri++;
                if(ri > 0 && ri <= length)
                {
                    FlagInfoRow record = new FlagInfoRow();
                    recordList.Add(record);

                    record.PkId = Convert.ToInt32(reader.GetValue(pkIdColumnIndex));
                    if(!reader.IsDBNull(flagIdColumnIndex))
                        record.FlagId = Convert.ToString(reader.GetValue(flagIdColumnIndex));
                    record.FlagName = Convert.ToString(reader.GetValue(flagNameColumnIndex));
                    if(!reader.IsDBNull(flagNameFrColumnIndex))
                        record.FlagNameFr = Convert.ToString(reader.GetValue(flagNameFrColumnIndex));
                    if(!reader.IsDBNull(flagDescriptionColumnIndex))
                        record.FlagDescription = Convert.ToString(reader.GetValue(flagDescriptionColumnIndex));
                    if(!reader.IsDBNull(flagDescriptionFrColumnIndex))
                        record.FlagDescriptionFr = Convert.ToString(reader.GetValue(flagDescriptionFrColumnIndex));
                    record.Category = Convert.ToString(reader.GetValue(categoryColumnIndex));
                    if(!reader.IsDBNull(fileNameColumnIndex))
                        record.FileName = Convert.ToString(reader.GetValue(fileNameColumnIndex));
                    record.SalesOrg = Convert.ToString(reader.GetValue(salesOrgColumnIndex));
                    record.DistributionChannel = Convert.ToString(reader.GetValue(distributionChannelColumnIndex));
                    if(!reader.IsDBNull(sequenceColumnIndex))
                        record.Sequence = Convert.ToInt32(reader.GetValue(sequenceColumnIndex));
                    if(!reader.IsDBNull(createdOnColumnIndex))
                        record.CreatedOn = Convert.ToDateTime(reader.GetValue(createdOnColumnIndex));
                    if(!reader.IsDBNull(updatedOnColumnIndex))
                        record.UpdatedOn = Convert.ToDateTime(reader.GetValue(updatedOnColumnIndex));
                    if (!reader.IsDBNull(localeColumnIndex))
                        record.Locale = Convert.ToString(reader.GetValue(localeColumnIndex));

                    if(ri == length && 0 != totalRecordCount)
                        break;
                }
            }

            totalRecordCount = 0 == totalRecordCount ? ri + startIndex : -1;
            return (FlagInfoRow[])(recordList.ToArray(typeof(FlagInfoRow)));
        }
 /// <summary>
 /// Updates a record in the <c>FlagInfo</c> table.
 /// </summary>
 /// <param name="value">The <see cref="FlagInfoRow"/>
 /// object used to update the table record.</param>
 /// <returns>true if the record was updated; otherwise, false.</returns>
 public virtual bool Update(FlagInfoRow value)
 {
     string sqlStr = "UPDATE [dbo].[FlagInfo] SET " +
         "[FlagId]=" + _db.CreateSqlParameterName("FlagId") + ", " +
         "[FlagName]=" + _db.CreateSqlParameterName("FlagName") + ", " +
         "[FlagNameFr]=" + _db.CreateSqlParameterName("FlagNameFr") + ", " +
         "[FlagDescription]=" + _db.CreateSqlParameterName("FlagDescription") + ", " +
         "[FlagDescriptionFr]=" + _db.CreateSqlParameterName("FlagDescriptionFr") + ", " +
         "[Category]=" + _db.CreateSqlParameterName("Category") + ", " +
         "[FileName]=" + _db.CreateSqlParameterName("FileName") + ", " +
         "[SalesOrg]=" + _db.CreateSqlParameterName("SalesOrg") + ", " +
         "[DistributionChannel]=" + _db.CreateSqlParameterName("DistributionChannel") + ", " +
         "[Sequence]=" + _db.CreateSqlParameterName("Sequence") + ", " +
         "[CreatedOn]=" + _db.CreateSqlParameterName("CreatedOn") + ", " +
         "[UpdatedOn]=" + _db.CreateSqlParameterName("UpdatedOn") + ", " +
         "[Locale]=" + _db.CreateSqlParameterName("Locale") +
         " WHERE " +
         "[pkId]=" + _db.CreateSqlParameterName("PkId");
     IDbCommand cmd = _db.CreateCommand(sqlStr);
     AddParameter(cmd, "FlagId", value.FlagId);
     AddParameter(cmd, "FlagName", value.FlagName);
     AddParameter(cmd, "FlagNameFr", value.FlagNameFr);
     AddParameter(cmd, "FlagDescription", value.FlagDescription);
     AddParameter(cmd, "FlagDescriptionFr", value.FlagDescriptionFr);
     AddParameter(cmd, "Category", value.Category);
     AddParameter(cmd, "FileName", value.FileName);
     AddParameter(cmd, "SalesOrg", value.SalesOrg);
     AddParameter(cmd, "DistributionChannel", value.DistributionChannel);
     AddParameter(cmd, "Sequence",
         value.IsSequenceNull ? DBNull.Value : (object)value.Sequence);
     AddParameter(cmd, "CreatedOn",
         value.IsCreatedOnNull ? DBNull.Value : (object)value.CreatedOn);
     AddParameter(cmd, "UpdatedOn",
         value.IsUpdatedOnNull ? DBNull.Value : (object)value.UpdatedOn);
     AddParameter(cmd, "Locale", value.Locale);
     AddParameter(cmd, "PkId", value.PkId);
     return 0 != cmd.ExecuteNonQuery();
 }
 /// <summary>
 /// Adds a new record into the <c>FlagInfo</c> table.
 /// </summary>
 /// <param name="value">The <see cref="FlagInfoRow"/> object to be inserted.</param>
 public virtual void Insert(FlagInfoRow value)
 {
     string sqlStr = "INSERT INTO [dbo].[FlagInfo] (" +
         "[FlagId], " +
         "[FlagName], " +
         "[FlagNameFr], " +
         "[FlagDescription], " +
         "[FlagDescriptionFr], " +
         "[Category], " +
         "[FileName], " +
         "[SalesOrg], " +
         "[DistributionChannel], " +
         "[Sequence], " +
         "[CreatedOn], " +
         "[UpdatedOn], " +
         "[Locale]" +
         ") VALUES (" +
         _db.CreateSqlParameterName("FlagId") + ", " +
         _db.CreateSqlParameterName("FlagName") + ", " +
         _db.CreateSqlParameterName("FlagNameFr") + ", " +
         _db.CreateSqlParameterName("FlagDescription") + ", " +
         _db.CreateSqlParameterName("FlagDescriptionFr") + ", " +
         _db.CreateSqlParameterName("Category") + ", " +
         _db.CreateSqlParameterName("FileName") + ", " +
         _db.CreateSqlParameterName("SalesOrg") + ", " +
         _db.CreateSqlParameterName("DistributionChannel") + ", " +
         _db.CreateSqlParameterName("Sequence") + ", " +
         _db.CreateSqlParameterName("CreatedOn") + ", " +
         _db.CreateSqlParameterName("UpdatedOn") + ", " +
         _db.CreateSqlParameterName("Locale") + ");SELECT @@IDENTITY";
     IDbCommand cmd = _db.CreateCommand(sqlStr);
     AddParameter(cmd, "FlagId", value.FlagId);
     AddParameter(cmd, "FlagName", value.FlagName);
     AddParameter(cmd, "FlagNameFr", value.FlagNameFr);
     AddParameter(cmd, "FlagDescription", value.FlagDescription);
     AddParameter(cmd, "FlagDescriptionFr", value.FlagDescriptionFr);
     AddParameter(cmd, "Category", value.Category);
     AddParameter(cmd, "FileName", value.FileName);
     AddParameter(cmd, "SalesOrg", value.SalesOrg);
     AddParameter(cmd, "DistributionChannel", value.DistributionChannel);
     AddParameter(cmd, "Sequence",
         value.IsSequenceNull ? DBNull.Value : (object)value.Sequence);
     AddParameter(cmd, "CreatedOn",
         value.IsCreatedOnNull ? DBNull.Value : (object)value.CreatedOn);
     AddParameter(cmd, "UpdatedOn",
         value.IsUpdatedOnNull ? DBNull.Value : (object)value.UpdatedOn);
     AddParameter(cmd, "Locale", value.Locale);
     value.PkId = Convert.ToInt32(cmd.ExecuteScalar());
 }
        public static string AddFlagInfoToDb(DataTable flagInfo, DataTable categoryInfo, string locale)
        {
            int flagInfoInserted = 0;
            int flagInfoUpdated = 0;
            int categoryInserted = 0;
            int categoryUpdated = 0;

            try
            {
                using (var db = new B2BProductCatalog())
                {
                    // Flag Info
                    for (int i = 0; i < flagInfo.Rows.Count; i++)
                    {
                        if (!string.IsNullOrEmpty(flagInfo.Rows[i]["Flag Name"].ToString()))
                        {
                            var row = new FlagInfoRow
                            {
                                FlagId = flagInfo.Rows[i]["Flag ID"].ToString().Replace('_', '-'),
                                FlagName = TruncateString(flagInfo.Rows[i]["Flag Name"].ToString(), 50),
                                Category = !string.IsNullOrEmpty(flagInfo.Rows[i]["Flag Category"].ToString()) ? TruncateString(flagInfo.Rows[i]["Flag Category"].ToString(), 50) : "N/A",
                                SalesOrg = SalesOrg,
                                DistributionChannel = DistChan,
                                Locale = locale
                            };

                            // Optional fields
                            if (flagInfo.Rows[i]["Flag Description"] != null
                                && !string.IsNullOrEmpty(flagInfo.Rows[i]["Flag Description"].ToString()))
                                row.FlagDescription = TruncateString(flagInfo.Rows[i]["Flag Description"].ToString(), 4000);
                            if (flagInfo.Rows[i]["Flag File Name"] != null
                                && !string.IsNullOrEmpty(flagInfo.Rows[i]["Flag File Name"].ToString()))
                                row.FileName = flagInfo.Rows[i]["Flag File Name"].ToString();
                            if (flagInfo.Columns.Contains("Flag Name (FR)") && flagInfo.Rows[i]["Flag Name (FR)"] != null
                                && !string.IsNullOrEmpty(flagInfo.Rows[i]["Flag Name (FR)"].ToString()))
                                row.FlagNameFr = TruncateString(flagInfo.Rows[i]["Flag Name (FR)"].ToString(), 75);
                            if (flagInfo.Columns.Contains("Flag Description (FR)") && flagInfo.Rows[i]["Flag Description (FR)"] != null
                                && !string.IsNullOrEmpty(flagInfo.Rows[i]["Flag Description (FR)"].ToString()))
                                row.FlagDescriptionFr = TruncateString(flagInfo.Rows[i]["Flag Description (FR)"].ToString(), 4000);
                            if (flagInfo.Rows[i]["Sequence"] != null
                                && !string.IsNullOrEmpty(flagInfo.Rows[i]["Sequence"].ToString()))
                                row.Sequence = int.Parse(flagInfo.Rows[i]["Sequence"].ToString());
                            else
                                row.Sequence = 9999; //per iCongo, default to 9999 if not supplied

                            var flagInfoRow = db.FlagInfoCollection.GetByFlagID(row.FlagId);

                            if (flagInfoRow == null)
                            {
                                row.CreatedOn = DateTime.Now;
                                db.FlagInfoCollection.Insert(row);
                                flagInfoInserted++;
                            }
                            else
                            {
                                row.PkId = flagInfoRow.PkId;
                                row.UpdatedOn = DateTime.Now;
                                db.FlagInfoCollection.Update(row);
                                flagInfoUpdated++;
                            }
                        }
                    }

                    // Flag Categories
                    for (int j = 0; j < categoryInfo.Rows.Count; j++)
                    {
                        if (!string.IsNullOrEmpty(categoryInfo.Rows[j]["Flag Category"].ToString()))
                        {
                            var row = new FlagCategoryRow
                                      {
                                          Category = TruncateString(categoryInfo.Rows[j]["Flag Category"].ToString(), 50),
                                          CategoryId = categoryInfo.Rows[j]["Flag Category ID"].ToString().Replace('_', '-'),
                                          SalesOrg = SalesOrg,
                                          DistributionChannel = DistChan,
                                          Locale = locale
                                      };

                            if (categoryInfo.Columns.Contains("Flag Category (FR)") && categoryInfo.Rows[j]["Flag Category (FR)"] != null
                              && !string.IsNullOrEmpty(categoryInfo.Rows[j]["Flag Category (FR)"].ToString()))
                                row.CategoryFr = TruncateString(categoryInfo.Rows[j]["Flag Category (FR)"].ToString(), 50);
                            if (categoryInfo.Rows[j]["Sequence"] != null && !string.IsNullOrEmpty(categoryInfo.Rows[j]["Sequence"].ToString()))
                                row.Sequence = int.Parse(categoryInfo.Rows[j]["Sequence"].ToString());
                            else
                                row.Sequence = 9999; //per iCongo, default to 9999 if not supplied

                            var flagCategoryRow = db.FlagCategoryCollection.GetByFlagCategory(row.Category, SalesOrg, DistChan, locale);

                            if (flagCategoryRow == null)
                            {
                                row.CreatedOn = DateTime.Now;
                                db.FlagCategoryCollection.Insert(row);
                                categoryInserted++;
                            }
                            else
                            {
                                row.PkId = flagCategoryRow.PkId;
                                row.UpdatedOn = DateTime.Now;
                                db.FlagCategoryCollection.Update(row);
                                categoryUpdated++;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.ErrorFormat("Error inserting values into Flags table. Exception: {0}", ex.Message);
            }

            return string.Format("{0}_{1}_{2}_{3}", flagInfoInserted, flagInfoUpdated, categoryInserted, categoryUpdated);
        }