/*
 * Copyright 2002-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/
package anyframe.core.hibernate;

import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;

import anyframe.common.util.DateUtil;
import anyframe.sample.model.bidirection.Category;
import anyframe.sample.model.bidirection.Country;
import anyframe.sample.model.bidirection.Movie;

public class SetUpInitData {

	/**
	 * 데이터 조회를 수행하기 위한 초기 데이터를 셋팅한다. <br>
	 * Category : Movie = m:n으로 양방향 관계로 정의되어 있으며, Category 클래스가 Master 역할 수행
	 * Country : Movie = 1:m으로 양방향 관계로 정의되어 있으며, Movie 클래스가 Master 역할 수행
	 * <ul>
	 * <li>Category : [CTGR-0001, Romantic, Romantic genre], [CTGR-0002,
	 * Comedy, Comedy genre], [CTGR-0003,Horror,Thriller genre]</li>
	 * <li>Country : [COUNTRY-0001, KR, Korea], [COUNTRY-0002, JP, Japan],
	 * [COUNTRY-0003, US, U.S.A]</li>
	 * <li>Movie : [MV-00001, My Sassy Girl, Jaeyong Gwak, 2001-07-27],
	 * [MV-00002, My Little Bride, Hojun Kim, 2004-04-02], [MV-00003, Ring2,
	 * Hideo Nakata, 2005-06-03]</li>
	 * </ul>
	 * 
	 * @throws Exception
	 */
	public static void initializeData(Session session) throws Exception {
		// 1. insert a new category information
		Category category1 = new Category();
		category1.setCategoryId("CTGR-0001");
		category1.setCategoryName("Romantic");
		category1.setCategoryDesc("Romantic genre");
		session.save(category1);

		Category category2 = new Category();
		category2.setCategoryId("CTGR-0002");
		category2.setCategoryName("Comedy");
		category2.setCategoryDesc("Comedy genre");
		session.save(category2);

		Category category3 = new Category();
		category3.setCategoryId("CTGR-0003");
		category3.setCategoryName("Horror");
		category3.setCategoryDesc("Thriller genre");
		session.save(category3);

		Category category4 = new Category();
		category4.setCategoryId("CTGR-0004");
		category4.setCategoryName("SF");
		category4.setCategoryDesc("Fantasy genre");
		session.save(category4);

		// 2. insert a country information with movies
		Movie movie1 = new Movie();
		movie1.setMovieId("MV-00001");
		movie1.setDirector("Jaeyong Gwak");
		movie1.setReleaseDate(DateUtil.string2Date("2001-07-27", "yyyy-MM-dd"));
		movie1.setTitle("My Sassy Girl");

		Set categories = new HashSet();
		categories.add(category1);
		categories.add(category2);
		movie1.setCategories(categories);

		Movie movie2 = new Movie();
		movie2.setMovieId("MV-00002");
		movie2.setDirector("Hojun Kim");
		movie2.setReleaseDate(DateUtil.string2Date("2004-04-02", "yyyy-MM-dd"));
		movie2.setTitle("My Little Bride");

		categories = new HashSet();
		categories.add(category1);
		categories.add(category2);
		movie2.setCategories(categories);

		Country country1 = new Country();
		String countryCode = "COUNTRY-0001";
		country1.setCountryCode(countryCode);
		country1.setCountryId("KR");
		country1.setCountryName("Korea");

		Set movies = new HashSet();
		movie1.setCountry(country1);
		movies.add(movie1);
		movie2.setCountry(country1);
		movies.add(movie2);
		country1.setMovies(movies);

		session.save(country1);

		Movie movie3 = new Movie();
		movie3.setMovieId("MV-00003");
		movie3.setDirector("Hideo Nakata");
		movie3.setReleaseDate(DateUtil.string2Date("2005-06-03", "yyyy-MM-dd"));
		movie3.setTitle("Ring 2");

		categories = new HashSet();
		categories.add(category3);
		movie3.setCategories(categories);

		Country country2 = new Country();
		countryCode = "COUNTRY-0002";
		country2.setCountryCode(countryCode);
		country2.setCountryId("JP");
		country2.setCountryName("Japan");

		movies = new HashSet();
		movie3.setCountry(country2);
		movies.add(movie3);
		country2.setMovies(movies);

		session.save(country2);

		Country country3 = new Country();
		countryCode = "COUNTRY-0003";
		country3.setCountryCode(countryCode);
		country3.setCountryId("US");
		country3.setCountryName("U.S.A");

		session.save(country3);
	}

}

