/*
 * This file is part of Nuts Framework.
 * Copyright(C) 2009-2012 Nuts Develop Team.
 *
 * Nuts Framework is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License any later version.
 *
 * Nuts Framework 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Nuts Framework. If not, see <http://www.gnu.org/licenses/>.
 */
package nuts.aems.action.template;

import nuts.aems.constant.AC;
import nuts.aems.constant.GC;
import nuts.aems.constant.RC;
import nuts.aems.model.bean.Template;
import nuts.aems.model.example.TemplateExample;
import nuts.core.lang.Locales;
import nuts.core.lang.Strings;

import java.util.List;
import java.util.Locale;


/**
 * TemplateExAction
 */
public class TemplateExAction extends TemplateAction {

	/**
	 * Constructor
	 */
	public TemplateExAction() {
		super();
	}

	/**
	 * queryListByExample
	 * @param example example
	 */
	@Override
	protected void queryListByExample(TemplateExample example) throws Exception {
		example.source().exclude();
		super.queryListByExample(example);
	}

	/**
	 * isValidLocale
	 * @param language lanaguage
	 * @param country country
	 * @return true - if locale is valid
	 */
	public boolean isValidLocale(String language, String country) {
		if (Strings.isNotEmpty(language) && Strings.isNotEmpty(country)) {
			if (!GC.LOCALE_ALL.equals(country)) {
				return Locales.isAvailableLocale(new Locale(language, country));
			}
		}
		return true;
	}
	
	private void addReloadMessage() {
		addApplicationMessage(AC.TEMPLATE_RELOAD, 
			getText(RC.MESSAGE_TEMPLATE_RELOAD));
	}

	/**
	 * commitInsert
	 * @param data data
	 */
	@Override
	protected void commitInsert(Template data) throws Exception {
		super.commitInsert(data);

//		Application.getDatabaseTemplateLoader().putTemplate(data.getName(), data.getLanguage(),
//			data.getCountry(), null, data.getSource(), data.getUtime().getTime());

		addReloadMessage();
	}

	/**
	 * commitUpdate
	 * @param data data
	 * @param srcData source data
	 */
	@Override
	protected void commitUpdate(Template data, Template srcData) throws Exception {
		super.commitUpdate(data, srcData);

//		Application.getDatabaseTemplateLoader().putTemplate(data.getName(), data.getLanguage(),
//			data.getCountry(), null, data.getSource(), data.getUtime().getTime());
		addReloadMessage();
	}

	/**
	 * commitDelete
	 * @param data data
	 * @param srcData source data
	 */
	@Override
	protected void commitDelete(Template data) throws Exception {
		super.commitDelete(data);

//		Application.getDatabaseTemplateLoader().putTemplate(data.getName(), data.getLanguage(),
//			data.getCountry(), null, null);
		addReloadMessage();
	}

	/**
	 * commitBulkDelete
	 * @param dataList data list
	 * @throws Exception if an error occurs
	 */
	@Override
	protected void commitBulkDelete(List<Template> dataList) throws Exception {
		super.commitBulkDelete(dataList);
		
//		for (Template data : dataList) {
//			Application.getDatabaseTemplateLoader().putTemplate(data.getName(), data.getLanguage(),
//				data.getCountry(), null, null);
//		}
		addReloadMessage();
	}
}
