aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server/src/main/webapp/WEB-INF/app/models/property.rb
blob: f32b9bfc2948cb565c7c19a0494433d8c8f44d6e (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
 #
 # Sonar, entreprise quality control tool.
 # Copyright (C) 2008-2011 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 {library}; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 #
class Property < ActiveRecord::Base
  
  def key
    prop_key
  end
  
  def value
    text_value
  end

  def self.hash(resource_id=nil)
    hash={}
    Property.find(:all, :conditions => {'resource_id' => resource_id, 'user_id' => nil}).each do |prop|
      hash[prop.key]=prop.value
    end
    hash
  end

  def self.value(key, resource_id=nil, default_value=nil)
    prop=Property.find(:first, :conditions => {'prop_key' => key, 'resource_id' => resource_id, 'user_id' => nil})
    prop ? prop.text_value : default_value
  end

  def self.values(key, resource_id=nil)
    Property.find(:all, :conditions => {'prop_key' => key, 'resource_id' => resource_id, 'user_id' => nil}).collect{|p| p.text_value}
  end

  def self.set(key, value, resource_id=nil)
    Property.delete_all('prop_key' => key, 'resource_id' => resource_id, 'user_id' => nil)
    Property.create(:prop_key => key, :text_value => value.to_s, :resource_id => resource_id)
    reload_java_configuration
  end

  def self.clear(key, resource_id=nil)
    Property.delete_all('prop_key' => key, 'resource_id' => resource_id, 'user_id' => nil)
    reload_java_configuration
  end

  def self.by_key(key, resource_id=nil)
    Property.find(:first, :conditions => {'prop_key' => key, 'resource_id' => resource_id, 'user_id' => nil})
  end

  def self.by_key_prefix(prefix)
    Property.find(:all, :conditions => ["prop_key like ?", prefix + '%'])
  end

  def self.update( key, value )
    property = Property.find(:first, :conditions => {:prop_key => key, :resource_id => nil, :user_id => nil});
    property.text_value = value
    property.save
    reload_java_configuration
    property
  end

  def to_hash_json
    {:key => key, :value => value.to_s}
  end

  def to_xml(xml=Builder::XmlMarkup.new(:indent => 0))
    xml.property do
      xml.key(prop_key)
      xml.value {xml.cdata!(text_value.to_s)}
    end
    xml
  end

  private

  def self.reload_java_configuration
    Java::OrgSonarServerUi::JRubyFacade.new.reloadConfiguration()
  end
end