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