<commit_before>package org.carlspring.strongbox.configuration;

import org.carlspring.strongbox.xml.parsers.GenericParser;

import javax.annotation.PostConstruct;
import javax.xml.bind.JAXBException;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Component;

/**
 * Repository configuration used custom serialization mechanism so we decided to decouple repository configuration cache
 * management logic.
 *
 * @author <NAME>
 */
@Component
public class ConfigurationCache
{

    private static final Logger logger = LoggerFactory.getLogger(ConfigurationRepository.class);

    @Autowired
    CacheManager cacheManager;

    private GenericParser<Configuration> parser;

    private Cache configurationCache;

    @PostConstruct
    public synchronized void init()
    {
        parser = new GenericParser<>();
        try
        {
            parser.setContext(Configuration.class);
        }
        catch (JAXBException e)
        {
            logger.error("Unable to set JAXB context", e);
        }

        configurationCache = cacheManager.getCache("configuration");
        if (configurationCache == null)
        {
            throw new RuntimeException("Unable to get configuration cache");
        }
    }

    public Configuration save(Configuration configuration)
    {
        if (configuration == null)
        {
            logger.warn("Unable to save NULL configuration.");
            return null;
        }

        // At the database layer actual saving could be replaced with in-memory database or even cache
        // so we need to check the id field and create it if necessary.
        //
        // Because we actually have only one instance of configuration object id could have any value
        // and ignore internal OrientDb ID format.
        if (configuration.getId() == null)
        {
            //configuration.setId("#0:0");
        }

        try<commit_msg>Request for non-null id's for configuration cache.<commit_after>package org.carlspring.strongbox.configuration;

import org.carlspring.strongbox.xml.parsers.GenericParser;

import javax.annotation.PostConstruct;
import javax.xml.bind.JAXBException;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Component;

/**
 * Repository configuration used custom serialization mechanism so we decided to decouple repository configuration cache
 * management logic.
 *
 * @author <NAME>
 */
@Component
public class ConfigurationCache
{

    private static final Logger logger = LoggerFactory.getLogger(ConfigurationRepository.class);

    @Autowired
    CacheManager cacheManager;

    private GenericParser<Configuration> parser;

    private Cache configurationCache;

    @PostConstruct
    public synchronized void init()
    {
        parser = new GenericParser<>();
        try
        {
            parser.setContext(Configuration.class);
        }
        catch (JAXBException e)
        {
            logger.error("Unable to set JAXB context", e);
        }

        configurationCache = cacheManager.getCache("configuration");
        if (configurationCache == null)
        {
            throw new RuntimeException("Unable to get configuration cache");
        }
    }

    public Configuration save(Configuration configuration)
    {
        if (configuration == null)
        {
            logger.warn("Unable to save NULL configuration.");
            return null;
        }

        if (configuration.getId() == null)
        {
            throw new IllegalArgumentException("Unable to save configuration into cache: id is null");
        }

        try
{'fraction_non_alphanumeric': 0.08137793310034948, 'fraction_numerical': 0.0014977533699450823, 'mean_word_length': 2.841802492809204, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '5331521', 'n_tokens_mistral': 969, 'n_tokens_neox': 956, 'n_words': 282}