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 table "config.switches".
      /// </summary>
      public class Switch : 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 "switches".
          /// </summary>
          public override string _ObjectName => "switches";
  
          /// <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 "config.switches".
          /// </summary>
          /// <returns>Returns the number of rows of the table "config.switches".</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 \"Switch\" was denied to the user with Login ID {LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT COUNT(*) FROM config.switches;";
              return Factory.Scalar<long>(this._Catalog, sql);
          }
  
          /// <summary>
          /// Executes a select query on the table "config.switches" to return a all instances of the "Switch" class to export. 
          /// </summary>
          /// <returns>Returns a non-live, non-mapped instances of "Switch" 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.Switch> 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 \"Switch\" was denied to the user with Login ID {LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM config.switches ORDER BY key;";
              return Factory.Get<MixERP.Net.Entities.Config.Switch>(this._Catalog, sql);
          }
  
          /// <summary>
          /// Executes a select query on the table "config.switches" with a where filter on the column "key" to return a single instance of the "Switch" class. 
          /// </summary>
          /// <param name="key">The column "key" parameter used on where filter.</param>
          /// <returns>Returns a non-live, non-mapped instance of "Switch" 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.Switch 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 \"Switch\" 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.switches WHERE key=@0;";
              return Factory.Get<MixERP.Net.Entities.Config.Switch>(this._Catalog, sql, key).FirstOrDefault();
          }
  
          /// <summary>
          /// Executes a select query on the table "config.switches" with a where filter on the column "key" to return a multiple instances of the "Switch" class. 
          /// </summary>
          /// <param name="keys">Array of column "key" parameter used on where filter.</param>
          /// <returns>Returns a non-live, non-mapped collection of "Switch" 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.Config.Switch> Get(string[] keys)
          {
              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 \"Switch\" was denied to the user with Login ID {LoginId}. keys: {keys}.", this._LoginId, keys);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM config.switches WHERE key IN (@0);";
  
              return Factory.Get<MixERP.Net.Entities.Config.Switch>(this._Catalog, sql, keys);
          }
  
          /// <summary>
          /// Custom fields are user defined form elements for config.switches.
          /// </summary>
          /// <returns>Returns an enumerable custom field collection for the table config.switches</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 \"Switch\" 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='config.switches' ORDER BY field_order;";
                  return Factory.Get<PetaPoco.CustomField>(this._Catalog, sql);
              }
  
              sql = "SELECT * from core.get_custom_field_definition('config.switches'::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 config.switches.
          /// </summary>
          /// <returns>Returns an enumerable name and value collection for the table config.switches</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 \"Switch\" 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.switches;";
              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 Switch class on the database table "config.switches".
          /// </summary>
          /// <param name="switchParameter">The instance of "Switch" 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(MixERP.Net.Entities.Config.Switch switchParameter, List<EntityParser.CustomField> customFields)
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              object primaryKeyValue;
  
              switchParameter.AuditUserId = this._UserId;
              switchParameter.AuditTs = System.DateTime.UtcNow;
  
              if (!string.IsNullOrWhiteSpace(switchParameter.Key))
              {
                  primaryKeyValue = switchParameter.Key;
                  this.Update(switchParameter, switchParameter.Key);
              }
              else
              {
  
gt:                 primaryKeyValue = this.Add(switchParameter);