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.api.batch;
021    
022    import org.sonar.api.design.Dependency;
023    import org.sonar.api.measures.Measure;
024    import org.sonar.api.measures.MeasuresFilter;
025    import org.sonar.api.measures.Metric;
026    import org.sonar.api.resources.DuplicatedSourceException;
027    import org.sonar.api.resources.Project;
028    import org.sonar.api.resources.ProjectLink;
029    import org.sonar.api.resources.Resource;
030    import org.sonar.api.rules.Violation;
031    import org.sonar.api.violations.ViolationQuery;
032    import org.sonar.graph.DirectedGraphAccessor;
033    
034    import java.util.Collection;
035    import java.util.Date;
036    import java.util.List;
037    import java.util.Set;
038    
039    public abstract class SonarIndex implements DirectedGraphAccessor<Resource, Dependency> {
040    
041      /**
042       * Indexes a resource as a direct child of project. This method does nothing and returns true if the resource already indexed.
043       * If the method resource.getParent() does not return null, then this parent will be indexed too.
044       *
045       * @return false if the resource is excluded
046       * @since 2.6
047       */
048      public abstract boolean index(Resource resource);
049    
050    
051      /**
052       * Indexes a resource. This method does nothing if the resource is already indexed.
053       *
054       * @param resource        the resource to index. Not nullable
055       * @param parentReference a reference to the indexed parent. If null, the resource is indexed as a direct child of project.
056       * @return false if the parent is not indexed or if the resource is excluded
057       * @since 2.6
058       */
059      public abstract boolean index(Resource resource, Resource parentReference);
060    
061      /**
062       * Returns true if the referenced resource is excluded. An excluded resource is not indexed.
063       * @since 2.6
064       */
065      public abstract boolean isExcluded(Resource reference);
066    
067      /**
068       * @since 2.6
069       */
070      public abstract boolean isIndexed(Resource reference, boolean acceptExcluded);
071    
072      /**
073       * Search for an indexed resource.
074       *
075       * @param reference the resource reference
076       * @return the indexed resource, null if it's not indexed
077       * @since 1.10. Generic types since 2.6.
078       */
079      public abstract <R extends Resource> R getResource(R reference);
080    
081      /**
082       * @since 2.6
083       */
084      public abstract Resource getParent(Resource reference);
085    
086      /**
087       * @since 2.6
088       */
089    
090      public abstract Collection<Resource> getChildren(Resource reference);
091    
092      /**
093       * Save the source code of a file. The file must be have been indexed before.
094       * Note: the source stream is not closed.
095       *
096       * @return false if the resource is excluded or not indexed
097       * @throws org.sonar.api.resources.DuplicatedSourceException
098       *          if the source has already been set on this resource
099       */
100      public abstract void setSource(Resource reference, String source) throws DuplicatedSourceException;
101    
102      public abstract Project getProject();
103    
104      public final Collection<Resource> getResources() {
105        return getVertices();
106      }
107    
108      /**
109       * Indexes the resource.
110       * @return the indexed resource, even if it's excluded
111       * @deprecated since 2.6. Use methods index()
112       */
113      @Deprecated
114      public abstract Resource addResource(Resource resource);
115    
116      public abstract Measure getMeasure(Resource resource, Metric metric);
117    
118      public abstract <M> M getMeasures(Resource resource, MeasuresFilter<M> filter);
119    
120      /**
121       * Returns the violations that match the {@link ViolationQuery} parameters.
122       * 
123       * @since 2.8
124       * @param violationQuery
125       *          the request parameters specified as a {@link ViolationQuery}
126       * @return the list of violations that match those parameters
127       */
128      public abstract List<Violation> getViolations(ViolationQuery violationQuery);
129    
130      /**
131       * Returns all the active (= non switched-off) violations found on the given resource. Equivalent to
132       * {@link #getViolations(ViolationQuery)} called with <code>ViolationQuery.create().forResource(resource).ignoreSwitchedOff(true)</code>
133       * as a parameter.
134       * 
135       * @since 2.7
136       * @param the
137       *          resource on which violations are searched
138       * @return the list of violations
139       */
140      public final List<Violation> getViolations(Resource resource) {
141        return getViolations(ViolationQuery.create().forResource(resource));
142      }
143    
144      /**
145       * @since 2.5
146       */
147      public abstract void addViolation(Violation violation, boolean force);
148    
149      public final void addViolation(Violation violation) {
150        addViolation(violation, false);
151      }
152    
153      /**
154       * Warning: the resource is automatically indexed for backward-compatibility, but it should be explictly
155       * indexed before. Next versions will deactivate this automatic indexation.
156       *
157       * @throws SonarException if the metric is unknown.
158       */
159      public abstract Measure addMeasure(Resource resource, Measure measure);
160    
161      public abstract Dependency addDependency(Dependency dependency);
162    
163      public abstract Set<Dependency> getDependencies();
164    
165      public abstract void addLink(ProjectLink link);
166    
167      public abstract void deleteLink(String key);
168    
169      public abstract List<Event> getEvents(Resource resource);
170    
171      public abstract void deleteEvent(Event event);
172    
173      public abstract Event addEvent(Resource resource, String name, String description, String category, Date date);
174    
175      public final Collection<Dependency> getOutgoingDependencies(Resource from) {
176        return getOutgoingEdges(from);
177      }
178    
179      public final Collection<Dependency> getIncomingDependencies(Resource to) {
180        return getIncomingEdges(to);
181      }
182    }