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.extensions.api.projects; 16 : 17 : import com.google.gerrit.extensions.client.ProjectState; 18 : import com.google.gerrit.extensions.common.ProjectInfo; 19 : import com.google.gerrit.extensions.restapi.NotImplementedException; 20 : import com.google.gerrit.extensions.restapi.RestApiException; 21 : import java.util.ArrayList; 22 : import java.util.Collections; 23 : import java.util.List; 24 : import java.util.Map; 25 : import java.util.SortedMap; 26 : 27 : public interface Projects { 28 : /** 29 : * Look up a project by name. 30 : * 31 : * <p><strong>Note:</strong> This method eagerly reads the project. Methods that mutate the 32 : * project do not necessarily re-read the project. Therefore, calling a getter method on an 33 : * instance after calling a mutation method on that same instance is not guaranteed to reflect the 34 : * mutation. It is not recommended to store references to {@code ProjectApi} instances. 35 : * 36 : * @param name project name. 37 : * @return API for accessing the project. 38 : * @throws RestApiException if an error occurred. 39 : */ 40 : ProjectApi name(String name) throws RestApiException; 41 : 42 : /** 43 : * Create a project using the default configuration. 44 : * 45 : * @param name project name. 46 : * @return API for accessing the newly-created project. 47 : * @throws RestApiException if an error occurred. 48 : */ 49 : ProjectApi create(String name) throws RestApiException; 50 : 51 : /** 52 : * Create a project. 53 : * 54 : * @param in project creation input; name must be set. 55 : * @return API for accessing the newly-created project. 56 : * @throws RestApiException if an error occurred. 57 : */ 58 : ProjectApi create(ProjectInput in) throws RestApiException; 59 : 60 : ListRequest list(); 61 : 62 : /** 63 : * Query projects. 64 : * 65 : * <p>Example code: {@code query().withQuery("name:project").get()} 66 : * 67 : * @return API for setting parameters and getting result. 68 : */ 69 : QueryRequest query(); 70 : 71 : /** 72 : * Query projects. 73 : * 74 : * <p>Shortcut API for {@code query().withQuery(String)}. 75 : * 76 : * @see #query() 77 : */ 78 : QueryRequest query(String query); 79 : 80 3 : abstract class ListRequest { 81 3 : public enum FilterType { 82 3 : CODE, 83 3 : PERMISSIONS, 84 3 : ALL 85 : } 86 : 87 3 : private final List<String> branches = new ArrayList<>(); 88 : private boolean description; 89 : private String prefix; 90 : private String substring; 91 : private String regex; 92 : private int limit; 93 : private int start; 94 : private boolean showTree; 95 : private boolean all; 96 3 : private FilterType type = FilterType.ALL; 97 3 : private ProjectState state = null; 98 : 99 : public List<ProjectInfo> get() throws RestApiException { 100 3 : Map<String, ProjectInfo> map = getAsMap(); 101 3 : List<ProjectInfo> result = new ArrayList<>(map.size()); 102 3 : for (Map.Entry<String, ProjectInfo> e : map.entrySet()) { 103 : // ListProjects "helpfully" nulls out names when converting to a map. 104 3 : e.getValue().name = e.getKey(); 105 3 : result.add(e.getValue()); 106 3 : } 107 3 : return Collections.unmodifiableList(result); 108 : } 109 : 110 : public abstract SortedMap<String, ProjectInfo> getAsMap() throws RestApiException; 111 : 112 : public ListRequest withDescription(boolean description) { 113 1 : this.description = description; 114 1 : return this; 115 : } 116 : 117 : public ListRequest withPrefix(String prefix) { 118 1 : this.prefix = prefix; 119 1 : return this; 120 : } 121 : 122 : public ListRequest withSubstring(String substring) { 123 1 : this.substring = substring; 124 1 : return this; 125 : } 126 : 127 : public ListRequest withRegex(String regex) { 128 1 : this.regex = regex; 129 1 : return this; 130 : } 131 : 132 : public ListRequest withLimit(int limit) { 133 1 : this.limit = limit; 134 1 : return this; 135 : } 136 : 137 : public ListRequest withStart(int start) { 138 1 : this.start = start; 139 1 : return this; 140 : } 141 : 142 : public ListRequest addShowBranch(String branch) { 143 1 : branches.add(branch); 144 1 : return this; 145 : } 146 : 147 : public ListRequest withTree(boolean show) { 148 1 : showTree = show; 149 1 : return this; 150 : } 151 : 152 : public ListRequest withType(FilterType type) { 153 1 : this.type = type != null ? type : FilterType.ALL; 154 1 : return this; 155 : } 156 : 157 : public ListRequest withAll(boolean all) { 158 1 : this.all = all; 159 1 : return this; 160 : } 161 : 162 : public ListRequest withState(ProjectState state) { 163 1 : this.state = state; 164 1 : return this; 165 : } 166 : 167 : public boolean getDescription() { 168 3 : return description; 169 : } 170 : 171 : public String getPrefix() { 172 3 : return prefix; 173 : } 174 : 175 : public String getSubstring() { 176 3 : return substring; 177 : } 178 : 179 : public String getRegex() { 180 3 : return regex; 181 : } 182 : 183 : public int getLimit() { 184 3 : return limit; 185 : } 186 : 187 : public int getStart() { 188 3 : return start; 189 : } 190 : 191 : public List<String> getBranches() { 192 3 : return Collections.unmodifiableList(branches); 193 : } 194 : 195 : public boolean getShowTree() { 196 3 : return showTree; 197 : } 198 : 199 : public FilterType getFilterType() { 200 3 : return type; 201 : } 202 : 203 : public boolean isAll() { 204 3 : return all; 205 : } 206 : 207 : public ProjectState getState() { 208 3 : return state; 209 : } 210 : } 211 : 212 : /** 213 : * API for setting parameters and getting result. Used for {@code query()}. 214 : * 215 : * @see #query() 216 : */ 217 3 : abstract class QueryRequest { 218 : private String query; 219 : private int limit; 220 : private int start; 221 : 222 : /** Execute query and returns the matched projects as list. */ 223 : public abstract List<ProjectInfo> get() throws RestApiException; 224 : 225 : /** 226 : * Set query. 227 : * 228 : * @param query needs to be in human-readable form. 229 : */ 230 : public QueryRequest withQuery(String query) { 231 3 : this.query = query; 232 3 : return this; 233 : } 234 : 235 : /** 236 : * Set limit for returned list of projects. Optional; server-default is used when not provided. 237 : */ 238 : public QueryRequest withLimit(int limit) { 239 2 : this.limit = limit; 240 2 : return this; 241 : } 242 : 243 : /** Set number of projects to skip. Optional; no projects are skipped when not provided. */ 244 : public QueryRequest withStart(int start) { 245 2 : this.start = start; 246 2 : return this; 247 : } 248 : 249 : public String getQuery() { 250 3 : return query; 251 : } 252 : 253 : public int getLimit() { 254 3 : return limit; 255 : } 256 : 257 : public int getStart() { 258 3 : return start; 259 : } 260 : } 261 : 262 : /** 263 : * A default implementation which allows source compatibility when adding new methods to the 264 : * interface. 265 : */ 266 0 : class NotImplemented implements Projects { 267 : @Override 268 : public ProjectApi name(String name) throws RestApiException { 269 0 : throw new NotImplementedException(); 270 : } 271 : 272 : @Override 273 : public ProjectApi create(ProjectInput in) throws RestApiException { 274 0 : throw new NotImplementedException(); 275 : } 276 : 277 : @Override 278 : public ProjectApi create(String name) throws RestApiException { 279 0 : throw new NotImplementedException(); 280 : } 281 : 282 : @Override 283 : public ListRequest list() { 284 0 : throw new NotImplementedException(); 285 : } 286 : 287 : @Override 288 : public QueryRequest query() { 289 0 : throw new NotImplementedException(); 290 : } 291 : 292 : @Override 293 : public QueryRequest query(String query) { 294 0 : throw new NotImplementedException(); 295 : } 296 : } 297 : }