diff options
| author | Alban Peignier | 2017-12-17 22:14:17 +0100 | 
|---|---|---|
| committer | Alban Peignier | 2018-01-05 10:23:29 +0100 | 
| commit | 78c2b9deaefa4aa5c0ac5173055cb2cacd3d27c1 (patch) | |
| tree | 7c61327521783f9a921e595319f2d4862b0342cf /app | |
| parent | f90488de1f657abf24026231485c87d3e42ee11d (diff) | |
| download | chouette-core-78c2b9deaefa4aa5c0ac5173055cb2cacd3d27c1.tar.bz2 | |
First try for route/stop_point and journey_pattern merge. RefsĀ #5299
Diffstat (limited to 'app')
| -rw-r--r-- | app/models/merge.rb | 103 | ||||
| -rw-r--r-- | app/models/referential.rb | 30 | 
2 files changed, 130 insertions, 3 deletions
| diff --git a/app/models/merge.rb b/app/models/merge.rb index 7c5a5d68c..21ef9fa5f 100644 --- a/app/models/merge.rb +++ b/app/models/merge.rb @@ -70,6 +70,109 @@ class Merge < ActiveRecord::Base      end      # let's merge data :) + +    # Routes + +    referential_routes = referential.switch do +      referential.routes.all.to_a +    end + +    new.switch do +      referential_routes.each do |route| +        existing_route = new.routes.find_by line_id: route.line_id, checksum: route.checksum +        unless existing_route +          attributes = route.attributes.merge( +            id: nil, +            objectid: "merge:route:#{route.checksum}", #FIXME +            # line_id is the same +            # all other primary must be changed +            opposite_route_id: nil #FIXME +          ) +          new_route = new.routes.create! attributes + +          # FIXME Route checksum changes if stop points are not defined +          if new_route.checksum != route.checksum +            raise "Checksum has changed: #{route.inspect} #{new_route.inspect}" +          end +        end +      end +    end + +    referential_routes_checksums = Hash[referential_routes.map { |r| [ r.id, r.checksum ] }] + +    # Stop Points + +    referential_stop_points = referential.switch do +      referential.stop_points.all.to_a +    end + +    new.switch do +      referential_stop_points.each do |stop_point| +        # find parent route by checksum +        associated_route_checksum = referential_routes_checksums[stop_point.route_id] +        existing_associated_route = new.routes.find_by checksum: associated_route_checksum + +        existing_stop_point = new.stop_points.find_by route_id: existing_associated_route.id, checksum: stop_point.checksum +        unless existing_stop_point +          attributes = stop_point.attributes.merge( +            id: nil, + +            objectid: "merge:stop_point:#{existing_associated_route.checksum}-#{stop_point.checksum}", #FIXME + +            # all other primary must be changed +            route_id: existing_associated_route.id, +          ) + +          new_stop_point = new.stop_points.create! attributes +          if new_stop_point.checksum != stop_point.checksum +            raise "Checksum has changed: #{stop_point.inspect} #{new_stop_point.inspect}" +          end +        end +      end +    end + +    referential_stop_points_checksums = Hash[referential_stop_points.map { |r| [ r.id, r.checksum ] }] + +    # JourneyPatterns + +    referential_journey_patterns = referential.switch do +      referential.journey_patterns.all.to_a +    end + +    new.switch do +      referential_journey_patterns.each do |journey_pattern| +        # find parent route by checksum +        associated_route_checksum = referential_routes_checksums[journey_pattern.route_id] +        existing_associated_route = new.routes.find_by checksum: associated_route_checksum + +        existing_journey_pattern = new.journey_patterns.find_by route_id: existing_associated_route.id, checksum: journey_pattern.checksum + +        unless existing_journey_pattern +          attributes = journey_pattern.attributes.merge( +            id: nil, + +            objectid: "merge:journey_pattern:#{existing_associated_route.checksum}-#{journey_pattern.checksum}", #FIXME + +            # all other primary must be changed +            route_id: existing_associated_route.id, + +            departure_stop_point_id: nil, # FIXME +            arrival_stop_point_id: nil +          ) + +          stop_points = journey_pattern.stop_point_ids.map do |stop_point_id| +            associated_stop_point_checksum = referential_stop_points_checksums[stop_point_id] +            new.stop_points.find_by checksum: associated_stop_point_checksum +          end +          attributes.merge!(stop_points: stop_points) + +          new_journey_pattern = new.journey_patterns.create! attributes +          if new_journey_pattern.checksum != journey_pattern.checksum +            raise "Checksum has changed: #{journey_pattern.inspect} #{new_journey_pattern.inspect}" +          end +        end +      end +    end    end    def save_current diff --git a/app/models/referential.rb b/app/models/referential.rb index 4fa353a37..8087ea61e 100644 --- a/app/models/referential.rb +++ b/app/models/referential.rb @@ -144,6 +144,18 @@ class Referential < ActiveRecord::Base      Chouette::PurchaseWindow.all    end +  def routes +    Chouette::Route.all +  end + +  def journey_patterns +    Chouette::JourneyPattern.all +  end + +  def stop_points +    Chouette::StopPoint.all +  end +    before_validation :define_default_attributes    def define_default_attributes @@ -151,10 +163,22 @@ class Referential < ActiveRecord::Base      self.objectid_format ||= workbench.objectid_format if workbench    end -  def switch +  def switch(&block)      raise "Referential not created" if new_record? -    Apartment::Tenant.switch!(slug) -    self + +    unless block_given? +      Rails.logger.debug "Referential switch to #{slug}" +      Apartment::Tenant.switch! slug +      self +    else +      result = nil +      Apartment::Tenant.switch slug do +        Rails.logger.debug "Referential switch to #{slug}" +        result = yield +      end +      Rails.logger.debug "Referential back" +      result +    end    end    def self.new_from(from, functional_scope) | 
