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 org.sonar.batch.protocol.GsonHelper;
023
024import javax.annotation.CheckForNull;
025import javax.annotation.Nullable;
026
027import java.util.*;
028
029/**
030 * Container for all project data going from server to batch.
031 * This is not an API since server and batch always share the same version.
032 */
033public class ProjectRepositories {
034
035  private long timestamp;
036  private Map<String, QProfile> qprofilesByLanguage = new HashMap<String, QProfile>();
037  private Collection<ActiveRule> activeRules = new ArrayList<ActiveRule>();
038  private Map<String, Map<String, String>> settingsByModule = new HashMap<String, Map<String, String>>();
039  private Map<String, Map<String, FileData>> fileDataByModuleAndPath = new HashMap<String, Map<String, FileData>>();
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 ProjectRepositories addSettings(String moduleKey, Map<String, String> settings) {
047    Map<String, String> existingSettings = settingsByModule.get(moduleKey);
048    if (existingSettings == null) {
049      existingSettings = new HashMap<>();
050      settingsByModule.put(moduleKey, existingSettings);
051    }
052    existingSettings.putAll(settings);
053    return this;
054  }
055
056  public Collection<QProfile> qProfiles() {
057    return qprofilesByLanguage.values();
058  }
059
060  public ProjectRepositories addQProfile(QProfile qProfile) {
061    qprofilesByLanguage.put(qProfile.language(), qProfile);
062    return this;
063  }
064
065  public Collection<ActiveRule> activeRules() {
066    return activeRules;
067  }
068
069  public ProjectRepositories addActiveRule(ActiveRule activeRule) {
070    activeRules.add(activeRule);
071    return this;
072  }
073
074  public Map<String, FileData> fileDataByPath(String moduleKey) {
075    return fileDataByModuleAndPath.containsKey(moduleKey) ? fileDataByModuleAndPath.get(moduleKey) : Collections.<String, FileData>emptyMap();
076  }
077
078  public ProjectRepositories addFileData(String moduleKey, String path, FileData fileData) {
079    Map<String, FileData> existingFileDataByPath = fileDataByModuleAndPath.get(moduleKey);
080    if (existingFileDataByPath == null) {
081      existingFileDataByPath = new HashMap<>();
082      fileDataByModuleAndPath.put(moduleKey, existingFileDataByPath);
083    }
084    existingFileDataByPath.put(path, fileData);
085    return this;
086  }
087
088  @CheckForNull
089  public FileData fileData(String projectKey, String path) {
090    return fileDataByPath(projectKey).get(path);
091  }
092
093  public long timestamp() {
094    return timestamp;
095  }
096
097  public void setTimestamp(long timestamp) {
098    this.timestamp = timestamp;
099  }
100
101  @CheckForNull
102  public Date lastAnalysisDate() {
103    return lastAnalysisDate;
104  }
105
106  public void setLastAnalysisDate(@Nullable Date lastAnalysisDate) {
107    this.lastAnalysisDate = lastAnalysisDate;
108  }
109
110  public String toJson() {
111    return GsonHelper.create().toJson(this);
112  }
113
114  public static ProjectRepositories fromJson(String json) {
115    return GsonHelper.create().fromJson(json, ProjectRepositories.class);
116  }
117
118}