Line data Source code
1 : // Copyright (C) 2008 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.entities; 16 : 17 : import static java.util.Objects.requireNonNull; 18 : 19 : import com.google.auto.value.AutoValue; 20 : import com.google.common.collect.ImmutableMap; 21 : import com.google.errorprone.annotations.Immutable; 22 : import com.google.gerrit.common.Nullable; 23 : import com.google.gerrit.extensions.client.InheritableBoolean; 24 : import com.google.gerrit.extensions.client.ProjectState; 25 : import com.google.gerrit.extensions.client.SubmitType; 26 : import java.io.Serializable; 27 : import java.util.Arrays; 28 : import java.util.HashMap; 29 : import java.util.Map; 30 : import java.util.Optional; 31 : 32 : /** Projects match a source code repository managed by Gerrit */ 33 : @AutoValue 34 152 : public abstract class Project { 35 : /** Default submit type for new projects. */ 36 162 : public static final SubmitType DEFAULT_SUBMIT_TYPE = SubmitType.MERGE_IF_NECESSARY; 37 : 38 : /** Default submit type for root project (All-Projects). */ 39 162 : public static final SubmitType DEFAULT_ALL_PROJECTS_SUBMIT_TYPE = SubmitType.MERGE_IF_NECESSARY; 40 : 41 : public static NameKey nameKey(String name) { 42 161 : return new NameKey(name); 43 : } 44 : 45 : /** 46 : * Project name key. 47 : * 48 : * <p>This class has subclasses such as {@code AllProjectsName}, which make Guice injection more 49 : * convenient. Subclasses must compare equal if they have the same name, regardless of the 50 : * specific class. This implies that subclasses may not add additional fields. 51 : * 52 : * <p>Because of this unusual subclassing behavior, this class is not an {@code @AutoValue}, 53 : * unlike other key types in this package. However, this is strictly an implementation detail; its 54 : * interface and semantics are otherwise analogous to the {@code @AutoValue} types. 55 : * 56 : * <p>This class is immutable and thread safe. 57 : */ 58 : @Immutable 59 : public static class NameKey implements Serializable, Comparable<NameKey> { 60 : private static final long serialVersionUID = 1L; 61 : 62 : /** Parse a Project.NameKey out of a string representation. */ 63 : public static NameKey parse(String str) { 64 50 : return nameKey(KeyUtil.decode(str)); 65 : } 66 : 67 : private final String name; 68 : 69 162 : protected NameKey(String name) { 70 162 : this.name = requireNonNull(name); 71 162 : } 72 : 73 : public String get() { 74 160 : return name; 75 : } 76 : 77 : @Override 78 : public final int hashCode() { 79 153 : return name.hashCode(); 80 : } 81 : 82 : @Override 83 : public final boolean equals(Object b) { 84 158 : if (b instanceof NameKey) { 85 158 : return name.equals(((NameKey) b).get()); 86 : } 87 0 : return false; 88 : } 89 : 90 : @Override 91 : public final int compareTo(NameKey o) { 92 147 : return name.compareTo(o.get()); 93 : } 94 : 95 : @Override 96 : public final String toString() { 97 127 : return KeyUtil.encode(name); 98 : } 99 : } 100 : 101 : public abstract NameKey getNameKey(); 102 : 103 : @Nullable 104 : public abstract String getDescription(); 105 : 106 : public abstract ImmutableMap<BooleanProjectConfig, InheritableBoolean> getBooleanConfigs(); 107 : 108 : /** 109 : * Submit type as configured in {@code project.config}. 110 : * 111 : * <p>Does not take inheritance into account, i.e. may return {@link SubmitType#INHERIT}. 112 : */ 113 : public abstract SubmitType getSubmitType(); 114 : 115 : public abstract ProjectState getState(); 116 : 117 : /** 118 : * Name key of the parent project. 119 : * 120 : * <p>{@code null} if this project is the wild project, {@code null} or the name key of the wild 121 : * project if this project is a direct child of the wild project. 122 : */ 123 : @Nullable 124 : public abstract NameKey getParent(); 125 : 126 : @Nullable 127 : public abstract String getMaxObjectSizeLimit(); 128 : 129 : @Nullable 130 : public abstract String getDefaultDashboard(); 131 : 132 : @Nullable 133 : public abstract String getLocalDefaultDashboard(); 134 : 135 : /** The {@code ObjectId} as 40 digit hex of {@code refs/meta/config}'s HEAD. */ 136 : @Nullable 137 : public abstract String getConfigRefState(); 138 : 139 : public static Builder builder(Project.NameKey nameKey) { 140 152 : Builder builder = 141 : new AutoValue_Project.Builder() 142 152 : .setNameKey(nameKey) 143 152 : .setSubmitType(SubmitType.MERGE_IF_NECESSARY) 144 152 : .setState(ProjectState.ACTIVE); 145 : ImmutableMap.Builder<BooleanProjectConfig, InheritableBoolean> booleans = 146 152 : ImmutableMap.builder(); 147 152 : Arrays.stream(BooleanProjectConfig.values()) 148 152 : .forEach(b -> booleans.put(b, InheritableBoolean.INHERIT)); 149 152 : builder.setBooleanConfigs(booleans.build()); 150 152 : return builder; 151 : } 152 : 153 : @Nullable 154 : public String getName() { 155 149 : return getNameKey() != null ? getNameKey().get() : null; 156 : } 157 : 158 : public InheritableBoolean getBooleanConfig(BooleanProjectConfig config) { 159 151 : return getBooleanConfigs().get(config); 160 : } 161 : 162 : /** 163 : * Returns the name key of the parent project. 164 : * 165 : * @param allProjectsName name key of the wild project 166 : * @return name key of the parent project, {@code null} if this project is the All-Projects 167 : * project 168 : */ 169 : @Nullable 170 : public Project.NameKey getParent(Project.NameKey allProjectsName) { 171 143 : if (getParent() != null) { 172 143 : return getParent(); 173 : } 174 : 175 19 : if (getNameKey().equals(allProjectsName)) { 176 8 : return null; 177 : } 178 : 179 13 : return allProjectsName; 180 : } 181 : 182 : @Nullable 183 : public String getParentName() { 184 152 : return getParent() != null ? getParent().get() : null; 185 : } 186 : 187 : @Override 188 : public final String toString() { 189 0 : return Optional.ofNullable(getName()).orElse("<null>"); 190 : } 191 : 192 : public abstract Builder toBuilder(); 193 : 194 : @AutoValue.Builder 195 152 : public abstract static class Builder { 196 : public abstract Builder setDescription(String description); 197 : 198 : public Builder setBooleanConfig(BooleanProjectConfig config, InheritableBoolean val) { 199 152 : Map<BooleanProjectConfig, InheritableBoolean> map = new HashMap<>(getBooleanConfigs()); 200 152 : map.replace(config, val); 201 152 : setBooleanConfigs(ImmutableMap.copyOf(map)); 202 152 : return this; 203 : } 204 : 205 : public abstract Builder setMaxObjectSizeLimit(String limit); 206 : 207 : public abstract Builder setSubmitType(SubmitType type); 208 : 209 : public abstract Builder setState(ProjectState newState); 210 : 211 : public abstract Builder setDefaultDashboard(String defaultDashboardId); 212 : 213 : public abstract Builder setLocalDefaultDashboard(String localDefaultDashboard); 214 : 215 : public abstract Builder setParent(NameKey n); 216 : 217 : public Builder setParent(String n) { 218 152 : return setParent(n != null ? nameKey(n) : null); 219 : } 220 : 221 : /** Sets the {@code ObjectId} as 40 digit hex of {@code refs/meta/config}'s HEAD. */ 222 : public abstract Builder setConfigRefState(String state); 223 : 224 : public abstract Project build(); 225 : 226 : protected abstract Builder setNameKey(Project.NameKey nameKey); 227 : 228 : protected abstract ImmutableMap<BooleanProjectConfig, InheritableBoolean> getBooleanConfigs(); 229 : 230 : protected abstract Builder setBooleanConfigs( 231 : ImmutableMap<BooleanProjectConfig, InheritableBoolean> booleanConfigs); 232 : } 233 : }