// 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.server.group;

import com.google.gerrit.extensions.common.GroupOptionsInfo;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.MethodNotAllowedException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.account.GroupCache;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import java.io.IOException;
import java.util.Collections;

@Singleton
public class PutOptions implements RestModifyView<GroupResource, GroupOptionsInfo> {
  private final GroupCache groupCache;
  private final Provider<ReviewDb> db;

  @Inject
  PutOptions(GroupCache groupCache, Provider<ReviewDb> db) {
    this.groupCache = groupCache;
    this.db = db;
  }

  @Override
  public GroupOptionsInfo apply(GroupResource resource, GroupOptionsInfo input)
      throws MethodNotAllowedException, AuthException, BadRequestException,
          ResourceNotFoundException, OrmException, IOException {
    if (resource.toAccountGroup() == null) {
      throw new MethodNotAllowedException();
    } else if (!resource.getControl().isOwner()) {
      throw new AuthException("Not group owner");
    }

    if (input == null) {
      throw new BadRequestException("options are required");
    }
    if (input.visibleToAll == null) {
      input.visibleToAll = false;
    }

    AccountGroup group = db.get().accountGroups().get(resource.toAccountGroup().getId());
    if (group == null) {
      throw new ResourceNotFoundException();
    }

    group.setVisibleToAll(input.visibleToAll);
    db.get().accountGroups().update(Collections.singleton(group));
    groupCache.evict(group);

    GroupOptionsInfo options = new GroupOptionsInfo();
    if (group.isVisibleToAll()) {
      options.visibleToAll = true;
    }
    return options;
  }
}

@@ -1140,60 +1140,8 @@
 Db;%0A
-import com.google.gerrit.server.account.GroupCache;%0A
 impo
@@ -1331,27 +1331,24 @@
 va.util.
-Collec
+Op
 tion
-s
+al
 ;%0A%0A@Sing
@@ -1454,37 +1454,37 @@
 e final 
-GroupCache groupCache
+Provider%3CReviewDb%3E db
 ;%0A  priv
@@ -1506,144 +1506,223 @@
 der%3C
-ReviewDb%3E db;%0A%0A  @Inject%0A  PutOptions(GroupCache groupCache, Provider%3CReviewDb%3E db) %7B%0A    this.groupCache = groupCache;%0A    this.db = db
+GroupsUpdate%3E groupsUpdateProvider;%0A%0A  @Inject%0A  PutOptions(Provider%3CReviewDb%3E db, @UserInitiated Provider%3CGroupsUpdate%3E groupsUpdateProvider) %7B%0A    this.db = db;%0A    this.groupsUpdateProvider = groupsUpdateProvider
 ;%0A  
@@ -1961,20 +1961,44 @@
 n %7B%0A    
-if (
+AccountGroup accountGroup = 
 resource
@@ -2014,16 +2014,38 @@
 tGroup()
+;%0A    if (accountGroup
  == null
@@ -2372,25 +2372,29 @@
 %0A    %7D%0A%0A    
-A
+if (a
 ccountGroup 
@@ -2396,162 +2396,276 @@
 roup
- group = db.get().accountGroups().get(resource.toAccountGroup().getId());%0A    if (group == null) %7B%0A      throw new ResourceNotFoundException();
+.isVisibleToAll() != input.visibleToAll) %7B%0A      Optional%3CAccountGroup%3E updatedGroup =%0A          groupsUpdateProvider%0A              .get()%0A              .updateGroup(%0A                  db.get(),%0A                  accountGroup.getGroupUUID(),
 %0A    
-%7D%0A%0A
    
+           group -%3E
  gro
@@ -2706,105 +2706,110 @@
 All)
+)
 ;%0A    
-db.get().accountGroups().update(Collections.singleton(group));%0A    groupCache.evict(group);
+  if (!updatedGroup.isPresent()) %7B%0A        throw new ResourceNotFoundException();%0A      %7D%0A    %7D
 %0A%0A  
@@ -2873,17 +2873,15 @@
 if (
-group.isV
+input.v
 isib
@@ -2887,18 +2887,16 @@
 bleToAll
-()
 ) %7B%0A    

Remove access of groups related db tables from PutOptions