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