Line data Source code
1 : // Copyright (C) 2013 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.project; 16 : 17 : import static java.util.stream.Collectors.toMap; 18 : 19 : import com.google.common.base.Strings; 20 : import com.google.common.collect.ImmutableList; 21 : import com.google.common.flogger.FluentLogger; 22 : import com.google.gerrit.entities.LabelType; 23 : import com.google.gerrit.entities.LabelValue; 24 : import com.google.gerrit.entities.Project; 25 : import com.google.gerrit.extensions.common.LabelTypeInfo; 26 : import com.google.gerrit.extensions.common.ProjectInfo; 27 : import com.google.gerrit.extensions.common.WebLinkInfo; 28 : import com.google.gerrit.extensions.restapi.Url; 29 : import com.google.gerrit.server.WebLinks; 30 : import com.google.gerrit.server.config.AllProjectsName; 31 : import com.google.inject.Inject; 32 : import com.google.inject.Singleton; 33 : import java.util.HashMap; 34 : 35 : /** Collection of routines to populate {@link ProjectInfo}. */ 36 : @Singleton 37 : public class ProjectJson { 38 149 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 39 : 40 : private final AllProjectsName allProjects; 41 : private final WebLinks webLinks; 42 : 43 : @Inject 44 149 : ProjectJson(AllProjectsName allProjectsName, WebLinks webLinks) { 45 149 : this.allProjects = allProjectsName; 46 149 : this.webLinks = webLinks; 47 149 : } 48 : 49 : public ProjectInfo format(ProjectState projectState) { 50 143 : ProjectInfo info = format(projectState.getProject()); 51 143 : info.labels = new HashMap<>(); 52 143 : for (LabelType t : projectState.getLabelTypes().getLabelTypes()) { 53 143 : LabelTypeInfo labelInfo = new LabelTypeInfo(); 54 143 : labelInfo.values = 55 143 : t.getValues().stream() 56 143 : .collect( 57 143 : toMap( 58 : LabelValue::formatValue, 59 : LabelValue::getText, 60 : (v1, v2) -> { 61 0 : logger.atSevere().log( 62 : "Duplicate values for project: %s, label: %s found: '%s':'%s'", 63 0 : projectState.getName(), t.getName(), v1, v2); 64 0 : return v1; 65 : })); 66 143 : labelInfo.defaultValue = t.getDefaultValue(); 67 143 : info.labels.put(t.getName(), labelInfo); 68 143 : } 69 : 70 143 : return info; 71 : } 72 : 73 : public ProjectInfo format(Project p) { 74 143 : ProjectInfo info = new ProjectInfo(); 75 143 : info.name = p.getName(); 76 143 : Project.NameKey parentName = p.getParent(allProjects); 77 143 : info.parent = parentName != null ? parentName.get() : null; 78 143 : info.description = Strings.emptyToNull(p.getDescription()); 79 143 : info.state = p.getState(); 80 143 : info.id = Url.encode(info.name); 81 143 : ImmutableList<WebLinkInfo> links = webLinks.getProjectLinks(p.getName()); 82 143 : info.webLinks = links.isEmpty() ? null : links; 83 143 : return info; 84 : } 85 : }