extract_sies_data.rb
4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/ruby
$LOAD_PATH.unshift('/usr/share/rails/activerecord/lib')
$LOAD_PATH.unshift('/usr/share/rails/activesupport/lib')
require 'activerecord'
require 'active_support'
require File.dirname(__FILE__) + "/../" + 'lib/noosfero/core_ext/string.rb'
LIMIT = (ENV['DUMP_ALL'] ? nil : 10)
DUMP_ALL = LIMIT.nil?
# To connect with the database that contains the data to be extracted cofigure it in the 'database_farejador.yml' with the name 'farejador'
ActiveRecord::Base.establish_connection(YAML::load(IO.read(File.dirname(__FILE__) + '/database_farejador.yml'))['farejador'])
class Enterprise < ActiveRecord::Base
  set_table_name 'cons_dadosbasicos'
  set_primary_key :id_sies
  has_many :products, :foreign_key => 'V00', :conditions => "tipo = 'produto'"
  has_many :input_products, :class_name => 'Product', :foreign_key => 'V00', :conditions => "tipo = 'insumo'"
  has_one :extra_data, :foreign_key => 'V00'
end
class ExtraData < ActiveRecord::Base
  set_table_name 'dados_extra'
end
class Product < ActiveRecord::Base
  set_table_name 'mapa_produtos'
  belongs_to :category, :foreign_key => 'id_prod'
end
class Category < ActiveRecord::Base
  set_table_name 'lista_produtos'
end
class Macroregion < ActiveRecord::Base
  set_table_name 'macrorregioes'
end
class State < ActiveRecord::Base
  set_table_name 'estados'
  set_primary_key :id_UF
  has_one :macroregion, :foreign_key => 'UF'
  def cities
    City.find(:all, :conditions => [ "id < 6000000 and id like '?%'", id_UF])
  end
end
class City < ActiveRecord::Base
  set_table_name 'cidades_ibge'
end
class Dumper
  def initialize
    @seq = 0
    @seqs = {}
    @r_seq = 0
    @r_seqs = {}
  end
  def pretty(str, alt = nil)
    if alt.nil?
      str
    else
      str + ' (' + alt + ')'
    end
  end
  def dump_category(cat, parent = nil)
    
    @seqs[cat] = @seq
    puts <<-EOF
cat#{@seq} = new_cat(#{pretty(cat.nome, cat.nome_alt).inspect}, #{parent ? 'cat' + @seqs[parent].to_s : 'nil' })
categories[#{cat.id}] = cat#{@seq}.id
    EOF
    @seq += 1
    Category.find(:all, :conditions => { :id_mae => cat.id }).each do |child|
      dump_category(child, cat) if (DUMP_ALL || (@seq <= LIMIT))
    end
  end
  def dump_enterprise(ent)
    email = nil
    contato = nil
    if (ent.corel =~ /@/)
      email = ent.corel
    else
      contato = ent.corel
    end
    endereco = ent.end
    if ent.cep
      endereco << " CEP: " << ent.cep
    end
    puts <<-EOF
new_ent({ :name => #{ent.nome.inspect}, 
          :identifier => #{ent.nome.to_slug.inspect}, 
          :contact_phone => #{ent.tel.inspect}, 
          :address => #{endereco.inspect}, 
          :lat => #{ent.lat.inspect}, 
          :lng => #{ent.long.inspect}, 
          :geocode_precision => #{ent.geomodificou.inspect}, 
          :data => { 
            :id_sies => #{ent.id_sies.inspect}
           }, 
          :contact_email => #{email.inspect},
          :foundation_year => #{ent.extra_data.ANO.inspect},
          :cnpj => #{ent.extra_data.CNPJ.inspect},
          :category_ids => [cities[#{ent.id_cidade}]].map(&:id)
        },
      [#{ent.products.map{|p| "{ :name => #{p.category.nome.inspect} , :product_category_id => categories[#{p.category.id}] }"}.join(', ')}], 
      [#{ent.input_products.map{|p| "{ :product_category_id => categories[#{p.category.id}]}" }.join(', ')}])
EOF
  end
  def dump_city(city)
    @r_seqs[city] = @r_seq
    puts <<-EOF
city#{@r_seq} = new_region(#{city.cidade.inspect}, STATES[#{city.id.to_s[0..1]}], #{city.latitude}, #{city.longitude})
cities[#{city.id}] = city#{@r_seq}
    EOF
    @r_seq += 1
  end
end
dumper = Dumper.new
puts <<-EOF
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../config/environment'
require File.dirname(__FILE__) + '/fbes_populate_helper.rb'
GetText.locale = 'pt_BR'
EOF
puts "categories = {}"
Category.find(:all, :conditions => 'id_mae is null or id_mae = -1', :limit => LIMIT).each do |cat|
  dumper.dump_category(cat, nil)
end
puts "cities = {}"
City.find(:all, :limit => LIMIT).each do |city|
  dumper.dump_city(city)
end
Enterprise.find(:all, :limit => LIMIT).each do |ent|
  dumper.dump_enterprise(ent)
end