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.sonar.api.batch.Decorator;
023    import org.sonar.api.batch.DecoratorContext;
024    import org.sonar.api.batch.Phase;
025    import org.sonar.api.database.DatabaseSession;
026    import org.sonar.api.measures.Measure;
027    import org.sonar.api.measures.MetricFinder;
028    import org.sonar.api.resources.Project;
029    import org.sonar.api.resources.Resource;
030    import org.sonar.jpa.entity.ManualMeasure;
031    
032    import java.util.List;
033    
034    @Phase(name = Phase.Name.PRE)
035    public class ManualMeasureDecorator implements Decorator {
036    
037      private DatabaseSession session;
038      private MetricFinder metricFinder;
039    
040      public ManualMeasureDecorator(DatabaseSession session, MetricFinder metricFinder) {
041        this.session = session;
042        this.metricFinder = metricFinder;
043      }
044    
045      public boolean shouldExecuteOnProject(Project project) {
046        return true;
047      }
048    
049      public void decorate(Resource resource, DecoratorContext context) {
050        if (resource.getId() != null) {
051          List<ManualMeasure> manualMeasures = session.getResults(ManualMeasure.class, "resourceId", resource.getId());
052          for (ManualMeasure manualMeasure : manualMeasures) {
053            context.saveMeasure(copy(manualMeasure));
054          }
055        }
056      }
057    
058      private Measure copy(ManualMeasure manualMeasure) {
059        Measure measure = new Measure(metricFinder.findById(manualMeasure.getMetricId()));
060        measure.setValue(manualMeasure.getValue(), 5);
061        measure.setData(manualMeasure.getTextValue());
062        measure.setDescription(manualMeasure.getDescription());
063        return measure;
064      }
065    
066      @Override
067      public String toString() {
068        return getClass().getSimpleName();
069      }
070    }