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.frequency_setups".
      /// </summary>
      public class FrequencySetup : 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 "frequency_setups".
          /// </summary>
          public override string _ObjectName => "frequency_setups";
  
          /// <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.frequency_setups".
          /// </summary>
          /// <returns>Returns the number of rows of the table "core.frequency_setups".</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 \"FrequencySetup\" was denied to the user with Login ID {LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT COUNT(*) FROM core.frequency_setups;";
              return Factory.Scalar<long>(this._Catalog, sql);
          }
  
          /// <summary>
          /// Executes a select query on the table "core.frequency_setups" to return a all instances of the "FrequencySetup" class to export. 
          /// </summary>
          /// <returns>Returns a non-live, non-mapped instances of "FrequencySetup" 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.FrequencySetup> 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 \"FrequencySetup\" was denied to the user with Login ID {LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM core.frequency_setups ORDER BY frequency_setup_id;";
              return Factory.Get<MixERP.Net.Entities.Core.FrequencySetup>(this._Catalog, sql);
          }
  
          /// <summary>
          /// Executes a select query on the table "core.frequency_setups" with a where filter on the column "frequency_setup_id" to return a single instance of the "FrequencySetup" class. 
          /// </summary>
          /// <param name="frequencySetupId">The column "frequency_setup_id" parameter used on where filter.</param>
          /// <returns>Returns a non-live, non-mapped instance of "FrequencySetup" 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.FrequencySetup Get(int frequencySetupId)
          {
              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 \"FrequencySetup\" filtered by \"FrequencySetupId\" with value {FrequencySetupId} was denied to the user with Login ID {_LoginId}", frequencySetupId, this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM core.frequency_setups WHERE frequency_setup_id=@0;";
              return Factory.Get<MixERP.Net.Entities.Core.FrequencySetup>(this._Catalog, sql, frequencySetupId).FirstOrDefault();
          }
  
          /// <summary>
          /// Executes a select query on the table "core.frequency_setups" with a where filter on the column "frequency_setup_id" to return a multiple instances of the "FrequencySetup" class. 
          /// </summary>
          /// <param name="frequencySetupIds">Array of column "frequency_setup_id" parameter used on where filter.</param>
          /// <returns>Returns a non-live, non-mapped collection of "FrequencySetup" 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.FrequencySetup> Get(int[] frequencySetupIds)
          {
              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 \"FrequencySetup\" was denied to the user with Login ID {LoginId}. frequencySetupIds: {frequencySetupIds}.", this._LoginId, frequencySetupIds);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT * FROM core.frequency_setups WHERE frequency_setup_id IN (@0);";
  
              return Factory.Get<MixERP.Net.Entities.Core.FrequencySetup>(this._Catalog, sql, frequencySetupIds);
          }
  
          /// <summary>
          /// Custom fields are user defined form elements for core.frequency_setups.
          /// </summary>
          /// <returns>Returns an enumerable custom field collection for the table core.frequency_setups</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 \"FrequencySetup\" 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.frequency_setups' ORDER BY field_order;";
                  return Factory.Get<PetaPoco.CustomField>(this._Catalog, sql);
              }
  
              sql = "SELECT * from core.get_custom_field_definition('core.frequency_setups'::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.frequency_setups.
          /// </summary>
          /// <returns>Returns an enumerable name and value collection for the table core.frequency_setups</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 \"FrequencySetup\" was denied to the user with Login ID {LoginId}", this._LoginId);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              const string sql = "SELECT frequency_setup_id AS key, frequency_setup_code as value FROM core.frequency_setups;";
              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 FrequencySetup class on the database table "core.frequency_setups".
          /// </summary>
          /// <param name="frequencySetup">The instance of "FrequencySetup" 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.Core.FrequencySetup frequencySetup, List<EntityParser.CustomField> customFields)
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              object primaryKeyValue;
  
              frequencySetup.AuditUserId = this._UserId;
              frequencySetup.AuditTs = System.DateTime.UtcNow;
  
              if (frequencySetup.FrequencySetupId > 0)
              {
                  primaryKeyValue = frequencySetup.FrequencySetupId;
                  this.Update(frequencySetup, frequencySetup.FrequencySetupId);
              }
              else
              {
                  primaryKeyValue = this.Add(frequencySetup);
              }
  
              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('core.frequency_setups')" +
                           ");";
  
              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('core.frequency_setups', @0::character varying(100)), " +
                        "@1, @2;";
  
                  Factory.NonQuery(this._Catalog, sql, field.FieldName, primaryKeyValue, field.Value);
              }
  
              return primaryKeyValue;
          }
  
          /// <summary>
          /// Inserts the instance of FrequencySetup class on the database table "core.frequency_setups".
          /// </summary>
          /// <param name="frequencySetup">The instance of "FrequencySetup" 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(MixERP.Net.Entities.Core.FrequencySetup frequencySetup)
          {
              if (string.IsNullOrWhiteSpace(this._Catalog))
              {
                  return null;
              }
  
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.Create, this._LoginId, false);
                  }
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to add entity \"FrequencySetup\" was denied to the user with Login ID {LoginId}. {FrequencySetup}", this._LoginId, frequencySetup);
                      throw new UnauthorizedException("Access is denied.");
                  }
              }
  
              return Factory.Insert(this._Catalog, frequencySetup);
          }
  
          /// <summary>
          /// Inserts or updates multiple instances of FrequencySetup class on the database table "core.frequency_setups";
          /// </summary>
          /// <param name="frequencySetups">List of "FrequencySetup" class to import.</param>
          /// <returns></returns>
          public List<object> BulkImport(List<MixERP.Net.Entities.Core.FrequencySetup> frequencySetups)
          {
              if (!this.SkipValidation)
              {
                  if (!this.Validated)
                  {
                      this.Validate(AccessTypeEnum.ImportData, this._LoginId, false);
                  }
  
                  if (!this.HasAccess)
                  {
                      Log.Information("Access to import entity \"FrequencySetup\" was denied to the user with Login ID {LoginId}. {frequencySetups}", this._LoginId, frequencySetups);
                      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 (var frequencySetup in frequencySetups)
                          {
                              line++;
  
                              frequencySetup.AuditUserId = this._UserId;
                              frequencySetup.AuditTs = System.DateTime.UtcNow;
  
                              if (frequencySetup.FrequencySetupId > 0)
                              {
                                  result.Add(frequencySetup.FrequencySetupId);
                                  db.Update(frequencySetup, frequencySetup.FrequencySetupId);
                              }
                              else
                              {
                                  result.Add(db.Insert(frequencySetup));
                              }
                          }
  
                          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);
                  }
  
  
gt:                 errorMessage += ex.Message;