// 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.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;

import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.phoenixctms.ctsms.compare.EntityIDComparator;
import org.phoenixctms.ctsms.compare.VOIDComparator;
import org.phoenixctms.ctsms.util.L10nUtil;
import org.phoenixctms.ctsms.util.L10nUtil.Locales;
import org.phoenixctms.ctsms.vo.TrialStatusActionVO;
import org.phoenixctms.ctsms.vo.TrialStatusTypeVO;

/**
 * @see TrialStatusType
 */
public class TrialStatusTypeDaoImpl
		extends TrialStatusTypeDaoBase {

	private final static EntityIDComparator ID_COMPARATOR = new EntityIDComparator<TrialStatusType>(false);
	private final static VOIDComparator ACTION_ID_COMPARATOR = new VOIDComparator<TrialStatusActionVO>(false);

	private org.hibernate.Criteria createTrialStatusTypeCriteria() {
		org.hibernate.Criteria trialStatusTypeCriteria = this.getSession().createCriteria(TrialStatusType.class);
		trialStatusTypeCriteria.setCacheable(true);
		return trialStatusTypeCriteria;
	}

	/**
	 * @inheritDoc
	 */
	@Override
	protected Collection<TrialStatusType> handleFindInitialStates() {
		org.hibernate.Criteria trialStatusTypeCriteria = createTrialStatusTypeCriteria();
		trialStatusTypeCriteria.add(Restrictions.eq("initial", true));
		trialStatusTypeCriteria.addOrder(Order.asc("id"));
		return trialStatusTypeCriteria.list();
	}

	/**
	 * @inheritDoc
	 */
	@Override
	protected Collection<TrialStatusType> handleFindTransitions(Long statusTypeId) {
		TrialStatusType statusType = this.load(statusTypeId);
		Iterator<TrialStatusType> it = null;
		if (statusType != null) {
			it = statusType.getTransitions().iterator();
		}
		ArrayList<TrialStatusType> result = new ArrayList<TrialStatusType>();
		if (it != null) { // force load:
			while (it.hasNext()) {
				result.add(this.load(it.next().getId()));
			}
		}
		Collections.sort(result, ID_COMPARATOR);
		return result;
	}

	/**
	 * 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 TrialStatusType loadTrialStatusTypeFromTrialStatusTypeVO(TrialStatusTypeVO trialStatusTypeVO) {
		TrialStatusType trialStatusType = null;
		Long id = trialStatusTypeVO.getId();
		if (id != null) {
			trialStatusType = this.load(id);
		}
		if (trialStatusType == null) {
			trialStatusType = TrialStatusType.Factory.newInstance();
		}
		return trialStatusType;
	}

	private ArrayList<TrialStatusActionVO> toTrialStatusActionVOCollection(Collection<TrialStatusAction> actions) {
		// related to http://forum.andromda.org/viewtopic.php?t=4288
		TrialStatusActionDao trialStatusActionDao = this.getTrialStatusActionDao();
		ArrayList<TrialStatusActionVO> result = new ArrayList<TrialStatusActionVO>(actions.size());
		Iterator<TrialStatusAction> it = actions.iterator();
		while (it.hasNext()) {
			result.add(trialStatusActionDao.toTrialStatusActionVO(it.next()));
		}
		Collections.sort(result, ACTION_ID_COMPARATOR);
		return result;
	}

	/**
	 * @inheritDoc
	 */
	@Override
	public TrialStatusTypeVO toTrialStatusTypeVO(final TrialStatusType entity) {
		return super.toTrialStatusTypeVO(entity);
	}

	/**
	 * @inheritDoc
	 */
	@Override
	public void toTrialStatusTypeVO(
			TrialStatusType source,
			TrialStatusTypeVO target) {
		super.toTrialStatusTypeVO(source, target);
		target.setName(L10nUtil.getTrialStatusTypeName(Locales.USER, source.getNameL10nKey()));
		target.setActions(toTrialStatusActionVOCollection(source.getActions()));
	}

	/**
	 * @inheritDoc
	 */
	@Override
	public TrialStatusType trialStatusTypeVOToEntity(TrialStatusTypeVO trialStatusTypeVO) {
		TrialStatusType entity = this.loadTrialStatusTypeFromTrialStatusTypeVO(trialStatusTypeVO);
		this.trialStatusTypeVOToEntity(trialStatusTypeVO, entity, true);
		return entity;
	}

	/**
	 * @inheritDoc
	 */
	@Override
	public void trialStatusTypeVOToEntity(
			TrialStatusTypeVO source,
			TrialStatusType target,
			boolean copyIfNull) {
		super.trialStatusTypeVOToEntity(source, target, copyIfNull);
		Collection actions = source.getActions();
		if (actions.size() > 0) {
			actions = new ArrayList(actions); //prevent changing VO
			this.getTrialStatusActionDao().trialStatusActionVOToEntityCollection(actions);
			target.setActions(actions);
		} else if (copyIfNull) {
			target.getActions().clear();
		}
	}
}