/*
 * [y] hybris Platform
 *
 * Copyright (c) 2000-2016 hybris AG
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of hybris
 * ("Confidential Information"). You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the
 * license agreement you entered into with hybris.
 */
package org.robbins.flashcards.context;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePropertySource;

public class FlashCardsApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext>
{
	private static final Logger LOGGER = LoggerFactory.getLogger(FlashCardsApplicationContextInitializer.class);

	private final String REPOSITORY_PROFILE_PROPERTY = "repository.profile";
	private final String SECURITY_PROFILE_PROPERTY = "security.profile";
	private final String DATABASE_PROFILE_PROPERTY = "database.profile";
	private final String TESTDATA_PROFILE_PROPERTY= "tests.data.profile";

	private final String DEFAULT_REPOSITORY_PROFILE = "spring-data";
	private final String DEFAULT_SECURITY_PROFILE = "security-http";
	private final String DEFAULT_DATABASE_PROFILE = "embedded";
	private final String DEFAULT_TESTDATA_PROFILE = "test-data";

	@Override
	public void initialize(final ConfigurableApplicationContext applicationContext) {
		final ConfigurableEnvironment environment = applicationContext.getEnvironment();

		addPropertiesToEnvironment(environment);
		addSpringProfiles(environment);
	}

	private void addPropertiesToEnvironment(final ConfigurableEnvironment environment)
	{
		addDefaultPropsToEnvironment(environment);
		addOverridePropsToEnvironment(environment);
	}

	private void addSpringProfiles(final ConfigurableEnvironment environment)
	{
		addDatabaseProfile(environment);
		addRepositoryProfile(environment);
		addSecurityProfile(environment);
		addTestDataProfile(environment);
	}

	private void addDefaultPropsToEnvironment(final ConfigurableEnvironment environment)
	{
		final String DEFAULT_PROPS = "default.properties";
		final String LOCATION = "classpath:" + DEFAULT_PROPS;

		addPropsToEnvironment(DEFAULT_PROPS, LOCATION, environment);
	}

	private void addOverridePropsToEnvironment(final ConfigurableEnvironment environment)
	{
		final String OVERRIDE_PROPS = "override.properties";
		final String LOCATION = "classpath:" + OVERRIDE_PROPS;

		addPropsToEnvironment(OVERRIDE_PROPS, LOCATION, environment);
	}

	private void addPropsToEnvironment(final String PROPS, final String LOCATION, final ConfigurableEnvironment environment)
	{
		try
		{
			final Resource defaultPropsResource = new DefaultResourceLoader().getResource(LOCATION);
			LOGGER.info(PROPS + " loaded from {}", defaultPropsResource.getURI());
			final PropertiesPropertySource defaultProps = new ResourcePropertySource(defaultPropsResource);
			environment.getPropertySources().addFirst(defaultProps);
		}
		catch (IOException e)
		{
			LOGGER.info("Unable to find properties file {}", PROPS);
		}
	}

	private void addRepositoryProfile(final ConfigurableEnvironment environment)
	{
		addProfile(REPOSITORY_PROFILE_PROPERTY, DEFAULT_REPOSITORY_PROFILE, environment);
	}

	private void addDatabaseProfile(final ConfigurableEnvironment environment)
	{
		addProfile(DATABASE_PROFILE_PROPERTY, DEFAULT_DATABASE_PROFILE, environment);
	}

	private void addSecurityProfile(final ConfigurableEnvironment environment)
	{
		addProfile(SECURITY_PROFILE_PROPERTY, DEFAULT_SECURITY_PROFILE, environment);
	}

	private void addTestDataProfile(final ConfigurableEnvironment environment)
	{
		addProfile(TESTDATA_PROFILE_PROPERTY, DEFAULT_TESTDATA_PROFILE, environment);
	}

	private void addProfile(final String profileName, final String defaultProfileValue, final ConfigurableEnvironment environment)
	{
		final String repositoryProfile = environment.getProperty(profileName, defaultProfileValue);
		LOGGER.info("Setting Spring Profile: {}={}", profileName, repositoryProfile);
		environment.addActiveProfile(repositoryProfile);
	}
}

@@ -408,37 +408,8 @@
 t;%0A%0A
-import java.io.IOException;%0A%0A
 impo
@@ -425,24 +425,24 @@
 f4j.Logger;%0A
+
 import org.s
@@ -888,16 +888,45 @@
 ource;%0A%0A
+import java.io.IOException;%0A%0A
 public c
@@ -1304,78 +1304,8 @@
 e%22;%0A
-%09private final String DATABASE_PROFILE_PROPERTY = %22database.profile%22;%0A
 %09pri
@@ -1508,69 +1508,8 @@
 p%22;%0A
-%09private final String DEFAULT_DATABASE_PROFILE = %22embedded%22;%0A
 %09pri
@@ -2090,43 +2090,8 @@
 %0A%09%7B%0A
-%09%09addDatabaseProfile(environment);%0A
 %09%09ad
@@ -3367,32 +3367,32 @@
 environment)%0A%09%7B%0A
+
 %09%09addProfile(REP
@@ -3467,171 +3467,8 @@
 %09%7D%0A%0A
-%09private void addDatabaseProfile(final ConfigurableEnvironment environment)%0A%09%7B%0A%09%09addProfile(DATABASE_PROFILE_PROPERTY, DEFAULT_DATABASE_PROFILE, environment);%0A%09%7D%0A%0A
 %09pri

remove 'embedded' profile from ApplicationContextInitializer