aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server/src/main/webapp/WEB-INF/app/controllers/dashboard_controller.rb
blob: b554f01d01ad7bd46abad05bd1e42cdb6b0f3e58 (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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#
# Sonar, entreprise quality control tool.
# Copyright (C) 2009 SonarSource SA
# 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 DashboardController < ApplicationController

  SECTION=Navigation::SECTION_RESOURCE

  verify :method => :post, :only => [:set_layout, :add_widget, :set_dashboard, :save_widget], :redirect_to => {:action => :index}
  before_filter :login_required, :except => [:index]

  def index
    # TODO display error page if no dashboard or no resource
    load_dashboard()
    load_resource()
    load_authorized_widget_definitions()
  end

  def configure
    # TODO display error page if no dashboard or no resource
    load_dashboard()
    load_resource()
    load_widget_definitions()
  end

  def edit_layout
    load_dashboard()
    load_resource()
  end

  def set_layout
    dashboard=Dashboard.find(params[:id].to_i)
    if dashboard.editable_by?(current_user)
      dashboard.column_layout=params[:layout]
      if dashboard.save
        columns=dashboard.column_layout.split('-')
        dashboard.widgets.find(:all, :conditions => ["column_index > ?",columns.size()]).each do |widget|
          widget.column_index=columns.size()
          widget.save
        end
      end
    end
    redirect_to :action => 'index', :id => dashboard.id, :resource => params[:resource]
  end

  def set_dashboard
    load_dashboard()

    dashboardstate=params[:dashboardstate]

    columns=dashboardstate.split(";")
    all_ids=[]
    columns.each_with_index do |col, index|
      ids=col.split(",")
      ids.each_with_index do |id, order|
        widget=@dashboard.widgets.to_a.find { |i| i.id==id.to_i() }
        if widget
          widget.column_index=index+1
          widget.row_index=order+1
          widget.save!
          all_ids<<widget.id
        end
      end 
    end
    @dashboard.widgets.reject{|w| all_ids.include?(w.id)}.each do |w|
      w.destroy
    end
    render :json => {:status => 'ok'}
  end

  def add_widget
    dashboard=Dashboard.find(params[:id].to_i)
    widget_id=nil
    if dashboard.editable_by?(current_user)
      definition=java_facade.getWidget(params[:widget])
      if definition
        new_widget=dashboard.widgets.create(:widget_key => definition.getId(),
                                           :name => definition.getTitle(),
                                           :column_index => dashboard.number_of_columns,
                                           :row_index => dashboard.column_size(dashboard.number_of_columns) + 1,
                                           :configured => !definition.hasRequiredProperties())
        widget_id=new_widget.id
      end
    end
    redirect_to :action => 'configure', :id => dashboard.id, :resource => params[:resource], :highlight => widget_id 
  end


  def save_widget
    widget=Widget.find(params[:id].to_i)
    #TODO check owner of dashboard
    definition=java_facade.getWidget(widget.widget_key)
    errors_by_property_key={}
    definition.getProperties().each do |property_def|
      value=params[property_def.key()] || property_def.defaultValue()
      value='false' if value.empty? && property_def.type.name()==WidgetProperty::TYPE_BOOLEAN

      errors=WidgetProperty.validate_definition(property_def, value)
      if errors.empty?
        widget.set_property(property_def.key(), value, property_def.type.name())
      else
        widget.unset_property(property_def.key())
        errors_by_property_key[property_def.key()]=errors
      end
    end

    if errors_by_property_key.empty?
      widget.configured=true
      widget.save
      widget.properties.each {|p| p.save}
      render :update do |page|
        page.redirect_to(url_for(:action => :configure, :id => widget.dashboard_id, :resource => params[:resource]))
      end
    else
      widget.configured=false
      widget.save
      render :update do |page|
        page.alert('errors ' + errors_by_property_key.inspect)
      end
    end
  end

  private

  def load_dashboard
    @active=nil
    if logged_in?
      if params[:id]
        @active=ActiveDashboard.find(:first, :include => 'dashboard', :conditions => ['active_dashboards.dashboard_id=? AND active_dashboards.user_id=?', params[:id].to_i, current_user.id])
      elsif params[:name]
        @active=ActiveDashboard.find(:first, :include => 'dashboard', :conditions => ['dashboards.name=? AND active_dashboards.user_id=?', params[:name], current_user.id])
      else
        @active=ActiveDashboard.find(:first, :include => 'dashboard', :conditions => ['active_dashboards.user_id=?', current_user.id], :order => 'order_index ASC')
      end
    end

    if @active.nil?
      # anonymous or not found in user dashboards
      if params[:id]
        @active=ActiveDashboard.find(:first, :include => 'dashboard', :conditions => ['active_dashboards.dashboard_id=? AND active_dashboards.user_id IS NULL', params[:id].to_i])
      elsif params[:name]
        @active=ActiveDashboard.find(:first, :include => 'dashboard', :conditions => ['dashboards.name=? AND active_dashboards.user_id IS NULL', params[:name]])
      else
        @active=ActiveDashboard.find(:first, :include => 'dashboard', :conditions => ['active_dashboards.user_id IS NULL'], :order => 'order_index ASC')
      end
    end
    @dashboard=(@active ? @active.dashboard : nil)
  end

  def load_resource
    @resource=Project.by_key(params[:resource])
    if @resource.nil?
      # TODO display error page
      redirect_to home_path
      return false
    end
    return access_denied unless has_role?(:user, @resource)
    @snapshot = @resource.last_snapshot
    @project=@resource  # variable name used in old widgets
  end

  def load_authorized_widget_definitions()
    @widget_definitions = java_facade.getWidgets(@resource.scope, @resource.qualifier, @resource.language)
    @widget_definitions=@widget_definitions.select do |widget|
      authorized=widget.getUserRoles().size==0
      unless authorized
        widget.getUserRoles().each do |role|
          authorized=(role=='user') || (role=='viewer') || has_role?(role, @resource)
          break if authorized
        end
      end
      authorized
    end
  end

  def load_widget_definitions()
    @widget_definitions = java_facade.getWidgets()
  end

end