Guest User

Untitled

a guest
Feb 18th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 KB | None | 0 0
  1. require 'csv'
  2.  
  3. class ParticipantAccount < ActiveRecord::Base
  4. ensure_tenant :parent => :tenant
  5.  
  6. has_many :emails
  7. has_many :voice_numbers
  8. has_many :pager_numbers
  9. has_many :fax_numbers
  10. has_many :participants_subscriptions
  11. has_many :utility_programs, :through => :participants_subscriptions
  12. # has_and_belongs_to_many :utility_groups
  13. has_and_belongs_to_many :groups
  14. has_many :dras_clients
  15.  
  16. attr_reader :password, :encrypted_password
  17.  
  18.  
  19.  
  20. validates_presence_of :login, :name
  21. validates :password, :presence => true, :length => {:minimum => 6}
  22. validates_confirmation_of :password
  23.  
  24.  
  25.  
  26. accepts_nested_attributes_for :fax_numbers
  27. accepts_nested_attributes_for :pager_numbers
  28. accepts_nested_attributes_for :voice_numbers
  29. accepts_nested_attributes_for :emails
  30.  
  31. paginates_per 5
  32.  
  33. scope :search_by_name, lambda{ |name| where('name LIKE ?', "%#{name}%") }
  34. scope :search_by_utility_program_id, lambda{ |utility_program_id|
  35. joins("INNER JOIN participants_subscriptions
  36. ON participants_subscriptions.participant_account_id = participant_accounts.id
  37. AND participants_subscriptions.utility_program_id = #{utility_program_id.to_i}")
  38. }
  39.  
  40. def self.create_from_csv(file_path, utility_program_id)
  41. utility_program = UtilityProgram.find(utility_program_id)
  42. result = {:participants => 0, :droms_clients => 0, :success => true, :errors => Array.new}
  43.  
  44. ParticipantAccount.transaction do
  45. CSV.foreach(file_path, {:headers => ['name','login','password','email','phone','pager','fax'], :skip_blanks => true}) do |row|
  46. next if (row.fields.count != 7 or row.to_s[0,1] == '#')
  47.  
  48. args = {:name => row.field('name'), :login => row.field('login'), :password => row.field('password')}
  49. participant_account = ParticipantAccount.new(args)
  50. participant_account.tenant_id = utility_program.tenant_id
  51. if participant_account.save
  52. result[:participants] += 1
  53.  
  54. participant_account.emails << Email.new(:value => row.field('email'))
  55. participant_account.voice_numbers << VoiceNumber.new(:value => row.field('phone'))
  56. participant_account.pager_numbers << PagerNumber.new(:value => row.field('pager'))
  57. participant_account.fax_numbers << FaxNumber.new(:value => row.field('fax'))
  58. else
  59. result[:success] = false
  60. result[:errors] << participant_account.errors.full_messages
  61. end
  62. end
  63.  
  64. if result[:success] == true
  65. dras_result = ParticipantAccount.create_dras_from_csv(file_path, utility_program)
  66.  
  67. if dras_result[:success] == true
  68. result[:droms_clients] = dras_result[:created]
  69. else
  70. result[:errors] << dras_result[:errors]
  71. result[:success] = false
  72. end
  73.  
  74. end
  75.  
  76. if result[:success] == false
  77. result[:participants] = 0
  78. raise ActiveRecord::Rollback
  79. end
  80. end
  81.  
  82. result
  83. end
  84.  
  85. def self.create_dras_from_csv(file_path, utility_program)
  86. result = {:created => 0, :success => true, :errors => Array.new}
  87.  
  88. DrasClient.transaction do
  89. headers = ['participant_login','client_type','online','connection_type','client_uri','client_authentication','polling_period','retry_count','retry_period']
  90. CSV.foreach(file_path, {:skip_blanks => true}) do |row|
  91. next if (row.join(',')[0,1] == '#' or row.count != 9)
  92.  
  93. hash = Hash.new
  94. row.each_with_index do |f, i|
  95. hash[headers[i].to_sym] = f
  96. end
  97.  
  98. login = hash.delete(:participant_login)
  99. participant_account = ParticipantAccount.where(:login => login).first
  100.  
  101. if participant_account.nil?
  102. result[:success] = false
  103. result[:errors] << "Could not find Participant Account with login '#{login}'"
  104. else
  105. hash = hash.merge(:participant_account_id => participant_account.id)
  106.  
  107. client = DrasClient.new(hash)
  108. client.tenant_id = participant_account.tenant_id
  109. if client.save
  110. result[:created] += 1
  111. else
  112. result[:success] = false
  113. result[:errors] << client.errors.full_messages
  114. end
  115. end
  116.  
  117. if result[:success] == false
  118. result[:created] = 0
  119. raise ActiveRecord::Rollback
  120. end
  121.  
  122. end
  123. end
  124.  
  125. result
  126. end
  127.  
  128. def self.to_csv
  129. # TODO
  130. end
  131.  
  132. def get_emails
  133. emails.map(&:value)
  134. end
  135.  
  136. def get_voice_numbers
  137. voice_numbers.map(&:value)
  138. end
  139.  
  140. def subscribed_to_program_at(utility_program)
  141. ParticipantsSubscription.where(:utility_program_id => utility_program.id,
  142. :participant_account_id => id).first.created_at
  143. end
  144.  
  145. def self.search(type, query)
  146. send(SEARCH_TYPES_SCOPES[type], query)
  147. end
  148.  
  149. def opt_out(utility_dr_event_id)
  150. event = UtilityDREvent.find(utility_dr_event_id)
  151. event.participant_accounts.delete self
  152. OptOut.create!(:utility_dr_event_id => utility_dr_event_id, :participant_account_id => id)
  153. end
  154.  
  155. def add_program_to_clients(utility_program)
  156. dras_clients.each do |dras_client|
  157. dras_client.utility_programs << utility_program
  158. end
  159. end
  160.  
  161. def remove_program_from_clients(utility_program)
  162. dras_clients.each do |dras_client|
  163. dras_client.utility_programs.delete utility_program
  164. end
  165. end
  166.  
  167. def password=(attribute)
  168. logger.info "***************** 1: #{attribute.to_s}"
  169. encrypted_password = attribute
  170. end
  171.  
  172. def password
  173. logger.info "***************** 2: #{encrypted_password.to_s}"
  174. encrypted_password
  175. end
  176.  
  177. SEARCH_TYPES = {'Participant Name' => '1' }
  178. SEARCH_TYPES_SCOPES = {'1' => :search_by_name}
  179. end
Add Comment
Please, Sign In to add comment