001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar 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 * Sonar 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
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019 */
020package org.sonar.gwt;
021
022import com.google.gwt.user.client.Window;
023
024public final class Links {
025
026  public static final String DEFAULT_POPUP_HTML_FEATURES = "height=800,width=900,scrollbars=1,resizable=1";
027
028  private Links() {
029    // only static methods
030  }
031
032  public static String baseUrl() {
033    return Configuration.getParameter("sonar_url");
034  }
035
036  public static String apiUrl() {
037    return baseUrl() + "/api";
038  }
039
040
041  public static String urlForResource(String resourceIdOrKey) {
042    return urlForMeasure(resourceIdOrKey, null);
043  }
044
045  public static String urlForMeasure(String resourceIdOrKey, String metricKey) {
046    String url = baseUrl() + "/resource/index/" + resourceIdOrKey;
047    if (metricKey != null) {
048      url += "?metric=" + metricKey;
049    }
050    return url;
051  }
052  
053  /**
054   *
055   * @param resourceIdOrKey
056   * @param pageId
057   * @param query additional query parameters. Can be null. Example "layout=false&param1=val1"
058   */
059  public static String urlForResourcePage(String resourceIdOrKey, String pageId, String query) {
060    String url = baseUrl() + "/plugins/resource/";
061    if (resourceIdOrKey != null) {
062      url += resourceIdOrKey;
063    }
064    url += "?page=";
065    url += pageId;
066    if (query != null) {
067      url += "&";
068      url += query;
069    }
070    return url;
071  }
072
073  public static String urlForRule(String ruleIdOrKey, boolean showLayout) {
074    return baseUrl() + "/rules/show/" + ruleIdOrKey + "?layout=" + showLayout;
075  }
076
077  public static String urlForDrilldown(String resourceIdOrKey, String metricKey) {
078    return baseUrl() + "/drilldown/measures/" + resourceIdOrKey + "?metric=" + metricKey;
079  }
080
081  public static void openResourcePopup(final String resourceIdOrKey) {
082    openMeasurePopup(resourceIdOrKey, null);
083  }
084
085  /**
086   * Open the resource in a popup with HTML features like: height=800,width=900,scrollbars=1,resizable=1
087   *
088   * @param resourceIdOrKey the id or key of the resource to display, not null
089   * @param metricKey       the metric to highlight (optional : can be null)
090   */
091  public static void openMeasurePopup(final String resourceIdOrKey, final String metricKey) {
092    openMeasurePopup(resourceIdOrKey, metricKey, DEFAULT_POPUP_HTML_FEATURES);
093  }
094
095
096  public static void openMeasurePopup(final String resourceKey, final String metricKey, final String htmlFeatures) {
097    String url = urlForMeasure(resourceKey, metricKey);
098    Window.open(url, "resource", htmlFeatures);
099  }
100
101
102  public static void openResourcePage(final String pageId, final String resourceIdOrKey, final String query) {
103    String url = urlForResourcePage(pageId, resourceIdOrKey, query);
104    Window.Location.assign(url);
105  }
106
107  public static void openRulePopup(final String ruleIdOrKey) {
108    openRulePopup(ruleIdOrKey, DEFAULT_POPUP_HTML_FEATURES);
109  }
110
111  public static void openRulePopup(final String ruleIdOrKey, final String htmlFeatures) {
112    String url = urlForRule(ruleIdOrKey, false);
113    Window.open(url, "rule", htmlFeatures);
114  }
115}