// Copyright (C) 2014 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.googlesource.gerrit.plugins.serviceuser;

import com.google.common.base.Strings;
import com.google.gerrit.common.errors.EmailException;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.DefaultInput;
import com.google.gerrit.extensions.restapi.MethodNotAllowedException;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
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.server.account.CreateEmail;
import com.google.gerrit.server.account.DeleteEmail;
import com.google.gerrit.server.account.PutPreferred;
import com.google.gerrit.server.config.ConfigResource;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Provider;

import com.googlesource.gerrit.plugins.serviceuser.PutEmail.Input;

public class PutEmail implements RestModifyView<ServiceUserResource, Input> {
  public static class Input {
    @DefaultInput
    public String email;
  }

  private final Provider<GetConfig> getConfig;
  private final Provider<GetEmail> getEmail;
  private final Provider<CreateEmail.Factory> createEmailFactory;
  private final Provider<DeleteEmail> deleteEmail;
  private final Provider<PutPreferred> putPreferred;

  @Inject
  PutEmail(Provider<GetConfig> getConfig,
      Provider<GetEmail> getEmail,
      Provider<CreateEmail.Factory> createEmailFactory,
      Provider<DeleteEmail> deleteEmail,
      Provider<PutPreferred> putPreferred) {
    this.getConfig = getConfig;
    this.getEmail = getEmail;
    this.createEmailFactory = createEmailFactory;
    this.deleteEmail = deleteEmail;
    this.putPreferred = putPreferred;
  }

  @Override
  public Response<?> apply(ServiceUserResource rsrc, Input input)
      throws AuthException, ResourceNotFoundException,
      ResourceConflictException, MethodNotAllowedException, OrmException,
      BadRequestException, EmailException {
    Boolean emailAllowed = getConfig.get().apply(new ConfigResource()).allowEmail;
    if (emailAllowed == null || !emailAllowed) {
      throw new ResourceConflictException("setting email not allowed");
    }

    String email = getEmail.get().apply(rsrc);
    if (Strings.emptyToNull(input.email) == null) {
      if (Strings.emptyToNull(email) == null) {
        return Response.none();
      } else {
        return deleteEmail.get().apply(rsrc.getUser(), input.email);
      }
    } else if (email.equals(input.email)) {
      return Response.ok(email);
    } else {
      deleteEmail.get().apply(rsrc.getUser(), email);
      CreateEmail.Input in = new CreateEmail.Input();
      in.email = input.email;
      in.noConfirmation = true;
      createEmailFactory.get().create(input.email).apply(rsrc.getUser(), in);
      putPreferred.get().apply(rsrc.getUser(), input.email);
      return Response.ok(input.email);
    }
  }
}

@@ -3199,38 +3199,32 @@
 rsrc.getUser(), 
-input.
 email);%0A      %7D%0A
@@ -3243,16 +3243,33 @@
 f (email
+ != null && email
 .equals(
@@ -3326,24 +3326,53 @@
    %7D else %7B%0A
+      if (email != null) %7B%0A  
       delete
@@ -3409,24 +3409,32 @@
 (), email);%0A
+      %7D%0A
       Create

Fix setting and deleting of service user email address