<commit_before>require 'json'

class Group < ActiveRecord::Base
  
  attr_accessor :guid, :form_title, :form_description
  
  has_and_belongs_to_many :items
  
  has_and_belongs_to_many :listings
  
  has_and_belongs_to_many :favors, :join_table => "groups_favors"
  
  class GroupConnection < ActiveResource::Base
    # This is an inner class to handle remote connection to COS database where the actual information
    # of group model is stored. This is subclass of ActiveResource so it includes some automatic
    # functionality to access REST interface.
    #
    # In practice we use here connection.post/get/put/delete and the URL and Parameters as described
    # in COS documentation at #{COS_URL}

    self.site = COS_URL
    self.format = :json 
    self.timeout = COS_TIMEOUT
    self.element_name = "groups"
    self.collection_name = "groups"
    
    def self.get_public_groups(cookie)
      return fix_alphabets(connection.get("#{prefix}#{element_name}/@public", {"Cookie" => cookie }))
    end
    
    def self.get_group(id, cookie)
      return fix_alphabets(connection.get("#{prefix}#{element_name}/#{id}", {"Cookie" => cookie }))
    end
    
    def self.get_members(id, cookie)
      return fix_alphabets(connection.get("#{prefix}#{element_name}/#{id}/@members", {"Cookie" => cookie }))
    end
    
    def self.create_group(params, cookie)
      creating_headers = {"Cookie" => cookie}
      response = connection.post("#{prefix}#{element_name}", params.to_json, creating_headers)
    end
    
    def self.put_attributes(params, id, cookie)
      connection.put("#{prefix}#{element_name}/#{id}/@self",{:group => params}.to_json, {"Cookie" => cookie} )   
    end
    
    #fixes utf8 letters
    def self.fix_alphabets(json_hash)
      #the parameter must be a hash that is decoded from JSON by activeResource messing up umlaut letters
      JSON.parse(json_hash.to_json.gsub(/\\\\u/,'\\u'))
    end
    
  end

  def self.create(params, cookie)
    if (cookie)
      # create to Common Services
      response = GroupConnection.create_group(params, cookie)
      #pick id from the response (same id in kassi and COS DBs)
      params[:id] = response.body[/"id":"([^"]+)"/, 1]
      puts "Iidee: " + response.body + "loppuu tähän" + params[:id]
      #create locally with less attributes
      super(params.except(:title, :description, :type))
    else
      # create given group ids to kassi db
      params.each do |param|
        super(param)
      end  
      # TODO: should work like this but doesn't
      # super(params)
    end    
  end
  
  def initialize(params={})
    self.guid = params[:id] #store GUID to temporary attribute
    super(params)
  end
  
  def after_initialize
    #self.id may already be correct in this point so use ||=
    self.id ||= self.guid
  end
  
  # Gets all groups from COS and adds to Kassi db all that are not already there.
  def self.add_new_public_groups_to_kassi_db(cookie=nil)
    cookie = Session.kassiCookie if cookie.nil?
    group_ids_not_in_kassi = []
    cos_group_ids = Group.get_group_ids(GroupConnection.get_public_groups(cookie))
    kassi_group_ids = Group.find(:all, :select => "id").collect(&:id)
    cos_group_ids.each do |id|
      unless kassi_group_ids.include?(id)
        group_ids_not_in_kassi << { :id => id }
      end
    end
    Group.create(group_ids_not_in_kassi, nil)
  end
  
  def title(cookie=nil)
    if new_record?
      return form_title ? form_title : ""<commit_msg>Lisätty selviäminen siitä, jos COSista poistuu ryhmä<commit_after>require 'json'

class Group < ActiveRecord::Base
  
  attr_accessor :guid, :form_title, :form_description
  
  has_and_belongs_to_many :items
  
  has_and_belongs_to_many :listings
  
  has_and_belongs_to_many :favors, :join_table => "groups_favors"
  
  class GroupConnection < ActiveResource::Base
    # This is an inner class to handle remote connection to COS database where the actual information
    # of group model is stored. This is subclass of ActiveResource so it includes some automatic
    # functionality to access REST interface.
    #
    # In practice we use here connection.post/get/put/delete and the URL and Parameters as described
    # in COS documentation at #{COS_URL}

    self.site = COS_URL
    self.format = :json 
    self.timeout = COS_TIMEOUT
    self.element_name = "groups"
    self.collection_name = "groups"
    
    def self.get_public_groups(cookie)
      return fix_alphabets(connection.get("#{prefix}#{element_name}/@public", {"Cookie" => cookie }))
    end
    
    def self.get_group(id, cookie)
      return fix_alphabets(connection.get("#{prefix}#{element_name}/#{id}", {"Cookie" => cookie }))
    end
    
    def self.get_members(id, cookie)
      return fix_alphabets(connection.get("#{prefix}#{element_name}/#{id}/@members", {"Cookie" => cookie }))
    end
    
    def self.create_group(params, cookie)
      creating_headers = {"Cookie" => cookie}
      response = connection.post("#{prefix}#{element_name}", params.to_json, creating_headers)
    end
    
    def self.put_attributes(params, id, cookie)
      connection.put("#{prefix}#{element_name}/#{id}/@self",{:group => params}.to_json, {"Cookie" => cookie} )   
    end
    
    #fixes utf8 letters
    def self.fix_alphabets(json_hash)
      #the parameter must be a hash that is decoded from JSON by activeResource messing up umlaut letters
      JSON.parse(json_hash.to_json.gsub(/\\\\u/,'\\u'))
    end
    
  end

  def self.create(params, cookie)
    if (cookie)
      # create to Common Services
      response = GroupConnection.create_group(params, cookie)
      #pick id from the response (same id in kassi and COS DBs)
      params[:id] = response.body[/"id":"([^"]+)"/, 1]
      puts "Iidee: " + response.body + "loppuu tähän" + params[:id]
      #create locally with less attributes
      super(params.except(:title, :description, :type))
    else
      # create given group ids to kassi db
      params.each do |param|
        super(param)
      end  
      # TODO: should work like this but doesn't
      # super(params)
    end    
  end
  
  def initialize(params={})
    self.guid = params[:id] #store GUID to temporary attribute
    super(params)
  end
  
  def after_initialize
    #self.id may already be correct in this point so use ||=
    self.id ||= self.guid
  end
  
  # Gets all groups from COS and adds to Kassi db all that are not already there.
  def self.add_new_public_groups_to_kassi_db(cookie=nil)
    cookie = Session.kassiCookie if cookie.nil?
    group_ids_not_in_kassi = []
    cos_group_ids = Group.get_group_ids(GroupConnection.get_public_groups(cookie))
    kassi_group_ids = Group.find(:all, :select => "id").collect(&:id)
    cos_group_ids.each do |id|
      unless kassi_group_ids.include?(id)
        group_ids_not_in_kassi << { :id => id }
      end
    end
    Group.create(group_ids_not_in_kassi, nil)
    
    # Do the checking also other way round. Remove any groups from Kassi DB that have been removed from COS
    # This needs to be changed when there are non public groups.
    kassi_group_ids.each do |id|
      unless cos_group_ids.include?(id)
        Group.find(id).destroy
      end
    end
    
  end
  
  def title(cookie=nil)
    if new_record?
      return form_title ? form_title : ""
{'fraction_non_alphanumeric': 0.10638881272278586, 'fraction_numerical': 0.0005483959418700301, 'mean_word_length': 3.175729822552948, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 21, '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': '189738', 'n_tokens_mistral': 2351, 'n_tokens_neox': 2337, 'n_words': 626}