001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 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     */
020    package org.sonar.batch.issue;
021    
022    import com.google.common.collect.Lists;
023    import org.sonar.api.component.Component;
024    import org.sonar.api.issue.Issuable;
025    import org.sonar.api.issue.Issue;
026    import org.sonar.api.issue.internal.DefaultIssue;
027    import org.sonar.api.resources.Project;
028    import org.sonar.core.issue.DefaultIssueBuilder;
029    
030    import java.util.List;
031    
032    /**
033     * @since 3.6
034     */
035    public class DefaultIssuable implements Issuable {
036    
037      private final ModuleIssues moduleIssues;
038      private final IssueCache cache;
039      private final Component component;
040      private final Project project;
041    
042      DefaultIssuable(Component component, Project project, ModuleIssues moduleIssues, IssueCache cache) {
043        this.component = component;
044        this.project = project;
045        this.moduleIssues = moduleIssues;
046        this.cache = cache;
047      }
048    
049      @Override
050      public IssueBuilder newIssueBuilder() {
051        return new DefaultIssueBuilder().componentKey(component.key()).projectKey(project.getKey());
052      }
053    
054      @Override
055      public boolean addIssue(Issue issue) {
056        return moduleIssues.initAndAddIssue((DefaultIssue) issue);
057      }
058    
059      @SuppressWarnings("unchecked")
060      @Override
061      public List<Issue> resolvedIssues() {
062        List<Issue> result = Lists.newArrayList();
063        for (DefaultIssue issue : cache.byComponent(component.key())) {
064          if (issue.resolution()!=null) {
065            result.add(issue);
066          }
067        }
068        return result;
069      }
070    
071      @SuppressWarnings("unchecked")
072      @Override
073      public List<Issue> issues() {
074        List<Issue> result = Lists.newArrayList();
075        for (DefaultIssue issue : cache.byComponent(component.key())) {
076          if (issue.resolution()==null) {
077            result.add(issue);
078          }
079        }
080        return result;
081      }
082    
083      @Override
084      public Component component() {
085        return component;
086      }
087    }