001/* 002 * SonarQube 003 * Copyright (C) 2009-2016 SonarSource SA 004 * mailto:contact AT sonarsource DOT com 005 * 006 * This program 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 * This program 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.input; 021 022import java.util.Collections; 023import java.util.Date; 024import java.util.HashMap; 025import java.util.Map; 026import javax.annotation.CheckForNull; 027import javax.annotation.Nullable; 028import org.sonar.batch.protocol.GsonHelper; 029 030/** 031 * Container for all project data going from server to batch. 032 * This is not an API since server and batch always share the same version. 033 */ 034public class ProjectRepositories { 035 036 private long timestamp; 037 private boolean exists; 038 private Map<String, Map<String, String>> settingsByModule = new HashMap<>(); 039 private Map<String, Map<String, FileData>> fileDataByModuleAndPath = new HashMap<>(); 040 private Date lastAnalysisDate; 041 042 public Map<String, String> settings(String moduleKey) { 043 return settingsByModule.containsKey(moduleKey) ? settingsByModule.get(moduleKey) : Collections.<String, String>emptyMap(); 044 } 045 046 public Map<String, Map<String, String>> settings() { 047 return settingsByModule; 048 } 049 050 public ProjectRepositories addSettings(String moduleKey, Map<String, String> settings) { 051 Map<String, String> existingSettings = settingsByModule.get(moduleKey); 052 if (existingSettings == null) { 053 existingSettings = new HashMap<>(); 054 settingsByModule.put(moduleKey, existingSettings); 055 } 056 existingSettings.putAll(settings); 057 return this; 058 } 059 060 public boolean exists() { 061 return exists; 062 } 063 064 public Map<String, Map<String, FileData>> fileDataByModuleAndPath() { 065 return fileDataByModuleAndPath; 066 } 067 068 public Map<String, FileData> fileDataByPath(String moduleKey) { 069 return fileDataByModuleAndPath.containsKey(moduleKey) ? fileDataByModuleAndPath.get(moduleKey) : Collections.<String, FileData>emptyMap(); 070 } 071 072 public ProjectRepositories addFileData(String moduleKey, @Nullable String path, FileData fileData) { 073 if (path == null || (fileData.hash() == null && fileData.revision() == null)) { 074 return this; 075 } 076 077 Map<String, FileData> existingFileDataByPath = fileDataByModuleAndPath.get(moduleKey); 078 if (existingFileDataByPath == null) { 079 existingFileDataByPath = new HashMap<>(); 080 fileDataByModuleAndPath.put(moduleKey, existingFileDataByPath); 081 } 082 existingFileDataByPath.put(path, fileData); 083 return this; 084 } 085 086 @CheckForNull 087 public FileData fileData(String projectKey, String path) { 088 return fileDataByPath(projectKey).get(path); 089 } 090 091 public long timestamp() { 092 return timestamp; 093 } 094 095 public void setTimestamp(long timestamp) { 096 this.timestamp = timestamp; 097 } 098 099 @CheckForNull 100 public Date lastAnalysisDate() { 101 return lastAnalysisDate; 102 } 103 104 public void setLastAnalysisDate(@Nullable Date lastAnalysisDate) { 105 this.lastAnalysisDate = lastAnalysisDate; 106 } 107 108 public String toJson() { 109 return GsonHelper.create().toJson(this); 110 } 111 112 public static ProjectRepositories fromJson(String json) { 113 return GsonHelper.create().fromJson(json, ProjectRepositories.class); 114 } 115}