Class: ODDB::MiGeLPlugin

Inherits:
Plugin show all
Defined in:
src/plugin/migel.rb

Constant Summary

SALE_TYPES =
{
  '1'  => :purchase,
  '2' => :rent,
  '3'  => :both,
}

Constants inherited from Plugin

ARCHIVE_PATH, RECIPIENTS

Instance Method Summary (collapse)

Methods inherited from Plugin

#initialize, #l10n_sessions, #log_info, #recipients, #report, #resolve_link, #update_rss_feeds

Methods included from HttpFile

#http_body, #http_file

Constructor Details

This class inherits a constructor from ODDB::Plugin

Instance Method Details

- (Object) date_object(date)



18
19
20
21
22
23
# File 'src/plugin/migel.rb', line 18

def date_object(date)
  date = date.to_s.split(".")
  if(date.size == 3)
    Date.new(date.at(2).to_i, date.at(1).to_i, date.at(0).to_i)
  end
end

- (Object) prune_old_revisions



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
# File 'src/plugin/migel.rb', line 24

def prune_old_revisions
  @revision = Time.local(Time.now.year)
  @app.migel_groups.each_value { |grp|
    grp.subgroups.each_value { |sbg|
      sbg.products.each_value { |prd|
        if((lt = prd.limitation_text) && lt.revision < @revision)
          @app.delete(lt.pointer)
        end
        if((pt = prd.product_text) && pt.revision < @revision)
          @app.delete(pt.pointer)
        end
        if((ut = prd.unit) && ut.revision < @revision)
          @app.delete(ut.pointer)
        end
        if(prd.revision < @revision)
          @app.delete(prd.pointer)
        end
      }
      if((lt = sbg.limitation_text) && lt.revision < @revision)
        @app.delete(lt.pointer)
      end
      if(sbg.products.empty? && sbg.revision < @revision)
        @app.delete(sbg.pointer)
      end
    }
    if((lt = grp.limitation_text) && lt.revision < @revision)
      @app.delete(lt.pointer)
    end
    if(grp.subgroups.empty? && grp.revision < @revision)
      @app.delete(grp.pointer)
    end
  }
end

- (Object) update(path, language)



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'src/plugin/migel.rb', line 57

def update(path, language)
  lines = CSV.read(path)
  #CSVParser.parse_with_file(path).each { |row|
  lines.shift
  lines.each { |row|
    id = row.at(13).to_s.split('.')
    if(id.empty?)
      id = row.at(4).to_s.split('.')
    else
      id[-1].replace(id[-1][0,1])
    end
    group = update_group(id, row, language)
    subgroup = update_subgroup(id, group, row, language)
    if(id.size > 2)
      update_product(id, subgroup, row, language)
    end
  }
end

- (Object) update_group(id, row, language)



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'src/plugin/migel.rb', line 75

def update_group(id, row, language)
  groupcd = id.at(0)
  pointer = Persistence::Pointer.new([:migel_group, groupcd])
  hash = {
    :code => groupcd,
    language  => row.at(2).to_s, 
  }
  group = @app.update(pointer.creator, hash, :migel)
  text = row.at(3).to_s
  text.tr!("\v", " ")
  text.strip!
  unless(text.empty?)
    desc_ptr = pointer + [:limitation_text]
    @app.update(desc_ptr.creator, {language => text}, :migel)
  end
  group
end

- (Object) update_product(id, subgroup, row, language)



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
157
158
159
160
161
# File 'src/plugin/migel.rb', line 107

def update_product(id,  subgroup, row, language)
  productcd = id[2,3].join(".")
  pointer = subgroup.pointer + [:product, productcd]
  name = row.at(12).to_s
  product_text = row.at(15).gsub(/[ \t]+/u, " ")
  product_text.tr!("\v", "\n")
  limitation = ''
  if(idx = product_text.index(/Limitation|Limitazione/u))
    limitation = product_text.slice!(idx..-1).strip
  end
  if(name.to_s.strip.empty?)
    name = product_text.slice!(/^[^\n]+/u)
  end
  product_text.strip!
  type = SALE_TYPES[id.at(4)]
  price = ((row.at(18).to_s[/\d[\d.]*/u].to_f) * 100).round
  date = date_object(row.at(20))
  lim_flag = row.at(14)
  hash = {
    language => name,
    :limitation => (lim_flag == 'L'),
    :price => price,
    :type => type,
    :date => date,
  }
  qty = row.at(16).to_i
  if(qty > 0)
    hash.store(:qty, qty)
  end
  product = @app.update(pointer.creator, hash, :migel) 
  unless(product_text.empty?)
    pt_ptr = pointer + [:product_text]
    @app.update(pt_ptr.creator, {language => product_text}, 
               :migel)
  end
  if(id[3] != "00")
    1.upto(3) { |num|
      prodcd =  [id[2], '00', num].join('.')
      if(prod = subgroup.product(prodcd))
        product.add_product(prod)
      end
    }
  end
  unless(limitation.empty?)
    lim_ptr = pointer + [:limitation_text]
    @app.update(lim_ptr.creator, {language => limitation},
               :migel)
  end
  unit = row.at(17).to_s
  unless(unit.empty?)
    uni_ptr = pointer + [:unit]
    @app.update(uni_ptr.creator, {language => unit}, :migel)
  end
  product
end

- (Object) update_subgroup(id, group, row, language)



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'src/plugin/migel.rb', line 92

def update_subgroup(id, group, row, language)
  sgcd = id.at(1)
  pointer = group.pointer + [:subgroup, sgcd]
  hash = {
    :code => sgcd,
    language => row.at(6).to_s,
  }
  subgroup = @app.update(pointer.creator, hash, :migel)
  text = row.at(7).to_s
  unless(text.empty?)
    lim_ptr = pointer + [:limitation_text]
    @app.update(lim_ptr.creator, {language => text}, :migel)
  end
  subgroup
end