<commit_before>package de.cinovo.cloudconductor.server.handler;

import de.cinovo.cloudconductor.api.model.ConfigValue;
import de.cinovo.cloudconductor.server.dao.IConfigValueDAO;
import de.cinovo.cloudconductor.server.dao.hibernate.ConfigValueDAOHib;
import de.cinovo.cloudconductor.server.model.EConfigValue;
import de.cinovo.cloudconductor.server.util.ReservedConfigKeyStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.ws.rs.NotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Copyright 2018 Cinovo AG<br>
 * <br>
 *
 * @author psigloch
 */
@Service
public class ConfigValueHandler {

	@Autowired
	private IConfigValueDAO configValueDAO;

	@Transactional
	public ConfigValue[] get(String template) {
		return this.swapVariables(template, this.getClean(template));
	}

	public ConfigValue[] getClean(String template) {
		Set<ConfigValue> result = new HashSet<>();
		for(EConfigValue ecv : this.configValueDAO.findBy(ConfigValueDAOHib.RESERVED_GLOBAL)) {
			result.add(ecv.toApi());
		}
		if(!template.equalsIgnoreCase(ConfigValueDAOHib.RESERVED_GLOBAL)) {
			for(EConfigValue ecv : this.configValueDAO.findBy(template)) {
				result.add(ecv.toApi());
			}
		}
		result.addAll(ReservedConfigKeyStore.instance.getReservedAsConfigValue());
		return result.toArray(new ConfigValue[0]);
	}

	public ConfigValue[] getCleanUnstacked(String template) {
		List<ConfigValue> result = new ArrayList<>();
		for(EConfigValue ecv : this.configValueDAO.findAll(template)) {
			result.add(ecv.toApi());
		}
		return result.toArray(new ConfigValue[0]);
	}

	public ConfigValue[] get(String template, String service) {
		ConfigValue[] result = this.getClean(template, service);
		return this.swapVariables(template, result);
	}

	public ConfigValue[] getClean(String template, String service) {
		Collection<ConfigValue> result = new HashSet<>(Arrays.asList(this.get(template)));
		for(EConfigValue ecv : this.configValueDAO.findBy(ConfigValueDAOHib.RESERVED_GLOBAL, service)) {
			result.add(ecv.toApi());
		}
		if(!template.equalsIgnoreCase(ConfigValueDAOHib.RESERVED_GLOBAL)) {
			for(EConfigValue ecv : this.configValueDAO.findBy(template, service)) {
				result.add(ecv.toApi());
			}
		}<commit_msg>fixes a bug in global overrides for configs<commit_after>package de.cinovo.cloudconductor.server.handler;

import de.cinovo.cloudconductor.api.model.ConfigValue;
import de.cinovo.cloudconductor.server.dao.IConfigValueDAO;
import de.cinovo.cloudconductor.server.dao.hibernate.ConfigValueDAOHib;
import de.cinovo.cloudconductor.server.model.EConfigValue;
import de.cinovo.cloudconductor.server.util.ReservedConfigKeyStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.ws.rs.NotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Copyright 2018 Cinovo AG<br>
 * <br>
 *
 * @author psigloch
 */
@Service
public class ConfigValueHandler {

	@Autowired
	private IConfigValueDAO configValueDAO;

	@Transactional
	public ConfigValue[] get(String template) {
		return this.swapVariables(template, this.getClean(template));
	}

	public ConfigValue[] getClean(String template) {
		Set<ConfigValue> result = new HashSet<>();
		if(!template.equalsIgnoreCase(ConfigValueDAOHib.RESERVED_GLOBAL)) {
			for(EConfigValue ecv : this.configValueDAO.findBy(template)) {
				result.add(ecv.toApi());
			}
		}
		for(EConfigValue ecv : this.configValueDAO.findBy(ConfigValueDAOHib.RESERVED_GLOBAL)) {
			result.add(ecv.toApi());
		}
		result.addAll(ReservedConfigKeyStore.instance.getReservedAsConfigValue());
		return result.toArray(new ConfigValue[0]);
	}

	public ConfigValue[] getCleanUnstacked(String template) {
		List<ConfigValue> result = new ArrayList<>();
		for(EConfigValue ecv : this.configValueDAO.findAll(template)) {
			result.add(ecv.toApi());
		}
		return result.toArray(new ConfigValue[0]);
	}

	public ConfigValue[] get(String template, String service) {
		ConfigValue[] result = this.getClean(template, service);
		return this.swapVariables(template, result);
	}

	public ConfigValue[] getClean(String template, String service) {
		Collection<ConfigValue> result = new HashSet<>(Arrays.asList(this.get(template)));
		if(!template.equalsIgnoreCase(ConfigValueDAOHib.RESERVED_GLOBAL)) {
			for(EConfigValue ecv : this.configValueDAO.findBy(template, service)) {
				result.add(ecv.toApi());
			}
		}
		for(EConfigValue ecv : this.configValueDAO.findBy(ConfigValueDAOHib.RESERVED_GLOBAL, service)) {
			result.add(ecv.toApi());
		}
{'fraction_non_alphanumeric': 0.12066182405165456, 'fraction_numerical': 0.002421307506053269, 'mean_word_length': 5.962078651685394, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1466682', 'n_tokens_mistral': 1697, 'n_tokens_neox': 1583, 'n_words': 257}