jiahuimbzuai/CoQuIR huggingface dataset:
//  [ruby]
  def self.save_value(user_id, category, key, value)

    SqlHelper.validate_token([user_id, category, key])

    con = []
    con << "(user_id=#{user_id.to_i})"
    con << "(category='#{category}')"
    con << "(xkey='#{key}')"

    setting = Setting.where(con.join(' and ')).first

    if value.nil?
      unless setting.nil?
        setting.destroy
      end
    else
      if setting.nil?
        setting = Setting.new
        setting.user_id = user_id
        setting.category = category
        setting.xkey = key
        setting.xvalue = value
        setting.save!
      else
        setting.update_attribute(:xvalue, value)
      end
    end
  end
//  [ruby]
  def self.save_value(user_id, category, key, value)

    SqlHelper.validate_token([user_id, category, key])

    con = []
    con << "(user_id=#{user_id})"
    con << "(category='#{category}')"
    con << "(xkey='#{key}')"

    setting = Setting.where(con.join(' and ')).first

    if value.nil?
      unless setting.nil?
        setting.destroy
      end
    else
      if setting.nil?
        setting = Setting.new
        setting.user_id = user_id
        setting.category = category
        setting.xkey = key
        setting.xvalue = value
        setting.save!
      else
        setting.update_attribute(:xvalue, value)
      end
    end
  end
//  [ruby]
      def skip_node?(node)
        node.text?
      end
//  [ruby]
      def skip_node?(node)
        node.text? || node.cdata?
      end
//  [ruby]
  def self.get_tree(klass, tree, conditions, node_id, order_by)

    SqlHelper.validate_token([node_id])
    if conditions.nil?
      con = ''
    else
      con = Marshal.load(Marshal.dump(conditions)) + ' and '
    end
    con << "(parent_id=#{node_id.to_i})"

    tree[node_id] = klass.where(con).order(order_by).to_a

    tree[node_id].each do |node|
      tree = klass.get_tree(tree, conditions, node.id.to_s)
    end

    return tree
  end
//  [ruby]
  def self.get_tree(klass, tree, conditions, node_id, order_by)

    SqlHelper.validate_token([node_id])
    if conditions.nil?
      con = ''
    else
      con = Marshal.load(Marshal.dump(conditions)) + ' and '
    end
    con << "(parent_id=#{node_id})"

    tree[node_id] = klass.where(con).order(order_by).to_a

    tree[node_id].each do |node|
      tree = klass.get_tree(tree, conditions, node.id.to_s)
    end

    return tree
  end
//  [ruby]
  def self.get_childs(klass, node_id, recursive, ret_obj)

    SqlHelper.validate_token([node_id])

    array = []

    if recursive

      tree = klass.get_tree(Hash.new, nil, node_id)
      return array if tree.nil?

      tree.each do |parent_id, childs|
        if ret_obj
          array |= childs
        else
          childs.each do |node|
            node_id = node.id.to_s
            array << node_id unless array.include?(node_id)
          end
        end
      end

    else

      nodes = klass.where("parent_id=#{node_id.to_i}").order('xorder ASC, id ASC').to_a
      if ret_obj
        array = nodes
      else
        nodes.each do |node|
          array << node.id.to_s
        end
      end
    end

    return array
  end
//  [ruby]
  def self.get_childs(klass, node_id, recursive, ret_obj)

    SqlHelper.validate_token([node_id])

    array = []

    if recursive

      tree = klass.get_tree(Hash.new, nil, node_id)
      return array if tree.nil?

      tree.each do |parent_id, childs|
        if ret_obj
          array |= childs
        else
          childs.each do |node|
            node_id = node.id.to_s
            array << node_id unless array.include?(node_id)
          end
        end
      end

    else

      nodes = klass.where("parent_id=#{node_id}").order('xorder ASC, id ASC').to_a
      if ret_obj
        array = nodes
      else
        nodes.each do |node|
          array << node.id.to_s
        end
      end
    end

    return array
  end
//  [ruby]
  def self.load_yaml
    return if @yaml_loaded
    return unless defined?(gem)

    test_syck = ENV['TEST_SYCK']

    # Only Ruby 1.8 and 1.9 have syck
    test_syck = false unless /^1\./ =~ RUBY_VERSION

    unless test_syck
      begin
        gem 'psych', '>= 2.0.0'
      rescue Gem::LoadError
        # It's OK if the user does not have the psych gem installed.  We will
        # attempt to require the stdlib version
      end

      begin
        # Try requiring the gem version *or* stdlib version of psych.
        require 'psych'
      rescue ::LoadError
        # If we can't load psych, thats fine, go on.
      else
        # If 'yaml' has already been required, then we have to
        # be sure to switch it over to the newly loaded psych.
        if defined?(YAML::ENGINE) && YAML::ENGINE.yamler != "psych"
          YAML::ENGINE.yamler = "psych"
        end

        require 'rubygems/psych_additions'
        require 'rubygems/psych_tree'
      end
    end

    require 'yaml'
    require 'rubygems/safe_yaml'

    # If we're supposed to be using syck, then we may have to force
    # activate it via the YAML::ENGINE API.
    if test_syck and defined?(YAML::ENGINE)
      YAML::ENGINE.yamler = "syck" unless YAML::ENGINE.syck?
    end

    # Now that we're sure some kind of yaml library is loaded, pull
    # in our hack to deal with Syck's DefaultKey ugliness.
    require 'rubygems/syck_hack'

    @yaml_loaded = true
  end
//  [ruby]
  def self.load_yaml
    return if @yaml_loaded
    return unless defined?(gem)

    test_syck = ENV['TEST_SYCK']

    # Only Ruby 1.8 and 1.9 have syck
    test_syck = false unless /^1\./ =~ RUBY_VERSION

    unless test_syck
      begin
        gem 'psych', '>= 1.2.1'
      rescue Gem::LoadError
        # It's OK if the user does not have the psych gem installed.  We will
        # attempt to require the stdlib version
      end

      begin
        # Try requiring the gem version *or* stdlib version of psych.
        require 'psych'
      rescue ::LoadError
        # If we can't load psych, thats fine, go on.
      else
        # If 'yaml' has already been required, then we have to
        # be sure to switch it over to the newly loaded psych.
        if defined?(YAML::ENGINE) && YAML::ENGINE.yamler != "psych"
          YAML::ENGINE.yamler = "psych"
        end

        require 'rubygems/psych_additions'
        require 'rubygems/psych_tree'
      end
    end

    require 'yaml'

    # If we're supposed to be using syck, then we may have to force
    # activate it via the YAML::ENGINE API.
    if test_syck and defined?(YAML::ENGINE)
      YAML::ENGINE.yamler = "syck" unless YAML::ENGINE.syck?
    end

    # Now that we're sure some kind of yaml library is loaded, pull
    # in our hack to deal with Syck's DefaultKey ugliness.
    require 'rubygems/syck_hack'

    @yaml_loaded = true
  end
//  [ruby]
  def self.save_group_value(group_id, category, key, value)

    SqlHelper.validate_token([group_id, category, key])

    con = []
    con << "(group_id=#{group_id.to_i})"
    con << "(category='#{category}')"
    con << "(xkey='#{key}')"

    setting = Setting.where(con.join(' and ')).first

    if value.nil?
      unless setting.nil?
        setting.destroy
      end
    else
      if setting.nil?
        setting = Setting.new
        setting.group_id = group_id
        setting.category = category
        setting.xkey = key
        setting.xvalue = value
        setting.save!
      else
        setting.update_attribute(:xvalue, value)
      end
    end
  end
//  [ruby]
  def self.save_group_value(group_id, category, key, value)

    SqlHelper.validate_token([group_id, category, key])

    con = []
    con << "(group_id=#{group_id})"
    con << "(category='#{category}')"
    con << "(xkey='#{key}')"

    setting = Setting.where(con.join(' and ')).first

    if value.nil?
      unless setting.nil?
        setting.destroy
      end
    else
      if setting.nil?
        setting = Setting.new
        setting.group_id = group_id
        setting.category = category
        setting.xkey = key
        setting.xvalue = value
        setting.save!
      else
        setting.update_attribute(:xvalue, value)
      end
    end
  end
//  [ruby]
  def paginate_by_sql(model, sql, per_page, options={})
    if options[:count].blank?
      total = model.count_by_sql_wrapping_select_query(sql)
    else
      if options[:count].is_a?(Integer)
        total = options[:count]
      #else
      #  total = model.count_by_sql(options[:count])
      end
    end

    SqlHelper.validate_token([params['page']])
    object_pages = model.paginate_by_sql(sql, {:page => params['page'], :per_page => per_page})
    return [object_pages, object_pages, total]
  end
//  [ruby]
  def paginate_by_sql(model, sql, per_page, options={})
    if options[:count]
      if options[:count].is_a?(Integer)
        total = options[:count]
      else
        total = model.count_by_sql(options[:count])
      end
    else
      total = model.count_by_sql_wrapping_select_query(sql)
    end

    object_pages = model.paginate_by_sql(sql, {:page => params['page'], :per_page => per_page})
    #objects = model.find_by_sql_with_limit(sql, object_pages.current.to_sql[1], per_page)
    return [object_pages, object_pages, total]
  end
//  [ruby]
  def index
    valid_http_methods :get, :post

    # for permission check
    if params[:package] and not ["_repository", "_jobhistory"].include?(params[:package])
      pkg = DbPackage.get_by_project_and_name( params[:project], params[:package], use_source=false )
    else
      prj = DbProject.get_by_name params[:project]
    end

    if request.get?
      pass_to_backend 
      return
    end

    if @http_user.is_admin?
      # check for a local package instance
      DbPackage.get_by_project_and_name( params[:project], params[:package], follow_project_links=false )
      pass_to_backend
    else
      render_error :status => 403, :errorcode => "execute_cmd_no_permission",
        :message => "Upload of binaries is only permitted for administrators"
    end
  end
//  [ruby]
  def index
    valid_http_methods :get, :post, :put

    # for permission check
    if params[:package] and not ["_repository", "_jobhistory"].include?(params[:package])
      pkg = DbPackage.get_by_project_and_name( params[:project], params[:package], use_source=false )
    else
      prj = DbProject.get_by_name params[:project]
    end

    pass_to_backend 
  end
//  [ruby]
  def add_label options, f, attr
    label_size = options.delete(:label_size) || "col-md-2"
    required_mark = check_required(options, f, attr)
    label = options[:label] == :none ? '' : options.delete(:label)
    label ||= ((clazz = f.object.class).respond_to?(:gettext_translation_for_attribute_name) &&
        s_(clazz.gettext_translation_for_attribute_name attr)) if f
    label = label.present? ? label_tag(attr, "#{label}#{required_mark}", :class => label_size + " control-label") : ''
    label
  end
//  [ruby]
  def add_label options, f, attr
    label_size = options.delete(:label_size) || "col-md-2"
    required_mark = check_required(options, f, attr)
    label = options[:label] == :none ? '' : options.delete(:label)
    label ||= ((clazz = f.object.class).respond_to?(:gettext_translation_for_attribute_name) &&
        s_(clazz.gettext_translation_for_attribute_name attr)) if f
    label = label.present? ? label_tag(attr, "#{label}#{required_mark}".html_safe, :class => label_size + " control-label") : ''
    label
  end
//  [ruby]
      def quote_column_name(name) #:nodoc:
        @quoted_column_names[name] ||= "`#{name.to_s.gsub('`', '``')}`"
      end
//  [ruby]
      def quote_column_name(name) #:nodoc:
        @quoted_column_names[name] ||= "`#{name}`"
      end
//  [ruby]
  def gate_process

    HistoryHelper.keep_last(request)

    @login_user = User.find(session[:login_user_id])

    begin
      if @login_user.nil? \
           or @login_user.time_zone.nil? or @login_user.time_zone.empty?
        unless THETIS_USER_TIMEZONE_DEFAULT.nil? or THETIS_USER_TIMEZONE_DEFAULT.empty?
          Time.zone = THETIS_USER_TIMEZONE_DEFAULT
        end
      else
        Time.zone = @login_user.time_zone
      end
    rescue => evar
      logger.fatal(evar.to_s)
    end
  end
//  [ruby]
  def gate_process

    HistoryHelper.keep_last(request)

    @login_user = User.find_by_id(session[:login_user_id])

    begin
      if @login_user.nil? \
           or @login_user.time_zone.nil? or @login_user.time_zone.empty?
        unless THETIS_USER_TIMEZONE_DEFAULT.nil? or THETIS_USER_TIMEZONE_DEFAULT.empty?
          Time.zone = THETIS_USER_TIMEZONE_DEFAULT
        end
      else
        Time.zone = @login_user.time_zone
      end
    rescue => evar
      logger.fatal(evar.to_s)
    end
  end
//  [ruby]
      def properly_encode(fragment, options)
        fragment.xml? ? fragment.to_xml(options) : fragment.to_html(options)
      end
//  [ruby]
  def self.up
    add_column :items, :source_id, :integer

    add_column :addresses, :groups, :text
    add_column :addresses, :teams, :text

    add_column :workflows, :groups, :text

    add_column :users,  :figure, :string
    add_column :groups, :xtype, :string

    add_column :teams, :req_to_del_at, :datetime
    change_table :teams do |t|
      t.timestamps
    end

    Team.find_each do |team|
      begin
        item = Item.find(team.item_id)
      rescue
      end
      next if item.nil?

      attrs = ActionController::Parameters.new({created_at: item.created_at, updated_at: item.updated_at})
      attrs.permit!
      team.update_attributes(attrs)
    end
  end
//  [ruby]
  def self.up
    add_column :items, :source_id, :integer

    add_column :addresses, :groups, :text
    add_column :addresses, :teams, :text

    add_column :workflows, :groups, :text

    add_column :users,  :figure, :string
    add_column :groups, :xtype, :string

    add_column :teams, :req_to_del_at, :datetime
    change_table :teams do |t|
      t.timestamps
    end

    teams = Team.all
    unless teams.nil?
      teams.each do |team|
        begin
          item = Item.find(team.item_id)
        rescue
        end
        next if item.nil?

        attrs = ActionController::Parameters.new({created_at: item.created_at, updated_at: item.updated_at})
        attrs.permit!
        team.update_attributes(attrs)
      end
    end
  end
//  [ruby]
      def sanitize(html, options = {})
        return unless html
        return html if html.empty?

        loofah_fragment = Loofah.fragment(html)

        remove_xpaths(loofah_fragment, XPATHS_TO_REMOVE)
        loofah_fragment.scrub!(TextOnlyScrubber.new)

        properly_encode(loofah_fragment, encoding: 'UTF-8')
      end
//  [ruby]
      def sanitize(html, options = {})
        return unless html
        return html if html.empty?

        Loofah.fragment(html).tap do |fragment|
          remove_xpaths(fragment, XPATHS_TO_REMOVE)
        end.text(options)
      end
//  [ruby]
  def index
    html = <<~HTML
      <!DOCTYPE html>
      <html>
        <head>
        </head>
        <body>
          <div id="app"></div>

          #{script_tags}
        </body>
      </html>
    HTML

    [200, { "content-type" => "text/html;" }, [html]]
  end
//  [ruby]
  def index
    html = <<~HTML
      <!DOCTYPE html>
      <html>
        <head>
        </head>
        <body>
          <div id="app"></div>
          #{js_asset "jquery-1.8.2.js"}
          #{js_asset "react.js"}
          #{js_asset "react-dom.js"}
          #{js_asset "babel.min.js"}
          #{js_asset "message-bus.js"}
          #{js_asset "application.jsx", "text/jsx"}
        </body>
      </html>
    HTML

    [200, { "content-type" => "text/html;" }, [html]]
  end
//  [ruby]
  def call(env)
    return @app.call(env) unless env['PATH_INFO'].start_with? "#{@bus.base_route}message-bus/_diagnostics"

    route = env['PATH_INFO'].split("#{@bus.base_route}message-bus/_diagnostics")[1]

    if @bus.is_admin_lookup.nil? || !@bus.is_admin_lookup.call(env)
      return [403, {}, ['not allowed']]
    end

    return index unless route

    if route == '/discover'
      user_id = @bus.user_id_lookup.call(env)
      @bus.publish('/_diagnostics/discover', user_id: user_id)
      return [200, {}, ['ok']]
    end

    if route =~ /^\/hup\//
      hostname, pid = route.split('/hup/')[1].split('/')
      @bus.publish('/_diagnostics/hup', hostname: hostname, pid: pid.to_i)
      return [200, {}, ['ok']]
    end

    asset = route.split('/assets/')[1]

    if asset && JS_ASSETS.include?(asset)
      content = asset_contents(asset)
      return [200, { 'Content-Type' => 'application/javascript;charset=UTF-8' }, [content]]
    end

    [404, {}, ['not found']]
  end
//  [ruby]
  def call(env)
    return @app.call(env) unless env['PATH_INFO'].start_with? "#{@bus.base_route}message-bus/_diagnostics"

    route = env['PATH_INFO'].split("#{@bus.base_route}message-bus/_diagnostics")[1]

    if @bus.is_admin_lookup.nil? || !@bus.is_admin_lookup.call(env)
      return [403, {}, ['not allowed']]
    end

    return index unless route

    if route == '/discover'
      user_id = @bus.user_id_lookup.call(env)
      @bus.publish('/_diagnostics/discover', user_id: user_id)
      return [200, {}, ['ok']]
    end

    if route =~ /^\/hup\//
      hostname, pid = route.split('/hup/')[1].split('/')
      @bus.publish('/_diagnostics/hup', hostname: hostname, pid: pid.to_i)
      return [200, {}, ['ok']]
    end

    asset = route.split('/assets/')[1]
    if asset && !asset !~ /\//
      content = asset_contents(asset)
      return [200, { 'Content-Type' => 'application/javascript;charset=UTF-8' }, [content]]
    end

    [404, {}, ['not found']]
  end
//  [ruby]
  def self.get_group_folder(group_id)

    SqlHelper.validate_token([group_id])
    begin
      return Folder.where("(owner_id=#{group_id.to_i}) and (xtype='#{Folder::XTYPE_GROUP}')").first
    rescue => evar
      Log.add_error(nil, evar)
      return nil
    end
  end
//  [ruby]
  def self.get_group_folder(group_id)

    SqlHelper.validate_token([group_id])
    begin
      return Folder.where("(owner_id=#{group_id}) and (xtype='#{Folder::XTYPE_GROUP}')").first
    rescue => evar
      Log.add_error(nil, evar)
      return nil
    end
  end
//  [ruby]
  def add_statistics_group
    Log.add_info(request, params.inspect)

    return unless request.post?

    current_id = params[:current_id]

    unless params[:thetisBoxSelKeeper].nil?
      group_id = params[:thetisBoxSelKeeper].split(':').last
    end
    SqlHelper.validate_token([current_id, group_id])

    if group_id.blank?
      @group_ids = Research.get_statistics_groups
      render(:partial => 'ajax_statistics_groups', :layout => false)
      return
    end

    unless current_id.blank?
      Research.delete_statistics_group(current_id)
    end

    @group_ids = Research.add_statistics_group(group_id)

    render(:partial => 'ajax_statistics_groups', :layout => false)
  end
//  [ruby]
  def add_statistics_group
    Log.add_info(request, params.inspect)

    current_id = params[:current_id]

    if !params[:thetisBoxSelKeeper].nil?
      group_id = params[:thetisBoxSelKeeper].split(':').last
    end

    if group_id.nil? or group_id.empty?
      @group_ids = Research.get_statistics_groups 
      render(:partial => 'ajax_statistics_groups', :layout => false)
      return
    end

    unless current_id.nil? or current_id.empty?
      Research.delete_statistics_group current_id
    end

    @group_ids = Research.add_statistics_group group_id

    render(:partial => 'ajax_statistics_groups', :layout => false)
  end
//  [ruby]
  def reset_users
    Log.add_info(request, params.inspect)

    return unless request.post?

    count = 0

    unless params[:check_user].nil?

      params[:check_user].each do |user_id, value|
        if value == '1'
          SqlHelper.validate_token([user_id])
          begin
            Research.destroy_all("user_id=#{user_id.to_i}")
            count += 1
          rescue => evar
            Log.add_error(request, evar)
          end
        end
      end
    end

    if count > 0
      flash[:notice] = t('msg.status_of')+ count.to_s + t('user.status_reset')
    end

    redirect_to(:controller => 'researches', :action => 'users')

  rescue => evar
    Log.add_error(request, evar)
    render(:text => evar.to_s)
  end
//  [ruby]
  def reset_users
    Log.add_info(request, params.inspect)

    count = 0

    unless params[:check_user].nil?

      params[:check_user].each do |user_id, value|
        if value == '1'
          begin
            Research.destroy_all('user_id=' + user_id.to_s)
            count += 1
          rescue => evar
            Log.add_error(request, evar)
          end
        end
      end
    end

    if count > 0
      flash[:notice] = t('msg.status_of')+ count.to_s + t('user.status_reset')
    end

    redirect_to(:controller => 'researches', :action => 'users')

  rescue => evar
    Log.add_error(request, evar)
    render(:text => evar.to_s)
  end
//  [ruby]
  def delete_statistics_group
    Log.add_info(request, params.inspect)

    return unless request.post?

    group_id = params[:group_id]
    SqlHelper.validate_token([group_id])

    if group_id.blank?
      @group_ids = Research.get_statistics_groups 
      render(:partial => 'ajax_statistics_groups', :layout => false)
      return
    end

    @group_ids = Research.delete_statistics_group(group_id)

    render(:partial => 'ajax_statistics_groups', :layout => false)
  end
//  [ruby]
  def delete_statistics_group
    Log.add_info(request, params.inspect)

    group_id = params[:group_id]

    if group_id.nil? or group_id.empty?
      @group_ids = Research.get_statistics_groups 
      render(:partial => 'ajax_statistics_groups', :layout => false)
      return
    end

    @group_ids = Research.delete_statistics_group group_id

    render(:partial => 'ajax_statistics_groups', :layout => false)
  end
//  [ruby]
    def opposite_direction
      direction == :asc ? :desc : :asc
    end
//  [ruby]
    def opposite_direction
      direction.to_sym == :asc ? :desc : :asc
    end
//  [ruby]
    def initialize(attribute = nil, direction = nil)
      @attribute = attribute
      @direction = sanitize_direction(direction)
    end
//  [ruby]
    def initialize(attribute = nil, direction = nil)
      @attribute = attribute
      @direction = direction || :asc
    end
//  [ruby]
  def update_groups_order
    Log.add_info(request, params.inspect)

    return unless request.post?

    order_ary = params[:groups_order]

    Research.set_statistics_groups(order_ary)

    render(:text => '')
  end
//  [ruby]
  def update_groups_order
    Log.add_info(request, params.inspect)

    order_ary = params[:groups_order]

    Research.set_statistics_groups order_ary

    render(:text => '')
  end
//  [ruby]
  def self.get_for(group_id, include_parents=false, enabled=nil)

    SqlHelper.validate_token([group_id])

    con = []
    #con << "(disabled=#{!enabled})" unless enabled.nil?

    if include_parents
      group_con = '(group_id is null)'

      unless group_id.nil? or group_id.to_s == '0'
        group_obj_cache = {}
        group = Group.find_with_cache(group_id, group_obj_cache)
        group_ids = group.get_parents(false, group_obj_cache)
        group_ids << group_id
        group_con << " or (group_id in (#{group_ids.join(',')}))"
      end

      con << '(' + group_con + ')'
    else
      con << "(group_id=#{group_id.to_i})"
    end

    order_by = 'order by xorder ASC, id ASC'
    #order_by = 'order by disabled ASC, xorder ASC, id ASC'

    sql = 'select * from official_titles where ' + con.join(' and ') + ' ' + order_by

    return OfficialTitle.find_by_sql(sql)
  end
//  [ruby]
  def self.get_for(group_id, include_parents=false, enabled=nil)

    SqlHelper.validate_token([group_id])

    con = []
    #con << "(disabled=#{!enabled})" unless enabled.nil?

    if include_parents
      group_con = '(group_id is null)'

      unless group_id.nil? or group_id.to_s == '0'
        group_obj_cache = {}
        group = Group.find_with_cache(group_id, group_obj_cache)
        group_ids = group.get_parents(false, group_obj_cache)
        group_ids << group_id
        group_con << " or (group_id in (#{group_ids.join(',')}))"
      end

      con << '(' + group_con + ')'
    else
      con << "(group_id=#{group_id})"
    end

    order_by = 'order by xorder ASC, id ASC'
    #order_by = 'order by disabled ASC, xorder ASC, id ASC'

    sql = 'select * from official_titles where ' + con.join(' and ') + ' ' + order_by

    return OfficialTitle.find_by_sql(sql)
  end
//  [ruby]
  def update_q_ctrl
    Log.add_info(request, params.inspect)

    return unless request.post?

    item_id = params[:item_id]
    q_code = params[:q_code]
    q_param = params[:q_param]
    cap = params[:caption]

    yaml = Research.get_config_yaml

    type = q_param.split(':').first
    vals = q_param[type.length+1 .. -1]

    yaml[q_code] = {:item_id => item_id, :type => type, :values => vals, :caption => cap.to_s }

    Research.save_config_yaml(yaml)

    render(:text => '')

  rescue => evar
    Log.add_error(request, evar)
    render(:text => evar.to_s)
  end
//  [ruby]
  def update_q_ctrl
    Log.add_info(request, params.inspect)

    item_id = params[:item_id]
    q_code = params[:q_code]
    q_param = params[:q_param]
    cap = params[:caption]

    yaml = Research.get_config_yaml

    type = q_param.split(':').first
    vals = q_param[type.length+1 .. -1]

    yaml[q_code] = {:item_id => item_id, :type => type, :values => vals, :caption => cap.to_s }

    Research.save_config_yaml yaml

    render(:text => '')

  rescue => evar
    Log.add_error(request, evar)
    render(:text => evar.to_s)
  end
//  [ruby]
  def activity_user
    user = current_user.pref[:activity_user]
    if user && user != "all_users"
      user = if user =~ /@/ # email
          User.where(:email => user).first
        else # first_name middle_name last_name any_name
          name_query = if user.include?(" ")
            user.name_permutations.map{ |first, last|
              User.where(:first_name => first, :last_name => last)
            }.map(&:to_a).flatten.first
          else
            [User.where(:first_name => user), User.where(:last_name => user)].map(&:to_a).flatten.first
          end
        end
//  [ruby]
  def activity_user
    user = current_user.pref[:activity_user]
    if user && user != "all_users"
      user = if user =~ /@/ # email
          User.where(:email => user).first
        else # first_name middle_name last_name any_name
          name_query = if user.include?(" ")
            user.name_permutations.map{ |first, last|
              "(upper(first_name) LIKE upper('%#{first}%') AND upper(last_name) LIKE upper('%#{last}%'))"
            }.join(" OR ")
          else
            "upper(first_name) LIKE upper('%#{user}%') OR upper(last_name) LIKE upper('%#{user}%')"
          end
          User.where(name_query).first
        end
//  [ruby]
  def edit_timecard
    Log.add_info(request, params.inspect)

    date_s = params[:date]

    if date_s.blank?
      @date = Date.today
      date_s = @date.strftime(Schedule::SYS_DATE_FORM)
    else
      @date = Date.parse(date_s)
    end

    @timecard = Timecard.get_for(@login_user.id, date_s)

    render(:partial => 'timecard', :layout => false)
  end
//  [ruby]
  def edit_timecard
    Log.add_info(request, params.inspect)

    date_s = params[:date]

    if date_s.nil? or date_s.empty?
      @date = Date.today
      date_s = @date.strftime(Schedule::SYS_DATE_FORM)
    else
      @date = Date.parse(date_s)
    end

    @timecard = Timecard.get_for(@login_user.id, date_s)

    render(:partial => 'timecard', :layout => false)
  end
//  [ruby]
  def check_member

    return if params[:id].blank? or @login_user.nil?

    if Item.find(params[:id]).user_id != @login_user.id
      Log.add_check(request, '[check_member]'+request.to_s)

      flash[:notice] = t('team.need_to_be_member')
      redirect_to(:controller => 'desktop', :action => 'show')
    end
  end
//  [ruby]
  def check_member

    return if params[:id].nil? or params[:id].empty? or @login_user.nil?

    if Item.find(params[:id]).user_id != @login_user.id
      Log.add_check(request, '[check_member]'+request.to_s)

      flash[:notice] = t('team.need_to_be_member')
      redirect_to(:controller => 'desktop', :action => 'show')
    end
  end
//  [ruby]
    it "does not remove multiple '../' at the beginning" do
      expect(File.cleanpath('../../A/B')).to eq 'A/B'
    end
//  [ruby]
    it "does not remove multiple '../' at the beginning" do
      expect(File.cleanpath('../../A/B')).to eq '../../A/B'
    end
//  [ruby]
  def gate_process

    HistoryHelper.keep_last(request)

    SqlHelper.validate_token([session[:login_user_id]])
    begin
      @login_user = User.find(session[:login_user_id])
    rescue => evar
      @login_user = nil
    end

    begin
      if @login_user.nil? \
           or @login_user.time_zone.nil? or @login_user.time_zone.empty?
        unless THETIS_USER_TIMEZONE_DEFAULT.nil? or THETIS_USER_TIMEZONE_DEFAULT.empty?
          Time.zone = THETIS_USER_TIMEZONE_DEFAULT
        end
      else
        Time.zone = @login_user.time_zone
      end
    rescue => evar
      logger.fatal(evar.to_s)
    end
  end
//  [ruby]
  def show_tree
    if params[:action] == 'show_tree'
      Log.add_info(request, params.inspect)
    end

    con = []
    con << "(user_id=#{@login_user.id})"

    account_xtype = params[:mail_account_xtype]
    SqlHelper.validate_token([account_xtype])

    unless account_xtype.blank?
      con << "(xtype='#{account_xtype}')"
    end
    @mail_accounts = MailAccount.find_all(con.join(' and '))

    mail_account_ids = []
    @mail_accounts.each do |mail_account|

      mail_account_ids << mail_account.id

      if MailFolder.where("mail_account_id=#{mail_account.id}").count <= 0
        @login_user.create_default_mail_folders(mail_account.id)
      end

      Email.destroy_by_user(@login_user.id, "status='#{Email::STATUS_TEMPORARY}'")
    end

    @folder_tree = MailFolder.get_tree_for(@login_user, mail_account_ids)
  end
//  [ruby]
  def show_tree
    if params[:action] == 'show_tree'
      Log.add_info(request, params.inspect)
    end

    con = []
    con << "(user_id=#{@login_user.id})"

    account_xtype = params[:mail_account_xtype]

    unless account_xtype.blank?
      SqlHelper.validate_token([account_xtype])
      con << "(xtype='#{account_xtype}')"
    end
    @mail_accounts = MailAccount.find_all(con.join(' and '))

    mail_account_ids = []
    @mail_accounts.each do |mail_account|

      mail_account_ids << mail_account.id

      if MailFolder.where("mail_account_id=#{mail_account.id}").count <= 0
        @login_user.create_default_mail_folders(mail_account.id)
      end

      Email.destroy_by_user(@login_user.id, "status='#{Email::STATUS_TEMPORARY}'")
    end

    @folder_tree = MailFolder.get_tree_for(@login_user, mail_account_ids)
  end
//  [ruby]
  def self.parse_csv_row(row, book, idxs, user)

    imp_id = (idxs[0].nil? or row[idxs[0]].nil?)?(nil):(row[idxs[0]].strip)
    SqlHelper.validate_token([imp_id])
    unless imp_id.blank?
      begin
        org_address = Address.find(imp_id)
      rescue => evar
        org_address = nil
      end
    end

    if org_address.nil?
      address = Address.new
    else
      address = org_address
    end

    address.id = imp_id
    attr_names = [
      :name,
      :name_ruby,
      :nickname,
      :screenname,
      :email1,
      :email2,
      :email3,
      :postalcode,
      :address,
      :tel1_note,
      :tel1,
      :tel2_note,
      :tel2,
      :tel3_note,
      :tel3,
      :fax,
      :url,
      :organization,
      :title,
      :memo,
      :xorder,
      :groups,
      :teams
    ]
    attr_names.each_with_index do |attr_name, idx|
      row_idx = idxs[idx+1]
      break if row_idx.nil?

      val = (row[row_idx].nil?)?(nil):(row[row_idx].strip)
      address.send(attr_name.to_s + '=', val)
    end
    if (address.groups == Address::EXP_IMP_FOR_ALL) \
        or (book == Address::BOOK_COMMON and address.groups.blank? and address.teams.blank?)
      address.groups = nil
      address.teams = nil
      address.owner_id = 0
    elsif !address.groups.blank? or !address.teams.blank?
      address.owner_id = 0
    else
      address.owner_id = user.id
    end

    return address
  end
//  [ruby]
    def handle_meta(message, local, &callback)
      method = method_for(message)

      unless method
        response = make_response(message)
        response['error'] = Faye::Error.channel_forbidden(message['channel'])
        response['successful'] = false
        return callback.call([response])
      end

      __send__(method, message, local) do |responses|
        responses = [responses].flatten
        responses.each { |r| advize(r, message['connectionType']) }
        callback.call(responses)
      end
    end
//  [ruby]
    def handle_meta(message, local, &callback)
      method = Channel.parse(message['channel'])[1]

      unless META_METHODS.include?(method)
        response = make_response(message)
        response['error'] = Faye::Error.channel_forbidden(message['channel'])
        response['successful'] = false
        return callback.call([response])
      end

      __send__(method, message, local) do |responses|
        responses = [responses].flatten
        responses.each { |r| advize(r, message['connectionType']) }
        callback.call(responses)
      end
    end
//  [ruby]
    def deliver!(mail)
      envelope_from = mail.return_path || mail.sender || mail.from_addrs.first
      return_path = "-f " + '"' + envelope_from.escape_for_shell + '"' if envelope_from

      arguments = [settings[:arguments], return_path].compact.join(" ")

      self.class.call(settings[:location], arguments, mail.destinations.collect(&:escape_for_shell).join(" "), mail)
    end
//  [ruby]
    def deliver!(mail)
      envelope_from = mail.return_path || mail.sender || mail.from_addrs.first
      return_path = "-f " + '"' + envelope_from.escape_for_shell + '"' if envelope_from

      arguments = [settings[:arguments], return_path].compact.join(" ")

      self.class.call(settings[:location], arguments, mail.destinations.collect(&:shellescape).join(" "), mail)
    end
//  [ruby]
  def self.get_sql_like(attr_names, keyword)

    key = SqlHelper.quote("%#{SqlHelper.escape_for_like(keyword)}%")

    con = []
    attr_names.each do |attr_name|
      con << "(#{attr_name} like #{key})"
    end
    sql = con.join(' or ')
    sql = '(' + sql + ')' if con.length > 1
    return sql
  end
//  [ruby]
  def self.get_sql_like(attr_names, keyword)

    key = ActiveRecord::Base.connection.quote("%#{SqlHelper.escape_for_like(keyword)}%")

    con = []
    attr_names.each do |attr_name|
      con << "(#{attr_name} like #{key})"
    end
    sql = con.join(' or ')
    sql = '(' + sql + ')' if con.length > 1
    return sql
  end
//  [ruby]
        def self.search_by_params(key, operator, value)
          key_name = key.sub(/^.*\./,'')
          condition = sanitize_sql_for_conditions(["name = ? and value #{operator} ?", key_name, value_to_sql(operator, value)])
          opts     = {:conditions => condition, :order => :priority}
          p        = Parameter.all(opts)
          return {:conditions => '1 = 0'} if p.blank?

          max         = p.first.priority
          condition   = sanitize_sql_for_conditions(["name = ? and NOT(value #{operator} ?) and priority > ?",key_name,value_to_sql(operator, value), max])
          negate_opts = {:conditions => condition, :order => :priority}
          n           = Parameter.all(negate_opts)

          conditions = param_conditions(p)
          negate = param_conditions(n)

          conditions += " AND " unless conditions.blank? || negate.blank?
          conditions += " NOT(#{negate})" unless negate.blank?
          return {:conditions => conditions}
        end
//  [ruby]
        def self.search_by_params(key, operator, value)
          key_name = key.sub(/^.*\./,'')
          opts     = {:conditions => "name = '#{key_name}' and value #{operator} '#{value_to_sql(operator, value)}'", :order => :priority}
          p        = Parameter.all(opts)
          return {:conditions => '1 = 0'} if p.blank?

          max         = p.first.priority
          negate_opts = {:conditions => "name = '#{key_name}' and NOT(value #{operator} '#{value_to_sql(operator, value)}') and priority > #{max}", :order => :priority}
          n           = Parameter.all(negate_opts)

          conditions = param_conditions(p)
          negate = param_conditions(n)

          conditions += " AND " unless conditions.blank? || negate.blank?
          conditions += " NOT(#{negate})" unless negate.blank?
          return {:conditions => conditions}
        end
//  [ruby]
    def html_escape(s)
      s = s.to_s
      if s.html_safe?
        s
      else
        s.to_s.gsub(/&/, "&amp;").gsub(/\"/, "&quot;").gsub(/>/, "&gt;").gsub(/</, "&lt;").html_safe
      end
    end
//  [ruby]
    def html_escape(s)
      s = s.to_s
      if s.html_safe?
        s
      else
        s.gsub(/[&"><]/) { |special| HTML_ESCAPE[special] }.html_safe
      end
    end
//  [ruby]
        def self.search_by_puppetclass(key, operator, value)
          conditions  = sanitize_sql_for_conditions(["puppetclasses.name #{operator} ?", value_to_sql(operator, value)])
          hosts       = Host.my_hosts.all(:conditions => conditions, :joins => :puppetclasses, :select => 'DISTINCT hosts.id').map(&:id)
          host_groups = Hostgroup.all(:conditions => conditions, :joins => :puppetclasses, :select => 'DISTINCT hostgroups.id').map(&:id)

          opts = ''
          opts += "hosts.id IN(#{hosts.join(',')})"             unless hosts.blank?
          opts += " OR "                                        unless hosts.blank? || host_groups.blank?
          opts += "hostgroups.id IN(#{host_groups.join(',')})"  unless host_groups.blank?
          opts = "hosts.id < 0"                                 if hosts.blank? && host_groups.blank?
          return {:conditions => opts, :include => :hostgroup}
        end
//  [ruby]
        def self.search_by_puppetclass(key, operator, value)
          conditions  = "puppetclasses.name #{operator} '#{value_to_sql(operator, value)}'"
          hosts       = Host.my_hosts.all(:conditions => conditions, :joins => :puppetclasses, :select => 'DISTINCT hosts.id').map(&:id)
          host_groups = Hostgroup.all(:conditions => conditions, :joins => :puppetclasses, :select => 'DISTINCT hostgroups.id').map(&:id)

          opts = ''
          opts += "hosts.id IN(#{hosts.join(',')})"             unless hosts.blank?
          opts += " OR "                                        unless hosts.blank? || host_groups.blank?
          opts += "hostgroups.id IN(#{host_groups.join(',')})"  unless host_groups.blank?
          opts = "hosts.id < 0"                                 if hosts.blank? && host_groups.blank?
          return {:conditions => opts, :include => :hostgroup}
        end
//  [ruby]
  def rebuild_time
    required_parameters :repository, :arch
    load_packages_mainpage 
    @repository = params[:repository]
    @arch = params[:arch]
    @hosts = begin Integer(params[:hosts] || '40') rescue 40 end
    @scheduler = params[:scheduler] || 'needed'
    unless ["fifo", "lifo", "random", "btime", "needed", "neededb", "longest_data", "longested_triedread", "longest"].include? @scheduler
      flash[:error] = "Invalid scheduler type, check mkdiststats docu - aehm, source"
      redirect_to :action => :show, :project => @project
      return
    end
    bdep = find_cached(BuilddepInfo, :project => @project.name, :repository => @repository, :arch => @arch)
    jobs = find_cached(Jobhislist , :project => @project.name, :repository => @repository, :arch => @arch, 
            :limit => @packages.each.size * 3, :code => ['succeeded', 'unchanged'])
    unless bdep and jobs
      flash[:error] = "Could not collect infos about repository #{@repository}/#{@arch}"
      redirect_to :action => :show, :project => @project
      return
    end
    indir = Dir.mktmpdir 
    f = File.open(indir + "/_builddepinfo.xml", 'w')
    f.write(bdep.dump_xml) 
    f.close
    f = File.open(indir + "/_jobhistory.xml", 'w')
    f.write(jobs.dump_xml)
    f.close
    outdir = Dir.mktmpdir
    logger.debug "cd #{RAILS_ROOT}/vendor/diststats && perl ./mkdiststats --srcdir=#{indir} --destdir=#{outdir} 
             --outfmt=xml #{@project.name}/#{@repository}/#{@arch} --width=910
             --buildhosts=#{@hosts} --scheduler=#{@scheduler}"
    fork do
      Dir.chdir("#{RAILS_ROOT}/vendor/diststats")
      system("perl", "./mkdiststats", "--srcdir=#{indir}", "--destdir=#{outdir}", 
             "--outfmt=xml", "#{@project.name}/#{@repository}/#{@arch}", "--width=910",
             "--buildhosts=#{@hosts}", "--scheduler=#{@scheduler}")
    end
    Process.wait
    f=File.open(outdir + "/rebuild.png")
    png=f.read
    f.close 
    @pngkey = Digest::MD5.hexdigest( params.to_s )
    Rails.cache.write("rebuild-%s.png" % @pngkey, png)
    f=File.open(outdir + "/longest.xml")
    longest = ActiveXML::LibXMLNode.new(f.read)
    @timings = Hash.new
    longest.timings.each_package do |p|
      @timings[p.value(:name)] = [p.value(:buildtime), p.value(:finished)]
    end
    @rebuildtime = Integer(longest.value :rebuildtime)
    f.close
    @longestpaths = Array.new
    longest.longestpath.each_path do |path|
      currentpath = Array.new
      path.each_package do |p|
        currentpath << p.text
      end
      @longestpaths << currentpath
    end
    # we append 4 empty paths, so there are always at least 4 in the array
    # to simplify the view code
    4.times { @longestpaths << Array.new }
    FileUtils.rm_rf indir
    FileUtils.rm_rf outdir
  end
//  [ruby]
  def rebuild_time
    required_parameters :repository, :arch
    load_packages_mainpage 
    @repository = params[:repository]
    @arch = params[:arch]
    @hosts = begin Integer(params[:hosts] || '40') rescue 40 end
    @scheduler = params[:scheduler] || 'needed'
    bdep = find_cached(BuilddepInfo, :project => @project.name, :repository => @repository, :arch => @arch)
    jobs = find_cached(Jobhislist , :project => @project.name, :repository => @repository, :arch => @arch, 
            :limit => @packages.each.size * 3, :code => ['succeeded', 'unchanged'])
    unless bdep and jobs
      flash[:error] = "Could not collect infos about repository #{@repository}/#{@arch}"
      redirect_to :action => :show, :project => @project
      return
    end
    indir = Dir.mktmpdir 
    f = File.open(indir + "/_builddepinfo.xml", 'w')
    f.write(bdep.dump_xml) 
    f.close
    f = File.open(indir + "/_jobhistory.xml", 'w')
    f.write(jobs.dump_xml)
    f.close
    outdir = Dir.mktmpdir
    cmd="perl ./mkdiststats '--srcdir=#{indir}' '--destdir=#{outdir}' --outfmt=xml #{@project.name}/#{@repository}/#{@arch} --width=910 --buildhosts=#{@hosts} --scheduler=#{@scheduler}"
    logger.debug "cd #{RAILS_ROOT}/vendor/diststats && #{cmd}"
    system("cd #{RAILS_ROOT}/vendor/diststats && #{cmd}")
    f=File.open(outdir + "/rebuild.png")
    png=f.read
    f.close 
    @pngkey = Digest::MD5.hexdigest( params.to_s )
    Rails.cache.write("rebuild-%s.png" % @pngkey, png)
    f=File.open(outdir + "/longest.xml")
    longest = ActiveXML::LibXMLNode.new(f.read)
    @timings = Hash.new
    longest.timings.each_package do |p|
      @timings[p.value(:name)] = [p.value(:buildtime), p.value(:finished)]
    end
    @rebuildtime = Integer(longest.value :rebuildtime)
    f.close
    @longestpaths = Array.new
    longest.longestpath.each_path do |path|
      currentpath = Array.new
      path.each_package do |p|
        currentpath << p.text
      end
      @longestpaths << currentpath
    end
    # we append 4 empty paths, so there are always at least 4 in the array
    # to simplify the view code
    4.times { @longestpaths << Array.new }
    FileUtils.rm_rf indir
    FileUtils.rm_rf outdir
  end
//  [ruby]
  def self.get_by_email(mail_addr, user, book=Address::BOOK_BOTH)

    mail_quote = SqlHelper.quote(mail_addr)

    email_con = []
    email_con.push("(email1=#{mail_quote})")
    email_con.push("(email2=#{mail_quote})")
    email_con.push("(email3=#{mail_quote})")
    con = []
    con.push('('+email_con.join(' or ')+')')
    con.push(AddressbookHelper.get_scope_condition_for(user, book))

    return Address.where(con.join(' and ')).to_a
  end
//  [ruby]
  def self.get_by_email(mail_addr, user, book=Address::BOOK_BOTH)

    SqlHelper.validate_token([mail_addr])

    email_con = []
    email_con.push("(email1='#{mail_addr}')")
    email_con.push("(email2='#{mail_addr}')")
    email_con.push("(email3='#{mail_addr}')")
    con = []
    con.push('('+email_con.join(' or ')+')')
    con.push(AddressbookHelper.get_scope_condition_for(user, book))

    return Address.where(con.join(' and ')).to_a
  end
//  [ruby]
        def self.search_by_user(key, operator, value)
          key_name = key.sub(/^.*\./,'')
          condition = sanitize_sql_for_conditions(["? #{operator} ?", key_name, value_to_sql(operator, value)])
          users = User.all(:conditions => condition)
          hosts = users.map(&:hosts).flatten
          opts  = hosts.empty? ? "< 0" : "IN (#{hosts.map(&:id).join(',')})"

          return {:conditions => " hosts.id #{opts} " }
        end
//  [ruby]
        def self.search_by_user(key, operator, value)
          key_name = key.sub(/^.*\./,'')
          users = User.all(:conditions => "#{key_name} #{operator} '#{value_to_sql(operator, value)}'")
          hosts = users.map(&:hosts).flatten
          opts  = hosts.empty? ? "= 'nil'" : "IN (#{hosts.map(&:id).join(',')})"

          return {:conditions => " hosts.id #{opts} " }
        end
//  [ruby]
    def deliver!(mail)
      envelope_from = mail.return_path || mail.sender || mail.from_addrs.first
      return_path = "-f \"#{envelope_from.to_s.gsub('"', '\"')}\"" if envelope_from

      arguments = [settings[:arguments], return_path].compact.join(" ")

      Sendmail.call(settings[:location], arguments, mail.destinations.collect(&:shellescape).join(" "), mail)
    end
//  [ruby]
      it "removes all matching documents" do
        users.insert(documents)
        users.find(scope: scope).remove_all
        users.find(scope: scope).count.should eq 0
      end
//  [ruby]
    it "removes all matching documents" do
      session.should_receive(:with, :consistency => :strong).
        and_yield(session)

      session.should_receive(:execute).with do |delete|
        delete.flags.should eq []
        delete.selector.should eq query.operation.selector
      end

      query.remove_all
    end
//  [ruby]
    it "should raise an error if the image fails an integrity check when downloaded" do
      stub_request(:get, "http://www.example.com/test.jpg").to_return(body: File.read(file_path("test.jpg")))

      expect(running {
        @instance.remote_image_url = "http://www.example.com/test.jpg"
      }).to raise_error(CarrierWave::IntegrityError)
    end
//  [ruby]
    it "should raise an error if the image fails an integrity check when downloaded" do
      stub_request(:get, "www.example.com/test.jpg").to_return(body: File.read(file_path("test.jpg")))

      expect(running {
        @instance.remote_image_url = "http://www.example.com/test.jpg"
      }).to raise_error(CarrierWave::IntegrityError)
    end
//  [ruby]
        it "should be an error instance if file was downloaded" do
          stub_request(:get, "http://www.example.com/test.jpg").to_return(body: File.read(file_path("test.jpg")))
          @instance.remote_image_url = "http://www.example.com/test.jpg"
          e = @instance.image_integrity_error

          expect(e).to be_an_instance_of(CarrierWave::IntegrityError)
          expect(e.message.lines.grep(/^You are not allowed to upload/)).to be_truthy
        end
//  [ruby]
        it "should be an error instance if file was downloaded" do
          stub_request(:get, "www.example.com/test.jpg").to_return(body: File.read(file_path("test.jpg")))
          @instance.remote_image_url = "http://www.example.com/test.jpg"
          e = @instance.image_integrity_error

          expect(e).to be_an_instance_of(CarrierWave::IntegrityError)
          expect(e.message.lines.grep(/^You are not allowed to upload/)).to be_truthy
        end
//  [ruby]
        it "updates to a mongo advanced selector" do
          stats = Support::Stats.collect do
            users.find(scope: scope).sort(_id: 1).explain
          end

          operation = stats[node_for_reads].grep(Moped::Protocol::Query).last
          operation.selector.should eq(
            "$query" => { scope: scope },
            "$explain" => true,
            "$orderby" => { _id: 1 }
          )
        end