This repository has been archived by the owner on Feb 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnotes.rb
158 lines (133 loc) · 5.48 KB
/
notes.rb
1
2
3
4
5
6
7
8
9
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
require_relative 'settings'
require 'ostruct'
require 'progressbar'
require 'ontologies_linked_data'
require 'date'
require_relative 'helpers/rest_helper'
only_these_ontologies = []
def create_reply(note, parent = nil)
user = RestHelper.user(note[:author])
reply = LinkedData::Models::Notes::Reply.new
reply.id = RDF::IRI.new("#{LinkedData::Models::Note.id_prefix.to_s}/#{note[:id].sub('Note_', '')}")
reply.body = note[:body]
reply.parent = parent if parent
reply.creator = LinkedData::Models::User.find(user.username).first
reply.created = DateTime.parse(Time.at(note[:created] / 1000).to_s)
reply.save rescue binding.pry
if note[:associated]
child_notes = note[:associated].first[:noteBean]
child_notes = child_notes.is_a?(Array) ? child_notes : [child_notes]
child_notes.each do |child_note|
create_reply(child_note, reply)
end
end
reply
end
def convert_proposal(note, new_note)
return unless note.type
proposal = LinkedData::Models::Notes::Proposal.new
case note.type
when "ProposalForCreateEntity"
values = note.values.first[:ProposalForCreateEntity]
reasonForChange = values[:reasonForChange]
contactInfo = values[:contactInfo]
proposal_type = LinkedData::Models::Notes::ProposalType.find("ProposalNewClass").first
id = values[:id].eql?("") ? nil : values[:id]
proposal.classId = id unless id.nil?
proposal.label = values[:preferredName]
synonym = values[:synonyms].select {|s| !s.nil? && !s.eql?("")}
synonym.map! {|s| s[:string].to_s rescue nil}.compact!
proposal.synonym = synonym unless synonym.empty?
proposal.definition = [values[:definition]]
proposal.parent = values[:parent].first[:string] if values[:parent].is_a?(Hash)
when "ProposalForChangeHierarchy"
values = note.values.first[:ProposalForChangeHierarchy]
reasonForChange = values[:reasonForChange]
contactInfo = values[:contactInfo]
proposal_type = LinkedData::Models::Notes::ProposalType.find("ProposalChangeHierarchy").first
proposal.newTarget = values[:relationshipTarget].first[:string]
proposal.oldTarget = values[:oldRelationshipTarget].first[:string]
proposal.newRelationshipType = [values[:relationshipType]]
else
binding.pry
end
proposal.type = proposal_type
proposal.reasonForChange = reasonForChange
proposal.contactInfo = contactInfo
proposal.save rescue binding.pry
new_note.proposal = proposal
nil
end
@errors = []
def convert_note(note)
user = RestHelper.user(note.author)
ont = LinkedData::Models::Ontology.find(RestHelper.safe_acronym(RestHelper.latest_ontology(note.ontologyId).abbreviation)).first
return if ont.nil?
nn = LinkedData::Models::Note.new
nn.id = RDF::IRI.new("#{LinkedData::Models::Note.id_prefix.to_s}#{note.id.sub('Note_', '')}")
nn.creator = LinkedData::Models::User.find(user.username).first
nn.created = DateTime.parse(Time.at((note.created || (Time.now.to_i * 1000)) / 1000).to_s)
nn.body = note.body.strip if note.body
nn.subject = note.subject.strip if note.subject
nn.relatedOntology = [ont]
nn.archived = note.archived
# Add class to note
if note.appliesToList.first && note.appliesToList.first[:appliesTo][:type].eql?("Class")
classId = note.appliesToList.first[:appliesTo][:id]
latest_ont = RestHelper.latest_ontology(note.ontologyId)
old_cls = RestHelper.concept(note.createdInOntologyVersion, classId) rescue nil
clean_class_id = old_cls.nil? ? classId : old_cls.id
cls = RestHelper.concept(latest_ont.id, clean_class_id) rescue nil
cls = RestHelper.concept(latest_ont.id, classId) rescue nil unless cls
if cls
ont.bring(:submissions)
submission = ont.latest_submission unless ont.submissions.empty?
relatedClass = LinkedData::Models::Class.find(RDF::IRI.new(cls.fullId)).in(submission).first rescue nil
nn.relatedClass = [relatedClass] unless relatedClass.nil?
end
@errors << "Could not find #{classId} for #{nn.id.to_s}" if cls.nil? && relatedClass.nil?
end
# Needs all versions available
# nn.createdInSubmission = note.createdInOntologyVersion
if note.type && !note.type.eql?("Comment")
convert_proposal(note, nn)
end
# Handle children
children = []
if note.associated
child_notes = note.associated.first[:noteBean] rescue binding.pry
child_notes = child_notes.is_a?(Array) ? child_notes : [child_notes]
child_notes.each do |child_note|
children << create_reply(child_note)
end
nn.reply = children
end
nn.save rescue binding.pry
nn
end
# Delete existing
if only_these_ontologies.empty?
puts "Deleting #{LinkedData::Models::Note.all.length} notes and their replies"
LinkedData::Models::Note.all.each {|n| n.delete}
LinkedData::Models::Notes::Reply.all.each {|r| r.delete}
end
ontologies = RestHelper.ontologies
puts "Number of ontologies to migrate: #{ontologies.length}"
pbar = ProgressBar.new("Migrating", ontologies.length*2)
ontologies.each_with_index do |ont, ind|
next if !only_these_ontologies.empty? && !only_these_ontologies.include?(ont.abbreviation)
ont.bring(:notes).notes.each {|n| n.delete} if !only_these_ontologies.empty?
begin
notes = RestHelper.ontology_notes(ont.ontologyId)
rescue OpenURI::HTTPError
retry
end
pbar.inc
notes.each_with_index do |note, index|
pbar.inc if index == notes.length / 2
new_note = convert_note(note)
end
pbar.inc
end
puts "Errors:", @errors.join("\n")
puts "\n\nCreated #{LinkedData::Models::Note.all.length} notes and #{LinkedData::Models::Notes::Reply.all.length} replies"