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 */
020package org.sonar.batch.protocol.output;
021
022import org.sonar.batch.protocol.ProtobufUtil;
023import org.sonar.batch.protocol.output.BatchReport.Issues;
024
025import java.io.File;
026import java.util.Collections;
027import java.util.List;
028
029public class BatchReportReader {
030
031  private final FileStructure fileStructure;
032
033  public BatchReportReader(File dir) {
034    this.fileStructure = new FileStructure(dir);
035  }
036
037  public BatchReport.Metadata readMetadata() {
038    File file = fileStructure.metadataFile();
039    if (!file.exists() || !file.isFile()) {
040      throw new IllegalStateException("Metadata file is missing in analysis report: " + file);
041    }
042    return ProtobufUtil.readFile(file, BatchReport.Metadata.PARSER);
043  }
044
045  public BatchReport.Component readComponent(int componentRef) {
046    File file = fileStructure.fileFor(FileStructure.Domain.COMPONENT, componentRef);
047    if (!file.exists() || !file.isFile()) {
048      throw new IllegalStateException("Unable to find report for component #" + componentRef + ". File does not exist: " + file);
049    }
050    return ProtobufUtil.readFile(file, BatchReport.Component.PARSER);
051  }
052
053  public List<BatchReport.Issue> readComponentIssues(int componentRef) {
054    File file = fileStructure.fileFor(FileStructure.Domain.ISSUES, componentRef);
055    if (file.exists() && file.isFile()) {
056      // all the issues are loaded in memory
057      BatchReport.Issues issues = ProtobufUtil.readFile(file, BatchReport.Issues.PARSER);
058      return issues.getListList();
059    }
060    return Collections.emptyList();
061  }
062
063  public Issues readDeletedComponentIssues(int deletedComponentRef) {
064    File file = fileStructure.fileFor(FileStructure.Domain.ISSUES_ON_DELETED, deletedComponentRef);
065    if (!file.exists() || !file.isFile()) {
066      throw new IllegalStateException("Unable to find report for deleted component #" + deletedComponentRef);
067    }
068    // all the issues are loaded in memory
069    return ProtobufUtil.readFile(file, Issues.PARSER);
070  }
071}