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 org.slf4j.LoggerFactory; 023 import org.sonar.api.batch.Decorator; 024 import org.sonar.api.batch.DecoratorContext; 025 import org.sonar.api.batch.Phase; 026 import org.sonar.api.resources.Project; 027 import org.sonar.api.resources.Resource; 028 import org.sonar.api.rules.RuleFinder; 029 import org.sonar.api.rules.RulePriority; 030 import org.sonar.api.rules.Violation; 031 import org.sonar.core.review.ReviewDao; 032 import org.sonar.core.review.ReviewDto; 033 import org.sonar.core.review.ReviewQuery; 034 035 import java.util.List; 036 037 @Phase(name = Phase.Name.PRE) 038 public class ManualViolationInjector implements Decorator { 039 040 private ReviewDao reviewDao; 041 private RuleFinder ruleFinder; 042 043 public ManualViolationInjector(ReviewDao reviewDao, RuleFinder ruleFinder) { 044 this.reviewDao = reviewDao; 045 this.ruleFinder = ruleFinder; 046 } 047 048 public boolean shouldExecuteOnProject(Project project) { 049 return true; 050 } 051 052 public void decorate(Resource resource, DecoratorContext context) { 053 if (resource.getId() != null) { 054 ReviewQuery query = ReviewQuery.create().setManualViolation(true).setResourceId(resource.getId()).setStatus(ReviewDto.STATUS_OPEN); 055 List<ReviewDto> reviewDtos = reviewDao.selectByQuery(query); 056 for (ReviewDto reviewDto : reviewDtos) { 057 if (reviewDto.getRuleId() == null) { 058 LoggerFactory.getLogger(getClass()).warn("No rule is defined on the review with id: " + reviewDto.getId()); 059 } 060 if (reviewDto.getViolationPermanentId() == null) { 061 LoggerFactory.getLogger(getClass()).warn("Permanent id of manual violation is missing on the review with id: " + reviewDto.getId()); 062 } 063 Violation violation = Violation.create(ruleFinder.findById(reviewDto.getRuleId()), resource); 064 violation.setManual(true); 065 violation.setLineId(reviewDto.getLine()); 066 violation.setPermanentId(reviewDto.getViolationPermanentId()); 067 violation.setSwitchedOff(false); 068 violation.setCreatedAt(reviewDto.getCreatedAt()); 069 violation.setMessage(reviewDto.getTitle()); 070 violation.setSeverity(RulePriority.valueOf(reviewDto.getSeverity())); 071 context.saveViolation(violation); 072 } 073 } 074 } 075 076 @Override 077 public String toString() { 078 return getClass().getSimpleName(); 079 } 080 }