001/*
002 * SonarQube
003 * Copyright (C) 2009-2016 SonarSource SA
004 * mailto:contact AT sonarsource DOT com
005 *
006 * This program is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.api.web;
021
022import com.google.common.collect.ArrayListMultimap;
023import com.google.common.collect.ListMultimap;
024import com.google.common.collect.Maps;
025import java.util.Collection;
026import java.util.List;
027import java.util.Map;
028
029/**
030 * Definition of a dashboard.
031 * <br>
032 * Its name and description can be retrieved using the i18n mechanism, using the keys "dashboard.&lt;id&gt;.name" and
033 * "dashboard.&lt;id&gt;.description".
034 *
035 * @since 2.13
036 */
037public final class Dashboard {
038
039  private String description;
040  private DashboardLayout layout = DashboardLayout.TWO_COLUMNS;
041  private ListMultimap<Integer, Widget> widgetsByColumn = ArrayListMultimap.create();
042  private boolean activated = true;
043  private boolean global = false;
044
045  private Dashboard() {
046  }
047
048  /**
049   * Creates a new {@link Dashboard}.
050   */
051  public static Dashboard create() {
052    return new Dashboard();
053  }
054
055  /**
056   * Add a widget with the given parameters, and return the newly created {@link Widget} object if one wants to add parameters to it.
057   *
058   * <p>The widget ids are listed by the web service /api/widgets
059   *
060   * @param widgetId id of an existing widget
061   * @param columnId column starts with 1. The widget is ignored if the column id does not match the layout.
062   */
063  public Widget addWidget(String widgetId, int columnId) {
064    if (columnId < 1) {
065      throw new IllegalArgumentException("Widget column starts with 1");
066    }
067
068    Widget widget = new Widget(widgetId);
069    widgetsByColumn.put(columnId, widget);
070    return widget;
071  }
072
073  public Collection<Widget> getWidgets() {
074    return widgetsByColumn.values();
075  }
076
077  public List<Widget> getWidgetsOfColumn(int columnId) {
078    return widgetsByColumn.get(columnId);
079  }
080
081  /**
082   * Returns the description of the dashboard.
083   *
084   * @return the description
085   */
086  public String getDescription() {
087    return description;
088  }
089
090  /**
091   * Sets the description of the dashboard.
092   * <br>
093   * Note: you should use the i18n mechanism for the description.
094   *
095   * @param description the description to set
096   */
097  public Dashboard setDescription(String description) {
098    this.description = description;
099    return this;
100  }
101
102  /**
103   * Returns the layout. Default value is the 2 columns mode with width 50%/50%.
104   *
105   * @return the layout
106   */
107  public DashboardLayout getLayout() {
108    return layout;
109  }
110
111  public Dashboard setLayout(DashboardLayout dl) {
112    if (dl == null) {
113      throw new IllegalArgumentException("The layout can not be null");
114    }
115    this.layout = dl;
116    return this;
117  }
118
119  /**
120   * A dashboard is global when it doesn't display information from a project but rather more general information.
121   * <p>Before version 3.1 no dashboard was global.
122   * <p>Since version 6.1, project dashboards are dropped. Project dashboards (global=false) are ignored.</p>
123   *
124   * @since 3.1
125   */
126  public boolean isGlobal() {
127    return global;
128  }
129
130  /**
131   * A dashboard is global when it doesn't display information from a project but rather more general information.
132   * <p>Before version 3.1 no dashboard was global.</p>
133   * <p>Since version 6.1, project dashboards are dropped. Project dashboards (global=false) are ignored.</p>
134   *
135   * @since 3.1
136   */
137  public Dashboard setGlobal(boolean global) {
138    this.global = global;
139    return this;
140  }
141
142  /**
143   * A dashboard can be activated for all anonymous users or users who haven't configured their own dashboards.
144   * <p>Before version 3.1 every dashboard created through this extension point was automatically activated.
145   * This is still the default behavior.
146   *
147   * @since 3.1
148   */
149  public boolean isActivated() {
150    return activated;
151  }
152
153  /**
154   * Set whether the dashboard is activated for all anonymous users or users who haven't configured their own dashboards.
155   *
156   * @since 3.1
157   */
158  public Dashboard setActivated(boolean activated) {
159    this.activated = activated;
160    return this;
161  }
162
163  /**
164   * Note that this class is an inner class to avoid confusion with the extension point org.sonar.api.web.Widget.
165   */
166  public static final class Widget {
167    private String id;
168    private Map<String, String> properties;
169
170    Widget(String id) {
171      this.id = id;
172      this.properties = Maps.newHashMap();
173    }
174
175    public Widget setProperty(String key, String value) {
176      properties.put(key, value);
177      return this;
178    }
179
180    /**
181     * Returns the properties of this widget.
182     *
183     * @return the properties
184     */
185    public Map<String, String> getProperties() {
186      return properties;
187    }
188
189    public String getProperty(String key) {
190      return properties.get(key);
191    }
192
193    /**
194     * Returns the identifier of this widget.
195     *
196     * @return the id
197     */
198    public String getId() {
199      return id;
200    }
201  }
202}