summaryrefslogtreecommitdiffstats
path: root/gerrit-server/src/main/java/com/google/gerrit/server/api/groups/GroupApiImpl.java
blob: 42213f739e467ee7a1037ed048a6b0100262ff5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright (C) 2015 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.server.api.groups;

import static com.google.gerrit.server.api.ApiUtil.asRestApiException;

import com.google.gerrit.extensions.api.groups.GroupApi;
import com.google.gerrit.extensions.common.AccountInfo;
import com.google.gerrit.extensions.common.GroupAuditEventInfo;
import com.google.gerrit.extensions.common.GroupInfo;
import com.google.gerrit.extensions.common.GroupOptionsInfo;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.server.group.AddMembers;
import com.google.gerrit.server.group.AddSubgroups;
import com.google.gerrit.server.group.DeleteMembers;
import com.google.gerrit.server.group.DeleteSubgroups;
import com.google.gerrit.server.group.GetAuditLog;
import com.google.gerrit.server.group.GetDescription;
import com.google.gerrit.server.group.GetDetail;
import com.google.gerrit.server.group.GetGroup;
import com.google.gerrit.server.group.GetName;
import com.google.gerrit.server.group.GetOptions;
import com.google.gerrit.server.group.GetOwner;
import com.google.gerrit.server.group.GroupResource;
import com.google.gerrit.server.group.Index;
import com.google.gerrit.server.group.ListMembers;
import com.google.gerrit.server.group.ListSubgroups;
import com.google.gerrit.server.group.PutDescription;
import com.google.gerrit.server.group.PutName;
import com.google.gerrit.server.group.PutOptions;
import com.google.gerrit.server.group.PutOwner;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import java.util.Arrays;
import java.util.List;

class GroupApiImpl implements GroupApi {
  interface Factory {
    GroupApiImpl create(GroupResource rsrc);
  }

  private final GetGroup getGroup;
  private final GetDetail getDetail;
  private final GetName getName;
  private final PutName putName;
  private final GetOwner getOwner;
  private final PutOwner putOwner;
  private final GetDescription getDescription;
  private final PutDescription putDescription;
  private final GetOptions getOptions;
  private final PutOptions putOptions;
  private final ListMembers listMembers;
  private final AddMembers addMembers;
  private final DeleteMembers deleteMembers;
  private final ListSubgroups listSubgroups;
  private final AddSubgroups addSubgroups;
  private final DeleteSubgroups deleteSubgroups;
  private final GetAuditLog getAuditLog;
  private final GroupResource rsrc;
  private final Index index;

  @Inject
  GroupApiImpl(
      GetGroup getGroup,
      GetDetail getDetail,
      GetName getName,
      PutName putName,
      GetOwner getOwner,
      PutOwner putOwner,
      GetDescription getDescription,
      PutDescription putDescription,
      GetOptions getOptions,
      PutOptions putOptions,
      ListMembers listMembers,
      AddMembers addMembers,
      DeleteMembers deleteMembers,
      ListSubgroups listSubgroups,
      AddSubgroups addSubgroups,
      DeleteSubgroups deleteSubgroups,
      GetAuditLog getAuditLog,
      Index index,
      @Assisted GroupResource rsrc) {
    this.getGroup = getGroup;
    this.getDetail = getDetail;
    this.getName = getName;
    this.putName = putName;
    this.getOwner = getOwner;
    this.putOwner = putOwner;
    this.getDescription = getDescription;
    this.putDescription = putDescription;
    this.getOptions = getOptions;
    this.putOptions = putOptions;
    this.listMembers = listMembers;
    this.addMembers = addMembers;
    this.deleteMembers = deleteMembers;
    this.listSubgroups = listSubgroups;
    this.addSubgroups = addSubgroups;
    this.deleteSubgroups = deleteSubgroups;
    this.getAuditLog = getAuditLog;
    this.index = index;
    this.rsrc = rsrc;
  }

  @Override
  public GroupInfo get() throws RestApiException {
    try {
      return getGroup.apply(rsrc);
    } catch (Exception e) {
      throw asRestApiException("Cannot retrieve group", e);
    }
  }

  @Override
  public GroupInfo detail() throws RestApiException {
    try {
      return getDetail.apply(rsrc);
    } catch (Exception e) {
      throw asRestApiException("Cannot retrieve group", e);
    }
  }

  @Override
  public String name() throws RestApiException {
    return getName.apply(rsrc);
  }

  @Override
  public void name(String name) throws RestApiException {
    PutName.Input in = new PutName.Input();
    in.name = name;
    try {
      putName.apply(rsrc, in);
    } catch (Exception e) {
      throw asRestApiException("Cannot put group name", e);
    }
  }

  @Override
  public GroupInfo owner() throws RestApiException {
    try {
      return getOwner.apply(rsrc);
    } catch (Exception e) {
      throw asRestApiException("Cannot get group owner", e);
    }
  }

  @Override
  public void owner(String owner) throws RestApiException {
    PutOwner.Input in = new PutOwner.Input();
    in.owner = owner;
    try {
      putOwner.apply(rsrc, in);
    } catch (Exception e) {
      throw asRestApiException("Cannot put group owner", e);
    }
  }

  @Override
  public String description() throws RestApiException {
    return getDescription.apply(rsrc);
  }

  @Override
  public void description(String description) throws RestApiException {
    PutDescription.Input in = new PutDescription.Input();
    in.description = description;
    try {
      putDescription.apply(rsrc, in);
    } catch (Exception e) {
      throw asRestApiException("Cannot put group description", e);
    }
  }

  @Override
  public GroupOptionsInfo options() throws RestApiException {
    return getOptions.apply(rsrc);
  }

  @Override
  public void options(GroupOptionsInfo options) throws RestApiException {
    try {
      putOptions.apply(rsrc, options);
    } catch (Exception e) {
      throw asRestApiException("Cannot put group options", e);
    }
  }

  @Override
  public List<AccountInfo> members() throws RestApiException {
    return members(false);
  }

  @Override
  public List<AccountInfo> members(boolean recursive) throws RestApiException {
    listMembers.setRecursive(recursive);
    try {
      return listMembers.apply(rsrc);
    } catch (Exception e) {
      throw asRestApiException("Cannot list group members", e);
    }
  }

  @Override
  public void addMembers(String... members) throws RestApiException {
    try {
      addMembers.apply(rsrc, AddMembers.Input.fromMembers(Arrays.asList(members)));
    } catch (Exception e) {
      throw asRestApiException("Cannot add group members", e);
    }
  }

  @Override
  public void removeMembers(String... members) throws RestApiException {
    try {
      deleteMembers.apply(rsrc, AddMembers.Input.fromMembers(Arrays.asList(members)));
    } catch (Exception e) {
      throw asRestApiException("Cannot remove group members", e);
    }
  }

  @Override
  public List<GroupInfo> includedGroups() throws RestApiException {
    try {
      return listSubgroups.apply(rsrc);
    } catch (Exception e) {
      throw asRestApiException("Cannot list subgroups", e);
    }
  }

  @Override
  public void addGroups(String... groups) throws RestApiException {
    try {
      addSubgroups.apply(rsrc, AddSubgroups.Input.fromGroups(Arrays.asList(groups)));
    } catch (Exception e) {
      throw asRestApiException("Cannot add subgroups", e);
    }
  }

  @Override
  public void removeGroups(String... groups) throws RestApiException {
    try {
      deleteSubgroups.apply(rsrc, AddSubgroups.Input.fromGroups(Arrays.asList(groups)));
    } catch (Exception e) {
      throw asRestApiException("Cannot remove subgroups", e);
    }
  }

  @Override
  public List<? extends GroupAuditEventInfo> auditLog() throws RestApiException {
    try {
      return getAuditLog.apply(rsrc);
    } catch (Exception e) {
      throw asRestApiException("Cannot get audit log", e);
    }
  }

  @Override
  public void index() throws RestApiException {
    try {
      index.apply(rsrc, new Index.Input());
    } catch (Exception e) {
      throw asRestApiException("Cannot index group", e);
    }
  }
}