001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info 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 java.util.Collection;
025import java.util.HashMap;
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 * @deprecated since 6.2, this extension is ignored as dashboards have been removed
037 */
038@Deprecated
039public final class Dashboard {
040
041  private String description;
042  private DashboardLayout layout = DashboardLayout.TWO_COLUMNS;
043  private ListMultimap<Integer, Widget> widgetsByColumn = ArrayListMultimap.create();
044  private boolean activated = true;
045  private boolean global = false;
046
047  private Dashboard() {
048  }
049
050  /**
051   * Creates a new {@link Dashboard}.
052   */
053  public static Dashboard create() {
054    return new Dashboard();
055  }
056
057  /**
058   * Add a widget with the given parameters, and return the newly created {@link Widget} object if one wants to add parameters to it.
059   *
060   * <p>The widget ids are listed by the web service /api/widgets
061   *
062   * @param widgetId id of an existing widget
063   * @param columnId column starts with 1. The widget is ignored if the column id does not match the layout.
064   */
065  public Widget addWidget(String widgetId, int columnId) {
066    if (columnId < 1) {
067      throw new IllegalArgumentException("Widget column starts with 1");
068    }
069
070    Widget widget = new Widget(widgetId);
071    widgetsByColumn.put(columnId, widget);
072    return widget;
073  }
074
075  public Collection<Widget> getWidgets() {
076    return widgetsByColumn.values();
077  }
078
079  public List<Widget> getWidgetsOfColumn(int columnId) {
080    return widgetsByColumn.get(columnId);
081  }
082
083  /**
084   * Returns the description of the dashboard.
085   *
086   * @return the description
087   */
088  public String getDescription() {
089    return description;
090  }
091
092  /**
093   * Sets the description of the dashboard.
094   * <br>
095   * Note: you should use the i18n mechanism for the description.
096   *
097   * @param description the description to set
098   */
099  public Dashboard setDescription(String description) {
100    this.description = description;
101    return this;
102  }
103
104  /**
105   * Returns the layout. Default value is the 2 columns mode with width 50%/50%.
106   *
107   * @return the layout
108   */
109  public DashboardLayout getLayout() {
110    return layout;
111  }
112
113  public Dashboard setLayout(DashboardLayout dl) {
114    if (dl == null) {
115      throw new IllegalArgumentException("The layout can not be null");
116    }
117    this.layout = dl;
118    return this;
119  }
120
121  /**
122   * A dashboard is global when it doesn't display information from a project but rather more general information.
123   * <p>Before version 3.1 no dashboard was global.
124   * <p>Since version 6.1, project dashboards are dropped. Project dashboards (global=false) are ignored.</p>
125   *
126   * @since 3.1
127   */
128  public boolean isGlobal() {
129    return global;
130  }
131
132  /**
133   * A dashboard is global when it doesn't display information from a project but rather more general information.
134   * <p>Before version 3.1 no dashboard was global.</p>
135   * <p>Since version 6.1, project dashboards are dropped. Project dashboards (global=false) are ignored.</p>
136   *
137   * @since 3.1
138   */
139  public Dashboard setGlobal(boolean global) {
140    this.global = global;
141    return this;
142  }
143
144  /**
145   * A dashboard can be activated for all anonymous users or users who haven't configured their own dashboards.
146   * <p>Before version 3.1 every dashboard created through this extension point was automatically activated.
147   * This is still the default behavior.
148   *
149   * @since 3.1
150   */
151  public boolean isActivated() {
152    return activated;
153  }
154
155  /**
156   * Set whether the dashboard is activated for all anonymous users or users who haven't configured their own dashboards.
157   *
158   * @since 3.1
159   */
160  public Dashboard setActivated(boolean activated) {
161    this.activated = activated;
162    return this;
163  }
164
165  /**
166   * Note that this class is an inner class to avoid confusion with the extension point org.sonar.api.web.Widget.
167   */
168  public static final class Widget {
169    private String id;
170    private Map<String, String> properties;
171
172    Widget(String id) {
173      this.id = id;
174      this.properties = new HashMap<>();
175    }
176
177    public Widget setProperty(String key, String value) {
178      properties.put(key, value);
179      return this;
180    }
181
182    /**
183     * Returns the properties of this widget.
184     *
185     * @return the properties
186     */
187    public Map<String, String> getProperties() {
188      return properties;
189    }
190
191    public String getProperty(String key) {
192      return properties.get(key);
193    }
194
195    /**
196     * Returns the identifier of this widget.
197     *
198     * @return the id
199     */
200    public String getId() {
201      return id;
202    }
203  }
204}