/**
 * Copyright (C) 2011 Callista Enterprise AB <info@callistaenterprise.se>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.callistasoftware.netcare.web.listener;

import javax.servlet.ServletContextEvent;

import org.callistasoftware.netcare.core.entity.CareGiverEntity;
import org.callistasoftware.netcare.core.entity.PatientEntity;
import org.callistasoftware.netcare.core.repository.CareGiverRepository;
import org.callistasoftware.netcare.core.repository.PatientRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class ApplicationListener extends ContextLoaderListener {

	private static final Logger log = LoggerFactory.getLogger(ApplicationListener.class);
	
	@Override
	public void contextInitialized(ServletContextEvent event) {
		super.contextInitialized(event);
		
		log.info("======== NETCARE STARTED ========");
		
		final WebApplicationContext wc = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
		final PatientRepository bean = wc.getBean(PatientRepository.class);
		final CareGiverRepository cgRepo = wc.getBean(CareGiverRepository.class);
		
		final CareGiverEntity cg1 = CareGiverEntity.newEntity("Dr. Test Testgren", "hsa-id-1234");
		
		cgRepo.save(cg1);
		cgRepo.flush();
		final CareGiverEntity cg = CareGiverEntity.newEntity("Doctor Hook", "12345-67");
		
		final PatientEntity p1 = PatientEntity.newEntity("Marcus Krantz", "198205134656", cg);
		bean.save(p1);
		
		final PatientEntity p2 = PatientEntity.newEntity("Peter Larsson", "191212121212", cg);
		bean.save(p2);
		
		final PatientEntity p3 = PatientEntity.newEntity("Arne Andersson", "123456789003", cg);
		p3.getProperties().put("testKey", "[Test Value]");
		bean.save(p3);
		
		final PatientEntity p4 = PatientEntity.newEntity("Anders Arnesson", "123456789004", cg);
		bean.save(p4);
	}
	
	@Override
	public void contextDestroyed(ServletContextEvent event) {
		super.contextDestroyed(event);
		
		log.info("======== NETCARE STOPPED ========");
	}
}

@@ -2069,25 +2069,8 @@
 1);%0A
-%09%09cgRepo.flush();
 %0A%09%09f
@@ -2145,24 +2145,64 @@
 12345-67%22);%0A
+%09%09cgRepo.save(cg);%0A%09%09%0A%09%09cgRepo.flush();%0A
 %09%09%0A%09%09final P
@@ -2274,24 +2274,25 @@
 5134656%22, cg
+1
 );%0A%09%09bean.sa
@@ -2384,24 +2384,25 @@
 2121212%22, cg
+1
 );%0A%09%09bean.sa
@@ -2495,24 +2495,25 @@
 6789003%22, cg
+1
 );%0A%09%09p3.getP
@@ -2664,16 +2664,17 @@
 004%22, cg
+1
 );%0A%09%09bea

Save cg.