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; 023 024import java.io.File; 025 026public class BatchReportWriter { 027 028 private final FileStructure fileStructure; 029 030 public BatchReportWriter(File dir) { 031 if (!dir.exists() && !dir.mkdirs()) { 032 throw new IllegalStateException("Unable to create directory: " + dir); 033 } 034 this.fileStructure = new FileStructure(dir); 035 } 036 037 FileStructure getFileStructure() { 038 return fileStructure; 039 } 040 041 public boolean hasComponentData(FileStructure.Domain domain, int componentRef) { 042 File file = fileStructure.fileFor(domain, componentRef); 043 return file.exists() && file.isFile(); 044 } 045 046 /** 047 * Metadata is mandatory 048 */ 049 public void writeMetadata(BatchReport.Metadata metadata) { 050 ProtobufUtil.writeToFile(metadata, fileStructure.metadataFile()); 051 } 052 053 public void writeComponent(BatchReport.Component component) { 054 File file = fileStructure.fileFor(FileStructure.Domain.COMPONENT, component.getRef()); 055 ProtobufUtil.writeToFile(component, file); 056 } 057 058 public void writeComponentIssues(int componentRef, Iterable<BatchReport.Issue> issues) { 059 BatchReport.Issues.Builder issuesBuilder = BatchReport.Issues.newBuilder(); 060 issuesBuilder.setComponentRef(componentRef); 061 issuesBuilder.addAllList(issues); 062 File file = fileStructure.fileFor(FileStructure.Domain.ISSUES, componentRef); 063 ProtobufUtil.writeToFile(issuesBuilder.build(), file); 064 } 065 066 /** 067 * Issues on components which have been deleted are stored in another location. 068 * Temporary hack, waiting for computation stack 069 */ 070 public void writeDeletedComponentIssues(int componentRef, String componentUuid, Iterable<BatchReport.Issue> issues) { 071 BatchReport.Issues.Builder issuesBuilder = BatchReport.Issues.newBuilder(); 072 issuesBuilder.setComponentRef(componentRef); 073 issuesBuilder.setComponentUuid(componentUuid); 074 issuesBuilder.addAllList(issues); 075 File file = fileStructure.fileFor(FileStructure.Domain.ISSUES_ON_DELETED, componentRef); 076 ProtobufUtil.writeToFile(issuesBuilder.build(), file); 077 } 078 079}