aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server/src/main/webapp/WEB-INF/app/models
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2012-09-27 15:17:59 +0200
committerSimon Brandhof <simon.brandhof@gmail.com>2012-09-27 15:17:59 +0200
commit8872c32eec2b8bb07dba35d566f1135c50843383 (patch)
treecc6b019a7aff2f303535ddaab2486bde7c525f84 /sonar-server/src/main/webapp/WEB-INF/app/models
parentbe8a52af8539dd972b081202aa569ccdc33dac40 (diff)
downloadsonarqube-8872c32eec2b8bb07dba35d566f1135c50843383.tar.gz
sonarqube-8872c32eec2b8bb07dba35d566f1135c50843383.zip
SONAR-2602 support project modules with different languages
Diffstat (limited to 'sonar-server/src/main/webapp/WEB-INF/app/models')
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/models/api/utils.rb4
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/models/profile.rb147
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/models/project.rb5
-rw-r--r--sonar-server/src/main/webapp/WEB-INF/app/models/property.rb16
4 files changed, 116 insertions, 56 deletions
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/api/utils.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/api/utils.rb
index ddf214b88e2..81e74632ca9 100644
--- a/sonar-server/src/main/webapp/WEB-INF/app/models/api/utils.rb
+++ b/sonar-server/src/main/webapp/WEB-INF/app/models/api/utils.rb
@@ -169,4 +169,8 @@ class Api::Utils
def self.java_facade
Java::OrgSonarServerUi::JRubyFacade.getInstance()
end
+
+ def self.languages
+ java_facade.getLanguages()
+ end
end
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/profile.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/profile.rb
index 079645ff6c6..a9bb3d3a20e 100644
--- a/sonar-server/src/main/webapp/WEB-INF/app/models/profile.rb
+++ b/sonar-server/src/main/webapp/WEB-INF/app/models/profile.rb
@@ -22,13 +22,21 @@ class Profile < ActiveRecord::Base
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', 'active_rule_note']
+ has_many :active_rules_with_params, :class_name => 'ActiveRule', :foreign_key => 'profile_id', :include => ['active_rule_parameters', 'active_rule_note']
+ has_many :projects, :class_name => 'Project', :finder_sql => %q(
+ select prj.* from projects prj, properties prop where prj.id=prop.resource_id and prop.resource_id is not null and prop.prop_key='sonar.profile.#{language}' and prop.text_value='#{name}'
+ )
+ has_many :changes, :class_name => 'ActiveRuleChange', :dependent => :destroy
+ has_many :children, :class_name => 'Profile', :finder_sql => %q(
+ select c.* from rules_profiles c where c.parent_name='#{name}' and c.language='#{language}'
+ )
validates_uniqueness_of :name, :scope => :language, :case_sensitive => false, :message => Api::Utils.message('quality_profiles.already_exists')
validates_presence_of :name, :message => Api::Utils.message('quality_profiles.please_type_profile_name')
+ MAX_NAME_LENGTH = 100
+ validates_length_of :name, :maximum => MAX_NAME_LENGTH, :message => Api::Utils.message('name_too_long_x', :params => [MAX_NAME_LENGTH])
+
# The warnings that are set on this record, equivalent to normal ActiveRecord errors but does not prevent
# the record from saving.
def warnings
@@ -36,7 +44,7 @@ class Profile < ActiveRecord::Base
end
def warnings?
- not warnings.empty?
+ !warnings.empty?
end
def notices
@@ -59,34 +67,16 @@ class Profile < ActiveRecord::Base
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
+ Profile.transaction do
+ Profile.find(:all, :conditions => {:language => language}).each do |profile|
+ if profile.id==id
+ profile.default_profile=true
+ else
+ profile.default_profile=false
+ end
+ profile.save
end
- profile.save
end
self
end
@@ -97,7 +87,7 @@ class Profile < ActiveRecord::Base
def self.options_for_select
array=[]
- Profile.find(:all, :conditions => {:enabled => true}, :order => 'name').each do |profile|
+ Profile.find(:all, :order => 'name').each do |profile|
label = profile.name
label = label + ' (active)' if profile.default_profile?
array<<[label, profile.id]
@@ -123,9 +113,9 @@ class Profile < ActiveRecord::Base
def count_overriding_rules
@count_overriding_rules||=
- begin
- active_rules.count(:conditions => ['inheritance=?', 'OVERRIDES'])
- end
+ begin
+ active_rules.count(:conditions => ['inheritance=?', 'OVERRIDES'])
+ end
end
def inherited?
@@ -134,13 +124,13 @@ class Profile < ActiveRecord::Base
def parent
@parent||=
- begin
- if parent_name.present?
- Profile.find(:first, :conditions => ['language=? and name=? and enabled=?', language, parent_name, true])
- else
- nil
- end
+ begin
+ if parent_name.present?
+ Profile.find(:first, :conditions => ['language=? and name=?', language, parent_name])
+ else
+ nil
end
+ end
end
def count_active_rules
@@ -149,21 +139,14 @@ class Profile < ActiveRecord::Base
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')
+ begin
+ array=[]
+ if parent
+ array<<parent
+ array.concat(parent.ancestors)
end
+ array
+ end
end
def import_configuration(importer_key, file)
@@ -178,4 +161,60 @@ class Profile < ActiveRecord::Base
notices.add_to_base msg
end
end
+
+ def before_destroy
+ Property.clear_for_resources("sonar.profile.#{language}", name)
+ #TODO clear global property sonar.profile.#{language} with value #{name}
+ end
+
+ def rename(new_name)
+ old_name=self.name
+ Profile.transaction do
+ children_to_be_renamed=children()
+ self.name=new_name
+ if save
+ children_to_be_renamed.each do |child|
+ child.parent_name=new_name
+ child.save
+ end
+ Property.update_all("text_value='#{new_name}'", ['prop_key=? and text_value=?', "sonar.profile.#{language}", old_name])
+ end
+ end
+ self
+ end
+
+ def add_project_id(project_id)
+ Property.set("sonar.profile.#{language}", name, project_id)
+ end
+
+ def remove_projects
+ Property.clear_for_resources("sonar.profile.#{language}", name)
+ end
+
+ def self.reset_default_profile_for_project_id(lang, project_id)
+ Property.clear("sonar.profile.#{lang}", project_id)
+ end
+
+ def self.by_project_id(language, project_id, returns_default_if_nil=false)
+ profile_name=Property.value("sonar.profile.#{language}", project_id)
+ profile = (profile_name.present? ? Profile.find_by_name_and_language(profile_name, language) : nil)
+
+ if !profile && returns_default_if_nil
+ profile = by_default(language)
+ end
+ profile
+ end
+
+ def self.by_default(language)
+ Profile.find(:first, :conditions => {:default_profile => true, :language => language})
+ end
+
+ # Results are NOT sorted
+ def self.all_by_language(language)
+ Profile.find(:all, :conditions => {:language => language})
+ end
+
+ def self.find_by_name_and_language(name, language)
+ Profile.find(:first, :conditions => {:name => name, :language => language})
+ end
end \ No newline at end of file
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/project.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/project.rb
index 07e3a251282..4acf953c17c 100644
--- a/sonar-server/src/main/webapp/WEB-INF/app/models/project.rb
+++ b/sonar-server/src/main/webapp/WEB-INF/app/models/project.rb
@@ -25,7 +25,6 @@ class Project < ActiveRecord::Base
has_many :processed_snapshots, :class_name => 'Snapshot', :conditions => "status='#{Snapshot::STATUS_PROCESSED}' AND qualifier<>'LIB'", :order => 'created_at asc'
has_many :events, :foreign_key => 'resource_id', :order => 'event_date DESC'
has_many :project_links, :dependent => :delete_all, :order => 'link_type'
- belongs_to :profile, :class_name => 'Profile', :foreign_key => 'profile_id'
has_many :user_roles, :foreign_key => 'resource_id'
has_many :group_roles, :foreign_key => 'resource_id'
has_many :manual_measures, :foreign_key => 'resource_id'
@@ -182,6 +181,10 @@ class Project < ActiveRecord::Base
last_snapshot ? last_snapshot.path_name : nil
end
+ def profile(lang, returns_default_if_nil=false)
+ Profile.by_project_id(lang, id, returns_default_if_nil)
+ end
+
private
def create_chart_measures(results, date_column_name, value_column_name)
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/property.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/property.rb
index 976fdf7480a..6371b51242d 100644
--- a/sonar-server/src/main/webapp/WEB-INF/app/models/property.rb
+++ b/sonar-server/src/main/webapp/WEB-INF/app/models/property.rb
@@ -21,9 +21,11 @@ class Property < ActiveRecord::Base
validates_presence_of :prop_key
named_scope :with_key, lambda { |value| {:conditions => {:prop_key, value}} }
+ named_scope :with_value, lambda { |value| {:conditions => {:text_value, value}} }
named_scope :with_resource, lambda { |value| {:conditions => {:resource_id => value}} }
named_scope :with_user, lambda { |value| {:conditions => {:user_id => value}} }
- named_scope :on_resource, :conditions => ['resource_id is not ?', nil]
+ named_scope :with_resources, :conditions => 'resource_id is not null'
+ named_scope :with_users, :conditions => 'user_id is not null'
def key
prop_key
@@ -47,6 +49,18 @@ class Property < ActiveRecord::Base
end
end
+ def self.clear_for_resources(key, value=nil)
+ scope=Property.with_resources().with_key(key)
+ if value
+ scope.with_value(value)
+ end
+ scope.delete_all
+ end
+
+ def self.clear_for_users(key)
+ Property.with_users().with_key(key).delete_all
+ end
+
def self.by_key(key, resource_id=nil, user_id=nil)
all(key, resource_id, user_id).first
end