aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server/src/main/webapp/WEB-INF/app/controllers/metrics_controller.rb
blob: c89791fc925b81ef9c238d33b4f725185d4e289f (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
#
# SonarQube, open source software quality management tool.
# Copyright (C) 2008-2013 SonarSource
# mailto:contact AT sonarsource DOT com
#
# SonarQube 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.
#
# SonarQube 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 this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
class MetricsController < ApplicationController

  before_filter :admin_required

  SECTION=Navigation::SECTION_CONFIGURATION

  def index
    prepare_metrics_and_domains

    if params['id']
      @metric=Metric.find(params['id'].to_i)
      params['domain']=@metric.domain(false)
    else
      @metric=Metric.new
    end
    render :action => 'index'
  end

 def create_form
   prepare_metrics_and_domains

    if params['id']
      @metric=Metric.find(params['id'].to_i)
      params['domain']=@metric.domain(false)
    else
      @metric=Metric.new
    end
    render :partial => 'metrics/create_form'
  end

  def edit_form
    prepare_metrics_and_domains

    @metric=Metric.find(params['id'].to_i)
    params['domain']=@metric.domain(false)

    render :partial => 'metrics/edit_form'
  end

  def reactivate_form
    render :partial => 'metrics/reactivate_form'
  end

  def save_from_web
    short_name = params[:metric][:short_name]
    metric_name = short_name.downcase.gsub(/\s/, '_')[0..59]
    
    if params[:id]
      metric = Metric.find(params[:id].to_i)
    else
      metric = Metric.first(:conditions => ["name = ?", metric_name])
      if metric
        @reactivate_metric = metric
      else
        metric = Metric.new
      end
    end

    metric.attributes=params[:metric]
    if metric.short_name(false)
      metric.name = metric.short_name(false).downcase.gsub(/\s/, '_')[0..59] unless params[:id]
    end
    unless params[:newdomain].blank?
      metric.domain = params[:newdomain]
    end
    metric.direction = 0
    metric.user_managed = true
    metric.origin = Metric::ORIGIN_GUI
    metric.enabled = true unless @reactivate_metric

    begin
      new_rec = metric.new_record?
      metric.save!
      unless @reactivate_metric
        Metric.clear_cache
        if new_rec
          flash[:notice] = 'Successfully created.'
        else
          flash[:notice] = 'Successfully updated.'
        end
      end
    rescue
      @errors = []
      @errors << metric.errors.full_messages.join("<br/>\n")
    end
    
    if @reactivate_metric
      prepare_metrics_and_domains
      render :partial => 'metrics/reactivate_form', :status => 400
    elsif @errors
      @metric = metric
      @domains = metric.domain
      render :partial => 'metrics/create_form', :status => 400
    else
      render :text => 'ok', :status => 200
    end
  end

  def reactivate
    begin
      metric = Metric.find(params[:id].to_i)
      metric.enabled = true
      metric.save!
      Metric.clear_cache
      flash[:notice] = 'Successfully reactivated.'
    rescue
      flash[:error] = metric.errors.full_messages.join("<br/>\n")
    end
    redirect_to :action => 'index', :domain => metric.domain(false)
  end

  def delete_from_web
    metric = Metric.by_id(params[:id].to_i) if params[:id] && params[:id].size > 0
    if metric
      Metric.delete_with_manual_measures(params[:id].to_i)
      flash[:notice] = 'Successfully deleted.'
      Metric.clear_cache
    else
      flash[:error] = 'Sorry there was a problem with deleting'
    end
    redirect_to :action => 'index'
  end
  
  private
  
  def prepare_metrics_and_domains
    @metrics = Metric.all.select { |metric| metric.user_managed? }
    @domains = Metric.all.map { |metric| metric.domain(false) }.compact.uniq.sort
  end
  
end