// Copyright (C) 2015 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.common.data.AccessSection;
import com.google.gerrit.extensions.client.ProjectState;
import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.extensions.restapi.TopLevelResource;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.git.meta.MetaDataUpdate;
import com.google.gerrit.server.permissions.PermissionBackendException;
import com.google.gerrit.server.project.ProjectCache;
import com.google.gerrit.server.project.ProjectConfig;
import com.google.gerrit.server.project.ProjectResource;
import com.google.gerrit.server.restapi.project.CreateProject;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.io.IOException;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.errors.RepositoryNotFoundException;

@Singleton
class HideProject {

  private final MetaDataUpdate.Server metaDataUpdateFactory;
  private final ProjectCache projectCache;
  private final CreateProject createProject;
  private final Configuration cfg;

  @Inject
  HideProject(
      MetaDataUpdate.Server metaDataUpdateFactory,
      ProjectCache projectCache,
      CreateProject createProject,
      Configuration cfg) {
    this.metaDataUpdateFactory = metaDataUpdateFactory;
    this.projectCache = projectCache;
    this.createProject = createProject;
    this.cfg = cfg;
  }

  public void apply(ProjectResource rsrc) throws IOException, RestApiException {
    try {
      MetaDataUpdate md = metaDataUpdateFactory.create(rsrc.getNameKey());

      ProjectConfig projectConfig = ProjectConfig.read(md);
      Project p = projectConfig.getProject();
      p.setState(ProjectState.HIDDEN);

      for (AccessSection as : projectConfig.getAccessSections()) {
        projectConfig.remove(as);
      }

      String parentForDeletedProjects = cfg.getDeletedProjectsParent();
      createProjectIfMissing(parentForDeletedProjects);
      p.setParentName(parentForDeletedProjects);

      md.setMessage("Hide project\n");
      projectConfig.commit(md);
      projectCache.evict(projectConfig.getProject());
    } catch (RepositoryNotFoundException e) {
      throw new ResourceNotFoundException();
    } catch (ConfigInvalidException e) {
      throw new ResourceConflictException(e.getMessage());
    }
  }

  private void createProjectIfMissing(String projectName) throws IOException, RestApiException {
    if (projectCache.get(new Project.NameKey(projectName)) == null) {
      try {
        createProject.apply(TopLevelResource.INSTANCE, IdString.fromDecoded(projectName), null);
      } catch (RestApiException | ConfigInvalidException | PermissionBackendException e) {
        throw new ResourceConflictException(
            String.format("Failed to create project %s", projectName));
      }
    }
  }
}

@@ -1923,16 +1923,76 @@
 ion cfg;
+%0A  private final ProjectConfig.Factory projectConfigFactory;
 %0A%0A  @Inj
@@ -2152,16 +2152,66 @@
 tion cfg
+,%0A      ProjectConfig.Factory projectConfigFactory
 ) %7B%0A    
@@ -2360,16 +2360,70 @@
  = cfg;%0A
+    this.projectConfigFactory = projectConfigFactory;%0A
   %7D%0A%0A  p
@@ -2622,17 +2622,17 @@
 onfig = 
-P
+p
 rojectCo
@@ -2635,16 +2635,23 @@
 ctConfig
+Factory
 .read(md

Update to new ProjectConfig.Factory