# handles people.csv and splits out family data for FamilyUpdater class PeopleUpdater < UpdateAgent self.resource = Person # split out family data and create a new FamilyUpdater def initialize(filename, options={}) super(filename, options) if options[:converter] converter = Kernel.const_get(@options['converter']['name'] + 'Converter').new(@options['converter']) @data = converter.convert(@data.clone) @attributes = @data.first.keys end check_for_missing_columns person_data = [] family_data = {} @data.each_with_index do |row, index| person, family = split_change_hash(row) # massaging if person.has_key?('email') person['email_changed'] = false end if person.has_key?('relationships') person['relationships_hash'] = Digest::SHA1.hexdigest(person['relationships'].split(',').sort.join(',')) end # link to cached family or cache new family if existing_family = family_data[family['legacy_id']] person['family'] = existing_family person_data << person else person['family'] = family person_data << person family['barcode_id_changed'] = false if family.has_key?('barcode_id') family_data[family['legacy_id']] = family end print "splitting family record #{index+1}\r" end puts @attributes << 'email_changed' if @attributes.include?('email') if @attributes.include?('relationships') @attributes << 'relationships_hash' @attributes.delete('relationships') end @data = person_data @attributes.reject! { |a| a =~ /^family_/ and a != 'family_id' } @family_agent = FamilyUpdater.new(family_data.values, @options) end @@required_columns = %w(legacy_id legacy_family_id) def check_for_missing_columns if row = @data.detect { |row| (@@required_columns - row.keys).any? } puts "Error: one or more required columns are missing." puts "Required but missing: #{(@@required_columns - row.keys).join(', ')}" exit(1) end end def name_for(row) "#{row['first_name']} #{row['last_name']}" rescue '' end def compare(force=false) @family_agent.compare(force) super(force) end def has_work? @family_agent.has_work? or super end def present @family_agent.present if @family_agent.has_work? super end def create_count @create.length + @family_agent.create.length end def update_count @update.length + @family_agent.update.length end def push @family_agent.push super end def finish_sync(create_items=true, mark_complete=true) if create_items @family_agent.sync = @sync @family_agent.finish_sync(true, false) super(true, false) end if mark_complete @sync.complete = true @sync.error_count = errors.length + @family_agent.errors.length @sync.success_count = (@create + @update).length + (@family_agent.create + @family_agent.update).length - @sync.error_count @sync.save end end protected # split hash of values into person and family values based on keys def split_change_hash(vals) person_vals = {} family_vals = {} vals.each do |key, val| if key =~ /^family_/ family_vals[key.sub(/^family_/, '')] = val else person_vals[key] = val end end family_vals['legacy_id'] ||= person_vals['legacy_family_id'] [person_vals, family_vals] end end