package de.terrestris.shogun2.init;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.authentication.AuthenticationProvider;

import de.terrestris.shogun2.model.PersistentObject;
import de.terrestris.shogun2.model.User;
import de.terrestris.shogun2.service.InitializationService;

/**
 * Class to initialize an initial set of content based on bean definitions.
 *
 * @author Nils Bühner
 *
 */
public class ContentInitializer {

	/**
	 * The Logger
	 */
	protected final Logger LOG = Logger.getLogger(getClass());

	/**
	 * Flag symbolizing if content initialization should be active on startup
	 */
	@Autowired
	@Qualifier("shogunInitEnabled")
	private Boolean shogunInitEnabled;

	/**
	 * Initialization Service to init shogun content like users or default
	 * applications.
	 */
	@Autowired
	protected InitializationService initService;

	/**
	 * We use the authenticationProvider to login with the admin user, that will
	 * be created in this initializer.
	 */
	@Autowired
	@Qualifier("shogun2AuthenticationProvider")
	protected AuthenticationProvider authenticationProvider;

	/**
	 * The list of objects that shall be persisted.
	 *
	 * The order of the objects is important here (as one object may require
	 * that another object has been created before to avoid errors like 'object
	 * references an unsaved transient instance save the transient instance
	 * before flushing').
	 */
	@Resource
	@Qualifier("objectsToCreate")
	private List<PersistentObject> objectsToCreate;

	/**
	 * The method called on initialization
	 *
	 */
	public void initializeDatabaseContent() {

		if (this.shogunInitEnabled) {

			LOG.info("Initializing SHOGun2 content");

			for (PersistentObject object : objectsToCreate) {
				if(object instanceof User) {
					// special handling of users to encrypt the password!
					initService.saveUser((User) object);
				} else {
					initService.savePersistentObject(object);
				}
			}

		} else {
			LOG.info("Not initializing anything for SHOGun2.");
		}
	}

}

@@ -250,83 +250,8 @@
 ier;
-%0Aimport org.springframework.security.authentication.AuthenticationProvider;
 %0A%0Aim
@@ -967,248 +967,8 @@
 e;%0A%0A
-%09/**%0A%09 * We use the authenticationProvider to login with the admin user, that will%0A%09 * be created in this initializer.%0A%09 */%0A%09@Autowired%0A%09@Qualifier(%22shogun2AuthenticationProvider%22)%0A%09protected AuthenticationProvider authenticationProvider;%0A%0A
 %09/**

Remove unused auth provider