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 */ 020package org.sonar.api.batch; 021 022import org.sonar.api.design.Dependency; 023import org.sonar.api.measures.Measure; 024import org.sonar.api.measures.MeasuresFilter; 025import org.sonar.api.measures.Metric; 026import org.sonar.api.resources.ProjectLink; 027import org.sonar.api.resources.Resource; 028import org.sonar.api.rules.Violation; 029 030import java.util.Collection; 031import java.util.Date; 032import java.util.List; 033import java.util.Set; 034 035/** 036 * @since 1.10 037 */ 038public interface SensorContext { 039 040 /** 041 * Indexes a resource as a direct child of project. This method does nothing and returns true if the resource already indexed. 042 * 043 * @return false if the resource is excluded 044 * @since 2.6 045 */ 046 boolean index(Resource resource); 047 048 049 /** 050 * Indexes a resource. This method does nothing if the resource is already indexed. 051 * 052 * @param resource the resource to index. Not nullable 053 * @param parentReference a reference to the parent. If null, the the resource is indexed as a direct child of project. 054 * @return false if the parent is not indexed or if the resource is excluded 055 * @since 2.6 056 */ 057 boolean index(Resource resource, Resource parentReference); 058 059 /** 060 * Returns true if the referenced resource is indexed and excluded. 061 * @since 2.6 062 */ 063 boolean isExcluded(Resource reference); 064 065 /** 066 * Returns true if the referenced resource is indexed. 067 * @since 2.6 068 */ 069 boolean isIndexed(Resource reference, boolean acceptExcluded); 070 071 /** 072 * Search for an indexed resource. 073 * 074 * @param reference the resource reference 075 * @return the indexed resource, null if it's not indexed 076 * @since 1.10. Generic types since 2.6. 077 */ 078 <R extends Resource> R getResource(R reference); 079 080 /** 081 * @since 2.6 082 */ 083 Resource getParent(Resource reference); 084 085 /** 086 * @since 2.6 087 */ 088 089 Collection<Resource> getChildren(Resource reference); 090 091 // ----------- MEASURES ON PROJECT -------------- 092 093 /** 094 * Find a project measure 095 */ 096 Measure getMeasure(Metric metric); 097 098 /** 099 * All measures of the project. Never return null. 100 */ 101 <M> M getMeasures(MeasuresFilter<M> filter); 102 103 /** 104 * Add a measure on project 105 */ 106 Measure saveMeasure(Measure measure); 107 108 /** 109 * Add a measure on project 110 */ 111 Measure saveMeasure(Metric metric, Double value); 112 113 // ----------- MEASURES ON RESOURCES -------------- 114 115 /** 116 * Find a measure for this project 117 */ 118 Measure getMeasure(Resource resource, Metric metric); 119 120 /** 121 * Key is updated when saving the resource. 122 * 123 * @return the key as saved in database. Null if the resource is set as excluded. 124 * @deprecated use the methods index() 125 */ 126 @Deprecated 127 String saveResource(Resource resource); 128 129 130 /** 131 * Find all measures for this project. Never return null. 132 */ 133 <M> M getMeasures(Resource resource, MeasuresFilter<M> filter); 134 135 /** 136 * Add or update a measure. 137 * <p> 138 * The resource is automatically saved, so there is no need to execute the method saveResource(). Does nothing if the resource is set as 139 * excluded. 140 * </p> 141 */ 142 Measure saveMeasure(Resource resource, Metric metric, Double value); 143 144 /** 145 * Add or update a measure. 146 * <p> 147 * The resource is automatically saved, so there is no need to execute the method saveResource(). Does nothing if the resource is set as 148 * excluded. 149 * </p> 150 */ 151 Measure saveMeasure(Resource resource, Measure measure); 152 153 // ----------- RULE VIOLATIONS -------------- 154 155 /** 156 * Save a coding rule violation. 157 * 158 * @param force allows to force creation of violation even if it was supressed by {@link org.sonar.api.rules.ViolationFilter} 159 * @since 2.5 160 */ 161 void saveViolation(Violation violation, boolean force); 162 163 /** 164 * Save a coding rule violation. 165 */ 166 void saveViolation(Violation violation); 167 168 /** 169 * Saves a list of violations. 170 */ 171 void saveViolations(Collection<Violation> violations); 172 173 // ----------- DEPENDENCIES BETWEEN RESOURCES -------------- 174 175 /** 176 * Build a new dependency : from depends upon to. The dependency is NOT saved. The method saveDependency() must still be executed. 177 */ 178 Dependency saveDependency(Dependency dependency); 179 180 Set<Dependency> getDependencies(); 181 182 Collection<Dependency> getIncomingDependencies(Resource to); 183 184 Collection<Dependency> getOutgoingDependencies(Resource from); 185 186 // ----------- FILE SOURCES -------------- 187 188 /** 189 * Save the source code of a file. The file must be have been indexed before. 190 * 191 * @return false if the resource is excluded or not indexed 192 * @throws org.sonar.api.resources.DuplicatedSourceException 193 * if the source has already been set on this resource 194 * @since 1.10. Returns a boolean since 2.6. 195 */ 196 void saveSource(Resource reference, String source); 197 198 // ----------- LINKS -------------- 199 200 /** 201 * add a link to an external page like project homepage, sources (subversion, ...), continuous integration server... Example : 202 * context.addLink(new ProjectLink("maven_site, "Maven site", "http://my.maven.com) 203 */ 204 void saveLink(ProjectLink link); 205 206 /** 207 * remove a link. It does not fail if key is unknown. 208 */ 209 void deleteLink(String key); 210 211 // ----------- EVENTS -------------- 212 213 /** 214 * @param resource set null for project events 215 */ 216 List<Event> getEvents(Resource resource); 217 218 /** 219 * Creates an event for a given date 220 * 221 * @param name the event name 222 * @param description the event description 223 * @param category the event category 224 * @param date the event date 225 * @return the created event 226 */ 227 Event createEvent(Resource resource, String name, String description, String category, Date date); 228 229 /** 230 * Deletes an event 231 * 232 * @param event the event to delete 233 */ 234 void deleteEvent(Event event); 235 236}