001/* 002 * SonarQube, open source software quality management tool. 003 * Copyright (C) 2008-2013 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * SonarQube 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 * SonarQube 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 License 017 * along with this program; if not, write to the Free Software Foundation, 018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 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.Project; 027import org.sonar.api.resources.ProjectLink; 028import org.sonar.api.resources.Resource; 029import org.sonar.api.rules.Violation; 030import org.sonar.api.violations.ViolationQuery; 031import org.sonar.graph.DirectedGraphAccessor; 032 033import java.util.Collection; 034import java.util.Date; 035import java.util.List; 036import java.util.Set; 037 038public abstract class SonarIndex implements DirectedGraphAccessor<Resource, Dependency> { 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 * If the method resource.getParent() does not return null, then this parent will be indexed too. 043 * 044 * @return false if the resource is excluded 045 * @since 2.6 046 */ 047 public abstract boolean index(Resource resource); 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 indexed parent. If null, 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 public abstract boolean index(Resource resource, Resource parentReference); 058 059 /** 060 * Returns true if the referenced resource is excluded. An excluded resource is not indexed. 061 * @since 2.6 062 */ 063 public abstract boolean isExcluded(Resource reference); 064 065 /** 066 * @since 2.6 067 */ 068 public abstract boolean isIndexed(Resource reference, boolean acceptExcluded); 069 070 /** 071 * Search for an indexed resource. 072 * 073 * @param reference the resource reference 074 * @return the indexed resource, null if it's not indexed 075 * @since 1.10. Generic types since 2.6. 076 */ 077 public abstract <R extends Resource> R getResource(R reference); 078 079 /** 080 * @since 2.6 081 */ 082 public abstract Resource getParent(Resource reference); 083 084 /** 085 * @since 2.6 086 */ 087 088 public abstract Collection<Resource> getChildren(Resource reference); 089 090 /** 091 * Save the source code of a file. The file must be have been indexed before. 092 * Note: the source stream is not closed. 093 * 094 * @throws org.sonar.api.resources.DuplicatedSourceException 095 * if the source has already been set on this resource 096 * @deprecated since 4.2 should not be used by plugins 097 */ 098 @Deprecated 099 public abstract void setSource(Resource reference, String source); 100 101 /** 102 * @return source code associated with a specified resource, <code>null</code> if not available 103 * @since 2.9 104 */ 105 public abstract String getSource(Resource resource); 106 107 public abstract Project getProject(); 108 109 public final Collection<Resource> getResources() { 110 return getVertices(); 111 } 112 113 /** 114 * Indexes the resource. 115 * @return the indexed resource, even if it's excluded 116 * @deprecated since 2.6. Use methods index() 117 */ 118 @Deprecated 119 public abstract Resource addResource(Resource resource); 120 121 public abstract Measure getMeasure(Resource resource, Metric metric); 122 123 public abstract <M> M getMeasures(Resource resource, MeasuresFilter<M> filter); 124 125 /** 126 * Returns the violations that match the {@link ViolationQuery} parameters. 127 * 128 * @since 2.8 129 * @param violationQuery 130 * the request parameters specified as a {@link ViolationQuery} 131 * @return the list of violations that match those parameters 132 * @deprecated in 3.6 133 */ 134 @Deprecated 135 public abstract List<Violation> getViolations(ViolationQuery violationQuery); 136 137 /** 138 * Returns all the active (= non switched-off) violations found on the given resource. Equivalent to 139 * {@link #getViolations(ViolationQuery)} called with <code>ViolationQuery.create().forResource(resource).ignoreSwitchedOff(true)</code> 140 * as a parameter. 141 * 142 * @since 2.7 143 * @return the list of violations 144 * @deprecated in 3.6 145 */ 146 @Deprecated 147 public final List<Violation> getViolations(Resource resource) { 148 return getViolations(ViolationQuery.create().forResource(resource)); 149 } 150 151 /** 152 * @since 2.5 153 * @deprecated in 3.6 154 */ 155 @Deprecated 156 public abstract void addViolation(Violation violation, boolean force); 157 158 /** 159 * @deprecated in 3.6 160 */ 161 @Deprecated 162 public final void addViolation(Violation violation) { 163 addViolation(violation, false); 164 } 165 166 /** 167 * Warning: the resource is automatically indexed for backward-compatibility, but it should be explictly 168 * indexed before. Next versions will deactivate this automatic indexation. 169 */ 170 public abstract Measure addMeasure(Resource resource, Measure measure); 171 172 public abstract Dependency addDependency(Dependency dependency); 173 174 public abstract Set<Dependency> getDependencies(); 175 176 public abstract void addLink(ProjectLink link); 177 178 public abstract void deleteLink(String key); 179 180 public abstract List<Event> getEvents(Resource resource); 181 182 public abstract void deleteEvent(Event event); 183 184 public abstract Event addEvent(Resource resource, String name, String description, String category, Date date); 185 186 public final Collection<Dependency> getOutgoingDependencies(Resource from) { 187 return getOutgoingEdges(from); 188 } 189 190 public final Collection<Dependency> getIncomingDependencies(Resource to) { 191 return getIncomingEdges(to); 192 } 193}