-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.rb
More file actions
194 lines (162 loc) · 4.94 KB
/
Copy pathmodels.rb
File metadata and controls
194 lines (162 loc) · 4.94 KB
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
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
# to play in the sequel console, do this:
# # sequel sqlite://`pwd`/annotationhub.sqlite3
require 'sequel'
require 'securerandom'
class Resource < Sequel::Model
def validate
super
required_fields = [:title, :dataprovider,
:description, :coordinate_1_based, :maintainer,
:rdatadateadded, :preparerclass]
for item in required_fields
thing = self.send(item)
errors.add(item, 'cannot be empty') if thing.nil? ||
(thing.respond_to?(:empty?) && thing.empty?)
end
end
def before_create
# h = self.to_hash
# unless Resource.find(h).nil?
# return false
# end
unless self.ah_id =~ /^AH/
self.ah_id = SecureRandom.base64
end
true
super
end
# def after_create
# super
# if self.record_id.nil?
# self.record_id = self.get_record_id()
# end
# true
# end
def is_duplicate?()
resources = DB[:resources]
auto_cols = [:id, :ah_id, :rdatadateadded, :resource_id]
cols_to_compare = resources.columns - auto_cols
h = {}
for col in cols_to_compare
h[col] = self.send(col)
end
existing = resources.where(Sequel.negate(id: self.id)).where(h)
if existing.count == 0
return false
end
dep_tables = []
for table in DB.tables
dep_tables << table if DB[table].columns.include? :resource_id
end
for table in dep_tables
ds = DB[table]
cols_to_compare = ds.columns - auto_cols
rows = self.send table
for row in rows
h = {}
for col in cols_to_compare
h[col] = row.send col
end
if table == :tags
existing = ds.where(h)
else
existing = ds.where(Sequel.negate(id: row.id)).where(h)
end
if existing.count == 0
return false
end
end
end
true
end
## Note: this will (re-)set all record_ids.
def self.set_record_ids()
resources = DB[:resources]
h = {}
count = 1
Resource.all.each do |resource|
key = "#{resource.taxonomyid}_#{resource.genome}_#{resource.preparerclass}"
key += "_#{resource.input_sources.map{|i| i.sourceurl}.join("_")}"
h[key] = [] unless h.has_key?(key)
h[key] << resource.id
end
h.each_with_index do |(key, value), index|
for id in value
r = Resource[id]
r.record_id = index
r.save(validate: false)
end
end
end
def get_record_id(force=false)
unless force
unless self.record_id.nil?
return self.record_id
end
end
max = DB[:resources].max(:record_id)
return 1 if (max.nil?)
resources = DB[:resources]
cands =
resources.where(Sequel.negate(ah_id: \
self.ah_id)).where(taxonomyid: self.taxonomyid,
genome: self.genome, preparerclass: self.preparerclass)
for cand in cands
input_sources = InputSource.where(resource_id: cand[:id])
if input_sources.count == self.input_sources.count
if input_sources.map{|i| i.sourceurl} == \
self.input_sources.map{|i| i.sourceurl}
rdatapaths = Rdatapath.where(resource_id: cand[:id])
if rdatapaths.count == self.rdatapaths.count
if rdatapaths.map{|i| i.rdataclass} == \
self.rdatapaths.map{|i| i.rdataclass}
return cand[:record_id]
end
end
end
end
end
max = DB[:resources].max(:record_id) # call again just in case
if (max.nil?) # should not happen
max = 1
else
max += 1
end
max
end
one_to_many :rdatapaths
one_to_many :input_sources
one_to_many :tags
one_to_many :biocversions
many_to_one :recipe
many_to_one :status
many_to_one :location_prefix
## FIXME - make this a trigger?
def after_save
super
if self.ah_id =~ /==$/
self.update(:ah_id=>"AH#{self.id}")
end
end
end
class Rdatapath < Sequel::Model
many_to_one :resource
end
class InputSource < Sequel::Model
many_to_one :resource
end
class Tag < Sequel::Model
many_to_one :resource
end
class Biocversion < Sequel::Model
many_to_one :resource
end
class Recipe < Sequel::Model
one_to_many :resource
end
class Status < Sequel::Model
one_to_many :resource
end
class LocationPrefix < Sequel::Model
one_to_many :resource
end