SqlCustomerActivityProvider.cs :  » Web-Frameworks » nopCommerce » NopSolutions » NopCommerce » DataAccess » Audit » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Web Frameworks » nopCommerce 
nopCommerce » NopSolutions » NopCommerce » DataAccess » Audit » SqlCustomerActivityProvider.cs
//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration.Provider;
using System.Data;
using System.Data.Common;
using System.Globalization;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace NopSolutions.NopCommerce.DataAccess.Audit{
    /// <summary>
    /// Customer activity provider for SQL Server
    /// </summary>
    public partial class SqlCustomerActivityProvider : DBCustomerActivityProvider
    {
        #region Fields
        private string _sqlConnectionString;
        #endregion

        #region Utilities
        private DBActivityLogType GetActivityLogTypeFromReader(IDataReader dataReader)
        {
            var item = new DBActivityLogType();
            item.ActivityLogTypeId = NopSqlDataHelper.GetInt(dataReader, "ActivityLogTypeID");
            item.SystemKeyword = NopSqlDataHelper.GetString(dataReader, "SystemKeyword");
            item.Name = NopSqlDataHelper.GetString(dataReader, "Name");
            item.Enabled = NopSqlDataHelper.GetBoolean(dataReader, "Enabled");
            return item;
        }
        
        private DBActivityLog GetActivityLogFromReader(IDataReader dataReader)
        {
            var item = new DBActivityLog();
            item.ActivityLogId = NopSqlDataHelper.GetInt(dataReader, "ActivityLogID");
            item.ActivityLogTypeId = NopSqlDataHelper.GetInt(dataReader, "ActivityLogTypeID");
            item.CustomerId = NopSqlDataHelper.GetInt(dataReader, "CustomerID");
            item.Comment = NopSqlDataHelper.GetString(dataReader, "Comment");
            item.CreatedOn = NopSqlDataHelper.GetUtcDateTime(dataReader, "CreatedOn");
            return item;
        }
        #endregion

        #region Methods
        /// <summary>
        /// Initializes the provider with the property values specified in the application's configuration file. This method is not intended to be used directly from your code
        /// </summary>
        /// <param name="name">The name of the provider instance to initialize</param>
        /// <param name="config">A NameValueCollection that contains the names and values of configuration options for the provider.</param>
        public override void Initialize(string name, NameValueCollection config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            base.Initialize(name, config);

            string connectionStringName = config["connectionStringName"];
            if (String.IsNullOrEmpty(connectionStringName))
                throw new ProviderException("Connection name not specified");
            this._sqlConnectionString = NopSqlDataHelper.GetConnectionString(connectionStringName);
            if ((this._sqlConnectionString == null) || (this._sqlConnectionString.Length < 1))
            {
                throw new ProviderException(string.Format("Connection string not found. {0}", connectionStringName));
            }
            config.Remove("connectionStringName");

            if (config.Count > 0)
            {
                string key = config.GetKey(0);
                if (!string.IsNullOrEmpty(key))
                {
                    throw new ProviderException(string.Format("Provider unrecognized attribute. {0}", new object[] { key }));
                }
            }
        }

        /// <summary>
        /// Inserts an activity log type item
        /// </summary>
        /// <param name="systemKeyword">The system keyword</param>
        /// <param name="name">The display name</param>
        /// <param name="enabled">Value indicating whether the activity log type is enabled</param>
        /// <returns>Activity log type item</returns>
        public override DBActivityLogType InsertActivityType(string systemKeyword,
            string name, bool enabled)
        {
            DBActivityLogType item = null;
            Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString);
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ActivityLogTypeInsert");
            db.AddOutParameter(dbCommand, "ActivityLogTypeID", DbType.Int32, 0);
            db.AddInParameter(dbCommand, "SystemKeyword", DbType.String, systemKeyword);
            db.AddInParameter(dbCommand, "Name", DbType.String, name);
            db.AddInParameter(dbCommand, "Enabled", DbType.Boolean, enabled);
            if (db.ExecuteNonQuery(dbCommand) > 0)
            {
                int activityLogTypeId = Convert.ToInt32(db.GetParameterValue(dbCommand, "@ActivityLogTypeID"));
                item = GetActivityTypeById(activityLogTypeId);
            }
            return item;
        }

        /// <summary>
        /// Updates an activity log type item
        /// </summary>
        /// <param name="activityLogTypeId">Activity log type identifier</param>
        /// <param name="systemKeyword">The system keyword</param>
        /// <param name="name">The display name</param>
        /// <param name="enabled">Value indicating whether the activity log type is enabled</param>
        /// <returns>Activity log type item</returns>
        public override DBActivityLogType UpdateActivityType(int activityLogTypeId,
            string systemKeyword, string name, bool enabled)
        {
            DBActivityLogType item = null;
            Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString);
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ActivityLogTypeUpdate");
            db.AddInParameter(dbCommand, "ActivityLogTypeID", DbType.Int32, activityLogTypeId);
            db.AddInParameter(dbCommand, "SystemKeyword", DbType.String, systemKeyword);
            db.AddInParameter(dbCommand, "Name", DbType.String, name);
            db.AddInParameter(dbCommand, "Enabled", DbType.Boolean, enabled);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                item = GetActivityTypeById(activityLogTypeId);
            return item;
        }

        /// <summary>
        /// Deletes an activity log type item
        /// </summary>
        /// <param name="activityLogTypeId">Activity log type identifier</param>
        public override void DeleteActivityType(int activityLogTypeId)
        {
            Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString);
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ActivityLogTypeDelete");
            db.AddInParameter(dbCommand, "ActivityLogTypeID", DbType.Int32, activityLogTypeId);
            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// Gets all activity log type items
        /// </summary>
        /// <returns>Activity log type collection</returns>
        public override DBActivityLogTypeCollection GetAllActivityTypes()
        {
            var result = new DBActivityLogTypeCollection();
            Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString);
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ActivityLogTypeLoadAll");
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    var item = GetActivityLogTypeFromReader(dataReader);
                    result.Add(item);
                }
            }

            return result;
        }

        /// <summary>
        /// Gets an activity log type item
        /// </summary>
        /// <param name="activityLogTypeId">Activity log type identifier</param>
        /// <returns>Activity log type item</returns>
        public override DBActivityLogType GetActivityTypeById(int activityLogTypeId)
        {
            DBActivityLogType item = null;
            Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString);
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ActivityLogTypeLoadByPrimaryKey");
            db.AddInParameter(dbCommand, "ActivityLogTypeID", DbType.Int32, activityLogTypeId);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                    item = GetActivityLogTypeFromReader(dataReader);
            }
            return item;
        }

        /// <summary>
        /// Inserts an activity log item
        /// </summary>
        /// <param name="activityLogTypeId">Activity log type identifier</param>
        /// <param name="customerId">The customer identifier</param>
        /// <param name="comment">The activity comment</param>
        /// <param name="createdOn">The date and time of instance creation</param>
        /// <returns>Activity log item</returns>
        public override DBActivityLog InsertActivity(int activityLogTypeId,
            int customerId, string comment, DateTime createdOn)
        {
            DBActivityLog item = null;
            Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString);
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ActivityLogInsert");
            db.AddOutParameter(dbCommand, "ActivityLogID", DbType.Int32, 0);
            db.AddInParameter(dbCommand, "ActivityLogTypeID", DbType.Int32, activityLogTypeId);
            db.AddInParameter(dbCommand, "CustomerID", DbType.Int32, customerId);
            db.AddInParameter(dbCommand, "Comment", DbType.String, comment);
            db.AddInParameter(dbCommand, "CreatedOn", DbType.DateTime, createdOn);
            if (db.ExecuteNonQuery(dbCommand) > 0)
            {
                int activityLogId = Convert.ToInt32(db.GetParameterValue(dbCommand, "@ActivityLogID"));
                item = GetActivityById(activityLogId);
            }
            return item;
        }

        /// <summary>
        /// Updates an activity log 
        /// </summary>
        /// <param name="activityLogId">Activity log identifier</param>
        /// <param name="activityLogTypeId">Activity log type identifier</param>
        /// <param name="customerId">The customer identifier</param>
        /// <param name="comment">The activity comment</param>
        /// <param name="createdOn">The date and time of instance creation</param>
        /// <returns>Activity log item</returns>
        public override DBActivityLog UpdateActivity(int activityLogId,
            int activityLogTypeId, int customerId, string comment, DateTime createdOn)
        {
            DBActivityLog item = null;
            Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString);
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ActivityLogUpdate");
            db.AddInParameter(dbCommand, "ActivityLogID", DbType.Int32, activityLogId);
            db.AddInParameter(dbCommand, "ActivityLogTypeID", DbType.Int32, activityLogTypeId);
            db.AddInParameter(dbCommand, "CustomerID", DbType.Int32, customerId);
            db.AddInParameter(dbCommand, "Comment", DbType.String, comment);
            db.AddInParameter(dbCommand, "CreatedOn", DbType.DateTime, createdOn);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                item = GetActivityById(activityLogId);
            return item;
        }

        /// <summary>
        /// Deletes an activity log item
        /// </summary>
        /// <param name="activityLogId">Activity log type identifier</param>
        public override void DeleteActivity(int activityLogId)
        {
            Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString);
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ActivityLogDelete");
            db.AddInParameter(dbCommand, "ActivityLogID", DbType.Int32, activityLogId);
            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// Gets all activity log items
        /// </summary>
        /// <param name="createdOnFrom">Log item creation from; null to load all customers</param>
        /// <param name="createdOnTo">Log item creation to; null to load all customers</param>
        /// <param name="email">Customer Email</param>
        /// <param name="username">Customer username</param>
        /// <param name="activityLogTypeId">Activity log type identifier</param>
        /// <param name="pageSize">Page size</param>
        /// <param name="pageIndex">Page index</param>
        /// <param name="totalRecords">Total records</param>
        /// <returns>Activity log collection</returns>
        public override DBActivityLogCollection GetAllActivities(DateTime? createdOnFrom,
            DateTime? createdOnTo, string email, string username, int activityLogTypeId,
            int pageSize, int pageIndex, out int totalRecords)
        {
            totalRecords = 0;
            var result = new DBActivityLogCollection();
            Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString);
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ActivityLogLoadAll");
            if (createdOnFrom.HasValue)
                db.AddInParameter(dbCommand, "CreatedOnFrom", DbType.DateTime, createdOnFrom.Value);
            else
                db.AddInParameter(dbCommand, "CreatedOnFrom", DbType.DateTime, null);
            if (createdOnTo.HasValue)
                db.AddInParameter(dbCommand, "CreatedOnTo", DbType.DateTime, createdOnTo.Value);
            else
                db.AddInParameter(dbCommand, "CreatedOnTo", DbType.DateTime, null);
            db.AddInParameter(dbCommand, "Email", DbType.String, email);
            db.AddInParameter(dbCommand, "Username", DbType.String, username);
            if (activityLogTypeId > 0)
                db.AddInParameter(dbCommand, "ActivityLogTypeID", DbType.Int32, activityLogTypeId);
            else
                db.AddInParameter(dbCommand, "ActivityLogTypeID", DbType.Int32, null);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, pageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, pageIndex);
            db.AddOutParameter(dbCommand, "TotalRecords", DbType.Int32, 0);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    var item = GetActivityLogFromReader(dataReader);
                    result.Add(item);
                }
            }
            totalRecords = Convert.ToInt32(db.GetParameterValue(dbCommand, "@TotalRecords"));

            return result;
        }

        /// <summary>
        /// Gets an activity log item
        /// </summary>
        /// <param name="activityLogId">Activity log identifier</param>
        /// <returns>Activity log item</returns>
        public override DBActivityLog GetActivityById(int activityLogId)
        {
            DBActivityLog item = null;
            Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString);
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ActivityLogLoadByPrimaryKey");
            db.AddInParameter(dbCommand, "ActivityLogID", DbType.Int32, activityLogId);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                    item = GetActivityLogFromReader(dataReader);
            }
            return item;
        }

        /// <summary>
        /// Clears activity log
        /// </summary>
        public override void ClearAllActivities()
        {
            Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString);
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_ActivityLogClearAll");
            db.ExecuteNonQuery(dbCommand);
        }
        #endregion
    }
}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.