diff options
| author | Edouard Maffert | 2016-08-10 17:51:42 +0200 | 
|---|---|---|
| committer | Edouard Maffert | 2016-08-10 17:51:42 +0200 | 
| commit | b66235384cf777a7195463495b412df1a5f34b80 (patch) | |
| tree | 1b81b1828b37f7ca3aa3877d701cda3e1bb7f696 /lib/stif/codif_line_synchronization.rb | |
| parent | 18f063711a92f1743fa08cd94a540a58d54f8dcc (diff) | |
| download | chouette-core-b66235384cf777a7195463495b412df1a5f34b80.tar.bz2 | |
add network and group of lines synchronization and create a rake task
Diffstat (limited to 'lib/stif/codif_line_synchronization.rb')
| -rw-r--r-- | lib/stif/codif_line_synchronization.rb | 72 | 
1 files changed, 58 insertions, 14 deletions
| diff --git a/lib/stif/codif_line_synchronization.rb b/lib/stif/codif_line_synchronization.rb index 9a8a92bce..65d82dc71 100644 --- a/lib/stif/codif_line_synchronization.rb +++ b/lib/stif/codif_line_synchronization.rb @@ -5,21 +5,27 @@ module Stif        def synchronize first_sync = false          # Check last synchronization and synchronization interval          date = DateTime.now.to_date - LineReferential.first.sync_interval.days -        last_sync = LineReferential.first.line_referential_sync.line_sync_operations.last.try(:created_at) +        last_sync = LineReferential.first.line_referential_sync.line_sync_operations.where(status: 'ok').last.try(:created_at)          return if (last_sync.nil? || last_sync.to_date > date) && !first_sync          # TODO Check exceptions and status messages          begin -          # Fetch Codifline operators and lines +          # Fetch Codifline data            client = Codifligne::API.new -          operators = client.operators -          lines = client.lines +          operators       = client.operators +          lines           = client.lines +          networks        = client.networks +          groups_of_lines = client.groups_of_lines -          operators.map{ |o| create_or_update_company(o) } -          lines.map{ |l| create_or_update_line(l) } +          operators.map       { |o| create_or_update_company(o) } +          lines.map           { |l| create_or_update_line(l) } +          networks.map        { |n| create_or_update_network(n) } +          groups_of_lines.map { |g| create_or_update_group_of_lines(g) } -          delete_deprecated_companies(operators) +          delete_deprecated(operators, Chouette::Company)            delete_deprecated_lines(lines) +          delete_deprecated(networks, Chouette::Network) +          delete_deprecated(groups_of_lines, Chouette::GroupOfLine)            LineReferential.first.line_referential_sync.record_status "OK"          rescue Exception => e @@ -31,9 +37,10 @@ module Stif        def create_or_update_company(api_operator)          params = {            name: api_operator.name, -          objectid: api_operator.stif_id +          objectid: api_operator.stif_id, +          import_xml: api_operator.xml          } -        company = save_or_update(params, Chouette::Company) +        save_or_update(params, Chouette::Company)        end        def create_or_update_line(api_line) @@ -41,7 +48,8 @@ module Stif            name: api_line.name,            objectid: api_line.stif_id,            number: api_line.short_name, -          deactivated: (api_line.status == "inactive" ? true : false) +          deactivated: (api_line.status == "inactive" ? true : false), +          import_xml: api_line.xml          }          # Find Company @@ -54,9 +62,45 @@ module Stif          save_or_update(params, Chouette::Line)        end -      def delete_deprecated_companies(operators) -        ids = operators.map{ |o| o.stif_id }.to_a -        Chouette::Company.where.not(objectid: ids).destroy_all +      def create_or_update_network(api_network) +        params = { +          name: api_network.name, +          objectid: api_network.stif_id, +          import_xml: api_network.xml +        } + +        # Find Lines +        params[:lines] = [] +        api_network.line_codes.each do |line| +          line_id = "STIF:CODIFLIGNE:Line:" + line +          params[:lines] << Chouette::Line.find_by(objectid: line_id) +        end + +        save_or_update(params, Chouette::Network) +      end + +      def create_or_update_group_of_lines(api_group_of_lines) +        params = { +          name: api_group_of_lines.name, +          objectid: api_group_of_lines.stif_id, +          import_xml: api_group_of_lines.xml +        } + +        # Find Lines +        params[:lines] = [] +        api_group_of_lines.line_codes.each do |line| +          line_id = "STIF:CODIFLIGNE:Line:" + line +          # TODO : handle when lines doesn't exist +          chouette_line = Chouette::Line.find_by(objectid: line_id) +          params[:lines] << chouette_line if chouette_line.present? +        end + +        save_or_update(params, Chouette::GroupOfLine) +      end + +      def delete_deprecated(objects, klass) +        ids = objects.map{ |o| o.stif_id }.to_a +        klass.where.not(objectid: ids).destroy_all        end        def delete_deprecated_lines(lines) @@ -65,7 +109,7 @@ module Stif        end        def save_or_update(params, klass) -        params[:line_referential] = LineReferential.first +        params[:line_referential] = LineReferential.first unless klass == Chouette::Network          object = klass.where(objectid: params[:objectid]).first          if object            object.assign_attributes(params) | 
