3235: Fix SQL query.
[arvados.git] / services / api / app / controllers / arvados / v1 / groups_controller.rb
index 49f0b02c5fdd84d9a8605ba33b7d3f71fdee0f8d..bb74b909eb64baeaa1341cd724c0a1ad21b4c280 100644 (file)
@@ -9,46 +9,19 @@ class Arvados::V1::GroupsController < ApplicationController
             })
   end
 
-  def contents
-    all_objects = []
-    all_available = 0
-
-    # Trick apply_where_limit_order_params into applying suitable
-    # per-table values. *_all are the real ones we'll apply to the
-    # aggregate set.
-    limit_all = @limit
-    offset_all = @offset
-    @orders = []
+  def render_404_if_no_object
+    if params[:action] == 'contents' and !params[:uuid]
+      # OK!
+      @object = nil
+    else
+      super
+    end
+  end
 
-    ArvadosModel.descendants.reject(&:abstract_class?).sort_by(&:to_s).
-      each do |klass|
-      case klass.to_s
-        # We might expect klass==Link etc. here, but we would be
-        # disappointed: when Rails reloads model classes, we get two
-        # distinct classes called Link which do not equal each
-        # other. But we can still rely on klass.to_s to be "Link".
-      when 'ApiClientAuthorization', 'UserAgreement', 'Link'
-        # Do not want.
-      else
-        @objects = klass.readable_by(*@read_users)
-        cond_sql = "#{klass.table_name}.owner_uuid = ?"
-        cond_params = [@object.uuid]
-        if params[:include_linked]
-          cond_sql += " OR #{klass.table_name}.uuid IN (SELECT head_uuid FROM links WHERE link_class=#{klass.sanitize 'name'} AND links.tail_uuid=#{klass.sanitize @object.uuid})"
-        end
-        @objects = @objects.where(cond_sql, *cond_params).order("#{klass.table_name}.uuid")
-        @limit = limit_all - all_objects.count
-        apply_where_limit_order_params
-        items_available = @objects.
-          except(:limit).except(:offset).
-          count(:id, distinct: true)
-        all_available += items_available
-        @offset = [@offset - items_available, 0].max
+  def contents
+    # Set @objects:
+    load_searchable_objects(owner_uuid: @object.andand.uuid, include_linked: params[:include_linked])
 
-        all_objects += @objects.to_a
-      end
-    end
-    @objects = all_objects || []
     @links = Link.where('link_class=? and tail_uuid=?'\
                         ' and head_uuid in (?)',
                         'name',
@@ -59,12 +32,60 @@ class Arvados::V1::GroupsController < ApplicationController
       :etag => "",
       :self_link => "",
       :links => @links.as_api_response(nil),
-      :offset => offset_all,
-      :limit => limit_all,
-      :items_available => all_available,
+      :offset => @offset,
+      :limit => @limit,
+      :items_available => @items_available,
       :items => @objects.as_api_response(nil)
     }
     render json: @object_list
   end
 
+  protected
+
+  def load_searchable_objects opts
+    all_objects = []
+    @items_available = 0
+
+    # Trick apply_where_limit_order_params into applying suitable
+    # per-table values. *_all are the real ones we'll apply to the
+    # aggregate set.
+    limit_all = @limit
+    offset_all = @offset
+    @orders = []
+
+    [Group,
+     Job, PipelineInstance, PipelineTemplate,
+     Collection,
+     Human, Specimen, Trait].each do |klass|
+      @objects = klass.readable_by(*@read_users)
+      conds = []
+      cond_params = []
+      if opts[:owner_uuid]
+        conds << "#{klass.table_name}.owner_uuid = ?"
+        cond_params << opts[:owner_uuid]
+      end
+      if opts[:include_linked]
+        conds << "#{klass.table_name}.uuid IN (SELECT head_uuid FROM links WHERE link_class=#{klass.sanitize 'name'} AND links.tail_uuid=#{klass.sanitize @object.uuid})"
+      end
+      if conds.any?
+        cond_sql = '(' + conds.join(') OR (') + ')'
+        @objects = @objects.where(cond_sql, *cond_params)
+      end
+      @objects = @objects.order("#{klass.table_name}.uuid")
+      @limit = limit_all - all_objects.count
+      apply_where_limit_order_params
+      klass_items_available = @objects.
+        except(:limit).except(:offset).
+        count(:id, distinct: true)
+      @items_available += klass_items_available
+      @offset = [@offset - klass_items_available, 0].max
+
+      all_objects += @objects.to_a
+    end
+
+    @objects = all_objects
+    @limit = limit_all
+    @offset = offset_all
+  end
+
 end