Class: ACaccess

Inherits:
RPCQooxdooPath
  • Object
show all
Defined in:
lib/africompta/acaccess.rb

Class Method Summary collapse

Class Method Details

.accounts_fetch(user) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/africompta/acaccess.rb', line 67

def self.accounts_fetch(user)
  Accounts.data.select { |_k, v| v._rev_index > user. }.
      collect { |k, _v| Accounts.get_data_instance(k) }.
      sort_by { |a| a.path }.
      collect { |a| a.to_s }.
      join("\n")
end

.get(p) ⇒ Object



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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/africompta/acaccess.rb', line 75

def self.get(p)
  # Two cases:
  # path/arg/user,pass - arg is used
  # path/user,pass - arg is nil
  path, arg, id = p.split('/')
  arg, id = id, arg if not id
  user, pass = id.split(',')

  log_msg 'ACaccess.get', "get-merge-path #{path} - #{arg} with " +
                            "user #{user} and pass #{pass}"
  u = Users.match_by_name(user)
  u_local = Users.match_by_name('local')
  if not (u and u.pass == pass)
    return "User #{user} not known with pass #{pass}"
  end

  case path
    when /accounts_get(.*)/
      # Gets all accounts available (to that user) that have been changed
      # since the last update, again, do it from the root(s), else we have
      # a problem for children without parents
      ret = ''
      dputs(2) { "user index is: #{u.}" }
      # Returns only one account
      case $1
        when '_one'
          return Accounts.match_by_global_id(arg).to_s
        when '_all'
          dputs(2) { 'Putting all accounts' }
          ret = Accounts.search_all.collect { |acc|
            dputs(4) { "Found account #{acc.name} with index #{acc.rev_index}" }
            acc.to_s(true)
          }.join("\n")
        when '_count'
          ret += Accounts.search_all.size.to_s
        when '_part'
          acc_start, acc_end = arg.split(',')
          dputs(2) { "Putting accounts #{acc_start}..#{acc_end}" }
          Accounts.search_all.select { |acc|
            acc.rev_index >= acc_start.to_i and acc.rev_index <= acc_end.to_i
          }.each { |acc|
            dputs(4) { "Found account #{acc.name} with index #{acc.rev_index}" }
            ret += "#{acc.to_s(true)}\n"
          }
        else
          dputs(2) { 'Starting to search accounts' }
          t = Time.now
          ret += ACaccess.accounts_fetch(u)
          dputs(2) { "Found #{ret.count("\n")} after #{Time.now - t} seconds" }
      end
      dputs(3) { 'Finished search' }
      return ret
    # Gets all movements (for the accounts of that user)
    when /movements_get(.*)/
      dputs(2) { "movements_get#{$1} with #{arg.inspect}" }
      start, stop = u.movement_index + 1, u_local.movement_index - 1
      ret = case $1
              when '_one'
                # Returns only one account
                Movements.match_by_global_id(arg).to_s
              when '_all_actual'
                start, stop = arg.split(/,/)
                print_movements_actual(start, stop)
              when '_all'
                Movements.search_all.collect { |m|
                  m.to_s
                }.join("\n")
              when '_range'
                start, stop = arg.split(/,/)
                print_movements(start, stop)
              else
                print_movements(start, stop)
            end
      dputs(2) { "Sending a total of #{ret.length}" }
      dputs(3) { "Sending:\n #{ret.inspect}" }
      return ret

    when 'version'
      return $VERSION.to_s

    when 'index'
      return [u_local., u_local.movement_index].join(',')

    when 'local_id'
      return u_local.full

    when 'reset_user_indexes'
      u.
      u.update_movement_index

    when 'reset_user_account_indexes'
      u.

    when 'reset_user_movement_indexes'
      u.update_movement_index

    when 'movement_delete'
      dputs(3) { "Going to delete movement #{arg}" }
      while mov = Movements.match_by_global_id(arg)
        dputs(3) { "Found movement #{mov.inspect}" }
        mov.delete
      end
      dputs(3) { 'Finished deleting' }

    when 'get_db'
      Entities.save_all
      return IO.read(Accounts.storage[:SQLiteAC].db_file)
  end

  return ''
end

.parse(r, p, q) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/africompta/acaccess.rb', line 8

def self.parse(r, p, q)
  dputs(2) { "in ACaccess: #{p} - #{q.inspect}" }
  method = p.gsub(/^\/acaccess\/merge\//, '')
  dputs(3) { "Calling method #{method} of #{r}" }
  case r
    when /GET/
      ret = self.get(method)
    when /POST/
      ret = self.post(method, q)
  end
  dputs(3) { "Result is #{ret.inspect}" }
  ret
end

.post(path, input) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/africompta/acaccess.rb', line 187

def self.post(path, input)
  #dputs_func
  dputs(5) { "self.post with #{path} and #{input.inspect}" }
  log_msg 'ACaccess.post', "post-merge-path #{path} with " +
                             "user #{input['user']} and pass #{input['pass']}"
  user, pass = input['user'], input['pass']
  u = Users.match_by_name(user)
  if not (u and u.pass == pass)
    dputs(1) { "Didn't find user #{user}" }
    return "User #{user} not known with pass #{pass}"
  end
  case path
    # Retrieves id of the path of the account
    when /account_get_id/
       = input['account']
      dputs(2) { "account_get_id with path #{}" }
      a = Accounts.get_id_by_path()
      a and return a
      dputs(2) { "didn't find anything" }
      return nil

    when 'movements_put'
      dputs(3) { "Going to put some movements: #{input['movements'].inspect}" }
      movs = ActiveSupport::JSON.decode(input['movements'])
      dputs(3) { "movs is now #{movs.inspect}" }
      if movs.size > 0
        movs.each { |m|
          if mov = Movements.from_json(m)
            dputs(2) { "Saved movement #{mov.global_id}" }
          else
            dputs(0) { "Error: couldn't create movement from #{m.inspect}" }
          end
          u.update_movement_index unless input._debug
        }
      end
    when 'movement_delete'
      dputs(3) { 'Going to delete movement' }
      while mov = Movements.match_by_global_id(input['global_id'])
        dputs(3) { "Found movement #{mov.inspect}" }
        mov.delete
      end
      dputs(3) { 'Finished deleting' }
    when 'account_put'
      dputs(3) { "Going to put account #{input['account'].inspect}" }
      acc = Accounts.from_s(input['account'])
      u. unless input._debug
      dputs(2) { "Saved account #{acc.global_id}" }
    when 'account_delete'
      dputs(3) { 'Going to delete account' }
      acc = Accounts.match_by_global_id(input['global_id'])
      acc.delete(true)
  end
  return 'ok'
end


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/africompta/acaccess.rb', line 22

def self.print_movements(start, stop)
  start, stop = start.to_i, stop.to_i
  dputs(2) { "Doing print_movements from #{start.class}:#{start}"+
      " to #{stop.class}:#{stop}" }
  ret = ''
#    Movements.search_all.select{|m|
#      mi = m.rev_index
#      m and mi and mi >= start and mi <= stop
  Movements.search_index_range(start, stop).each { |m|
    if start > 0
      dputs(4) { "Mer: Movement #{m.desc}, #{m.value}" }
    end
    ret += m.to_s + "\n"
  }
  dputs(3) { "Found movements: #{ret.inspect}" }
  ret
end


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
# File 'lib/africompta/acaccess.rb', line 40

def self.print_movements_actual(start, stop)
  start, stop = start.to_i, stop.to_i
  dputs(2) { "Doing print_movements_actual from #{start.class}:#{start}"+
      " to #{stop.class}:#{stop}" }
  ret = ''
  actual_ids = []
  AccountRoot.actual.get_tree { |a|
    actual_ids.push a.id
  }
  movs = Movements.search_all.select { |m|
    mi = m.rev_index
    m and mi and mi >= start and mi <= stop and actual_ids.find(m.)
  }
  dputs(2) { "Found #{movs.length} movements between #{start}..#{stop}" }
  movs.each { |m|
    if start > 0
      dputs(4) { "Mer: Movement #{m.desc}, #{m.value}" }
      ai = actual_ids.find(m..id)
      dputs(4) { "movement_src is #{m..inspect} from #{ai.inspect}" }
      dputs(4) { "actual_ids is #{actual_ids.inspect}" }
    end
    ret += m.to_s + "\n"
  }
  dputs(3) { "Found movements: #{ret.inspect}" }
  ret
end