/*
 * 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 java.util.List;
import java.util.Locale;

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.i18n.LocaleUtils;
import nuts.core.lang.StringUtils;


/**
 * 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 (StringUtils.isNotEmpty(language) && StringUtils.isNotEmpty(country)) {
			if (!GC.LOCALE_ALL.equals(country)) {
				return LocaleUtils.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();
	}
}
