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.Core.Data
  {
      /// <summary>
      /// Provides simplified data access features to perform SCRUD operation on the database table "core.tax_rate_types".
      /// </summary>
      public class TaxRateType : DbAccess
      {
          /// <summary>
          /// The schema of this table. Returns literal "core".
          /// </summary>
  	    public override string ObjectNamespace => "core";
  
          /// <summary>
          /// The schema unqualified name of this table. Returns literal "tax_rate_types".
          /// </summary>
  	    public override string ObjectName => "tax_rate_types";
  
          /// <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 "core.tax_rate_types".
  		/// </summary>
  		/// <returns>Returns the number of rows of the table "core.tax_rate_types".</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 \"TaxRateType\" was denied to the user with Login ID {LoginId}", this.LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  	
  			const string sql = "SELECT COUNT(*) FROM core.tax_rate_types;";
  			return Factory.Scalar<long>(this.Catalog, sql);
  		}
  
  		/// <summary>
  		/// Executes a select query on the table "core.tax_rate_types" with a where filter on the column "tax_rate_type_code" to return a single instance of the "TaxRateType" class. 
  		/// </summary>
  		/// <param name="taxRateTypeCode">The column "tax_rate_type_code" parameter used on where filter.</param>
  		/// <returns>Returns a non-live, non-mapped instance of "TaxRateType" 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.Core.TaxRateType Get(string taxRateTypeCode)
  		{
  			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 \"TaxRateType\" filtered by \"TaxRateTypeCode\" with value {TaxRateTypeCode} was denied to the user with Login ID {LoginId}", taxRateTypeCode, this.LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  	
  			const string sql = "SELECT * FROM core.tax_rate_types WHERE tax_rate_type_code=@0;";
  			return Factory.Get<MixERP.Net.Entities.Core.TaxRateType>(this.Catalog, sql, taxRateTypeCode).FirstOrDefault();
  		}
  
          /// <summary>
          /// Displayfields provide a minimal name/value context for data binding the row collection of core.tax_rate_types.
          /// </summary>
          /// <returns>Returns an enumerable name and value collection for the table core.tax_rate_types</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 \"TaxRateType\" was denied to the user with Login ID {LoginId}", this.LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  	
  			const string sql = "SELECT tax_rate_type_code AS key, tax_rate_type_code || ' (' || tax_rate_type_name || ')' as value FROM core.tax_rate_types;";
  			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 TaxRateType class on the database table "core.tax_rate_types".
  		/// </summary>
  		/// <param name="taxRateType">The instance of "TaxRateType" 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.Core.TaxRateType taxRateType)
  		{
  			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 \"TaxRateType\" was denied to the user with Login ID {LoginId}. {TaxRateType}", this.LoginId, taxRateType);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  	
  			Factory.Insert(this.Catalog, taxRateType);
  		}
  
  		/// <summary>
  		/// Updates the row of the table "core.tax_rate_types" with an instance of "TaxRateType" class against the primary key value.
  		/// </summary>
  		/// <param name="taxRateType">The instance of "TaxRateType" class to update.</param>
  		/// <param name="taxRateTypeCode">The value of the column "tax_rate_type_code" 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.Core.TaxRateType taxRateType, string taxRateTypeCode)
  		{
  			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 \"TaxRateType\" with Primary Key {PrimaryKey} was denied to the user with Login ID {LoginId}. {TaxRateType}", taxRateTypeCode, this.LoginId, taxRateType);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  	
  			Factory.Update(this.Catalog, taxRateType, taxRateTypeCode);
  		}
  
  		/// <summary>
  		/// Deletes the row of the table "core.tax_rate_types" against the primary key value.
  		/// </summary>
  		/// <param name="taxRateTypeCode">The value of the column "tax_rate_type_code" which will be deleted.</param>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
  		public void Delete(string taxRateTypeCode)
  		{
  			if(string.IsNullOrWhiteSpace(this.Catalog))
  			{
  				return;
  			}
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Delete, this.LoginId, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to delete entity \"TaxRateType\" with Primary Key {PrimaryKey} was denied to the user with Login ID {LoginId}.", taxRateTypeCode, this.LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  	
  			const string sql = "DELETE FROM core.tax_rate_types WHERE tax_rate_type_code=@0;";
  			Factory.NonQuery(this.Catalog, sql, taxRateTypeCode);
  		}
  
  		/// <summary>
  		/// Performs a select statement on table "core.tax_rate_types" producing a paged result of 25.
  		/// </summary>
  		/// <returns>Returns the first page of collection of "TaxRateType" class.</returns>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
  		public IEnumerable<MixERP.Net.Entities.Core.TaxRateType> GetPagedResult()
  		{
  			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 first page of the entity \"TaxRateType\" was denied to the user with Login ID {LoginId}.", this.LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  	
  			const string sql = "SELECT * FROM core.tax_rate_types ORDER BY tax_rate_type_code LIMIT 25 OFFSET 0;";
  			return Factory.Get<MixERP.Net.Entities.Core.TaxRateType>(this.Catalog, sql);
  		}
  
  		/// <summary>
  		/// Performs a select statement on table "core.tax_rate_types" producing a paged result of 25.
  		/// </summary>
  		/// <param name="pageNumber">Enter the page number to produce the paged result.</param>
  		/// <returns>Returns collection of "TaxRateType" class.</returns>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
  		public IEnumerable<MixERP.Net.Entities.Core.TaxRateType> GetPagedResult(long pageNumber)
  		{
  			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 Page #{Page} of the entity \"TaxRateType\" was denied to the user with Login ID {LoginId}.", pageNumber, this.LoginId);
  
gt:                     throw new UnauthorizedException("Access is denied.");