// Copyright (C) 2012 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.deleteproject;

import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.project.ProjectResource;
import com.google.gerrit.server.project.ProjectState;
import com.google.gerrit.sshd.CommandMetaData;
import com.google.gerrit.sshd.SshCommand;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import java.io.IOException;
import java.util.Collection;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;

@CommandMetaData(name = "delete", description = "Delete specific project")
public final class DeleteCommand extends SshCommand {
  @Argument(index = 0, required = true, metaVar = "NAME", usage = "project to delete")
  private ProjectState projectState;

  @Option(name = "--yes-really-delete", usage = "confirmation to delete the project")
  private boolean yesReallyDelete;

  @Option(name = "--force", usage = "delete the project even if it has open changes")
  private boolean force = false;

  @Option(name = "--preserve-git-repository", usage = "don't delete git repository directory")
  private boolean preserveGitRepository = false;

  private final DeleteProject deleteProject;

  private final CurrentUser user;

  @Inject
  protected DeleteCommand(DeleteProject deleteProject, CurrentUser user) {
    this.deleteProject = deleteProject;
    this.user = user;
  }

  @Override
  public void run() throws Failure {
    try {
      DeleteProject.Input input = new DeleteProject.Input();
      input.force = force;
      input.preserve = preserveGitRepository;

      ProjectResource rsrc = new ProjectResource(projectState, user);
      deleteProject.assertDeletePermission(rsrc);
      deleteProject.assertCanDelete(rsrc, input);

      if (!yesReallyDelete) {
        StringBuilder msgBuilder = new StringBuilder();
        msgBuilder.append("Really delete ");
        msgBuilder.append(rsrc.getName());
        msgBuilder.append("?\n");
        msgBuilder.append("This is an operation which permanently deletes ");
        msgBuilder.append("data. This cannot be undone!\n");
        msgBuilder.append("If you are sure you wish to delete this project, ");
        msgBuilder.append("re-run\n");
        msgBuilder.append("with the --yes-really-delete flag.\n");
        throw new UnloggedFailure(msgBuilder.toString());
      }

      if (!force) {
        Collection<String> warnings = deleteProject.getWarnings(rsrc);
        if (warnings != null && !warnings.isEmpty()) {
          StringBuilder msgBuilder = new StringBuilder();
          msgBuilder.append("There are warnings against deleting ");
          msgBuilder.append(rsrc.getName());
          msgBuilder.append(":\n");
          for (String warning : warnings) {
            msgBuilder.append(" * ");
            msgBuilder.append(warning);
            msgBuilder.append("\n");
          }
          msgBuilder.append("To really delete ");
          msgBuilder.append(rsrc.getName());
          msgBuilder.append(", re-run with the --force flag.");
          throw new UnloggedFailure(msgBuilder.toString());
        }
      }

      deleteProject.doDelete(rsrc, input);
    } catch (AuthException
        | ResourceNotFoundException
        | ResourceConflictException
        | OrmException
        | IOException e) {
      die(e);
    }
  }
}

@@ -863,53 +863,8 @@
 on;%0A
-import com.google.gerrit.server.CurrentUser;%0A
 impo
@@ -1957,43 +1957,8 @@
 t;%0A%0A
-  private final CurrentUser user;%0A%0A
   @I
@@ -2020,26 +2020,8 @@
 ject
-, CurrentUser user
 ) %7B%0A
@@ -2064,30 +2064,8 @@
 ct;%0A
-    this.user = user;%0A
   %7D%0A

Remove redundant injected CurrentUser