add tutorials and references to home page
[arvados.git] / apps / workbench / app / models / arvados_base.rb
1 class ArvadosBase < ActiveRecord::Base
2   self.abstract_class = true
3   attr_accessor :attribute_sortkey
4
5   def self.uuid_infix_object_kind
6     @@uuid_infix_object_kind ||= {
7       '4zz18' => 'arvados#collection',
8       'tpzed' => 'arvados#user',
9       'ozdt8' => 'arvados#api_client',
10       '8i9sb' => 'arvados#job',
11       'o0j2j' => 'arvados#link',
12       '57u5n' => 'arvados#log',
13       'j58dm' => 'arvados#specimen',
14       'p5p6p' => 'arvados#pipeline_template',
15       'mxsvm' => 'arvados#pipeline_template', # legacy Pipeline objects
16       'd1hrv' => 'arvados#pipeline_instance',
17       'uo14g' => 'arvados#pipeline_instance', # legacy PipelineInstance objects
18       'j7d0g' => 'arvados#group',
19       'ldvyl' => 'arvados#group' # only needed for legacy Project objects
20     }
21   end
22
23   def initialize(*args)
24     super(*args)
25     @attribute_sortkey ||= {
26       'id' => nil,
27       'uuid' => '000',
28       'owner' => '001',
29       'created_at' => '002',
30       'modified_at' => '003',
31       'modified_by_user' => '004',
32       'modified_by_client' => '005',
33       'name' => '050',
34       'tail_kind' => '100',
35       'tail_uuid' => '100',
36       'head_kind' => '101',
37       'head_uuid' => '101',
38       'info' => 'zzz-000',
39       'updated_at' => 'zzz-999'
40     }
41   end
42
43   def self.columns
44     return @columns unless @columns.nil?
45     @columns = []
46     @attribute_info ||= {}
47     return @columns if $arvados_api_client.arvados_schema[self.to_s.to_sym].nil?
48     $arvados_api_client.arvados_schema[self.to_s.to_sym].each do |coldef|
49       k = coldef[:name].to_sym
50       if coldef[:type] == coldef[:type].downcase
51         @columns << column(k, coldef[:type].to_sym)
52       else
53         @columns << column(k, :text)
54         serialize k, coldef[:type].constantize
55       end
56       attr_accessible k
57       @attribute_info[k] = coldef
58     end
59     attr_reader :etag
60     attr_reader :kind
61     @columns
62   end
63   def self.column(name, sql_type = nil, default = nil, null = true)
64     ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
65   end
66   def self.attribute_info
67     self.columns
68     @attribute_info
69   end
70   def self.find(uuid)
71     if uuid.class != String or uuid.length < 27 then
72       raise 'argument to find() must be a uuid string. Acceptable formats: warehouse locator or string with format xxxxx-xxxxx-xxxxxxxxxxxxxxx'
73     end
74     new.private_reload(uuid)
75   end
76   def self.order(*args)
77     ArvadosResourceList.new(self).order(*args)
78   end
79   def self.where(*args)
80     ArvadosResourceList.new(self).where(*args)
81   end
82   def self.limit(*args)
83     ArvadosResourceList.new(self).limit(*args)
84   end
85   def self.eager(*args)
86     ArvadosResourceList.new(self).eager(*args)
87   end
88   def self.all(*args)
89     ArvadosResourceList.new(self).all(*args)
90   end
91   def save
92     obdata = {}
93     self.class.columns.each do |col|
94       obdata[col.name.to_sym] = self.send(col.name.to_sym)
95     end
96     obdata.delete :id
97     postdata = { self.class.to_s.underscore => obdata }
98     if etag
99       postdata['_method'] = 'PUT'
100       obdata.delete :uuid
101       resp = $arvados_api_client.api(self.class, '/' + uuid, postdata)
102     else
103       resp = $arvados_api_client.api(self.class, '', postdata)
104     end
105     return false if !resp[:etag] || !resp[:uuid]
106
107     # set read-only non-database attributes
108     @etag = resp[:etag]
109     @kind = resp[:kind]
110
111     # these attrs can be modified by "save" -- we should update our copies
112     %w(uuid owner created_at
113        modified_at modified_by_user modified_by_client
114       ).each do |attr|
115       self.send(attr + '=', resp[attr.to_sym])
116     end
117
118     self
119   end
120   def save!
121     self.save or raise Exception.new("Save failed")
122   end
123
124   def destroy
125     if etag || uuid
126       postdata = { '_method' => 'DELETE' }
127       resp = $arvados_api_client.api(self.class, '/' + uuid, postdata)
128       resp[:etag] && resp[:uuid] && resp
129     else
130       true
131     end
132   end
133       
134   def links(*args)
135     o = {}
136     o.merge!(args.pop) if args[-1].is_a? Hash
137     o[:link_class] ||= args.shift
138     o[:name] ||= args.shift
139     o[:head_kind] ||= args.shift
140     o[:tail_kind] = self.kind
141     o[:tail_uuid] = self.uuid
142     if all_links
143       return all_links.select do |m|
144         ok = true
145         o.each do |k,v|
146           if !v.nil?
147             test_v = m.send(k)
148             if (v.respond_to?(:uuid) ? v.uuid : v.to_s) != (test_v.respond_to?(:uuid) ? test_v.uuid : test_v.to_s)
149               ok = false
150             end
151           end
152         end
153         ok
154       end
155     end
156     @links = $arvados_api_client.api Link, '', { _method: 'GET', where: o, eager: true }
157     @links = $arvados_api_client.unpack_api_response(@links)
158   end
159   def all_links
160     return @all_links if @all_links
161     res = $arvados_api_client.api Link, '', {
162       _method: 'GET',
163       where: {
164         tail_kind: self.kind,
165         tail_uuid: self.uuid
166       },
167       eager: true
168     }
169     @all_links = $arvados_api_client.unpack_api_response(res)
170   end
171   def reload
172     private_reload(self.uuid)
173   end
174   def private_reload(uuid_or_hash)
175     raise "No such object" if !uuid_or_hash
176     if uuid_or_hash.is_a? Hash
177       hash = uuid_or_hash
178     else
179       hash = $arvados_api_client.api(self.class, '/' + uuid_or_hash)
180     end
181     hash.each do |k,v|
182       if self.respond_to?(k.to_s + '=')
183         self.send(k.to_s + '=', v)
184       else
185         # When ArvadosApiClient#schema starts telling us what to expect
186         # in API responses (not just the server side database
187         # columns), this sort of awfulness can be avoided:
188         self.instance_variable_set('@' + k.to_s, v)
189         if !self.respond_to? k
190           singleton = class << self; self end
191           singleton.send :define_method, k, lambda { instance_variable_get('@' + k.to_s) }
192         end
193       end
194     end
195     @all_links = nil
196     self
197   end
198   def dup
199     super.forget_uuid!
200   end
201
202   def attributes_for_display
203     self.attributes.reject { |k,v|
204       attribute_sortkey.has_key?(k) and !attribute_sortkey[k]
205     }.sort_by { |k,v|
206       attribute_sortkey[k] or k
207     }
208   end
209
210   def self.creatable?
211     current_user
212   end
213
214   def editable?
215     (current_user and
216      (current_user.is_admin or
217       current_user.uuid == self.owner))
218   end
219
220   def attribute_editable?(attr)
221     if "created_at modified_at modified_by_user modified_by_client updated_at".index(attr.to_s)
222       false
223     elsif "uuid owner".index(attr.to_s)
224       current_user and current_user.is_admin
225     else
226       current_user and current_user.uuid == owner
227     end
228   end
229
230   def self.resource_class_for_uuid(uuid, opts={})
231     if uuid.is_a? ArvadosBase
232       return uuid.class
233     end
234     unless uuid.is_a? String
235       return nil
236     end
237     if opts[:class].is_a? Class
238       return opts[:class]
239     end
240     if uuid.match /^[0-9a-f]{32}(\+[^,]+)*(,[0-9a-f]{32}(\+[^,]+)*)*$/
241       return Collection
242     end
243     resource_class = nil
244     uuid.match /^[0-9a-z]{5}-([0-9a-z]{5})-[0-9a-z]{15}$/ do |re|
245       resource_class ||= $arvados_api_client.
246         kind_class(self.uuid_infix_object_kind[re[1]])
247     end
248     if opts[:referring_object] and
249         opts[:referring_attr] and
250         opts[:referring_attr].match /_uuid$/
251       resource_class ||= $arvados_api_client.
252         kind_class(opts[:referring_object].
253                    attributes[opts[:referring_attr].
254                               sub(/_uuid$/, '_kind')])
255     end
256     resource_class
257   end
258
259   protected
260
261   def forget_uuid!
262     self.uuid = nil
263     @etag = nil
264     self
265   end
266
267   def self.current_user
268     Thread.current[:user] ||= User.current if Thread.current[:arvados_api_token]
269     Thread.current[:user]
270   end
271   def current_user
272     self.class.current_user
273   end
274 end