lib/rhoconnect/client.rb in rhoconnect-3.4.5 vs lib/rhoconnect/client.rb in rhoconnect-4.0.0.beta.10
- old
+ new
@@ -1,9 +1,9 @@
module Rhoconnect
class InvalidSourceNameError < RuntimeError; end
- class Client < Model
+ class Client < StoreOrm
field :device_type, :string
field :device_push_type, :string
field :device_pin, :string
field :device_port, :string
field :phone_id, :string
@@ -13,10 +13,36 @@
attr_accessor :source_name
validates_presence_of :app_id, :user_id
include Document
include LockOps
+
+ define_valid_doctypes([:cd,
+ :cd_size,
+ :page,
+ :delete_page,
+ :create_links,
+ :create_links_page,
+ :metadata_page,
+ :total_count_page,
+ :page_token,
+ :schema_sha1,
+ :schema_page,
+ :metadata_sha1,
+ :metadata_page,
+ :search,
+ :search_token,
+ :search_page,
+ :search_errors,
+ :create_errors,
+ :create_errors_page,
+ :update_errors,
+ :update_errors_page,
+ :update_rollback,
+ :update_rollback_page,
+ :delete_errors,
+ :delete_errors_page])
def self.create(fields,params={})
Rhoconnect.license.check_and_use_seat
fields[:id] ||= get_random_uuid
res = super(fields,params)
@@ -39,44 +65,68 @@
@app ||= App.load(app_id)
end
def doc_suffix(doctype)
doctype = doctype.to_s
- if doctype == '*'
- "#{self.user_id}:#{self.id}:*"
- elsif self.source_name
+ if self.source_name
"#{self.user_id}:#{self.id}:#{self.source_name}:#{doctype}"
else
raise InvalidSourceNameError.new('Invalid Source Name For Client')
end
end
+ def store_index(doctype)
+ source = Source.load(self.source_name,{:app_id => app_id,:user_id => self.user_id})
+ index = compute_store_index(doctype, source, user_id)
+ end
+
def delete
- flash_data('*')
+ flush_all_documents
Rhoconnect.license.free_seat
if Rhoconnect.stats
Rhoconnect::Stats::Record.set('clients') { Store.decr('client:count') }
else
Store.decr('client:count')
end
super
end
-
+
+ def flush_all_documents
+ app.sources.each do |sourcename|
+ flush_source_documents(sourcename)
+ end
+ end
+
+ def flush_source_documents(source_name)
+ self.class.valid_doctypes.each do |doctype|
+ flush_source_data(doctype, source_name)
+ end
+ end
+
+ def flush_source_data(doctype, from_source)
+ verify_doctype(doctype)
+ self.source_name=from_source
+ docnamestr = docname('') + doctype.to_s
+ Store.flush_data(docnamestr)
+ end
+
def switch_user(new_user_id)
- flash_data('*')
+ loaded_source = self.source_name
+ flush_all_documents
User.load(self.user_id).clients.delete(self.id)
User.load(new_user_id).clients << self.id
self.user_id = new_user_id
+ self.source_name = loaded_source
end
def update_clientdoc(sources)
# TODO: We need to store schema info and data info in bulk data
# source masterdoc and source schema might have changed!
sources.each do |source|
s = Source.load(source,{:app_id => app_id,:user_id => user_id})
+ self.source_name = source
unless s.sync_type.to_sym == :bulk_sync_only
- self.source_name = source
- Store.clone(s.docname(:md_copy),self.docname(:cd))
+ s.clone(:md_copy,self.docname(:cd))
end
self.put_value(:schema_sha1,s.get_value(:schema_sha1))
end
end
\ No newline at end of file