aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server/src/main/webapp/WEB-INF/app/models/profile.rb
blob: 78691867b8f171db415f02cbcfd88c2df6bb8909 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#
# Sonar, open source software quality management tool.
# Copyright (C) 2008-2012 SonarSource
# 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 = Profile.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, :enabled => true})
  end

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

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

  def set_as_default
    default_profile=nil
    Profile.find(:all, :conditions => {:language => language, :enabled => true}).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=[]
    Profile.find(:all, :conditions => {:enabled => true}, :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

  def deletable?
    !provided? && !default_profile? && children.empty?
  end

  def count_overriding_rules
    @count_overriding_rules||=
      begin
        active_rules.count(:conditions => ['inheritance=?', 'OVERRIDES'])
      end
  end

  def inherited?
    parent_name.present?
  end

  def parent
    @parent||=
      begin
        if parent_name.present?
          Profile.find(:first, :conditions => ['language=? and name=? and enabled=?', language, parent_name, true])
        else
          nil
        end
      end
  end

  def count_active_rules
    active_rules.select{|ar| ar.rule.enabled}.size
  end

  def ancestors
    @ancestors ||=
      begin
        array=[]
        if parent
          array<<parent
          array.concat(parent.ancestors)
        end
        array
      end
  end

  def children
    @children ||=
      begin
        Profile.find(:all, :conditions => ['language=? and parent_name=? and enabled=?', language, name, true], :order => 'name')
      end
  end
end