001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
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.api.web.gwt.client.webservices;
021    
022    import java.util.ArrayList;
023    import java.util.HashMap;
024    import java.util.List;
025    import java.util.Map;
026    
027    public class Violations extends ResponsePOJO {
028      private List<Violation> violations;
029      private Map<Integer, List<Violation>> byLines;
030    
031      public Violations(List<Violation> violations) {
032        this.violations = violations;
033      }
034    
035      public Violations() {
036        this.violations = new ArrayList<Violation>();
037      }
038    
039      public void add(Violation v) {
040        violations.add(v);
041        byLines = null;
042      }
043    
044      public List<Violation> getAll() {
045        return violations;
046      }
047    
048    
049      public Map<Integer, List<Violation>> getByLines() {
050        if (byLines == null) {
051          byLines = new HashMap<Integer, List<Violation>>();
052          for (Violation violation : violations) {
053            List<Violation> lineViolations = byLines.get(violation.getLine());
054            if (lineViolations == null) {
055              lineViolations = new ArrayList<Violation>();
056              byLines.put(violation.getLine(), lineViolations);
057            }
058            lineViolations.add(violation);
059          }
060        }
061        return byLines;
062      }
063    
064      public String getLevelForLine(Integer line) {
065        List<Violation> lineViolations = getByLines().get(line);
066        String level = "";
067        if (lineViolations != null) {
068          for (Violation lineViolation : lineViolations) {
069            if ("BLOCKER".equals(lineViolation.getPriority()) || "CRITICAL".equals(lineViolation.getPriority()) || "MAJOR".equals(lineViolation.getPriority())) {
070              level = "error";
071    
072            } else if (!"error".equals(level)) {
073              level = "warning";
074            }
075          }
076        }
077        return level;
078      }
079    
080      public int countForLine(Integer line) {
081        List<Violation> lineViolations = getByLines().get(line);
082        if (lineViolations == null) {
083          return 0;
084        }
085        return lineViolations.size();
086      }
087    }