Exemplo n.º 1
0
        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);
        }
 /// <summary>
 /// Deletes the specified object from the <c>FlagCategory</c> table.
 /// </summary>
 /// <param name="value">The <see cref="FlagCategoryRow"/> object to delete.</param>
 /// <returns>true if the record was deleted; otherwise, false.</returns>
 public bool Delete(FlagCategoryRow value)
 {
     return DeleteByPrimaryKey(value.PkId);
 }
 /// <summary>
 /// Converts <see cref="System.Data.DataRow"/> to <see cref="FlagCategoryRow"/>.
 /// </summary>
 /// <param name="row">The <see cref="System.Data.DataRow"/> object to be mapped.</param>
 /// <returns>A reference to the <see cref="FlagCategoryRow"/> object.</returns>
 protected virtual FlagCategoryRow MapRow(DataRow row)
 {
     FlagCategoryRow mappedObject = new FlagCategoryRow();
     DataTable dataTable = row.Table;
     DataColumn dataColumn;
     // Column "PkId"
     dataColumn = dataTable.Columns["PkId"];
     if(!row.IsNull(dataColumn))
         mappedObject.PkId = (int)row[dataColumn];
     // Column "Category"
     dataColumn = dataTable.Columns["Category"];
     if(!row.IsNull(dataColumn))
         mappedObject.Category = (string)row[dataColumn];
     // Column "CategoryFr"
     dataColumn = dataTable.Columns["CategoryFr"];
     if(!row.IsNull(dataColumn))
         mappedObject.CategoryFr = (string)row[dataColumn];
     // Column "CategoryId"
     dataColumn = dataTable.Columns["CategoryId"];
     if(!row.IsNull(dataColumn))
         mappedObject.CategoryId = (string)row[dataColumn];
     // Column "Sequence"
     dataColumn = dataTable.Columns["Sequence"];
     if(!row.IsNull(dataColumn))
         mappedObject.Sequence = (int)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 "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="FlagCategoryRow"/> objects.</returns>
        protected virtual FlagCategoryRow[] 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 categoryColumnIndex = reader.GetOrdinal("Category");
            int categoryFrColumnIndex = reader.GetOrdinal("CategoryFr");
            int categoryIdColumnIndex = reader.GetOrdinal("CategoryId");
            int sequenceColumnIndex = reader.GetOrdinal("Sequence");
            int salesOrgColumnIndex = reader.GetOrdinal("SalesOrg");
            int distributionChannelColumnIndex = reader.GetOrdinal("DistributionChannel");
            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)
                {
                    FlagCategoryRow record = new FlagCategoryRow();
                    recordList.Add(record);

                    record.PkId = Convert.ToInt32(reader.GetValue(pkIdColumnIndex));
                    record.Category = Convert.ToString(reader.GetValue(categoryColumnIndex));
                    if(!reader.IsDBNull(categoryFrColumnIndex))
                        record.CategoryFr = Convert.ToString(reader.GetValue(categoryFrColumnIndex));
                    record.CategoryId = Convert.ToString(reader.GetValue(categoryIdColumnIndex));
                    record.Sequence = Convert.ToInt32(reader.GetValue(sequenceColumnIndex));
                    record.SalesOrg = Convert.ToString(reader.GetValue(salesOrgColumnIndex));
                    record.DistributionChannel = Convert.ToString(reader.GetValue(distributionChannelColumnIndex));
                    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 (FlagCategoryRow[])(recordList.ToArray(typeof(FlagCategoryRow)));
        }
 /// <summary>
 /// Updates a record in the <c>FlagCategory</c> table.
 /// </summary>
 /// <param name="value">The <see cref="FlagCategoryRow"/>
 /// object used to update the table record.</param>
 /// <returns>true if the record was updated; otherwise, false.</returns>
 public virtual bool Update(FlagCategoryRow value)
 {
     string sqlStr = "UPDATE [dbo].[FlagCategory] SET " +
         "[Category]=" + _db.CreateSqlParameterName("Category") + ", " +
         "[CategoryFr]=" + _db.CreateSqlParameterName("CategoryFr") + ", " +
         "[CategoryId]=" + _db.CreateSqlParameterName("CategoryId") + ", " +
         "[Sequence]=" + _db.CreateSqlParameterName("Sequence") + ", " +
         "[SalesOrg]=" + _db.CreateSqlParameterName("SalesOrg") + ", " +
         "[DistributionChannel]=" + _db.CreateSqlParameterName("DistributionChannel") + ", " +
         "[CreatedOn]=" + _db.CreateSqlParameterName("CreatedOn") + ", " +
         "[UpdatedOn]=" + _db.CreateSqlParameterName("UpdatedOn") + ", " +
         "[Locale]=" + _db.CreateSqlParameterName("Locale") +
         " WHERE " +
         "[pkId]=" + _db.CreateSqlParameterName("PkId");
     IDbCommand cmd = _db.CreateCommand(sqlStr);
     AddParameter(cmd, "Category", value.Category);
     AddParameter(cmd, "CategoryFr", value.CategoryFr);
     AddParameter(cmd, "CategoryId", value.CategoryId);
     AddParameter(cmd, "Sequence", value.Sequence);
     AddParameter(cmd, "SalesOrg", value.SalesOrg);
     AddParameter(cmd, "DistributionChannel", value.DistributionChannel);
     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>FlagCategory</c> table.
 /// </summary>
 /// <param name="value">The <see cref="FlagCategoryRow"/> object to be inserted.</param>
 public virtual void Insert(FlagCategoryRow value)
 {
     string sqlStr = "INSERT INTO [dbo].[FlagCategory] (" +
         "[Category], " +
         "[CategoryFr], " +
         "[CategoryId], " +
         "[Sequence], " +
         "[SalesOrg], " +
         "[DistributionChannel], " +
         "[CreatedOn], " +
         "[UpdatedOn], " +
         "[Locale]" +
         ") VALUES (" +
         _db.CreateSqlParameterName("Category") + ", " +
         _db.CreateSqlParameterName("CategoryFr") + ", " +
         _db.CreateSqlParameterName("CategoryId") + ", " +
         _db.CreateSqlParameterName("Sequence") + ", " +
         _db.CreateSqlParameterName("SalesOrg") + ", " +
         _db.CreateSqlParameterName("DistributionChannel") + ", " +
         _db.CreateSqlParameterName("CreatedOn") + ", " +
         _db.CreateSqlParameterName("UpdatedOn") + ", " +
         _db.CreateSqlParameterName("Locale") + ");SELECT @@IDENTITY";
     IDbCommand cmd = _db.CreateCommand(sqlStr);
     AddParameter(cmd, "Category", value.Category);
     AddParameter(cmd, "CategoryFr", value.CategoryFr);
     AddParameter(cmd, "CategoryId", value.CategoryId);
     AddParameter(cmd, "Sequence", value.Sequence);
     AddParameter(cmd, "SalesOrg", value.SalesOrg);
     AddParameter(cmd, "DistributionChannel", value.DistributionChannel);
     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());
 }