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  /**
051   * Indexes a resource. This method does nothing if the resource is already indexed.
052   *
053   * @param resource        the resource to index. Not nullable
054   * @param parentReference a reference to the indexed parent. If null, the resource is indexed as a direct child of project.
055   * @return false if the parent is not indexed or if the resource is excluded
056   * @since 2.6
057   */
058  public abstract boolean index(Resource resource, Resource parentReference);
059
060  /**
061   * Returns true if the referenced resource is excluded. An excluded resource is not indexed.
062   * @since 2.6
063   */
064  public abstract boolean isExcluded(Resource reference);
065
066  /**
067   * @since 2.6
068   */
069  public abstract 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  public abstract <R extends Resource> R getResource(R reference);
079
080  /**
081   * @since 2.6
082   */
083  public abstract Resource getParent(Resource reference);
084
085  /**
086   * @since 2.6
087   */
088
089  public abstract Collection<Resource> getChildren(Resource reference);
090
091  /**
092   * Save the source code of a file. The file must be have been indexed before.
093   * Note: the source stream is not closed.
094   *
095   * @throws org.sonar.api.resources.DuplicatedSourceException
096   *          if the source has already been set on this resource
097   */
098  public abstract void setSource(Resource reference, String source);
099
100  /**
101   * @return source code associated with a specified resource, <code>null</code> if not available
102   * @since 2.9
103   */
104  public abstract String getSource(Resource resource);
105
106  public abstract Project getProject();
107
108  public final Collection<Resource> getResources() {
109    return getVertices();
110  }
111
112  /**
113   * Indexes the resource.
114   * @return the indexed resource, even if it's excluded
115   * @deprecated since 2.6. Use methods index()
116   */
117  @Deprecated
118  public abstract Resource addResource(Resource resource);
119
120  public abstract Measure getMeasure(Resource resource, Metric metric);
121
122  public abstract <M> M getMeasures(Resource resource, MeasuresFilter<M> filter);
123
124  /**
125   * Returns the violations that match the {@link ViolationQuery} parameters.
126   *
127   * @since 2.8
128   * @param violationQuery
129   *          the request parameters specified as a {@link ViolationQuery}
130   * @return the list of violations that match those parameters
131   * @deprecated in 3.6
132   */
133  @Deprecated
134  public abstract List<Violation> getViolations(ViolationQuery violationQuery);
135
136  /**
137   * Returns all the active (= non switched-off) violations found on the given resource. Equivalent to
138   * {@link #getViolations(ViolationQuery)} called with <code>ViolationQuery.create().forResource(resource).ignoreSwitchedOff(true)</code>
139   * as a parameter.
140   *
141   * @since 2.7
142   * @return the list of violations
143   * @deprecated in 3.6
144   */
145  @Deprecated
146  public final List<Violation> getViolations(Resource resource) {
147    return getViolations(ViolationQuery.create().forResource(resource));
148  }
149
150  /**
151   * @since 2.5
152   * @deprecated in 3.6
153   */
154  @Deprecated
155  public abstract void addViolation(Violation violation, boolean force);
156
157  /**
158   * @deprecated in 3.6
159   */
160  @Deprecated
161  public final void addViolation(Violation violation) {
162    addViolation(violation, false);
163  }
164
165  /**
166   * Warning: the resource is automatically indexed for backward-compatibility, but it should be explictly
167   * indexed before. Next versions will deactivate this automatic indexation.
168   */
169  public abstract Measure addMeasure(Resource resource, Measure measure);
170
171  public abstract Dependency addDependency(Dependency dependency);
172
173  public abstract Set<Dependency> getDependencies();
174
175  public abstract void addLink(ProjectLink link);
176
177  public abstract void deleteLink(String key);
178
179  public abstract List<Event> getEvents(Resource resource);
180
181  public abstract void deleteEvent(Event event);
182
183  public abstract Event addEvent(Resource resource, String name, String description, String category, Date date);
184
185  public final Collection<Dependency> getOutgoingDependencies(Resource from) {
186    return getOutgoingEdges(from);
187  }
188
189  public final Collection<Dependency> getIncomingDependencies(Resource to) {
190    return getIncomingEdges(to);
191  }
192}