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.Core.Data
  {
      /// <summary>
      /// Provides simplified data access features to perform SCRUD operation on the database table "core.custom_field_setup".
      /// </summary>
      public class CustomFieldSetup : 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 "custom_field_setup".
          /// </summary>
          public override string _ObjectName => "custom_field_setup";
  
          /// <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 "core.custom_field_setup".
          /// </summary>
          /// <returns>Returns the number of rows of the table "core.custom_field_setup".</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 \"CustomFieldSetup\" was denied to the user with Login ID {LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT COUNT(*) FROM core.custom_field_setup;";
              return Factory.Scalar<long>(this._Catalog, sql);
          }
  
          /// <summary>
          /// Executes a select query on the table "core.custom_field_setup" to return a all instances of the "CustomFieldSetup" class to export. 
          /// </summary>
          /// <returns>Returns a non-live, non-mapped instances of "CustomFieldSetup" 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.CustomFieldSetup> Get()
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.ExportData, this._LoginId, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to the export entity \"CustomFieldSetup\" was denied to the user with Login ID {LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM core.custom_field_setup ORDER BY custom_field_setup_id;";
              return Factory.Get<MixERP.Net.Entities.Core.CustomFieldSetup>(this._Catalog, sql);
          }
  
          /// <summary>
          /// Executes a select query on the table "core.custom_field_setup" with a where filter on the column "custom_field_setup_id" to return a single instance of the "CustomFieldSetup" class. 
          /// </summary>
          /// <param name="customFieldSetupId">The column "custom_field_setup_id" parameter used on where filter.</param>
          /// <returns>Returns a non-live, non-mapped instance of "CustomFieldSetup" 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.CustomFieldSetup Get(int customFieldSetupId)
          {
              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 \"CustomFieldSetup\" filtered by \"CustomFieldSetupId\" with value {CustomFieldSetupId} was denied to the user with Login ID {_LoginId}", customFieldSetupId, this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM core.custom_field_setup WHERE custom_field_setup_id=@0;";
              return Factory.Get<MixERP.Net.Entities.Core.CustomFieldSetup>(this._Catalog, sql, customFieldSetupId).FirstOrDefault();
          }
  
          /// <summary>
          /// Executes a select query on the table "core.custom_field_setup" with a where filter on the column "custom_field_setup_id" to return a multiple instances of the "CustomFieldSetup" class. 
          /// </summary>
          /// <param name="customFieldSetupIds">Array of column "custom_field_setup_id" parameter used on where filter.</param>
          /// <returns>Returns a non-live, non-mapped collection of "CustomFieldSetup" 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.Core.CustomFieldSetup> Get(int[] customFieldSetupIds)
          {
              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 entity \"CustomFieldSetup\" was denied to the user with Login ID {LoginId}. customFieldSetupIds: {customFieldSetupIds}.", this._LoginId, customFieldSetupIds);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM core.custom_field_setup WHERE custom_field_setup_id IN (@0);";
  
              return Factory.Get<MixERP.Net.Entities.Core.CustomFieldSetup>(this._Catalog, sql, customFieldSetupIds);
          }
  
          /// <summary>
          /// Custom fields are user defined form elements for core.custom_field_setup.
          /// </summary>
          /// <returns>Returns an enumerable custom field collection for the table core.custom_field_setup</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, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to get custom fields for entity \"CustomFieldSetup\" 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='core.custom_field_setup' ORDER BY field_order;";
                  return Factory.Get<PetaPoco.CustomField>(this._Catalog, sql);
              }
  
              sql = "SELECT * from core.get_custom_field_definition('core.custom_field_setup'::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 core.custom_field_setup.
          /// </summary>
          /// <returns>Returns an enumerable name and value collection for the table core.custom_field_setup</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 \"CustomFieldSetup\" was denied to the user with Login ID {LoginId}", this._LoginId);
  
gt:                     throw new UnauthorizedException("Access is denied.");