Module: RTM::Merging::Topic

Includes:
MergeItemIdentifiers, ReWrap
Defined in:
lib/rtm/activerecord/merging.rb

Instance Method Summary collapse

Instance Method Details

#merge(other) ⇒ Object Also known as: merge_in



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
186
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
# File 'lib/rtm/activerecord/merging.rb', line 142

def merge(other)
  return unless other
  # The procedure for merging two topic items A and B
  # -> we will instead just modify a
  # (whose [parent] properties shall contain the same topic map item) is given below.
  raise TopicMergeException, "Both topics must belong to the same topic map!" if self.parent.__getobj__ != other.parent.__getobj__
  # It is an error if A and B both have non-null values in their [reified] properties which are different.
  raise TopicMergeException, "The Topics must not reify different things." if self.reified && other.reified && self.reified.__getobj__ != other.reified.__getobj__

  # 1. Create a new topic item C.
  # 2. Replace A by C wherever it appears in one of the following properties
  #    of an information item: [topics], [scope], [type], [player], and [reifier].
  # -> nothing to do here

  # 3. Repeat for B.
  # [topics]
  # done below, in rewrap
  # [scope]
  RTM::AR::TMDM::ScopedObjectsTopic.move_all(["topic_id", other.__getobj__.id, self.__getobj__.id])
  # [type]
  RTM::AR::TMDM::Association.move_all(["ttype_id", other.__getobj__.id, self.__getobj__.id])
  RTM::AR::TMDM::Role.move_all(["ttype_id", other.__getobj__.id, self.__getobj__.id])
  RTM::AR::TMDM::Name.move_all(["ttype_id", other.__getobj__.id, self.__getobj__.id])
  RTM::AR::TMDM::Occurrence.move_all(["ttype_id", other.__getobj__.id, self.__getobj__.id])

  # [player]
  RTM::AR::TMDM::AssociationRole.move_all(["topic_id", other.__getobj__.id, self.__getobj__.id])

  # [reifier]
  if other.reified
    self.reified = other.reified
  end
  # 4. Set C's [topic names] property to the union of the values of A and B's [topic names] properties.
  #self.names.add_all other.names
  count = RTM::AR::TMDM::Name.move_all(["topic_id", other.__getobj__.id, self.__getobj__.id])
  if count != 0 && count != names.size
    names.each_with_index do |n,i|
      (i+1).upto(names.size-1) do |j|
        if n == names[j]
          n.merge names[j]
        end
      end
    end
  end

  # 5. Set C's [occurrences] property to the union of the values of A and B's [occurrences] properties.
  #self.occurrences.add_all other.occurrences
  RTM::AR::TMDM::Occurrence.move_all(["topic_id", other.__getobj__.id, self.__getobj__.id])
  if count != 0 && count != occurrences.size
    occurrences.each_with_index do |o,i|
      (i+1).upto(occurrences.size-1) do |j|
        if o == occurrences[j]
          o.merge occurrences[j]
        end
      end
    end
  end

  # 6. Set C's [subject identifiers] property to the union of the values of A and B's [subject identifiers] properties.
  #self.subject_identifiers.add_all other.subject_identifiers
  RTM::AR::TMDM::SubjectIdentifier.move_all(["topic_id", other.__getobj__.id, self.__getobj__.id])

  # 7. Set C's [subject locators] property to the union of the values of A and B's [subject locators] properties.
  #self.subject_locators.add_all other.subject_locators
  RTM::AR::TMDM::SubjectLocator.move_all(["topic_id", other.__getobj__.id, self.__getobj__.id])

  # 8. Set C's [item identifiers] property to the union of the values of A and B's [item identifiers] properties.
  #self.item_identifiers.add_all other.item_identifiers
  #RTM::AR::TMDM::ItemIdentifier.update_all "construct_id = #{self.__getobj__.id}, construct_type = '#{self.__getobj__.class.name}'", "construct_id = #{other.__getobj__.id} and construct_type = '#{other.__getobj__.class.name}'"
  RTM::AR::TMDM::ItemIdentifier.move_all(
    ["construct_id", other.__getobj__.id, self.__getobj__.id],
    ["construct_type", other.__getobj__.class.name, self.__getobj__.class.name]
  )

  self.merge_rewrap other

  self
end