// 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.config;

import com.google.common.base.Objects;
import com.google.common.collect.Iterables;
import com.google.gerrit.server.git.ProjectConfig;
import com.google.gerrit.server.project.ProjectState;

import org.eclipse.jgit.lib.Config;

import java.util.Arrays;
import java.util.Set;

public class PluginConfig {
  private static final String PLUGIN = "plugin";

  private final String pluginName;
  private Config cfg;
  private final ProjectConfig projectConfig;

  public PluginConfig(String pluginName, Config cfg) {
    this(pluginName, cfg, null);
  }

  public PluginConfig(String pluginName, Config cfg, ProjectConfig projectConfig) {
    this.pluginName = pluginName;
    this.cfg = cfg;
    this.projectConfig = projectConfig;
  }

  PluginConfig withInheritance(ProjectState.Factory projectStateFactory) {
    if (projectConfig == null) {
      return this;
    }

    ProjectState state = projectStateFactory.create(projectConfig);
    ProjectState parent = Iterables.getFirst(state.parents(), null);
    if (parent != null) {
      PluginConfig parentPluginConfig =
          parent.getConfig().getPluginConfig(pluginName)
              .withInheritance(projectStateFactory);
      Set<String> allNames = cfg.getNames(PLUGIN, pluginName);
      cfg = new Config(cfg);
      for (String name : parentPluginConfig.cfg.getNames(PLUGIN, pluginName)) {
        if (!allNames.contains(name)) {
          cfg.setStringList(PLUGIN, pluginName, name, Arrays
              .asList(parentPluginConfig.cfg.getStringList(PLUGIN, pluginName, name)));
        }
      }
    }
    return this;
  }

  public String getString(String name) {
    return cfg.getString(PLUGIN, pluginName, name);
  }

  public String getString(String name, String defaultValue) {
    if (defaultValue == null) {
      return cfg.getString(PLUGIN, pluginName, name);
    } else {
      return Objects.firstNonNull(cfg.getString(PLUGIN, pluginName, name), defaultValue);
    }
  }

  public String[] getStringList(String name) {
    return cfg.getStringList(PLUGIN, pluginName, name);
  }

  public int getInt(String name, int defaultValue) {
    return cfg.getInt(PLUGIN, pluginName, name, defaultValue);
  }

  public long getLong(String name, long defaultValue) {
    return cfg.getLong(PLUGIN, pluginName, name, defaultValue);
  }

  public boolean getBoolean(String name, boolean defaultValue) {
    return cfg.getBoolean(PLUGIN, pluginName, name, defaultValue);
  }

  public <T extends Enum<?>> T getEnum(String name, T defaultValue) {
    return cfg.getEnum(PLUGIN, pluginName, name, defaultValue);
  }

  public <T extends Enum<?>> T getEnum(T[] all, String name, T defaultValue) {
    return cfg.getEnum(all, PLUGIN, pluginName, name, defaultValue);
  }
}

@@ -683,16 +683,55 @@
 bjects;%0A
+import com.google.common.base.Strings;%0A
 import c
@@ -934,16 +934,39 @@
 Arrays;%0A
+import java.util.List;%0A
 import j
@@ -2664,101 +2664,551 @@
 lic 
-String%5B%5D getStringList(String name) %7B%0A    return cfg.getStringList(PLUGIN, pluginName, name);
+void setString(String name, String value) %7B%0A    if (Strings.isNullOrEmpty(value)) %7B%0A      cfg.unset(PLUGIN, pluginName, name);%0A    %7D else %7B%0A      cfg.setString(PLUGIN, pluginName, name, value);%0A    %7D%0A  %7D%0A%0A  public String%5B%5D getStringList(String name) %7B%0A    return cfg.getStringList(PLUGIN, pluginName, name);%0A  %7D%0A%0A  public void setStringList(String name, List%3CString%3E values) %7B%0A    if (values == null %7C%7C values.isEmpty()) %7B%0A      cfg.unset(PLUGIN, pluginName, name);%0A    %7D else %7B%0A      cfg.setStringList(PLUGIN, pluginName, name, values);%0A    %7D
 %0A  %7D
@@ -3322,32 +3322,133 @@
 ultValue);%0A  %7D%0A%0A
+  public void setInt(String name, int value) %7B%0A    cfg.setInt(PLUGIN, pluginName, name, value);%0A  %7D%0A%0A
   public long ge
@@ -3548,32 +3548,136 @@
 ultValue);%0A  %7D%0A%0A
+  public void setLong(String name, long value) %7B%0A    cfg.setLong(PLUGIN, pluginName, name, value);%0A  %7D%0A%0A
   public boolean
@@ -3789,32 +3789,145 @@
 ultValue);%0A  %7D%0A%0A
+  public void setBoolean(String name, boolean value) %7B%0A    cfg.setBoolean(PLUGIN, pluginName, name, value);%0A  %7D%0A%0A
   public %3CT exte
@@ -4041,32 +4041,153 @@
 ultValue);%0A  %7D%0A%0A
+  public %3CT extends Enum%3C?%3E%3E void setEnum(String name, T value) %7B%0A    cfg.setEnum(PLUGIN, pluginName, name, value);%0A  %7D%0A%0A
   public %3CT exte
@@ -4321,11 +4321,92 @@
 ue);%0A  %7D
+%0A%0A  public void unset(String name) %7B%0A    cfg.unset(PLUGIN, pluginName, name);%0A  %7D
 %0A%7D%0A

Support to update project plugin configuration