diff options
Diffstat (limited to 'lib/stif')
| -rw-r--r-- | lib/stif/reflex_synchronization.rb | 87 |
1 files changed, 46 insertions, 41 deletions
diff --git a/lib/stif/reflex_synchronization.rb b/lib/stif/reflex_synchronization.rb index 956f94696..471b226c2 100644 --- a/lib/stif/reflex_synchronization.rb +++ b/lib/stif/reflex_synchronization.rb @@ -14,7 +14,6 @@ module Stif client = Reflex::API.new processed = [] initial_count = Chouette::StopArea.where(deleted_at: nil).count - existing_records = Hash[Chouette::StopArea.pluck(:import_xml, :objectid).map{|xml, id| [id, xml]}] ['getOR', 'getOP'].each do |method| start = Time.now @@ -25,19 +24,18 @@ module Stif end # Create or update stop_area for every quay, stop_place - stop_areas = results[:Quay].merge(results[:StopPlace]) - start = Time.now - stop_areas.each do |id, entry| - processed << entry.id - next unless need_update_or_create?(existing_records, entry) + stop_areas = results[:Quay] | results[:StopPlace] + + start = Time.now + stop_areas.each do |entry| + processed << entry['id'] self.create_or_update_stop_area entry end Rails.logger.info "Reflex:sync - Create or update StopArea done in #{Time.now - start} seconds" # Walk through every entry and set parent stop_area start = Time.now - stop_areas.each do |id, entry| - next unless need_update_or_create?(existing_records, entry) + stop_areas.each do |entry| self.stop_area_set_parent entry end Rails.logger.info "Reflex:sync - StopArea set parent done in #{Time.now - start} seconds" @@ -48,10 +46,6 @@ module Stif } end - def need_update_or_create? existing_records, entry - return true unless existing_records.key?(entry.id) && entry.xml == existing_records[entry.id] - end - def set_deleted_stop_area processed start = Time.now deleted = Chouette::StopArea.where(deleted_at: nil).pluck(:objectid).uniq - processed @@ -63,18 +57,18 @@ module Stif end def stop_area_set_parent entry - return false unless entry.try(:parent_site_ref) || entry.try(:quays) - stop = self.find_by_object_id entry.id + return false unless entry['parent'] || entry['quays'] + stop = self.find_by_object_id entry['id'] return false unless stop - if entry.try(:parent_site_ref) - stop.parent = self.find_by_object_id entry.parent_site_ref + if entry['parent'] + stop.parent = self.find_by_object_id entry['parent'] stop.save! if stop.changed end - if entry.try(:quays) - entry.quays.each do |quay| - children = self.find_by_object_id(quay[:ref]) + if entry['quays'] + entry['quays'].each do |id| + children = self.find_by_object_id id next unless children children.parent = stop children.save! if children.changed? @@ -82,39 +76,50 @@ module Stif end end + def access_point_access_type entry + if entry['IsEntry'] == 'true' && entry['IsExit'] == 'true' + 'in_out' + elsif entry['IsEntry'] == 'true' + 'in' + elsif entry['IsExit'] == 'true' + 'out' + end + end + def create_or_update_access_point entry, stop_area - access = Chouette::AccessPoint.find_or_create_by(objectid: "dummy:AccessPoint:#{entry.id.tr(':', '')}") + access = Chouette::AccessPoint.find_or_create_by(objectid: "dummy:AccessPoint:#{entry['id'].tr(':', '')}") # Hack, on save object_version will be incremented by 1 - entry.version = entry.version.to_i + 1 if access.persisted? + entry['version'] = entry['version'].to_i + 1 if access.persisted? + access.access_type = self.access_point_access_type(entry) access.stop_area = stop_area { - :name => :name, - :access_type => :access_type, - :object_version => :version, - :zip_code => :postal_code, - :city_name => :city, - :import_xml => :xml - }.each do |k, v| access[k] = entry.try(v) end - access.save if access.changed? + :name => 'Name', + :object_version => 'version', + :zip_code => 'PostalRegion', + :city_name => 'Town' + }.each do |k, v| access[k] = entry[v] end + access.save! if access.changed? end def create_or_update_stop_area entry - stop = Chouette::StopArea.find_or_create_by(objectid: entry.id) + stop = Chouette::StopArea.find_or_create_by(objectid: entry['id']) stop.deleted_at = nil stop.stop_area_referential = self.defaut_referential { - :name => :name, - :creation_time => :created, - :area_type => :area_type, - :object_version => :version, - :zip_code => :postal_code, - :city_name => :city, - :import_xml => :xml - }.each do |k, v| stop[k] = entry.try(v) end - stop.save! if stop.changed? + :name => 'Name', + :creation_time => 'created', + :area_type => 'type', + :object_version => 'version', + :zip_code => 'PostalRegion', + :city_name => 'Town' + }.each do |k, v| stop[k] = entry[v] end + if stop.changed? + stop.import_xml = entry[:xml] + stop.save! + end # Create AccessPoint from StopPlaceEntrance - if entry.try(:entrances) - entry.entrances.each do |entrance| + if entry[:stop_place_entrances] + entry[:stop_place_entrances].each do |entrance| self.create_or_update_access_point entrance, stop end end |
