app/models/project.rb
# It assumes that
# - annotations are already normal, and
# - documents exist in the database
It assumes that
annotations are already normal, and
documents exist in the database
It assumes that annotations are already normal and documents exist in the database
def store_annotations_collection(annotations_collection, options)
		messages = []
		num_skipped = 0
		annotations_collection_with_doc = annotations_collection.collect do |annotations|
			sourcedb, sourceid = if annotations.is_a? Array
				a = annotations.first
				[a[:sourcedb], a[:sourceid]]
			else
				[annotations[:sourcedb], annotations[:sourceid]]
			end
			docs = Doc.where(sourcedb:sourcedb, sourceid:sourceid)
			if docs.count == 1
				[annotations, docs.first]
			else
				error_message = if docs.empty?
					'Document does not exist.'
				else
					'Multiple entries of the document.'
				end
				messages << {sourcedb:sourcedb, sourceid:sourceid, body:error_message[0 .. 250]}
				[annotations, nil]
			end
		end.reject{|e| e[1].nil?}
		num_annotations_with_doc = annotations_collection_with_doc.count
		if options[:mode] == 'skip'
			annotations_collection_with_doc.select! do |annotations, doc|
				ProjectDoc.where(project_id:id, doc_id:doc.id).pluck(:denotations_num).first == 0
			end
			num_skipped = num_annotations_with_doc - annotations_collection_with_doc.count
		end
		annotations_collection_with_doc.each do |annotations, doc|
			messages += Annotation.prepare_annotations!(annotations, doc, options)
		end
		aligned_collection = []
		annotations_collection_with_doc.each do |annotations, doc|
			ann = annotations.is_a?(Array) ? annotations : [annotations]
			if options[:mode] == 'replace'
				delete_doc_annotations(doc)
			else
				case options[:mode]
				when 'add'
					ann.each{|a| reid_annotations!(a, doc)}
				when 'merge'
					ann.each{|a| reid_annotations!(a, doc)}
					base_annotations = doc.hannotations(self)
					ann.each{|a| Annotation.prepare_annotations_for_merging!(a, base_annotations)}
				end
			end
			aligned_collection += ann
		rescue StandardError => e
			messages << {sourcedb: doc.sourcedb, sourceid: doc.sourceid, body: e.message[0 .. 250]}
		end
		messages << {body: "Uploading for #{num_skipped} documents were skipped due to existing annotations."} if num_skipped > 0
		instantiate_and_save_annotations_collection(aligned_collection) if aligned_collection.present?
		messages
	end
def store_annotations_collection ( annotations_collection , options ) messages = [ ] num_skipped = 0 annotations_collection_with_doc = annotations_collection . collect do | annotations | sourcedb , sourceid = if annotations . is_a? Array a = annotations . first [ a [ :sourcedb ] , a [ :sourceid ] ] else [ annotations [ :sourcedb ] , annotations [ :sourceid ] ] end docs = Doc . where ( sourcedb : sourcedb , sourceid : sourceid ) if docs . count == 1 [ annotations , docs . first ] else error_message = if docs . empty? 'Document does not exist.' else 'Multiple entries of the document.' end messages << { sourcedb : sourcedb , sourceid : sourceid , body : error_message [ 0 .. 250 ] } [ annotations , nil ] end end . reject { | e | e [ 1 ] . nil? } num_annotations_with_doc = annotations_collection_with_doc . count if options [ :mode ] == 'skip' annotations_collection_with_doc . select! do | annotations , doc | ProjectDoc . where ( project_id : id , doc_id : doc . id ) . pluck ( :denotations_num ) . first == 0 end num_skipped = num_annotations_with_doc - annotations_collection_with_doc . count end annotations_collection_with_doc . each do | annotations , doc | messages += Annotation . prepare_annotations! ( annotations , doc , options ) end aligned_collection = [ ] annotations_collection_with_doc . each do | annotations , doc | ann = annotations . is_a? ( Array ) ? annotations : [ annotations ] if options [ :mode ] == 'replace' delete_doc_annotations ( doc ) else case options [ :mode ] when 'add' ann . each { | a | reid_annotations! ( a , doc ) } when 'merge' ann . each { | a | reid_annotations! ( a , doc ) } base_annotations = doc . hannotations ( self ) ann . each { | a | Annotation . prepare_annotations_for_merging! ( a , base_annotations ) } end end aligned_collection += ann rescue StandardError => e messages << { sourcedb : doc . sourcedb , sourceid : doc . sourceid , body : e . message [ 0 .. 250 ] } end messages << { body : "Uploading for #{num_skipped} documents were skipped due to existing annotations." } if num_skipped > 0 instantiate_and_save_annotations_collection ( aligned_collection ) if aligned_collection . present? messages end
It assumes that
 annotations are already normal, and
 documents exist in the database
It assumes that annotations are already normal and documents exist in the database
# To find the doc for each annotation object # skip option
annotations_collection
<not_specific>
options
<not_specific>