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