context: // ReSharper disable All
  using System.Collections.Generic;
  using System.Data;
  using System.Dynamic;
  using System.Linq;
  using MixERP.Net.DbFactory;
  using MixERP.Net.EntityParser;
  using MixERP.Net.Framework;
  using MixERP.Net.Framework.Extensions;
  using Npgsql;
  using PetaPoco;
  using Serilog;
  
  namespace MixERP.Net.Schemas.Policy.Data
  {
      /// <summary>
      /// Provides simplified data access features to perform SCRUD operation on the database table "policy.access_types".
      /// </summary>
      public class AccessType : DbAccess, IAccessTypeRepository
      {
          /// <summary>
          /// The schema of this table. Returns literal "policy".
          /// </summary>
          public override string _ObjectNamespace => "policy";
  
          /// <summary>
          /// The schema unqualified name of this table. Returns literal "access_types".
          /// </summary>
          public override string _ObjectName => "access_types";
  
          /// <summary>
          /// Login id of application user accessing this table.
          /// </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 table "policy.access_types".
          /// </summary>
          /// <returns>Returns the number of rows of the table "policy.access_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, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to count entity \"AccessType\" was denied to the user with Login ID {LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT COUNT(*) FROM policy.access_types;";
              return Factory.Scalar<long>(this._Catalog, sql);
          }
  
          /// <summary>
          /// Executes a select query on the table "policy.access_types" to return all instances of the "AccessType" class. 
          /// </summary>
          /// <returns>Returns a non-live, non-mapped instances of "AccessType" 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.Policy.AccessType> GetAll()
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.ExportData, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to the export entity \"AccessType\" was denied to the user with Login ID {LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM policy.access_types ORDER BY access_type_id;";
              return Factory.Get<MixERP.Net.Entities.Policy.AccessType>(this._Catalog, sql);
          }
  
          /// <summary>
          /// Executes a select query on the table "policy.access_types" to return all instances of the "AccessType" class to export. 
          /// </summary>
          /// <returns>Returns a non-live, non-mapped instances of "AccessType" class.</returns>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
          public IEnumerable<dynamic> Export()
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.ExportData, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to the export entity \"AccessType\" was denied to the user with Login ID {LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM policy.access_types ORDER BY access_type_id;";
              return Factory.Get<dynamic>(this._Catalog, sql);
          }
  
          /// <summary>
          /// Executes a select query on the table "policy.access_types" with a where filter on the column "access_type_id" to return a single instance of the "AccessType" class. 
          /// </summary>
          /// <param name="accessTypeId">The column "access_type_id" parameter used on where filter.</param>
          /// <returns>Returns a non-live, non-mapped instance of "AccessType" 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.Policy.AccessType Get(int accessTypeId)
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to the get entity \"AccessType\" filtered by \"AccessTypeId\" with value {AccessTypeId} was denied to the user with Login ID {_LoginId}", accessTypeId, this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM policy.access_types WHERE access_type_id=@0;";
              return Factory.Get<MixERP.Net.Entities.Policy.AccessType>(this._Catalog, sql, accessTypeId).FirstOrDefault();
          }
  
          /// <summary>
          /// Gets the first record of the table "policy.access_types". 
          /// </summary>
          /// <returns>Returns a non-live, non-mapped instance of "AccessType" 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.Policy.AccessType GetFirst()
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to the get the first record of entity \"AccessType\" was denied to the user with Login ID {_LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM policy.access_types ORDER BY access_type_id LIMIT 1;";
              return Factory.Get<MixERP.Net.Entities.Policy.AccessType>(this._Catalog, sql).FirstOrDefault();
          }
  
          /// <summary>
          /// Gets the previous record of the table "policy.access_types" sorted by accessTypeId.
          /// </summary>
          /// <param name="accessTypeId">The column "access_type_id" parameter used to find the next record.</param>
          /// <returns>Returns a non-live, non-mapped instance of "AccessType" 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.Policy.AccessType GetPrevious(int accessTypeId)
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to the get the previous entity of \"AccessType\" by \"AccessTypeId\" with value {AccessTypeId} was denied to the user with Login ID {_LoginId}", accessTypeId, this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM policy.access_types WHERE access_type_id < @0 ORDER BY access_type_id DESC LIMIT 1;";
              return Factory.Get<MixERP.Net.Entities.Policy.AccessType>(this._Catalog, sql, accessTypeId).FirstOrDefault();
          }
  
          /// <summary>
          /// Gets the next record of the table "policy.access_types" sorted by accessTypeId.
          /// </summary>
          /// <param name="accessTypeId">The column "access_type_id" parameter used to find the next record.</param>
          /// <returns>Returns a non-live, non-mapped instance of "AccessType" 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.Policy.AccessType GetNext(int accessTypeId)
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to the get the next entity of \"AccessType\" by \"AccessTypeId\" with value {AccessTypeId} was denied to the user with Login ID {_LoginId}", accessTypeId, this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM policy.access_types WHERE access_type_id > @0 ORDER BY access_type_id LIMIT 1;";
              return Factory.Get<MixERP.Net.Entities.Policy.AccessType>(this._Catalog, sql, accessTypeId).FirstOrDefault();
          }
  
  
          /// <summary>
          /// Gets the last record of the table "policy.access_types". 
          /// </summary>
          /// <returns>Returns a non-live, non-mapped instance of "AccessType" 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.Policy.AccessType GetLast()
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to the get the last record of entity \"AccessType\" was denied to the user with Login ID {_LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM policy.access_types ORDER BY access_type_id DESC LIMIT 1;";
              return Factory.Get<MixERP.Net.Entities.Policy.AccessType>(this._Catalog, sql).FirstOrDefault();
          }
  
          /// <summary>
          /// Executes a select query on the table "policy.access_types" with a where filter on the column "access_type_id" to return a multiple instances of the "AccessType" class. 
          /// </summary>
          /// <param name="accessTypeIds">Array of column "access_type_id" parameter used on where filter.</param>
          /// <returns>Returns a non-live, non-mapped collection of "AccessType" 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 IEnumerable<MixERP.Net.Entities.Policy.AccessType> Get(int[] accessTypeIds)
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to entity \"AccessType\" was denied to the user with Login ID {LoginId}. accessTypeIds: {accessTypeIds}.", this._LoginId, accessTypeIds);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM policy.access_types WHERE access_type_id IN (@0);";
  
              return Factory.Get<MixERP.Net.Entities.Policy.AccessType>(this._Catalog, sql, accessTypeIds);
          }
  
          /// <summary>
          /// Custom fields are user defined form elements for policy.access_types.
          /// </summary>
          /// <returns>Returns an enumerable custom field collection for the table policy.access_types</returns>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
          public IEnumerable<PetaPoco.CustomField> GetCustomFields(string resourceId)
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to get custom fields for entity \"AccessType\" was denied to the user with Login ID {LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              string sql;
              if (string.IsNullOrWhiteSpace(resourceId))
              {
                  sql = "SELECT * FROM core.custom_field_definition_view WHERE table_name='policy.access_types' ORDER BY field_order;";
                  return Factory.Get<PetaPoco.CustomField>(this._Catalog, sql);
              }
  
              sql = "SELECT * from core.get_custom_field_definition('policy.access_types'::text, @0::text) ORDER BY field_order;";
              return Factory.Get<PetaPoco.CustomField>(this._Catalog, sql, resourceId);
          }
  
          /// <summary>
          /// Displayfields provide a minimal name/value context for data binding the row collection of policy.access_types.
          /// </summary>
          /// <returns>Returns an enumerable name and value collection for the table policy.access_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, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to get display field for entity \"AccessType\" was denied to the user with Login ID {LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT access_type_id AS key, access_type_name as value FROM policy.access_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 or updates the instance of AccessType class on the database table "policy.access_types".
          /// </summary>
          /// <param name="accessType">The instance of "AccessType" class to insert or update.</param>
          /// <param name="customFields">The custom field collection.</param>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
          public object AddOrEdit(dynamic accessType, List<EntityParser.CustomField> customFields)
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
  
  
              object primaryKeyValue = accessType.access_type_id;
  
              if (Cast.To<int>(primaryKeyValue) > 0)
              {
                  primaryKeyValue = accessType.access_type_id;
                  this.Update(accessType, int.Parse(accessType.access_type_id));
              }
              else
              {
                  primaryKeyValue = this.Add(accessType);
              }
  
              string sql = "DELETE FROM core.custom_fields WHERE custom_field_setup_id IN(" +
                           "SELECT custom_field_setup_id " +
                           "FROM core.custom_field_setup " +
                           "WHERE form_name=core.get_custom_field_form_name('policy.access_types')" +
                           ");";
  
              Factory.NonQuery(this._Catalog, sql);
  
              if (customFields == null)
              {
                  return primaryKeyValue;
              }
  
              foreach (var field in customFields)
              {
                  sql = "INSERT INTO core.custom_fields(custom_field_setup_id, resource_id, value) " +
                        "SELECT core.get_custom_field_setup_id_by_table_name('policy.access_types', @0::character varying(100)), " +
                        "@1, @2;";
  
                  Factory.NonQuery(this._Catalog, sql, field.FieldName, primaryKeyValue, field.Value);
              }
  
              return primaryKeyValue;
          }
  
          /// <summary>
          /// Inserts the instance of AccessType class on the database table "policy.access_types".
          /// </summary>
          /// <param name="accessType">The instance of "AccessType" class to insert.</param>
          /// <exception cref="UnauthorizedException">Thown when the application user does not have sufficient privilege to perform this action.</exception>
          public object Add(dynamic accessType)
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Create, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to add entity \"AccessType\" was denied to the user with Login ID {LoginId}. {AccessType}", this._LoginId, accessType);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              return Factory.Insert(this._Catalog, accessType, "policy.access_types", "access_type_id");
          }
  
          /// <summary>
          /// Inserts or updates multiple instances of AccessType class on the database table "policy.access_types";
          /// </summary>
          /// <param name="accessTypes">List of "AccessType" class to import.</param>
          /// <returns></returns>
          public List<object> BulkImport(List<ExpandoObject> accessTypes)
          {
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.ImportData, this._LoginId, this._Catalog, false);
                  }
  
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to import entity \"AccessType\" was denied to the user with Login ID {LoginId}. {accessTypes}", this._LoginId, accessTypes);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              var result = new List<object>();
              int line = 0;
              try
              {
                  using (Database db = new Database(Factory.GetConnectionString(this._Catalog), Factory.ProviderName))
                  {
                      using (Transaction transaction = db.GetTransaction())
                      {
                          foreach (dynamic accessType in accessTypes)
                          {
                              line++;
  
  
  
                              object primaryKeyValue = accessType.access_type_id;
  
                              if (Cast.To<int>(primaryKeyValue) > 0)
                              {
                                  result.Add(accessType.access_type_id);
                                  db.Update("policy.access_types", "access_type_id", accessType, accessType.access_type_id);
                              }
                              else
                              {
                                  result.Add(db.Insert("policy.access_types", "access_type_id", accessType));
                              }
                          }
  
                          transaction.Complete();
                      }
  
                      return result;
                  }
              }
              catch (NpgsqlException ex)
              {
                  string errorMessage = $"Error on line {line} ";
  
                  if (ex.Code.StartsWith("P"))
                  {
                      errorMessage += Factory.GetDBErrorResource(ex);
  
                      throw new MixERPException(errorMessage, ex);
                  }
  
                  errorMessage += ex.Message;
                  throw new MixERPException(errorMessage, ex);
              }
              catch (System.Exception ex)
              {
                  string errorMessage = $"Error on line {line} ";
                  throw new MixERPException(errorMessage, ex);
              }
          }
  
          /// <summary>
          /// Updates the row of the table "policy.access_types" with an instance of "AccessType" class against the primary key value.
          /// </summary>
          /// <param name="accessType">The instance of "AccessType" class to update.</param>
          /// <param name="accessTypeId">The value of the column "access_type_id" 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(dynamic accessType, int accessTypeId)
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Edit, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to edit entity \"AccessType\" with Primary Key {PrimaryKey} was denied to the user with Login ID {LoginId}. {AccessType}", accessTypeId, this._LoginId, accessType);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              Factory.Update(this._Catalog, accessType, accessTypeId, "policy.access_types", "access_type_id");
          }
  
          /// <summary>
          /// Deletes the row of the table "policy.access_types" against the primary key value.
          /// </summary>
          /// <param name="accessTypeId">The value of the column "access_type_id" 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(int accessTypeId)
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Delete, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to delete entity \"AccessType\" with Primary Key {PrimaryKey} was denied to the user with Login ID {LoginId}.", accessTypeId, this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "DELETE FROM policy.access_types WHERE access_type_id=@0;";
              Factory.NonQuery(this._Catalog, sql, accessTypeId);
          }
  
          /// <summary>
          /// Performs a select statement on table "policy.access_types" producing a paginated result of 10.
          /// </summary>
          /// <returns>Returns the first page of collection of "AccessType" 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.Policy.AccessType> GetPaginatedResult()
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to the first page of the entity \"AccessType\" was denied to the user with Login ID {LoginId}.", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM policy.access_types ORDER BY access_type_id LIMIT 10 OFFSET 0;";
              return Factory.Get<MixERP.Net.Entities.Policy.AccessType>(this._Catalog, sql);
          }
  
          /// <summary>
          /// Performs a select statement on table "policy.access_types" producing a paginated result of 10.
          /// </summary>
          /// <param name="pageNumber">Enter the page number to produce the paginated result.</param>
          /// <returns>Returns collection of "AccessType" 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.Policy.AccessType> GetPaginatedResult(long pageNumber)
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to Page #{Page} of the entity \"AccessType\" 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 policy.access_types ORDER BY access_type_id LIMIT 10 OFFSET @0;";
  
              return Factory.Get<MixERP.Net.Entities.Policy.AccessType>(this._Catalog, sql, offset);
          }
  
          public List<EntityParser.Filter> GetFilters(string catalog, string filterName)
          {
              const string sql = "SELECT * FROM core.filters WHERE object_name='policy.access_types' AND lower(filter_name)=lower(@0);";
              return Factory.Get<EntityParser.Filter>(catalog, sql, filterName).ToList();
          }
  
          /// <summary>
          /// Performs a filtered count on table "policy.access_types".
          /// </summary>
          /// <param name="filters">The list of filter conditions.</param>
          /// <returns>Returns number of rows of "AccessType" 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, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to count entity \"AccessType\" 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 policy.access_types WHERE 1 = 1");
              MixERP.Net.EntityParser.Data.Service.AddFilters(ref sql, new MixERP.Net.Entities.Policy.AccessType(), filters);
  
              return Factory.Scalar<long>(this._Catalog, sql);
          }
  
          /// <summary>
          /// Performs a filtered select statement on table "policy.access_types" producing a paginated result of 10.
          /// </summary>
          /// <param name="pageNumber">Enter the page number to produce the paginated result. If you provide a negative number, the result will not be paginated.</param>
          /// <param name="filters">The list of filter conditions.</param>
          /// <returns>Returns collection of "AccessType" 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.Policy.AccessType> 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, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to Page #{Page} of the filtered entity \"AccessType\" was denied to the user with Login ID {LoginId}. Filters: {Filters}.", pageNumber, this._LoginId, filters);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              long offset = (pageNumber - 1) * 10;
              Sql sql = Sql.Builder.Append("SELECT * FROM policy.access_types WHERE 1 = 1");
  
              MixERP.Net.EntityParser.Data.Service.AddFilters(ref sql, new MixERP.Net.Entities.Policy.AccessType(), filters);
  
              sql.OrderBy("access_type_id");
  
              if (pageNumber > 0)
              {
                  sql.Append("LIMIT @0", 10);
                  sql.Append("OFFSET @0", offset);
              }
  
              return Factory.Get<MixERP.Net.Entities.Policy.AccessType>(this._Catalog, sql);
          }
  
          /// <summary>
          /// Performs a filtered count on table "policy.access_types".
          /// </summary>
          /// <param name="filterName">The named filter.</param>
          /// <returns>Returns number of rows of "AccessType" 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 CountFiltered(string filterName)
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return 0;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Read, this._LoginId, this._Catalog, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to count entity \"AccessType\" was denied to the user with Login ID {LoginId}. Filter: {Filter}.", this._LoginId, filterName);
  
gt:                     throw new UnauthorizedException("Access is denied.");