001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube 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     * SonarQube 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     */
020    package org.sonar.batch.issue;
021    
022    import com.google.common.collect.Lists;
023    import org.sonar.api.BatchComponent;
024    import org.sonar.api.issue.internal.DefaultIssue;
025    import org.sonar.api.resources.Resource;
026    import org.sonar.api.rules.Rule;
027    import org.sonar.api.rules.RuleFinder;
028    import org.sonar.api.rules.RulePriority;
029    import org.sonar.api.rules.Violation;
030    import org.sonar.batch.index.ResourceCache;
031    
032    import java.util.List;
033    
034    /**
035     * Bridge with violations, that have been deprecated in 3.6.
036     *
037     * @since 3.6
038     */
039    public class DeprecatedViolations implements BatchComponent {
040    
041      private final IssueCache issueCache;
042      private final RuleFinder ruleFinder;
043      private final ResourceCache resourceCache;
044    
045      public DeprecatedViolations(IssueCache issueCache, RuleFinder ruleFinder, ResourceCache resourceCache) {
046        this.issueCache = issueCache;
047        this.ruleFinder = ruleFinder;
048        this.resourceCache = resourceCache;
049      }
050    
051      public List<Violation> get(String componentKey) {
052        Iterable<DefaultIssue> issues = issueCache.byComponent(componentKey);
053        List<Violation> violations = Lists.newArrayList();
054        for (DefaultIssue issue : issues) {
055          violations.add(toViolation(issue));
056        }
057        return violations;
058      }
059    
060      public Violation toViolation(DefaultIssue issue) {
061        Rule rule = ruleFinder.findByKey(issue.ruleKey());
062        Resource resource = resourceCache.get(issue.componentKey());
063        Violation violation = new Violation(rule, resource);
064        violation.setNew(issue.isNew());
065        violation.setChecksum(issue.checksum());
066        violation.setMessage(issue.message());
067        violation.setCost(issue.effortToFix());
068        violation.setLineId(issue.line());
069        violation.setCreatedAt(issue.creationDate());
070        violation.setManual(issue.reporter() != null);
071        violation.setSeverity(RulePriority.valueOf(issue.severity()));
072        violation.setSwitchedOff(issue.resolution() != null);
073        return violation;
074      }
075    }