diff options
| -rw-r--r-- | Gemfile.lock | 14 | ||||
| -rw-r--r-- | app/models/chouette/group_of_line.rb | 2 | ||||
| -rw-r--r-- | app/models/chouette/network.rb | 4 | ||||
| -rw-r--r-- | config/schedule.rb | 4 | ||||
| -rw-r--r-- | db/schema.rb | 4 | ||||
| -rw-r--r-- | lib/stif/codif_line_synchronization.rb | 72 | ||||
| -rw-r--r-- | lib/tasks/codifligne.rake | 6 | 
7 files changed, 84 insertions, 22 deletions
| diff --git a/Gemfile.lock b/Gemfile.lock index 17e0287b6..752ae9d2a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -7,9 +7,9 @@ GIT  GIT    remote: git@github.com:AF83/stif-codifline-api.git -  revision: 3d44f52aea3aea0cc45291ce0855bbb339f228c6 +  revision: e38210d88c447ca793b9fc3af6ec4c2a682f054c    specs: -    codifligne (0.0.1) +    codifligne (0.0.2)        nokogiri (~> 1.6)  GEM @@ -244,7 +244,7 @@ GEM      method_source (0.8.2)      mime-types (2.4.3)      mimemagic (0.3.0) -    mini_portile (0.6.2) +    mini_portile2 (2.1.0)      minitest (5.8.3)      multi_json (1.11.2)      multipart-post (2.0.0) @@ -257,9 +257,10 @@ GEM      net-ssh-gateway (1.2.0)        net-ssh (>= 2.6.5)      newrelic_rpm (3.11.2.286) -    nokogiri (1.6.6.2) -      mini_portile (~> 0.6.0) -    nokogiri (1.6.6.2-java) +    nokogiri (1.6.8) +      mini_portile2 (~> 2.1.0) +      pkg-config (~> 1.1.7) +    nokogiri (1.6.8-java)      notiffany (0.0.6)        nenv (~> 0.1)        shellany (~> 0.0) @@ -268,6 +269,7 @@ GEM      parser (2.2.0.3)        ast (>= 1.1, < 3.0)      pg (0.18.1) +    pkg-config (1.1.7)      poltergeist (1.6.0)        capybara (~> 2.1)        cliver (~> 0.3.1) diff --git a/app/models/chouette/group_of_line.rb b/app/models/chouette/group_of_line.rb index 0b91f3442..d954c6699 100644 --- a/app/models/chouette/group_of_line.rb +++ b/app/models/chouette/group_of_line.rb @@ -1,5 +1,5 @@  class Chouette::GroupOfLine < Chouette::ActiveRecord -  include DefaultAttributesSupport +  include DefaultNetexAttributesSupport    include GroupOfLineRestrictions    include LineReferentialSupport diff --git a/app/models/chouette/network.rb b/app/models/chouette/network.rb index c8926506c..690c39344 100644 --- a/app/models/chouette/network.rb +++ b/app/models/chouette/network.rb @@ -1,5 +1,7 @@ -class Chouette::Network < Chouette::TridentActiveRecord +class Chouette::Network < Chouette::ActiveRecord +  include DefaultNetexAttributesSupport    include NetworkRestrictions +  include LineReferentialSupport    # FIXME http://jira.codehaus.org/browse/JRUBY-6358    self.primary_key = "id" diff --git a/config/schedule.rb b/config/schedule.rb index bf2c804df..6adbc0219 100644 --- a/config/schedule.rb +++ b/config/schedule.rb @@ -23,3 +23,7 @@ every :hour do    rake "organisations:sync"    rake "users:sync"  end + +every :day do +  rake "codifligne:sync" +end
\ No newline at end of file diff --git a/db/schema.rb b/db/schema.rb index aaf227a9c..0de1ce9f1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -92,6 +92,7 @@ ActiveRecord::Schema.define(version: 20160804134627) do      t.string   "url"      t.string   "time_zone"      t.integer  "line_referential_id" +    t.text     "import_xml"    end    add_index "companies", ["line_referential_id"], :name => "index_companies_on_line_referential_id" @@ -203,6 +204,7 @@ ActiveRecord::Schema.define(version: 20160804134627) do      t.string   "comment"      t.string   "registration_number"      t.integer  "line_referential_id" +    t.text     "import_xml"    end    add_index "group_of_lines", ["line_referential_id"], :name => "index_group_of_lines_on_line_referential_id" @@ -315,6 +317,7 @@ ActiveRecord::Schema.define(version: 20160804134627) do      t.string   "stable_id"      t.integer  "line_referential_id"      t.boolean  "deactivated",                               default: false +    t.text     "import_xml"    end    add_index "lines", ["line_referential_id"], :name => "index_lines_on_line_referential_id" @@ -334,6 +337,7 @@ ActiveRecord::Schema.define(version: 20160804134627) do      t.string   "source_type"      t.string   "source_identifier"      t.string   "comment" +    t.text     "import_xml"    end    add_index "networks", ["objectid"], :name => "networks_objectid_key", :unique => true 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) diff --git a/lib/tasks/codifligne.rake b/lib/tasks/codifligne.rake new file mode 100644 index 000000000..8b7a4f669 --- /dev/null +++ b/lib/tasks/codifligne.rake @@ -0,0 +1,6 @@ +namespace :codifligne do +  desc "Sync lines, companies, networks, and group of lines from codifligne" +  task sync: :environment  do +    Stif::CodifLineSynchronization.synchronize +  end +end | 
