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 */
020 package org.sonar.batch.rule;
021
022 import com.google.common.collect.Sets;
023 import com.google.gson.JsonArray;
024 import com.google.gson.JsonElement;
025 import com.google.gson.JsonObject;
026 import com.google.gson.JsonParser;
027 import org.sonar.api.utils.text.JsonWriter;
028 import org.sonar.core.UtcDateUtils;
029
030 import javax.annotation.concurrent.Immutable;
031
032 import java.io.StringWriter;
033 import java.util.Collection;
034 import java.util.Comparator;
035 import java.util.HashMap;
036 import java.util.Map;
037 import java.util.SortedSet;
038
039 @Immutable
040 public class UsedQProfiles {
041
042 private final SortedSet<QProfile> profiles = Sets.newTreeSet(new Comparator<QProfile>() {
043 @Override
044 public int compare(QProfile o1, QProfile o2) {
045 int c = o1.getLanguage().compareTo(o2.getLanguage());
046 if (c == 0) {
047 c = o1.getName().compareTo(o2.getName());
048 }
049 return c;
050 }
051 });
052
053 public static UsedQProfiles fromJson(String json) {
054 UsedQProfiles result = new UsedQProfiles();
055 JsonArray jsonRoot = new JsonParser().parse(json).getAsJsonArray();
056 for (JsonElement jsonElt : jsonRoot) {
057 JsonObject jsonProfile = jsonElt.getAsJsonObject();
058 QProfile profile = new QProfile();
059 profile.setKey(jsonProfile.get("key").getAsString());
060 profile.setName(jsonProfile.get("name").getAsString());
061 profile.setLanguage(jsonProfile.get("language").getAsString());
062 profile.setRulesUpdatedAt(UtcDateUtils.parseDateTime(jsonProfile.get("rulesUpdatedAt").getAsString()));
063 result.add(profile);
064 }
065 return result;
066 }
067
068 public String toJson() {
069 StringWriter json = new StringWriter();
070 JsonWriter writer = JsonWriter.of(json);
071 writer.beginArray();
072 for (QProfile profile : profiles) {
073 writer
074 .beginObject()
075 .prop("key", profile.getKey())
076 .prop("language", profile.getLanguage())
077 .prop("name", profile.getName())
078 .prop("rulesUpdatedAt", UtcDateUtils.formatDateTime(profile.getRulesUpdatedAt()))
079 .endObject();
080 }
081 writer.endArray();
082 writer.close();
083 return json.toString();
084 }
085
086 public UsedQProfiles add(UsedQProfiles other) {
087 addAll(other.profiles);
088 return this;
089 }
090
091 public UsedQProfiles add(QProfile profile) {
092 profiles.add(profile);
093 return this;
094 }
095
096 public UsedQProfiles addAll(Collection<QProfile> profiles) {
097 this.profiles.addAll(profiles);
098 return this;
099 }
100
101 public SortedSet<QProfile> profiles() {
102 return profiles;
103 }
104
105 public Map<String, QProfile> profilesByKey() {
106 Map<String, QProfile> map = new HashMap<String, QProfile>();
107 for (QProfile profile : profiles) {
108 map.put(profile.getKey(), profile);
109 }
110 return map;
111 }
112 }