rename projects
[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     return @columns if $arvados_api_client.arvados_schema[self.to_s.to_sym].nil?
47     $arvados_api_client.arvados_schema[self.to_s.to_sym].each do |coldef|
48       k = coldef[:name].to_sym
49       if coldef[:type] == coldef[:type].downcase
50         @columns << column(k, coldef[:type].to_sym)
51       else
52         @columns << column(k, :text)
53         serialize k, coldef[:type].constantize
54       end
55       attr_accessible k
56     end
57     attr_reader :etag
58     attr_reader :kind
59     @columns
60   end
61   def self.column(name, sql_type = nil, default = nil, null = true)
62     ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
63   end
64   def self.find(uuid)
65     if uuid.class != String or uuid.length < 27 then
66       raise 'argument to find() must be a uuid string. Acceptable formats: warehouse locator or string with format xxxxx-xxxxx-xxxxxxxxxxxxxxx'
67     end
68     new.private_reload(uuid)
69   end
70   def self.where(*args)
71     ArvadosResourceList.new(self).where(*args)
72   end
73   def self.limit(*args)
74     ArvadosResourceList.new(self).limit(*args)
75   end
76   def self.eager(*args)
77     ArvadosResourceList.new(self).eager(*args)
78   end
79   def self.all(*args)
80     ArvadosResourceList.new(self).all(*args)
81   end
82   def save
83     obdata = {}
84     self.class.columns.each do |col|
85       obdata[col.name.to_sym] = self.send(col.name.to_sym)
86     end
87     obdata.delete :id
88     postdata = { self.class.to_s.underscore => obdata }
89     if etag
90       postdata['_method'] = 'PUT'
91       obdata.delete :uuid
92       resp = $arvados_api_client.api(self.class, '/' + uuid, postdata)
93     else
94       resp = $arvados_api_client.api(self.class, '', postdata)
95     end
96     return false if !resp[:etag] || !resp[:uuid]
97
98     # set read-only non-database attributes
99     @etag = resp[:etag]
100     @kind = resp[:kind]
101
102     # these attrs can be modified by "save" -- we should update our copies
103     %w(uuid owner created_at
104        modified_at modified_by_user modified_by_client
105       ).each do |attr|
106       self.send(attr + '=', resp[attr.to_sym])
107     end
108
109     self
110   end
111   def save!
112     self.save or raise Exception.new("Save failed")
113   end
114   def links(*args)
115     o = {}
116     o.merge!(args.pop) if args[-1].is_a? Hash
117     o[:link_class] ||= args.shift
118     o[:name] ||= args.shift
119     o[:head_kind] ||= args.shift
120     o[:tail_kind] = self.kind
121     o[:tail_uuid] = self.uuid
122     if all_links
123       return all_links.select do |m|
124         ok = true
125         o.each do |k,v|
126           if !v.nil?
127             test_v = m.send(k)
128             if (v.respond_to?(:uuid) ? v.uuid : v.to_s) != (test_v.respond_to?(:uuid) ? test_v.uuid : test_v.to_s)
129               ok = false
130             end
131           end
132         end
133         ok
134       end
135     end
136     @links = $arvados_api_client.api Link, '', { _method: 'GET', where: o, eager: true }
137     @links = $arvados_api_client.unpack_api_response(@links)
138   end
139   def all_links
140     return @all_links if @all_links
141     res = $arvados_api_client.api Link, '', {
142       _method: 'GET',
143       where: {
144         tail_kind: self.kind,
145         tail_uuid: self.uuid
146       },
147       eager: true
148     }
149     @all_links = $arvados_api_client.unpack_api_response(res)
150   end
151   def reload
152     private_reload(self.uuid)
153   end
154   def private_reload(uuid_or_hash)
155     raise "No such object" if !uuid_or_hash
156     if uuid_or_hash.is_a? Hash
157       hash = uuid_or_hash
158     else
159       hash = $arvados_api_client.api(self.class, '/' + uuid_or_hash)
160     end
161     hash.each do |k,v|
162       if self.respond_to?(k.to_s + '=')
163         self.send(k.to_s + '=', v)
164       else
165         # When ArvadosApiClient#schema starts telling us what to expect
166         # in API responses (not just the server side database
167         # columns), this sort of awfulness can be avoided:
168         self.instance_variable_set('@' + k.to_s, v)
169         if !self.respond_to? k
170           singleton = class << self; self end
171           singleton.send :define_method, k, lambda { instance_variable_get('@' + k.to_s) }
172         end
173       end
174     end
175     @all_links = nil
176     self
177   end
178   def dup
179     super.forget_uuid!
180   end
181
182   def attributes_for_display
183     self.attributes.reject { |k,v|
184       attribute_sortkey.has_key?(k) and !attribute_sortkey[k]
185     }.sort_by { |k,v|
186       attribute_sortkey[k] or k
187     }
188   end
189
190   def self.resource_class_for_uuid(uuid, opts={})
191     if uuid.is_a? ArvadosBase
192       return uuid.class
193     end
194     unless uuid.is_a? String
195       return nil
196     end
197     if opts[:class].is_a? Class
198       return opts[:class]
199     end
200     if uuid.match /^[0-9a-f]{32}(\+[^,]+)*(,[0-9a-f]{32}(\+[^,]+)*)*$/
201       return Collection
202     end
203     resource_class = nil
204     uuid.match /^[0-9a-z]{5}-([0-9a-z]{5})-[0-9a-z]{15}$/ do |re|
205       resource_class ||= $arvados_api_client.
206         kind_class(self.uuid_infix_object_kind[re[1]])
207     end
208     if opts[:referring_object] and
209         opts[:referring_attr] and
210         opts[:referring_attr].match /_uuid$/
211       resource_class ||= $arvados_api_client.
212         kind_class(opts[:referring_object].
213                    attributes[opts[:referring_attr].
214                               sub(/_uuid$/, '_kind')])
215     end
216     resource_class
217   end
218
219   protected
220
221   def forget_uuid!
222     self.uuid = nil
223     @etag = nil
224     self
225   end
226 end