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     */
020    package org.sonar.plugins.core.sensors;
021    
022    import com.google.common.collect.Maps;
023    import org.sonar.api.batch.*;
024    import org.sonar.api.resources.Project;
025    import org.sonar.api.resources.Resource;
026    import org.sonar.api.rules.RulePriority;
027    import org.sonar.api.rules.Violation;
028    import org.sonar.core.review.ReviewDao;
029    import org.sonar.core.review.ReviewDto;
030    import org.sonar.core.review.ReviewPredicates;
031    import org.sonar.plugins.core.timemachine.ViolationTrackingDecorator;
032    
033    import java.util.Collection;
034    import java.util.List;
035    import java.util.Map;
036    
037    /**
038     * Severity of violations can be explicitely changed by end-users. In this case the severity is fixed and must not be changed
039     * by rule engines.
040     *
041     * @since 2.13
042     */
043    @DependsUpon(DecoratorBarriers.START_VIOLATION_TRACKING)
044    @DependedUpon(DecoratorBarriers.END_OF_VIOLATION_TRACKING)
045    public class ViolationSeverityUpdater implements Decorator {
046    
047      private ReviewDao reviewDao;
048    
049      public ViolationSeverityUpdater(ReviewDao reviewDao) {
050        this.reviewDao = reviewDao;
051      }
052    
053      public boolean shouldExecuteOnProject(Project project) {
054        return true;
055      }
056    
057      @DependsUpon
058      public Class dependsUponViolationTracking() {
059        // permanent ids of violations have been updated, so we can link them with reviews
060        return ViolationTrackingDecorator.class;
061      }
062    
063      public void decorate(Resource resource, DecoratorContext context) {
064        if (resource.getId()==null) {
065          return;
066        }
067        Map<Integer, Violation> violationMap = filterViolationsPerPermanent(context.getViolations());
068        if (!violationMap.isEmpty()) {
069          Collection<ReviewDto> reviews = selectReviewsWithManualSeverity(resource.getId());
070          for (ReviewDto review : reviews) {
071            Violation violation = violationMap.get(review.getViolationPermanentId());
072            if (violation != null) {
073              violation.setSeverity(RulePriority.valueOf(review.getSeverity()));
074            }
075          }
076        }
077      }
078    
079      private Collection<ReviewDto> selectReviewsWithManualSeverity(long resourceId) {
080        return reviewDao.selectOpenByResourceId(resourceId, ReviewPredicates.manualSeverity());
081      }
082    
083      private Map<Integer, Violation> filterViolationsPerPermanent(List<Violation> violations) {
084        Map<Integer, Violation> result = Maps.newHashMap();
085        for (Violation violation : violations) {
086          if (violation.getPermanentId() != null) {
087            result.put(violation.getPermanentId(), violation);
088          }
089        }
090        return result;
091      }
092    
093      @Override
094      public String toString() {
095        return getClass().getSimpleName();
096      }
097    }