Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'csv'
- class ParticipantAccount < ActiveRecord::Base
- ensure_tenant :parent => :tenant
- has_many :emails
- has_many :voice_numbers
- has_many :pager_numbers
- has_many :fax_numbers
- has_many :participants_subscriptions
- has_many :utility_programs, :through => :participants_subscriptions
- # has_and_belongs_to_many :utility_groups
- has_and_belongs_to_many :groups
- has_many :dras_clients
- attr_reader :password, :encrypted_password
- validates_presence_of :login, :name
- validates :password, :presence => true, :length => {:minimum => 6}
- validates_confirmation_of :password
- accepts_nested_attributes_for :fax_numbers
- accepts_nested_attributes_for :pager_numbers
- accepts_nested_attributes_for :voice_numbers
- accepts_nested_attributes_for :emails
- paginates_per 5
- scope :search_by_name, lambda{ |name| where('name LIKE ?', "%#{name}%") }
- scope :search_by_utility_program_id, lambda{ |utility_program_id|
- joins("INNER JOIN participants_subscriptions
- ON participants_subscriptions.participant_account_id = participant_accounts.id
- AND participants_subscriptions.utility_program_id = #{utility_program_id.to_i}")
- }
- def self.create_from_csv(file_path, utility_program_id)
- utility_program = UtilityProgram.find(utility_program_id)
- result = {:participants => 0, :droms_clients => 0, :success => true, :errors => Array.new}
- ParticipantAccount.transaction do
- CSV.foreach(file_path, {:headers => ['name','login','password','email','phone','pager','fax'], :skip_blanks => true}) do |row|
- next if (row.fields.count != 7 or row.to_s[0,1] == '#')
- args = {:name => row.field('name'), :login => row.field('login'), :password => row.field('password')}
- participant_account = ParticipantAccount.new(args)
- participant_account.tenant_id = utility_program.tenant_id
- if participant_account.save
- result[:participants] += 1
- participant_account.emails << Email.new(:value => row.field('email'))
- participant_account.voice_numbers << VoiceNumber.new(:value => row.field('phone'))
- participant_account.pager_numbers << PagerNumber.new(:value => row.field('pager'))
- participant_account.fax_numbers << FaxNumber.new(:value => row.field('fax'))
- else
- result[:success] = false
- result[:errors] << participant_account.errors.full_messages
- end
- end
- if result[:success] == true
- dras_result = ParticipantAccount.create_dras_from_csv(file_path, utility_program)
- if dras_result[:success] == true
- result[:droms_clients] = dras_result[:created]
- else
- result[:errors] << dras_result[:errors]
- result[:success] = false
- end
- end
- if result[:success] == false
- result[:participants] = 0
- raise ActiveRecord::Rollback
- end
- end
- result
- end
- def self.create_dras_from_csv(file_path, utility_program)
- result = {:created => 0, :success => true, :errors => Array.new}
- DrasClient.transaction do
- headers = ['participant_login','client_type','online','connection_type','client_uri','client_authentication','polling_period','retry_count','retry_period']
- CSV.foreach(file_path, {:skip_blanks => true}) do |row|
- next if (row.join(',')[0,1] == '#' or row.count != 9)
- hash = Hash.new
- row.each_with_index do |f, i|
- hash[headers[i].to_sym] = f
- end
- login = hash.delete(:participant_login)
- participant_account = ParticipantAccount.where(:login => login).first
- if participant_account.nil?
- result[:success] = false
- result[:errors] << "Could not find Participant Account with login '#{login}'"
- else
- hash = hash.merge(:participant_account_id => participant_account.id)
- client = DrasClient.new(hash)
- client.tenant_id = participant_account.tenant_id
- if client.save
- result[:created] += 1
- else
- result[:success] = false
- result[:errors] << client.errors.full_messages
- end
- end
- if result[:success] == false
- result[:created] = 0
- raise ActiveRecord::Rollback
- end
- end
- end
- result
- end
- def self.to_csv
- # TODO
- end
- def get_emails
- emails.map(&:value)
- end
- def get_voice_numbers
- voice_numbers.map(&:value)
- end
- def subscribed_to_program_at(utility_program)
- ParticipantsSubscription.where(:utility_program_id => utility_program.id,
- :participant_account_id => id).first.created_at
- end
- def self.search(type, query)
- send(SEARCH_TYPES_SCOPES[type], query)
- end
- def opt_out(utility_dr_event_id)
- event = UtilityDREvent.find(utility_dr_event_id)
- event.participant_accounts.delete self
- OptOut.create!(:utility_dr_event_id => utility_dr_event_id, :participant_account_id => id)
- end
- def add_program_to_clients(utility_program)
- dras_clients.each do |dras_client|
- dras_client.utility_programs << utility_program
- end
- end
- def remove_program_from_clients(utility_program)
- dras_clients.each do |dras_client|
- dras_client.utility_programs.delete utility_program
- end
- end
- def password=(attribute)
- logger.info "***************** 1: #{attribute.to_s}"
- encrypted_password = attribute
- end
- def password
- logger.info "***************** 2: #{encrypted_password.to_s}"
- encrypted_password
- end
- SEARCH_TYPES = {'Participant Name' => '1' }
- SEARCH_TYPES_SCOPES = {'1' => :search_by_name}
- end
Add Comment
Please, Sign In to add comment