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.batch;
021    
022    import org.sonar.api.batch.Event;
023    import org.sonar.api.batch.SensorContext;
024    import org.sonar.api.batch.SonarIndex;
025    import org.sonar.api.design.Dependency;
026    import org.sonar.api.measures.Measure;
027    import org.sonar.api.measures.MeasuresFilter;
028    import org.sonar.api.measures.Metric;
029    import org.sonar.api.resources.*;
030    import org.sonar.api.rules.Violation;
031    
032    import java.util.Collection;
033    import java.util.Date;
034    import java.util.List;
035    import java.util.Set;
036    
037    public class DefaultSensorContext implements SensorContext {
038    
039      private SonarIndex index;
040      private Project project;
041    
042      public DefaultSensorContext(SonarIndex index, Project project) {
043        this.index = index;
044        this.project = project;
045      }
046    
047      public Project getProject() {
048        return project;
049      }
050    
051      public boolean index(Resource resource) {
052        return index.index(resource);
053      }
054    
055      public boolean index(Resource resource, Resource parentReference) {
056        return index.index(resource, parentReference);
057      }
058    
059      public boolean isExcluded(Resource reference) {
060        return index.isExcluded(reference);
061      }
062    
063      public boolean isIndexed(Resource reference, boolean acceptExcluded) {
064        return index.isIndexed(reference, acceptExcluded);
065      }
066    
067      public Resource getParent(Resource reference) {
068        return index.getParent(reference);
069      }
070    
071      public Collection<Resource> getChildren(Resource reference) {
072        return index.getChildren(reference);
073      }
074    
075      public Measure getMeasure(Metric metric) {
076        return index.getMeasure(project, metric);
077      }
078    
079      public <M> M getMeasures(MeasuresFilter<M> filter) {
080        return index.getMeasures(project, filter);
081      }
082    
083      public Measure saveMeasure(Measure measure) {
084        return index.addMeasure(project, measure);
085      }
086    
087      public Measure saveMeasure(Metric metric, Double value) {
088        return index.addMeasure(project, new Measure(metric, value));
089      }
090    
091      public Measure getMeasure(Resource resource, Metric metric) {
092        return index.getMeasure(resource, metric);
093      }
094    
095      public String saveResource(Resource resource) {
096        Resource persistedResource = index.addResource(resource);
097        if (persistedResource != null) {
098          return persistedResource.getEffectiveKey();
099        }
100        return null;
101      }
102    
103      public boolean saveResource(Resource resource, Resource parentReference) {
104        return index.index(resource, parentReference);
105      }
106    
107      public Resource getResource(Resource resource) {
108        return index.getResource(resource);
109      }
110    
111      public <M> M getMeasures(Resource resource, MeasuresFilter<M> filter) {
112        return index.getMeasures(resource, filter);
113      }
114    
115      public Measure saveMeasure(Resource resource, Metric metric, Double value) {
116        return index.addMeasure(resourceOrProject(resource), new Measure(metric, value));
117      }
118    
119      public Measure saveMeasure(Resource resource, Measure measure) {
120        return index.addMeasure(resourceOrProject(resource), measure);
121      }
122    
123      public void saveViolation(Violation violation, boolean force) {
124        if (violation.getResource() == null) {
125          violation.setResource(resourceOrProject(violation.getResource()));
126        }
127        index.addViolation(violation, force);
128      }
129    
130      public void saveViolation(Violation violation) {
131        saveViolation(violation, false);
132      }
133    
134      public void saveViolations(Collection<Violation> violations) {
135        if (violations != null) {
136          for (Violation violation : violations) {
137            saveViolation(violation);
138          }
139        }
140      }
141    
142      public Dependency saveDependency(Dependency dependency) {
143        return index.addDependency(dependency);
144      }
145    
146      public Set<Dependency> getDependencies() {
147        return index.getDependencies();
148      }
149    
150      public Collection<Dependency> getIncomingDependencies(Resource to) {
151        return index.getIncomingEdges(resourceOrProject(to));
152      }
153    
154      public Collection<Dependency> getOutgoingDependencies(Resource from) {
155        return index.getOutgoingEdges(resourceOrProject(from));
156      }
157    
158      public void saveSource(Resource reference, String source) {
159        index.setSource(reference, source);
160      }
161    
162      public void saveLink(ProjectLink link) {
163        index.addLink(link);
164      }
165    
166      public void deleteLink(String key) {
167        index.deleteLink(key);
168      }
169    
170      public List<Event> getEvents(Resource resource) {
171        return index.getEvents(resource);
172      }
173    
174      public Event createEvent(Resource resource, String name, String description, String category, Date date) {
175        return index.addEvent(resource, name, description, category, date);
176      }
177    
178      public void deleteEvent(Event event) {
179        index.deleteEvent(event);
180      }
181    
182      private Resource resourceOrProject(Resource resource) {
183        return (resource != null ? resource : project);
184      }
185    }