summaryrefslogtreecommitdiffstats
path: root/gerrit-gwtui/src/main/java/com/google/gerrit/client/groups/GroupApi.java
blob: 74cfaf18e5a93bad56b3f81bc99d56744a80d326 (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
// Copyright (C) 2013 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.client.groups;

import com.google.gerrit.client.VoidResult;
import com.google.gerrit.client.info.AccountInfo;
import com.google.gerrit.client.info.GroupInfo;
import com.google.gerrit.client.rpc.NativeString;
import com.google.gerrit.client.rpc.Natives;
import com.google.gerrit.client.rpc.RestApi;
import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.user.client.rpc.AsyncCallback;
import java.util.Set;

/** A collection of static methods which work on the Gerrit REST API for specific groups. */
public class GroupApi {
  /** Create a new group */
  public static void createGroup(String groupName, AsyncCallback<GroupInfo> cb) {
    JavaScriptObject in = JavaScriptObject.createObject();
    new RestApi("/groups/").id(groupName).ifNoneMatch().put(in, cb);
  }

  public static void getGroupDetail(String group, AsyncCallback<GroupInfo> cb) {
    group(group).view("detail").get(cb);
  }

  /** Get the name of a group */
  public static void getGroupName(AccountGroup.UUID group, AsyncCallback<NativeString> cb) {
    group(group).view("name").get(cb);
  }

  /** Check if the current user is owner of a group */
  public static void isGroupOwner(String groupName, final AsyncCallback<Boolean> cb) {
    GroupMap.myOwned(
        groupName,
        new AsyncCallback<GroupMap>() {
          @Override
          public void onSuccess(GroupMap result) {
            cb.onSuccess(!result.isEmpty());
          }

          @Override
          public void onFailure(Throwable caught) {
            cb.onFailure(caught);
          }
        });
  }

  /** Rename a group */
  public static void renameGroup(
      AccountGroup.UUID group, String newName, AsyncCallback<VoidResult> cb) {
    GroupInput in = GroupInput.create();
    in.name(newName);
    group(group).view("name").put(in, cb);
  }

  /** Set description for a group */
  public static void setGroupDescription(
      AccountGroup.UUID group, String description, AsyncCallback<VoidResult> cb) {
    RestApi call = group(group).view("description");
    if (description != null && !description.isEmpty()) {
      GroupInput in = GroupInput.create();
      in.description(description);
      call.put(in, cb);
    } else {
      call.delete(cb);
    }
  }

  /** Set owner for a group */
  public static void setGroupOwner(
      AccountGroup.UUID group, String owner, AsyncCallback<GroupInfo> cb) {
    GroupInput in = GroupInput.create();
    in.owner(owner);
    group(group).view("owner").put(in, cb);
  }

  /** Set the options for a group */
  public static void setGroupOptions(
      AccountGroup.UUID group, boolean isVisibleToAll, AsyncCallback<VoidResult> cb) {
    GroupOptionsInput in = GroupOptionsInput.create();
    in.visibleToAll(isVisibleToAll);
    group(group).view("options").put(in, cb);
  }

  /** Add member to a group. */
  public static void addMember(
      AccountGroup.UUID group, String member, AsyncCallback<AccountInfo> cb) {
    members(group).id(member).put(cb);
  }

  /** Add members to a group. */
  public static void addMembers(
      AccountGroup.UUID group, Set<String> members, final AsyncCallback<JsArray<AccountInfo>> cb) {
    if (members.size() == 1) {
      addMember(
          group,
          members.iterator().next(),
          new AsyncCallback<AccountInfo>() {
            @Override
            public void onSuccess(AccountInfo result) {
              cb.onSuccess(Natives.arrayOf(result));
            }

            @Override
            public void onFailure(Throwable caught) {
              cb.onFailure(caught);
            }
          });
    } else {
      MemberInput input = MemberInput.create();
      for (String member : members) {
        input.addMember(member);
      }
      members(group).post(input, cb);
    }
  }

  /** Remove members from a group. */
  public static void removeMembers(
      AccountGroup.UUID group, Set<Integer> ids, final AsyncCallback<VoidResult> cb) {
    if (ids.size() == 1) {
      members(group).id(ids.iterator().next().toString()).delete(cb);
    } else {
      MemberInput in = MemberInput.create();
      for (Integer id : ids) {
        in.addMember(id.toString());
      }
      group(group).view("members.delete").post(in, cb);
    }
  }

  /** Include a group into a group. */
  public static void addIncludedGroup(
      AccountGroup.UUID group, String include, AsyncCallback<GroupInfo> cb) {
    groups(group).id(include).put(cb);
  }

  /** Include groups into a group. */
  public static void addIncludedGroups(
      AccountGroup.UUID group,
      Set<String> includedGroups,
      final AsyncCallback<JsArray<GroupInfo>> cb) {
    if (includedGroups.size() == 1) {
      addIncludedGroup(
          group,
          includedGroups.iterator().next(),
          new AsyncCallback<GroupInfo>() {
            @Override
            public void onSuccess(GroupInfo result) {
              cb.onSuccess(Natives.arrayOf(result));
            }

            @Override
            public void onFailure(Throwable caught) {
              cb.onFailure(caught);
            }
          });
    } else {
      IncludedGroupInput input = IncludedGroupInput.create();
      for (String includedGroup : includedGroups) {
        input.addGroup(includedGroup);
      }
      groups(group).post(input, cb);
    }
  }

  /** Remove included groups from a group. */
  public static void removeIncludedGroups(
      AccountGroup.UUID group, Set<AccountGroup.UUID> ids, final AsyncCallback<VoidResult> cb) {
    if (ids.size() == 1) {
      AccountGroup.UUID g = ids.iterator().next();
      groups(group).id(g.get()).delete(cb);
    } else {
      IncludedGroupInput in = IncludedGroupInput.create();
      for (AccountGroup.UUID g : ids) {
        in.addGroup(g.get());
      }
      group(group).view("groups.delete").post(in, cb);
    }
  }

  /** Get audit log of a group. */
  public static void getAuditLog(
      AccountGroup.UUID group, AsyncCallback<JsArray<GroupAuditEventInfo>> cb) {
    group(group).view("log.audit").get(cb);
  }

  private static RestApi members(AccountGroup.UUID group) {
    return group(group).view("members");
  }

  private static RestApi groups(AccountGroup.UUID group) {
    return group(group).view("groups");
  }

  private static RestApi group(AccountGroup.UUID group) {
    return group(group.get());
  }

  private static RestApi group(String group) {
    return new RestApi("/groups/").id(group);
  }

  private static class GroupInput extends JavaScriptObject {
    final native void description(String d) /*-{ if(d)this.description=d; }-*/;

    final native void name(String n) /*-{ if(n)this.name=n; }-*/;

    final native void owner(String o) /*-{ if(o)this.owner=o; }-*/;

    static GroupInput create() {
      return (GroupInput) createObject();
    }

    protected GroupInput() {}
  }

  private static class GroupOptionsInput extends JavaScriptObject {
    final native void visibleToAll(boolean v) /*-{ if(v)this.visible_to_all=v; }-*/;

    static GroupOptionsInput create() {
      return (GroupOptionsInput) createObject();
    }

    protected GroupOptionsInput() {}
  }

  private static class MemberInput extends JavaScriptObject {
    final native void init() /*-{ this.members = []; }-*/;

    final native void addMember(String n) /*-{ this.members.push(n); }-*/;

    static MemberInput create() {
      MemberInput m = (MemberInput) createObject();
      m.init();
      return m;
    }

    protected MemberInput() {}
  }

  private static class IncludedGroupInput extends JavaScriptObject {
    final native void init() /*-{ this.groups = []; }-*/;

    final native void addGroup(String n) /*-{ this.groups.push(n); }-*/;

    static IncludedGroupInput create() {
      IncludedGroupInput g = (IncludedGroupInput) createObject();
      g.init();
      return g;
    }

    protected IncludedGroupInput() {}
  }
}