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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
#
# 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 Sonar; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
#
class Project < ActiveRecord::Base
include Comparable
include Resourceable
has_many :snapshots
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'
belongs_to :root, :class_name => 'Project', :foreign_key => 'root_id'
def self.by_key(k)
begin
ki=Integer(k)
Project.find(ki)
rescue
Project.find(:first, :conditions => {:kee => k})
end
end
def self.delete_project(project)
if project
Snapshot.update_all(['islast=?', false], ['(root_project_id=? OR project_id=?) AND islast=?', project.id, project.id, true])
Project.delete_all(['id=? OR root_id=? or copy_resource_id=?', project.id, project.id, project.id])
ResourceIndex.delete_all(['root_project_id=?', project.id])
end
end
def project
root||self
end
def root_project
@root_project ||=
begin
parent_module(self)
end
end
def last_snapshot
@last_snapshot ||=
begin
snapshot=Snapshot.find(:first, :conditions => {:islast => true, :project_id => id})
if snapshot
snapshot.project=self
end
snapshot
end
end
def events_with_snapshot
events.select { |event| !event.snapshot_id.nil? }
end
def key
kee
end
def links
project_links
end
def link(type)
# to_a avoids conflicts with ActiveRecord:Base.find
links.to_a.find { |l| l.link_type==type }
end
def custom_links
links.select { |l| l.custom? }
end
def standard_links
links.reject { |l| l.custom? }
end
def chart_measures(metric_id)
sql = Project.send(:sanitize_sql, ['select s.created_at as created_at, m.value as value ' +
' from project_measures m, snapshots s ' +
' where s.id=m.snapshot_id and ' +
" s.status='%s' and " +
' s.project_id=%s and m.metric_id=%s ', Snapshot::STATUS_PROCESSED, self.id, metric_id]) +
' and m.rule_id IS NULL and m.rule_priority IS NULL' +
' order by s.created_at'
create_chart_measures(Project.connection.select_all(sql), 'created_at', 'value')
end
def <=>(other)
kee <=> other.kee
end
def name(long_if_defined=false)
if long_if_defined
long_name || read_attribute(:name)
else
read_attribute(:name)
end
end
def fullname
name
end
def branch
if project? || module?
s=kee.split(':')
if s.size>=3
return s[2]
end
end
nil
end
def resource_id_for_authorization
if library?
# no security on libraries
nil
elsif set?
self.root_id || self.id
elsif last_snapshot
last_snapshot.resource_id_for_authorization
else
nil
end
end
def path_name
last_snapshot ? last_snapshot.path_name : nil
end
private
def create_chart_measures(results, date_column_name, value_column_name)
chart_measures = []
if results and results.first != nil
# :sanitize_sql is protected so its behaviour cannot be predicted exactly,
# the jdbc active record impl adapter returns a db typed objects array
# when regular active record impl return string typed objects
if results.first[date_column_name].class == Time
results.each do |hash|
chart_measures << ChartMeasure.new(hash[date_column_name], hash[value_column_name])
end
else
results.each do |hash|
chart_measures << ChartMeasure.new(Time.parse(hash[date_column_name]), hash[value_column_name].to_d)
end
end
end
chart_measures
end
def parent_module(current_module)
current_module.root ? parent_module(current_module.root) : current_module
end
end
|