context: /********************************************************************************
  Copyright (C) MixERP Inc. (http://mixof.org).
  
  This file is part of MixERP.
  
  MixERP is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, version 2 of the License.
  
  
  MixERP is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with MixERP.  If not, see <http://www.gnu.org/licenses/>.
  ***********************************************************************************/
  using System.Collections.Generic;
  using System.Data;
  using System.Linq;
  using MixERP.Net.DbFactory;
  using MixERP.Net.EntityParser;
  using MixERP.Net.Framework;
  using Npgsql;
  using PetaPoco;
  using Serilog;
  
  namespace MixERP.Net.Schemas.Config.Data
  {
      /// <summary>
      /// Provides simplified data access features to perform SCRUD operation on the database table "config.scrud_factory".
      /// </summary>
      public class ScrudFactory : DbAccess
      {
          /// <summary>
          /// The schema of this table. Returns literal "config".
          /// </summary>
  	    public override string ObjectNamespace => "config";
  
          /// <summary>
          /// The schema unqualified name of this table. Returns literal "scrud_factory".
          /// </summary>
  	    public override string ObjectName => "scrud_factory";
  
          /// <summary>
          /// Login id of application user accessing this table.
          /// </summary>
  		public long LoginId { get; set; }
  
          /// <summary>
          /// The name of the database on which queries are being executed to.
          /// </summary>
          public string Catalog { get; set; }
  
  		/// <summary>
  		/// Performs SQL count on the table "config.scrud_factory".
  		/// </summary>
  		/// <returns>Returns the number of rows of the table "config.scrud_factory".</returns>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
  		public long Count()
  		{
  			if(string.IsNullOrWhiteSpace(this.Catalog))
  			{
  				return 0;
  			}
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this.LoginId, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to count entity \"ScrudFactory\" was denied to the user with Login ID {LoginId}", this.LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  	
  			const string sql = "SELECT COUNT(*) FROM config.scrud_factory;";
  			return Factory.Scalar<long>(this.Catalog, sql);
  		}
  
  		/// <summary>
  		/// Executes a select query on the table "config.scrud_factory" with a where filter on the column "key" to return a single instance of the "ScrudFactory" class. 
  		/// </summary>
  		/// <param name="key">The column "key" parameter used on where filter.</param>
  		/// <returns>Returns a non-live, non-mapped instance of "ScrudFactory" class mapped to the database row.</returns>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
  		public MixERP.Net.Entities.Config.ScrudFactory Get(string key)
  		{
  			if(string.IsNullOrWhiteSpace(this.Catalog))
  			{
  				return null;
  			}
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this.LoginId, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to the get entity \"ScrudFactory\" filtered by \"Key\" with value {Key} was denied to the user with Login ID {LoginId}", key, this.LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  	
  			const string sql = "SELECT * FROM config.scrud_factory WHERE key=@0;";
  			return Factory.Get<MixERP.Net.Entities.Config.ScrudFactory>(this.Catalog, sql, key).FirstOrDefault();
  		}
  
          /// <summary>
          /// Displayfields provide a minimal name/value context for data binding the row collection of config.scrud_factory.
          /// </summary>
          /// <returns>Returns an enumerable name and value collection for the table config.scrud_factory</returns>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
  		public IEnumerable<DisplayField> GetDisplayFields()
  		{
  			List<DisplayField> displayFields = new List<DisplayField>();
  
  			if(string.IsNullOrWhiteSpace(this.Catalog))
  			{
  				return displayFields;
  			}
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this.LoginId, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to get display field for entity \"ScrudFactory\" was denied to the user with Login ID {LoginId}", this.LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  	
  			const string sql = "SELECT key AS key, key as value FROM config.scrud_factory;";
  			using (NpgsqlCommand command = new NpgsqlCommand(sql))
  			{
  				using (DataTable table = DbOperation.GetDataTable(this.Catalog, command))
  				{
  					if (table?.Rows == null || table.Rows.Count == 0)
  					{
  						return displayFields;
  					}
  
  					foreach (DataRow row in table.Rows)
  					{
  						if (row != null)
  						{
  							DisplayField displayField = new DisplayField
  							{
  								Key = row["key"].ToString(),
  								Value = row["value"].ToString()
  							};
  
  							displayFields.Add(displayField);
  						}
  					}
  				}
  			}
  
  			return displayFields;
  		}
  
  		/// <summary>
  		/// Inserts the instance of ScrudFactory class on the database table "config.scrud_factory".
  		/// </summary>
  		/// <param name="scrudFactory">The instance of "ScrudFactory" class to insert.</param>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
  		public void Add(MixERP.Net.Entities.Config.ScrudFactory scrudFactory)
  		{
  			if(string.IsNullOrWhiteSpace(this.Catalog))
  			{
  				return;
  			}
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Create, this.LoginId, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to add entity \"ScrudFactory\" was denied to the user with Login ID {LoginId}. {ScrudFactory}", this.LoginId, scrudFactory);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  	
  			Factory.Insert(this.Catalog, scrudFactory);
  		}
  
  		/// <summary>
  		/// Updates the row of the table "config.scrud_factory" with an instance of "ScrudFactory" class against the primary key value.
  		/// </summary>
  		/// <param name="scrudFactory">The instance of "ScrudFactory" class to update.</param>
  		/// <param name="key">The value of the column "key" which will be updated.</param>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
  		public void Update(MixERP.Net.Entities.Config.ScrudFactory scrudFactory, string key)
  		{
  			if(string.IsNullOrWhiteSpace(this.Catalog))
  			{
  				return;
  			}
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Edit, this.LoginId, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to edit entity \"ScrudFactory\" with Primary Key {PrimaryKey} was denied to the user with Login ID {LoginId}. {ScrudFactory}", key, this.LoginId, scrudFactory);
  
gt:                     throw new UnauthorizedException("Access is denied.");