001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 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     */
020    package org.sonar.plugins.core.hotspots.client.widget;
021    
022    import com.google.gwt.event.dom.client.ClickEvent;
023    import com.google.gwt.event.dom.client.ClickHandler;
024    import com.google.gwt.i18n.client.Dictionary;
025    import com.google.gwt.user.client.Window;
026    import com.google.gwt.user.client.ui.*;
027    import org.sonar.gwt.Links;
028    import org.sonar.gwt.Metrics;
029    import org.sonar.gwt.ui.Icons;
030    import org.sonar.wsclient.gwt.AbstractListCallback;
031    import org.sonar.wsclient.gwt.Sonar;
032    import org.sonar.wsclient.services.Measure;
033    import org.sonar.wsclient.services.Resource;
034    import org.sonar.wsclient.services.ResourceQuery;
035    
036    import java.util.List;
037    import java.util.Map;
038    
039    public class MostViolatedResources extends AbstractHotspot {
040    
041      public MostViolatedResources(Resource resource) {
042        super("violated-files-hotspot", resource);
043      }
044    
045      @Override
046      Widget createHeader() {
047        Dictionary l10n = Dictionary.getDictionary("l10n");
048        final Label label = new Label(l10n.get("hotspot.titleMostViolatedResources"));
049        label.setStyleName("header");
050    
051        final Anchor moreLink = new Anchor(l10n.get("hotspot.moreDetails"));
052        moreLink.getElement().setId("more-violated-resources");
053        moreLink.addClickHandler(new ClickHandler() {
054          public void onClick(ClickEvent event) {
055            Window.Location.assign(Links.baseUrl() + "/drilldown/measures/" + getResource().getId() + "?metric=" + Metrics.WEIGHTED_VIOLATIONS);
056          }
057        });
058    
059        final HorizontalPanel horizontal = new HorizontalPanel();
060        horizontal.setVerticalAlignment(HasAlignment.ALIGN_MIDDLE);
061        horizontal.setWidth("98%");
062        horizontal.add(label);
063        horizontal.add(moreLink);
064        horizontal.setCellHorizontalAlignment(label, HorizontalPanel.ALIGN_LEFT);
065        horizontal.setCellHorizontalAlignment(moreLink, HorizontalPanel.ALIGN_RIGHT);
066    
067        return horizontal;
068      }
069    
070      @Override
071      void doLoadData() {
072        final ResourceQuery query = getResourceQuery();
073        Sonar.getInstance().findAll(query, new AbstractListCallback<Resource>() {
074    
075          @Override
076          protected void doOnResponse(List<Resource> resources) {
077            Grid grid = new Grid(resources.size(), 11);
078            grid.setStyleName("gwt-Hotspot");
079            int row = 0;
080            for (Resource resource : resources) {
081              if (resource.getMeasures().size() > 0) {
082                renderNameCell(grid, resource, Metrics.WEIGHTED_VIOLATIONS, row, 0);
083                renderPriorities(grid, resource, row);
084                row++;
085              }
086            }
087            if (row == 0) {
088              renderEmptyResults();
089            } else {
090              render(grid);
091            }
092          }
093        });
094      }
095    
096    
097      private void renderPriorities(Grid grid, Resource resource, int row) {
098        Measure debt = resource.getMeasures().get(0);
099        if (debt != null && debt.getData() != null) {
100          Map<String, String> map = debt.getDataAsMap(";");
101          renderSeverity(grid, row, map, 1, "BLOCKER");
102          renderSeverity(grid, row, map, 3, "CRITICAL");
103          renderSeverity(grid, row, map, 5, "MAJOR");
104          renderSeverity(grid, row, map, 7, "MINOR");
105          renderSeverity(grid, row, map, 9, "INFO");
106        }
107      }
108    
109      private void renderSeverity(Grid grid, int row, Map<String, String> map, int column, String severity) {
110        grid.setWidget(row, column, Icons.forPriority(severity).createImage());
111        grid.getCellFormatter().setStyleName(row, column, getRowCssClass(row) + " small right");
112    
113        if (map.containsKey(severity)) {
114          grid.setWidget(row, column + 1, new HTML(map.get(severity)));
115        } else {
116          grid.setWidget(row, column + 1, new HTML("0"));
117        }
118        grid.getCellFormatter().setStyleName(row, column + 1, getRowCssClass(row) + " small left");
119      }
120    
121      private ResourceQuery getResourceQuery() {
122        return ResourceQuery.createForResource(getResource(), Metrics.WEIGHTED_VIOLATIONS)
123            .setScopes(Resource.SCOPE_ENTITY)
124            .setQualifiers(Resource.QUALIFIER_CLASS, Resource.QUALIFIER_FILE, Resource.QUALIFIER_PROJECT)
125            .setDepth(ResourceQuery.DEPTH_UNLIMITED)
126            .setLimit(LIMIT);
127      }
128    }