// Generated by: hibernate/SpringHibernateDaoImpl.vsl in andromda-spring-cartridge.
// license-header java merge-point
/**
 * This is only generated once! It will never be overwritten.
 * You can (and have to!) safely modify it by hand.
 */
package org.phoenixctms.ctsms.domain;

import java.util.Collection;

import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.phoenixctms.ctsms.query.CategoryCriterion;
import org.phoenixctms.ctsms.query.CriteriaUtil;
import org.phoenixctms.ctsms.util.DefaultSettings;
import org.phoenixctms.ctsms.util.SettingCodes;
import org.phoenixctms.ctsms.util.Settings;
import org.phoenixctms.ctsms.util.Settings.Bundle;
import org.phoenixctms.ctsms.vo.BankIdentificationVO;

/**
 * @see BankIdentification
 */
public class BankIdentificationDaoImpl
		extends BankIdentificationDaoBase {

	private static void applyBankIdentificationCriterions(org.hibernate.Criteria bankIdentificationCriteria, String bankCodeNumberPrefix, String bicPrefix, String bankNameInfix) {
		CategoryCriterion.applyAnd(bankIdentificationCriteria,
				new CategoryCriterion(bankCodeNumberPrefix, "bankCodeNumber", MatchMode.START),
				new CategoryCriterion(bicPrefix, "bic", MatchMode.START),
				new CategoryCriterion(bankNameInfix, "bankName", MatchMode.ANYWHERE));
	}

	/**
	 * @inheritDoc
	 */
	@Override
	public BankIdentification bankIdentificationVOToEntity(BankIdentificationVO bankIdentificationVO) {
		BankIdentification entity = this.loadBankIdentificationFromBankIdentificationVO(bankIdentificationVO);
		this.bankIdentificationVOToEntity(bankIdentificationVO, entity, true);
		return entity;
	}

	/**
	 * @inheritDoc
	 */
	@Override
	public void bankIdentificationVOToEntity(
			BankIdentificationVO source,
			BankIdentification target,
			boolean copyIfNull) {
		super.bankIdentificationVOToEntity(source, target, copyIfNull);
	}

	private org.hibernate.Criteria createBankIdentificationCriteria() {
		org.hibernate.Criteria bankIdentificationCriteria = this.getSession().createCriteria(BankIdentification.class);
		bankIdentificationCriteria.setCacheable(true);
		return bankIdentificationCriteria;
	}

	/**
	 * @inheritDoc
	 */
	@Override
	protected Collection<String> handleFindBankCodeNumbers(String bankCodeNumberPrefix, String bicPrefix, String bankNameInfix, Integer limit) {
		org.hibernate.Criteria bankIdentificationCriteria = createBankIdentificationCriteria();
		applyBankIdentificationCriterions(bankIdentificationCriteria, bankCodeNumberPrefix, bicPrefix, bankNameInfix);
		bankIdentificationCriteria.add(Restrictions.not(Restrictions.or(Restrictions.eq("bankCodeNumber", ""), Restrictions.isNull("bankCodeNumber"))));
		bankIdentificationCriteria.addOrder(Order.asc("bankCodeNumber"));
		bankIdentificationCriteria.setProjection(Projections.distinct(Projections.property("bankCodeNumber")));
		CriteriaUtil.applyLimit(limit, Settings.getIntNullable(SettingCodes.BANK_CODE_NUMBER_AUTOCOMPLETE_DEFAULT_RESULT_LIMIT, Bundle.SETTINGS,
				DefaultSettings.BANK_CODE_NUMBER_AUTOCOMPLETE_DEFAULT_RESULT_LIMIT), bankIdentificationCriteria);
		return bankIdentificationCriteria.list();
	}

	/**
	 * @inheritDoc
	 */
	@Override
	protected Collection<BankIdentificationVO> handleFindBankIdentifications(String bankCodeNumberPrefix, String bicPrefix, String bankNameInfix, Integer limit) {
		org.hibernate.Criteria bankIdentificationCriteria = createBankIdentificationCriteria();
		applyBankIdentificationCriterions(bankIdentificationCriteria, bankCodeNumberPrefix, bicPrefix, bankNameInfix);
		bankIdentificationCriteria.addOrder(Order.asc("bankName"));
		CriteriaUtil.applyLimit(limit, Settings.getIntNullable(SettingCodes.BANK_IDENTIFICATION_AUTOCOMPLETE_DEFAULT_RESULT_LIMIT, Bundle.SETTINGS,
				DefaultSettings.BANK_IDENTIFICATION_AUTOCOMPLETE_DEFAULT_RESULT_LIMIT), bankIdentificationCriteria);
		return bankIdentificationCriteria.list();
	}

	/**
	 * @inheritDoc
	 */
	@Override
	protected Collection<String> handleFindBankNames(String bankCodeNumberPrefix, String bicPrefix, String bankNameInfix, Integer limit) {
		org.hibernate.Criteria bankIdentificationCriteria = createBankIdentificationCriteria();
		applyBankIdentificationCriterions(bankIdentificationCriteria, bankCodeNumberPrefix, bicPrefix, bankNameInfix);
		bankIdentificationCriteria.add(Restrictions.not(Restrictions.or(Restrictions.eq("bankName", ""), Restrictions.isNull("bankName"))));
		bankIdentificationCriteria.addOrder(Order.asc("bankName"));
		bankIdentificationCriteria.setProjection(Projections.distinct(Projections.property("bankName")));
		CriteriaUtil.applyLimit(limit,
				Settings.getIntNullable(SettingCodes.BANK_NAME_AUTOCOMPLETE_DEFAULT_RESULT_LIMIT, Bundle.SETTINGS, DefaultSettings.BANK_NAME_AUTOCOMPLETE_DEFAULT_RESULT_LIMIT),
				bankIdentificationCriteria);
		return bankIdentificationCriteria.list();
	}

	/**
	 * @inheritDoc
	 */
	@Override
	protected Collection<String> handleFindBics(String bankCodeNumberPrefix, String bicPrefix, String bankNameInfix, Integer limit) {
		org.hibernate.Criteria bankIdentificationCriteria = createBankIdentificationCriteria();
		applyBankIdentificationCriterions(bankIdentificationCriteria, bankCodeNumberPrefix, bicPrefix, bankNameInfix);
		bankIdentificationCriteria.add(Restrictions.not(Restrictions.or(Restrictions.eq("bic", ""), Restrictions.isNull("bic"))));
		bankIdentificationCriteria.addOrder(Order.asc("bic"));
		bankIdentificationCriteria.setProjection(Projections.distinct(Projections.property("bic")));
		CriteriaUtil.applyLimit(limit,
				Settings.getIntNullable(SettingCodes.BIC_AUTOCOMPLETE_DEFAULT_RESULT_LIMIT, Bundle.SETTINGS, DefaultSettings.BIC_AUTOCOMPLETE_DEFAULT_RESULT_LIMIT),
				bankIdentificationCriteria);
		return bankIdentificationCriteria.list();
	}

	/**
	 * Retrieves the entity object that is associated with the specified value object
	 * from the object store. If no such entity object exists in the object store,
	 * a new, blank entity is created
	 */
	private BankIdentification loadBankIdentificationFromBankIdentificationVO(BankIdentificationVO bankIdentificationVO) {
		BankIdentification bankIdentification = null;
		Long id = bankIdentificationVO.getId();
		if (id != null) {
			bankIdentification = this.load(id);
		}
		if (bankIdentification == null) {
			bankIdentification = BankIdentification.Factory.newInstance();
		}
		return bankIdentification;
	}

	/**
	 * @inheritDoc
	 */
	@Override
	public BankIdentificationVO toBankIdentificationVO(final BankIdentification entity) {
		return super.toBankIdentificationVO(entity);
	}

	/**
	 * @inheritDoc
	 */
	@Override
	public void toBankIdentificationVO(
			BankIdentification source,
			BankIdentificationVO target) {
		super.toBankIdentificationVO(source, target);
	}
}