<commit_before>require 'wrappers/timing'

#
# == SeasonCreator
#
# Collection of functions used for ceating a new season based on an older one
# When creating new season should consider:
# - season
# - category_types
# - meetings (with sessions and events)
#
# @author   Leega
# @version  4.00.829
#
class SeasonCreator
  include SqlConvertable

  # These can be edited later on:
  attr_accessor :older_season, :description, :new_id, :begin_date, :end_date, :header_year, :edition, 
                :new_season, :categories, :meetings, :meeting_sessions, :meeting_events

  # Initialization
  #
  # == Params:
  # An instance of season used as base for the new one
  #
  def initialize( older_season, description )
    unless older_season && older_season.instance_of?( Season )
      raise ArgumentError.new("Needs the older season to use as model")
    end
    unless description && description.instance_of?( String )
      raise ArgumentError.new("Needs the new season description")
    end
    @older_season = older_season
    @description  = description
    
    @new_id       = older_season.id + 10
    @begin_date   = older_season.begin_date.next_year
    @end_date     = older_season.end_date.next_year
    @header_year  = SeasonCreator.next_header_year( older_season.header_year ) 
    @edition      = older_season.edition + 1 

    if Season.exists?( @new_id )
      raise ArgumentError.new("Season " + @older_season.description + " already duplicated")
    end

    @new_season       = nil
    @categories       = []
    @meetings         = []
    @meeting_sessions = []
    @meeting_events   = []
  end
  #-- --------------------------------------------------------------------------
  #++

  # Prepare data for duplication
  # 
  def prepare_new_season
    create_sql_diff_header( "Duplicating season #{@older_season.id}-#{@older_season.description} into #{@new_id}-#{@description}" )
    @new_season = renew_season
    @categories = renew_categories
    @meetings   = renew_meetings
    create_sql_diff_footer( "#{@new_id}-#{@description} duplication done" )
  end

  # Retreive older season categories and prepare them
  # for the new season
  # 
  def renew_season
    sql_diff_text_log << "-- Season\r\n"
    newer_season = Season.new( @older_season.attributes.reject{ |e| ['id', 'lock_version','created_at','updated_at'].include?(e) } )
    newer_season.id          = @new_id
    newer_season.description = @description
    newer_season.begin_date  = @begin_date
    newer_season.end_date    = @end_date
    newer_season.header_year = @header_year
    newer_season.edition     = @edition
    newer_season.rules       = nil
    newer_season.save
    sql_diff_text_log << to_sql_insert( newer_season, false, "\r\n\r\n" ) # no additional comment
    newer_season
  end

  # Retreive older season categories and prepare them
  # for the new season
  # No particular change required to category types data 
  # just associate with new season
  # 
  def renew_categories
    newer_categories = []
    sql_diff_text_log << "-- Categories\r\n"
    @older_season.category_types.each do |category_type|
      newer_category = CategoryType.new( category_type.attributes.reject{ |e| ['id', 'lock_version','created_at','updated_at'].include?(e) } )
      newer_category.season_id = @new_id
      newer_category.save
      sql_diff_text_log << to_sql_insert( newer_category, false, "\r\n" ) # no additional comment
      newer_categories << newer_category
    end
    sql_diff_text_log << "\r\n"
    newer_categories
  end

  # Retreive older season meetings and prepare them
  # for the new season
  # 
  def renew_meetings
    newer_meetings = []
    add_sql_diff_comment( "Meetings" )
    @older_season.meetings.each do |meeting|
      sql_diff_text_log << "-- #{meeting.id}-#{meeting.code} #{meeting.description} #{meeting.header_date}\r\n"
      newer_meeting = Meeting.new( meeting.attributes.reject{ |e| [''id, 'lock_version','created_at','updated_at'].include?(e) } )
      newer_meeting.id                   = meeting.id + 1000
      newer_meeting.season_id            = @new_id
      newer_meeting.header_date          = SeasonCreator.next_year_eq_day( newer_meeting.header_date ) 
      newer_meeting.entry_deadline       = SeasonCreator.next_year_eq_day( newer_meeting.entry_deadline )
      newer_meeting.header_year          = SeasonCreator.next_header_year( newer_meeting.header_year )
      newer_meeting.edition              = self.edition + 1 if self.edition
      newer_meeting.are_results_acquired = false
      newer_meeting.is_autofilled        = true
      newer_meeting.has_start_list       = false
      newer_meeting.has_invitation       = false
      newer_meeting.invitation           = nil
      newer_meeting.is_confirmed         = false<commit_msg>The third chance<commit_after>require 'wrappers/timing'

#
# == SeasonCreator
#
# Collection of functions used for ceating a new season based on an older one
# When creating new season should consider:
# - season
# - category_types
# - meetings (with sessions and events)
#
# @author   Leega
# @version  4.00.829
#
class SeasonCreator
  include SqlConvertable

  # These can be edited later on:
  attr_accessor :older_season, :description, :new_id, :begin_date, :end_date, :header_year, :edition, 
                :new_season, :categories, :meetings, :meeting_sessions, :meeting_events

  # Initialization
  #
  # == Params:
  # An instance of season used as base for the new one
  #
  def initialize( older_season, description )
    unless older_season && older_season.instance_of?( Season )
      raise ArgumentError.new("Needs the older season to use as model")
    end
    unless description && description.instance_of?( String )
      raise ArgumentError.new("Needs the new season description")
    end
    @older_season = older_season
    @description  = description
    
    @new_id       = older_season.id + 10
    @begin_date   = older_season.begin_date.next_year
    @end_date     = older_season.end_date.next_year
    @header_year  = SeasonCreator.next_header_year( older_season.header_year ) 
    @edition      = older_season.edition + 1 

    if Season.exists?( @new_id )
      raise ArgumentError.new("Season " + @older_season.description + " already duplicated")
    end

    @new_season       = nil
    @categories       = []
    @meetings         = []
    @meeting_sessions = []
    @meeting_events   = []
  end
  #-- --------------------------------------------------------------------------
  #++

  # Prepare data for duplication
  # 
  def prepare_new_season
    create_sql_diff_header( "Duplicating season #{@older_season.id}-#{@older_season.description} into #{@new_id}-#{@description}" )
    @new_season = renew_season
    @categories = renew_categories
    @meetings   = renew_meetings
    create_sql_diff_footer( "#{@new_id}-#{@description} duplication done" )
  end

  # Retreive older season categories and prepare them
  # for the new season
  # 
  def renew_season
    sql_diff_text_log << "-- Season\r\n"
    newer_season = Season.new( @older_season.attributes.reject{ |e| ['id', 'lock_version','created_at','updated_at'].include?(e) } )
    newer_season.id          = @new_id
    newer_season.description = @description
    newer_season.begin_date  = @begin_date
    newer_season.end_date    = @end_date
    newer_season.header_year = @header_year
    newer_season.edition     = @edition
    newer_season.rules       = nil
    newer_season.save
    sql_diff_text_log << to_sql_insert( newer_season, false, "\r\n\r\n" ) # no additional comment
    newer_season
  end

  # Retreive older season categories and prepare them
  # for the new season
  # No particular change required to category types data 
  # just associate with new season
  # 
  def renew_categories
    newer_categories = []
    sql_diff_text_log << "-- Categories\r\n"
    @older_season.category_types.each do |category_type|
      newer_category = CategoryType.new( category_type.attributes.reject{ |e| ['id', 'lock_version','created_at','updated_at'].include?(e) } )
      newer_category.season_id = @new_id
      newer_category.save
      sql_diff_text_log << to_sql_insert( newer_category, false, "\r\n" ) # no additional comment
      newer_categories << newer_category
    end
    sql_diff_text_log << "\r\n"
    newer_categories
  end

  # Retreive older season meetings and prepare them
  # for the new season
  # 
  def renew_meetings
    newer_meetings = []
    add_sql_diff_comment( "Meetings" )
    @older_season.meetings.each do |meeting|
      sql_diff_text_log << "-- #{meeting.id}-#{meeting.code} #{meeting.description} #{meeting.header_date}\r\n"
      newer_meeting = Meeting.new( meeting.attributes.reject{ |e| ['id', 'lock_version','created_at','updated_at'].include?(e) } )
      newer_meeting.id                   = meeting.id + 1000
      newer_meeting.season_id            = @new_id
      newer_meeting.header_date          = SeasonCreator.next_year_eq_day( newer_meeting.header_date ) 
      newer_meeting.entry_deadline       = SeasonCreator.next_year_eq_day( newer_meeting.entry_deadline )
      newer_meeting.header_year          = SeasonCreator.next_header_year( newer_meeting.header_year )
      newer_meeting.edition              = self.edition + 1 if self.edition
      newer_meeting.are_results_acquired = false
      newer_meeting.is_autofilled        = true
      newer_meeting.has_start_list       = false
      newer_meeting.has_invitation       = false
      newer_meeting.invitation           = nil
      newer_meeting.is_confirmed         = false
{'fraction_non_alphanumeric': 0.10996132538935925, 'fraction_numerical': 0.0029267272917319954, 'mean_word_length': 2.985006247396918, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '499473', 'n_tokens_mistral': 3002, 'n_tokens_neox': 2997, 'n_words': 635}