/*
 * Copyright (C) 2017 the original author or authors.
 *
 * This file is part of jBB Application Project.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
 */

package org.jbb.system.impl.cache.logic;

import org.apache.commons.lang3.Validate;
import org.jbb.lib.cache.CacheProperties;
import org.jbb.lib.cache.JbbCacheManager;
import org.jbb.system.api.model.cache.CacheSettings;
import org.jbb.system.api.service.CacheSettingsService;
import org.jbb.system.impl.cache.data.CacheSettingsImpl;
import org.jbb.system.impl.database.logic.ConnectionToDatabaseEventSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CacheSettingsServiceImpl implements CacheSettingsService {
    private final CacheProperties cacheProperties;
    private final JbbCacheManager jbbCacheManager;
    private final ConnectionToDatabaseEventSender connectionToDatabaseEventSender;

    @Autowired
    public CacheSettingsServiceImpl(CacheProperties cacheProperties,
                                    JbbCacheManager jbbCacheManager,
                                    ConnectionToDatabaseEventSender connectionToDatabaseEventSender) {
        this.cacheProperties = cacheProperties;
        this.jbbCacheManager = jbbCacheManager;
        this.connectionToDatabaseEventSender = connectionToDatabaseEventSender;
    }

    @Override
    public CacheSettings getCacheSettings() {
        return CacheSettingsImpl.builder()
                .applicationCacheEnabled(cacheProperties.applicationCacheEnabled())
                .secondLevelCacheEnabled(cacheProperties.secondLevelCacheEnabled())
                .queryCacheEnabled(cacheProperties.queryCacheEnabled())
                .build();
    }

    @Override
    public void setCacheSettings(CacheSettings newCacheSettings) {
        Validate.notNull(newCacheSettings);

        cacheProperties.setProperty(CacheProperties.APPLICATION_CACHE_ENABLED,
                Boolean.toString(newCacheSettings.isApplicationCacheEnabled()));
        cacheProperties.setProperty(CacheProperties.SECOND_LEVEL_CACHE_ENABLED,
                Boolean.toString(newCacheSettings.isSecondLevelCacheEnabled()));
        cacheProperties.setProperty(CacheProperties.QUERY_CACHE_ENABLED,
                Boolean.toString(newCacheSettings.isQueryCacheEnabled()));

        jbbCacheManager.refresh();

        connectionToDatabaseEventSender.emitEvent();
    }
}

@@ -405,50 +405,8 @@
 es;%0A
-import org.jbb.lib.cache.JbbCacheManager;%0A
 impo
@@ -571,83 +571,8 @@
 pl;%0A
-import org.jbb.system.impl.database.logic.ConnectionToDatabaseEventSender;%0A
 impo
@@ -814,573 +814,141 @@
 es;%0A
-    private final JbbCacheManager jbbCacheManager;%0A    private final ConnectionToDatabaseEventSender connectionToDatabaseEventSender;%0A%0A    @Autowired%0A    public CacheSettingsServiceImpl(CacheProperties cacheProperties,%0A                                    JbbCacheManager jbbCacheManager,%0A                                    ConnectionToDatabaseEventSender connectionToDatabaseEventSender) %7B%0A        this.cacheProperties = cacheProperties;%0A        this.jbbCacheManager = jbbCacheManager;%0A        this.connectionToDatabaseEventSender = connectionToDatabaseEventSender
+%0A    @Autowired%0A    public CacheSettingsServiceImpl(CacheProperties cacheProperties) %7B%0A        this.cacheProperties = cacheProperties
 ;%0A  
@@ -1926,98 +1926,8 @@
 )));
-%0A%0A        jbbCacheManager.refresh();%0A%0A        connectionToDatabaseEventSender.emitEvent();
 %0A   

simplify CacheSettingsService