Module: Rhoconnect::Model::Helpers::FindDuplicatesOnUpdate

Defined in:
lib/rhoconnect/model/helpers/find_duplicates_on_update.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
# File 'lib/rhoconnect/model/helpers/find_duplicates_on_update.rb', line 6

def self.included(base)
	base.extend Rhoconnect::Model::Helpers::FindDuplicatesOnUpdate::ClassMethods
end

Instance Method Details

#find_duplicates_on_update(options, invalid_meta, operation, operation_data, client_ids) ⇒ Object



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
# File 'lib/rhoconnect/model/helpers/find_duplicates_on_update.rb', line 10

def find_duplicates_on_update(options, invalid_meta, operation, operation_data, client_ids)
 invalid_meta ||= {}
 # processed_ids = {}
 processed_objs = {}
 operation_data.each_with_index do |client_operation_data,index|
   client_id = client_ids[index]
   client_operation_data.each do |source_operation_entry|
     source_id = source_operation_entry[0]
     list_of_objs = source_operation_entry[1]
     list_of_objs.each_with_index do |obj_entry, objindex|
       key = obj_entry[0]
       objvalue = obj_entry[1]

       processed_objs[source_id] ||= {}
       processed_source_objs = processed_objs[source_id]
       obj_not_a_duplicate = true
       if processed_source_objs.has_key? key
         processed_records = processed_source_objs[key]
         processed_records.each do |processed_record|
           master_obj = processed_record[:value]
           # master_client_id = processed_record[:client_id]
           master_queue_index = processed_record[:queue_index].to_i
           master_obj_index = processed_record[:index].to_i

           if master_obj == objvalue
             obj_not_a_duplicate = false
              if options[:raise_error]
         		  invalid_meta[index] ||= {}
         		  invalid_meta[index][source_id] ||= {}
         		  invalid_meta[index][source_id][objindex] ||= {}
         		  invalid_meta[index][source_id][objindex][:error] = "Error during #{operation}: object conflict detected"
         	  else
              invalid_meta[index] ||= {}
              invalid_meta[index][source_id] ||= {}
              invalid_meta[index][source_id][objindex] ||= {}
              invalid_meta[index][source_id][objindex][:duplicate_of] = true

              invalid_meta[master_queue_index] ||= {}
              invalid_meta[master_queue_index][source_id] ||= {}
              invalid_meta[master_queue_index][source_id][master_obj_index] ||= {}
              invalid_meta[master_queue_index][source_id][master_obj_index][:duplicates] ||= []
              invalid_meta[master_queue_index][source_id][master_obj_index][:duplicates] <<  {:client_id => client_id, :key => key, :value => objvalue}
              end
             break
           end
         end
       end
       # objects are not equal - add to already processed
       if obj_not_a_duplicate
         processed_source_objs[key] ||= []
         processed_source_objs[key] << {:value => objvalue, :client_id => client_id, :queue_index => index, :index => objindex}
       end
     end
   end
 end
  if options[:handler] and invalid_meta.size > 0
 	proc = bind_handler :find_duplicates_on_update, options[:handler]
    invalid_meta = proc.call options, invalid_meta, operation, operation_data, client_ids
  end
 invalid_meta
end