Class: Rhoconnect::Source

Inherits:
MemoryModel show all
Includes:
Document, LockOps
Defined in:
lib/rhoconnect/source.rb

Instance Attribute Summary collapse

Attributes inherited from MemoryModel

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LockOps

#lock

Methods included from Document

#delete_data, #docname, #flash_data, #flash_source_data, #flush_zdata, #get_data, #get_list, #get_object, #get_value, #get_zdata, #put_data, #put_list, #put_object, #put_value, #put_zdata, #remove_objects, #rename, #update_count, #update_objects

Methods inherited from MemoryModel

class_prefix, define_fields, is_exist?, #to_array, #update_fields, validates_presence_of

Constructor Details

#initialize(fields) ⇒ Source

Returns a new instance of Source.



102
103
104
105
# File 'lib/rhoconnect/source.rb', line 102

def initialize(fields)
  self.name = fields['name'] || fields[:name]
  update_fields(fields)
end

Instance Attribute Details

#app_idObject

Returns the value of attribute app_id.



91
92
93
# File 'lib/rhoconnect/source.rb', line 91

def app_id
  @app_id
end

#user_idObject

Returns the value of attribute user_id.



91
92
93
# File 'lib/rhoconnect/source.rb', line 91

def user_id
  @user_id
end

Class Method Details

.create(fields, params) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/rhoconnect/source.rb', line 123

def self.create(fields,params)
  fields = fields.with_indifferent_access # so we can access hash keys as symbols
  super(fields,params)
  @@model_data[fields[:name].to_sym] = {}
  set_defaults(fields)
  obj = new(fields)
  obj.assign_args(params)
  obj
end

.delete_allObject



172
173
174
175
176
# File 'lib/rhoconnect/source.rb', line 172

def self.delete_all
  params = {:app_id => APP_NAME,:user_id => '*'}
  @@model_data.each { |k,v| Source.load(k,params).flash_store_data }
  @@model_data = {}
end

.load(obj_id, params) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/rhoconnect/source.rb', line 133

def self.load(obj_id,params)
  validate_attributes(params)
  
  # if source is pre-defined 
  # create it dynamically here
  Rhoconnect.create_predefined_source(obj_id,params)
  
  model_hash = @@model_data[obj_id.to_sym]
  obj = new(model_hash) if model_hash
  if obj
    obj = obj.dup
    obj.assign_args(params) 
  end
  obj
end

.set_defaults(fields) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rhoconnect/source.rb', line 107

def self.set_defaults(fields)
  fields[:url] ||= ''
  fields[:login] ||= ''
  fields[:password] ||= ''
  fields[:priority] ||= 3
  fields[:partition_type] = fields[:partition_type] ? fields[:partition_type].to_sym : :user
  fields[:poll_interval] ||= 300
  fields[:sync_type] = fields[:sync_type] ? fields[:sync_type].to_sym : :incremental
  fields[:id] = fields[:name]
  fields[:rho__id] = fields[:name]
  fields[:belongs_to] = fields[:belongs_to].to_json if fields[:belongs_to]
  fields[:schema] = fields[:schema].to_json if fields[:schema]
  fields[:retry_limit] = fields[:retry_limit] ? fields[:retry_limit] : 0
  fields[:simulate_time] = fields[:simulate_time] ? fields[:simulate_time] : 0
end

.update_associations(sources) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/rhoconnect/source.rb', line 149

def self.update_associations(sources)
  params = {:app_id => APP_NAME,:user_id => '*'}
  sources.each { |source| Source.load(source, params).has_many = nil }
  sources.each do |source|
    s = Source.load(source, params)
    if s.belongs_to
      belongs_to = JSON.parse(s.belongs_to)
      if belongs_to.is_a?(Array)
        belongs_to.each do |entry|
          attrib = entry.keys[0]
          model = entry[attrib]
          owner = Source.load(model, params)
          owner.has_many ||= ''
          owner.has_many = owner.has_many+',' if owner.has_many.length > 0
          owner.has_many += [source,attrib].join(',')
        end
      else
        log "WARNING: Incorrect belongs_to format for #{source}, belongs_to should be an array."
      end
    end
  end
end

Instance Method Details

#appObject

Return the app the source belongs to



222
223
224
# File 'lib/rhoconnect/source.rb', line 222

def app
  App.load(self.app_id)
end

#assign_args(params) ⇒ Object



178
179
180
181
# File 'lib/rhoconnect/source.rb', line 178

def assign_args(params)
  self.user_id = params[:user_id]
  self.app_id = params[:app_id]   
end

#blob_attribsObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/rhoconnect/source.rb', line 183

def blob_attribs
  return '' unless self.schema
  schema = JSON.parse(self.schema)
  blob_attribs = []
  schema['property'].each do |key,value|
    values = value ? value.split(',') : []
    if values.include?('blob')
      attrib = key.dup
      attrib << "," + (values.include?('overwrite') ? '1' : '0')
      blob_attribs << attrib 
    end
  end
  blob_attribs.sort.join(',')
end

#check_refresh_timeObject



275
276
277
278
# File 'lib/rhoconnect/source.rb', line 275

def check_refresh_time
  self.poll_interval == 0 or 
  (self.poll_interval != -1 and self.read_state.refresh_time <= Time.now.to_i)
end

#clone(src_doctype, dst_doctype) ⇒ Object



203
204
205
# File 'lib/rhoconnect/source.rb', line 203

def clone(src_doctype,dst_doctype)
  Store.clone(docname(src_doctype),docname(dst_doctype))
end

#deleteObject



258
259
260
261
# File 'lib/rhoconnect/source.rb', line 258

def delete
  flash_store_data
  @@model_data.delete(rho__id.to_sym) if rho__id
end

#delete_user_read_stateObject



242
243
244
245
246
# File 'lib/rhoconnect/source.rb', line 242

def delete_user_read_state
  id = {:app_id => self.app_id,:user_id => user_by_partition,
    :source_name => self.name}
  ReadState.delete_user(id)
end

#doc_suffix(doctype) ⇒ Object



248
249
250
# File 'lib/rhoconnect/source.rb', line 248

def doc_suffix(doctype)
  "#{user_by_partition}:#{self.name}:#{doctype.to_s}"
end

#flash_store_dataObject



252
253
254
255
256
# File 'lib/rhoconnect/source.rb', line 252

def flash_store_data
  delete_user_read_state
  flash_data('*')
  flash_data(poll_interval_key)
end

#if_need_refresh(client_id = nil, params = nil) {|client_id, params| ... } ⇒ Object

Yields:

  • (client_id, params)


280
281
282
283
284
285
286
287
288
# File 'lib/rhoconnect/source.rb', line 280

def if_need_refresh(client_id=nil,params=nil)
  need_refresh = lock(:md) do |s|
    check = check_refresh_time
    self.read_state.prev_refresh_time = self.read_state.refresh_time if check
    self.read_state.refresh_time = Time.now.to_i + self.poll_interval if check
    check
  end
  yield client_id,params if need_refresh
end

#is_pass_through?Boolean

Returns:

  • (Boolean)


325
326
327
# File 'lib/rhoconnect/source.rb', line 325

def is_pass_through?
  self.pass_through and self.pass_through == 'true'
end

#load_read_stateObject



236
237
238
239
240
# File 'lib/rhoconnect/source.rb', line 236

def load_read_state
  id = {:app_id => self.app_id,:user_id => user_by_partition,
    :source_name => self.name}
  ReadState.load(id)
end

#partitionObject



263
264
265
# File 'lib/rhoconnect/source.rb', line 263

def partition
  self.partition_type.to_sym
end

#partition=(value) ⇒ Object



267
268
269
# File 'lib/rhoconnect/source.rb', line 267

def partition=(value)
  self.partition_type = value
end

#poll_intervalObject



207
208
209
210
# File 'lib/rhoconnect/source.rb', line 207

def poll_interval
  value = Store.get_value(poll_interval_key)
  value ? value.to_i : nil
end

#poll_interval=(interval) ⇒ Object



212
213
214
# File 'lib/rhoconnect/source.rb', line 212

def poll_interval=(interval)
  Store.put_value(poll_interval_key, interval)
end

#read_stateObject



230
231
232
233
234
# File 'lib/rhoconnect/source.rb', line 230

def read_state
  id = {:app_id => self.app_id,:user_id => user_by_partition,
    :source_name => self.name}
  load_read_state || ReadState.create(id)
end

#rewind_refresh_time(query_failure) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/rhoconnect/source.rb', line 290

def rewind_refresh_time(query_failure)
  return if self.poll_interval == 0
  lock(:md) do |s|
    rewind_time = false
    # reset number of retries 
    # and prev_refresh_time on succesfull query
    # or if last refresh was more than 'poll_interval' time ago
    if not query_failure or ((Time.now.to_i - self.read_state.prev_refresh_time) >= self.poll_interval)
      # we need to reset the prev_refresh_time here
      # otherwise in case of expired poll interval
      # and repeating failures - it will reset the counter 
      # on every error
      self.read_state.prev_refresh_time = Time.now.to_i
      self.read_state.retry_counter = 0
    end
  
    # rewind the refresh time on failure
    # if retry limit is not reached
    if query_failure
      if self.read_state.retry_counter < self.retry_limit
        self.read_state.increment!(:retry_counter)
        rewind_time = true
        # we have reached the limit - do not rewind the refresh time
        # and reset the counter
      else
        self.read_state.retry_counter = 0
      end
    end
    
    if rewind_time
      self.read_state.refresh_time = self.read_state.prev_refresh_time
    end
  end 
end

#schemaObject



226
227
228
# File 'lib/rhoconnect/source.rb', line 226

def schema
  self.get_value(:schema)
end

#update(fields) ⇒ Object



198
199
200
201
# File 'lib/rhoconnect/source.rb', line 198

def update(fields)
  fields = fields.with_indifferent_access # so we can access hash keys as symbols
  self.class.set_defaults(fields)
end

#userObject

Return the user associated with a source



217
218
219
# File 'lib/rhoconnect/source.rb', line 217

def user
  User.load(self.user_id)
end

#user_by_partitionObject



271
272
273
# File 'lib/rhoconnect/source.rb', line 271

def user_by_partition
  self.partition.to_sym == :user ? self.user_id : '__shared__'
end