context: // ReSharper disable All
  /********************************************************************************
  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 view "config.mixerp_scrud_view".
      /// </summary>
      public class MixerpScrudView : DbAccess
      {
          /// <summary>
          /// The schema of this view. Returns literal "config".
          /// </summary>
          public override string _ObjectNamespace => "config";
  
          /// <summary>
          /// The schema unqualified name of this view. Returns literal "mixerp_scrud_view".
          /// </summary>
          public override string _ObjectName => "mixerp_scrud_view";
  
          /// <summary>
          /// Login id of application user accessing this view.
          /// </summary>
          public long _LoginId { get; set; }
  
          /// <summary>
          /// User id of application user accessing this table.
          /// </summary>
          public int _UserId { 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 view "config.mixerp_scrud_view".
          /// </summary>
          /// <returns>Returns the number of rows of the view "config.mixerp_scrud_view".</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 \"MixerpScrudView\" was denied to the user with Login ID {LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT COUNT(*) FROM config.mixerp_scrud_view;";
              return Factory.Scalar<long>(this._Catalog, sql);
          }
  
  
  
          /// <summary>
          /// Performs a select statement on the view "config.mixerp_scrud_view" producing a paged result of 10.
          /// </summary>
          /// <returns>Returns the first page of collection of "MixerpScrudView" 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.Config.MixerpScrudView> 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 \"MixerpScrudView\" was denied to the user with Login ID {LoginId}.", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM config.mixerp_scrud_view ORDER BY 1 LIMIT 10 OFFSET 0;";
              return Factory.Get<MixERP.Net.Entities.Config.MixerpScrudView>(this._Catalog, sql);
          }
  
          /// <summary>
          /// Performs a select statement on the view "config.mixerp_scrud_view" producing a paged result of 10.
          /// </summary>
          /// <param name="pageNumber">Enter the page number to produce the paged result.</param>
          /// <returns>Returns collection of "MixerpScrudView" 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.Config.MixerpScrudView> 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 \"MixerpScrudView\" was denied to the user with Login ID {LoginId}.", pageNumber, this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              long offset = (pageNumber - 1) * 10;
              const string sql = "SELECT * FROM config.mixerp_scrud_view ORDER BY 1 LIMIT 10 OFFSET @0;";
  
              return Factory.Get<MixERP.Net.Entities.Config.MixerpScrudView>(this._Catalog, sql, offset);
          }
  
          private List<EntityParser.Filter> GetFilters(string catalog, string filterName)
          {
              const string sql = "SELECT * FROM core.filters WHERE object_name='config.mixerp_scrud_view' AND lower(filter_name)=lower(@0);";
              return Factory.Get<EntityParser.Filter>(catalog, sql, filterName).ToList();
          }
  
          /// <summary>
          /// Performs a filtered count on view "config.mixerp_scrud_view".
          /// </summary>
          /// <param name="filters">The list of filter conditions.</param>
          /// <returns>Returns number of rows of "MixerpScrudView" class using the filter.</returns>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
          public long CountWhere(List<EntityParser.Filter> filters)
          {
              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 \"MixerpScrudView\" was denied to the user with Login ID {LoginId}. Filters: {Filters}.", this._LoginId, filters);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              Sql sql = Sql.Builder.Append("SELECT COUNT(*) FROM config.mixerp_scrud_view WHERE 1 = 1");
              MixERP.Net.EntityParser.Data.Service.AddFilters(ref sql, new MixERP.Net.Entities.Config.MixerpScrudView(), filters);
  
              return Factory.Scalar<long>(this._Catalog, sql);
          }
  
          /// <summary>
          /// Performs a filtered select statement on view "config.mixerp_scrud_view" producing a paged result of 10.
          /// </summary>
          /// <param name="pageNumber">Enter the page number to produce the paged result.</param>
          /// <param name="filters">The list of filter conditions.</param>
          /// <returns>Returns collection of "MixerpScrudView" 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.Config.MixerpScrudView> GetWhere(long pageNumber, List<EntityParser.Filter> filters)
          {
              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 filtered entity \"MixerpScrudView\" was denied to the user with Login ID {LoginId}. Filters: {Filters}.", pageNumber, this._LoginId, filters);
  
gt:                     throw new UnauthorizedException("Access is denied.");