All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.trails.component.search.NumberSearchField Maven / Gradle / Ivy

package org.trails.component.search;

import java.lang.reflect.Constructor;

import org.apache.tapestry.form.translator.NumberTranslator;
import org.hibernate.criterion.Restrictions;
import org.trails.TrailsRuntimeException;

public abstract class NumberSearchField extends SimpleSearchField
{

	public Object getTypeSpecificValue()
	{
		/**
		 * Warning: dorky code ahead. Need to take what will be some
		 * subclass Number and convert it to the right specific type.
		 * Since all Number subclasses have a String constructor, this will work
		 * @note: The Translator should take care of this.
		 **/
		try
		{
			Class type = getPropertyDescriptor().getPropertyType();
			Constructor cons = type.getConstructor(new Class[]{String.class});
			return cons.newInstance(getValue().toString());
		}
		catch (Exception ex)
		{
			throw new TrailsRuntimeException(ex);
		}
	}

	public NumberTranslator getTranslator()
	{
		NumberTranslator numberTranslator = (NumberTranslator) getValidatorTranslatorService().getTranslator(getPropertyDescriptor());
		numberTranslator.setOmitZero(true);
		return numberTranslator;
	}


	@Override
	public void buildCriterion()
	{
		if (getValue() != null)
		{
			getCriteria().add(Restrictions.eq(getPropertyDescriptor().getName(), getTypeSpecificValue()));
		}
	}


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy