// 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.common.base.Strings;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.DefaultInput;
import com.google.gerrit.extensions.restapi.MethodNotAllowedException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.Response;
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.gerrit.server.group.PutDescription.Input;
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 PutDescription implements RestModifyView<GroupResource, Input> {
  public static class Input {
    @DefaultInput public String description;
  }

  private final GroupCache groupCache;
  private final Provider<ReviewDb> db;

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

  @Override
  public Response<String> apply(GroupResource resource, Input input)
      throws AuthException, MethodNotAllowedException, ResourceNotFoundException, OrmException,
          IOException {
    if (input == null) {
      input = new Input(); // Delete would set description to null.
    }

    if (resource.toAccountGroup() == null) {
      throw new MethodNotAllowedException();
    } else if (!resource.getControl().isOwner()) {
      throw new AuthException("Not group owner");
    }

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

    group.setDescription(Strings.emptyToNull(input.description));
    db.get().accountGroups().update(Collections.singleton(group));
    groupCache.evict(group);

    return Strings.isNullOrEmpty(input.description)
        ? Response.<String>none()
        : Response.ok(input.description);
  }
}

@@ -1165,60 +1165,8 @@
 Db;%0A
-import com.google.gerrit.server.account.GroupCache;%0A
 impo
@@ -1416,27 +1416,50 @@
 va.util.
-Collec
+Objects;%0Aimport java.util.Op
 tion
-s
+al
 ;%0A%0A@Sing
@@ -1638,37 +1638,37 @@
 e final 
-GroupCache groupCache
+Provider%3CReviewDb%3E db
 ;%0A  priv
@@ -1690,148 +1690,234 @@
 der%3C
-ReviewDb%3E db;%0A%0A  @Inject%0A  PutDescription(GroupCache groupCache, Provider%3CReviewDb%3E db) %7B%0A    this.groupCache = groupCache;%0A    this.db = db
+GroupsUpdate%3E groupsUpdateProvider;%0A%0A  @Inject%0A  PutDescription(%0A      Provider%3CReviewDb%3E db, @UserInitiated Provider%3CGroupsUpdate%3E groupsUpdateProvider) %7B%0A    this.db = db;%0A    this.groupsUpdateProvider = groupsUpdateProvider
 ;%0A  
@@ -2224,20 +2224,44 @@
  %7D%0A%0A    
-if (
+AccountGroup accountGroup = 
 resource
@@ -2277,16 +2277,38 @@
 tGroup()
+;%0A    if (accountGroup
  == null
@@ -2472,38 +2472,96 @@
     
-AccountGroup group = db.get().
+String newDescription = Strings.emptyToNull(input.description);%0A    if (!Objects.equals(
 acco
@@ -2572,290 +2572,370 @@
 roup
-s()
 .get
-(resource.toAccountGroup().getId());%0A    if (group == null) %7B%0A      throw new ResourceNotFoundException();%0A    %7D%0A%0A    group.setDescription(Strings.emptyToNull(input.description));%0A    db.get().accountGroups().update(Collections.singleton(group));%0A    groupCache.evict(group);
+Description(), newDescription)) %7B%0A      Optional%3CAccountGroup%3E updatedGroup =%0A          groupsUpdateProvider%0A              .get()%0A              .updateGroup(%0A                  db.get(), resource.getGroupUUID(), group -%3E group.setDescription(newDescription));%0A      if (!updatedGroup.isPresent()) %7B%0A        throw new ResourceNotFoundException();%0A      %7D%0A    %7D
 %0A%0A  

Remove access of groups related db tables from PutDescription