Line data Source code
1 : // Copyright (C) 2017 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.api.projects; 16 : 17 : import static com.google.gerrit.server.api.ApiUtil.asRestApiException; 18 : 19 : import com.google.gerrit.common.Nullable; 20 : import com.google.gerrit.extensions.api.projects.DashboardApi; 21 : import com.google.gerrit.extensions.api.projects.DashboardInfo; 22 : import com.google.gerrit.extensions.api.projects.SetDashboardInput; 23 : import com.google.gerrit.extensions.restapi.IdString; 24 : import com.google.gerrit.extensions.restapi.RestApiException; 25 : import com.google.gerrit.server.permissions.PermissionBackendException; 26 : import com.google.gerrit.server.project.DashboardResource; 27 : import com.google.gerrit.server.project.ProjectResource; 28 : import com.google.gerrit.server.restapi.project.DashboardsCollection; 29 : import com.google.gerrit.server.restapi.project.GetDashboard; 30 : import com.google.gerrit.server.restapi.project.SetDashboard; 31 : import com.google.inject.Inject; 32 : import com.google.inject.Provider; 33 : import com.google.inject.assistedinject.Assisted; 34 : import java.io.IOException; 35 : import org.eclipse.jgit.errors.ConfigInvalidException; 36 : 37 : public class DashboardApiImpl implements DashboardApi { 38 : interface Factory { 39 : DashboardApiImpl create(ProjectResource project, String id); 40 : } 41 : 42 : private final DashboardsCollection dashboards; 43 : private final Provider<GetDashboard> get; 44 : private final SetDashboard set; 45 : private final ProjectResource project; 46 : private final String id; 47 : 48 : @Inject 49 : DashboardApiImpl( 50 : DashboardsCollection dashboards, 51 : Provider<GetDashboard> get, 52 : SetDashboard set, 53 : @Assisted ProjectResource project, 54 1 : @Assisted @Nullable String id) { 55 1 : this.dashboards = dashboards; 56 1 : this.get = get; 57 1 : this.set = set; 58 1 : this.project = project; 59 1 : this.id = id; 60 1 : } 61 : 62 : @Override 63 : public DashboardInfo get() throws RestApiException { 64 1 : return get(false); 65 : } 66 : 67 : @Override 68 : public DashboardInfo get(boolean inherited) throws RestApiException { 69 : try { 70 1 : return get.get().setInherited(inherited).apply(resource()).value(); 71 1 : } catch (Exception e) { 72 1 : throw asRestApiException("Cannot read dashboard", e); 73 : } 74 : } 75 : 76 : @Override 77 : public void setDefault() throws RestApiException { 78 1 : SetDashboardInput input = new SetDashboardInput(); 79 1 : input.id = id; 80 : try { 81 1 : set.apply( 82 1 : DashboardResource.projectDefault(project.getProjectState(), project.getUser()), input); 83 0 : } catch (Exception e) { 84 0 : String msg = String.format("Cannot %s default dashboard", id != null ? "set" : "remove"); 85 0 : throw asRestApiException(msg, e); 86 1 : } 87 1 : } 88 : 89 : private DashboardResource resource() 90 : throws RestApiException, IOException, ConfigInvalidException, PermissionBackendException { 91 1 : return dashboards.parse(project, IdString.fromDecoded(id)); 92 : } 93 : }