links.rb revision a686bfa88234792b2dd2515da6a60aa110380916
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen key = Link::KINDS_MAPPING.keys.find {|k| typename.include?(k) }
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen kind = Link::KINDS.find {|k| typename.downcase.include?(k) }
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen def update_or_create_from_hash(hash, user, timestamp = Time.now)
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen raise ArgumentError, 'No hash given.' unless hash.is_a? Hash
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen # hash['name'] # maybe nil, in this case, we need to generate a name
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen link_iri = iri_for_child(hash['name'] || hash['linkid'])
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen source_iri = hash['source_iri'] || iri_for_child(hash['source'])
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen target_iri = hash['target_iri'] || iri_for_child(hash['target'])
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen source = Ontology.find_with_iri(source_iri) || (raise ArgumentError,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "source #{Settings.OMS} not found: #{source_iri}")
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen target = Ontology.find_with_iri(target_iri) || (raise ArgumentError,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "target #{Settings.OMS} not found: #{target_iri}")
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen gmorphism = 'http://purl.net/dol/translations/' + gmorphism unless gmorphism.include?('://')
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen # finally, create or update the link
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen link.versions << link_version
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen link.update_version!(to: link_version)
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen # entity mapping
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen if hash["map"]
445f9e31c6c3aa6c0a72be8565da8f6e594d24fbTimo Sirainen source = Entity.where(text: hash["map"].first["text"],ontology_id: link.source.id).first
445f9e31c6c3aa6c0a72be8565da8f6e594d24fbTimo Sirainen target = Entity.where(text: hash["map"].second["text"], ontology_id: link.target.id).first
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen entity_mapping = EntityMapping.
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen where(source_id: source, target_id: target, link_id: link).
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen first_or_create!