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.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, String path, FileData fileData) {
073    Map<String, FileData> existingFileDataByPath = fileDataByModuleAndPath.get(moduleKey);
074    if (existingFileDataByPath == null) {
075      existingFileDataByPath = new HashMap<>();
076      fileDataByModuleAndPath.put(moduleKey, existingFileDataByPath);
077    }
078    existingFileDataByPath.put(path, fileData);
079    return this;
080  }
081
082  @CheckForNull
083  public FileData fileData(String projectKey, String path) {
084    return fileDataByPath(projectKey).get(path);
085  }
086
087  public long timestamp() {
088    return timestamp;
089  }
090
091  public void setTimestamp(long timestamp) {
092    this.timestamp = timestamp;
093  }
094
095  @CheckForNull
096  public Date lastAnalysisDate() {
097    return lastAnalysisDate;
098  }
099
100  public void setLastAnalysisDate(@Nullable Date lastAnalysisDate) {
101    this.lastAnalysisDate = lastAnalysisDate;
102  }
103
104  public String toJson() {
105    return GsonHelper.create().toJson(this);
106  }
107
108  public static ProjectRepositories fromJson(String json) {
109    return GsonHelper.create().fromJson(json, ProjectRepositories.class);
110  }
111}