aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server/src/main/webapp/WEB-INF/app/models/profile.rb
blob: 13b9559f964076317c8340749ebf3f6b975119e5 (plain)
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
#
# Sonar, open source software quality management tool.
# Copyright (C) 2009 SonarSource SA
# mailto:contact AT sonarsource DOT com
#
# Sonar is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# Sonar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with Sonar; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
#
class Profile < ActiveRecord::Base
  set_table_name 'rules_profiles'

  has_many :alerts, :dependent => :delete_all
  has_many :active_rules, :class_name => 'ActiveRule', :foreign_key => 'profile_id', :dependent => :destroy, :include => ['rule']
  has_many :projects, :order => 'name asc'
  has_many :active_rules_with_params, :class_name => 'ActiveRule', :foreign_key => 'profile_id',
      :include => ['active_rule_parameters']

  validates_uniqueness_of :name, :scope => :language, :case_sensitive => false
  validates_presence_of :name
  
  DEFAULT_PROFILE_NAME = 'Sun checks'
  
  def active?
    active
  end
  
  def key
    "#{language}_#{name}"
  end
  
  def provided?
    provided
  end
  
  def validate_copy(name)
    new_rule_profile = RulesProfile.new(:name => name, :provided => false, :default_profile => false, :language => language)
    new_rule_profile.valid?
    new_rule_profile.errors
  end

  def self.find_by_name_and_language(name, language)
    Profile.find(:first, :conditions => {:name => name, :language => language})
  end

  def self.find_active_profile_by_language(language)
    Profile.find(:first, :conditions => {:default_profile => true, :language => language})
  end

  def self.default_profile
    Profile.find(:first, :conditions => {:default_profile => true})
  end

  def set_as_default
    default_profile=nil
    Profile.find(:all, :conditions => {:language => language}).each do |profile|
      if profile.id==id
        profile.default_profile=true
        default_profile=profile
      else
        profile.default_profile=false
      end
      profile.save
    end
    self
  end

  def active_by_rule_id(rule_id)
    active_hash_by_rule_id[rule_id]
  end

  def self.options_for_select
    array=[]
    RulesProfile.find(:all, :order => 'name').each do |profile|
      label = profile.name
      label = label + ' (active)' if profile.default_profile?
      array<<[label, profile.id]
    end
    array
  end

  @active_hash_by_rule_id=nil
  def active_hash_by_rule_id
    if @active_hash_by_rule_id.nil?
      @active_hash_by_rule_id={}
      active_rules_with_params.each do |active_rule|
        @active_hash_by_rule_id[active_rule.rule_id]=active_rule
      end
    end
    @active_hash_by_rule_id
  end
end