001    package org.trails.component.search;
002    
003    import java.lang.reflect.Constructor;
004    
005    import org.apache.commons.lang.ClassUtils;
006    
007    import org.apache.tapestry.form.translator.NumberTranslator;
008    import org.hibernate.criterion.Restrictions;
009    import org.trails.exception.TrailsRuntimeException;
010    
011    public abstract class NumberSearchField extends SimpleSearchField
012    {
013    
014            public Object getTypeSpecificValue()
015            {
016                    /**
017                     * Warning: dorky code ahead. Need to take what will be some
018                     * subclass Number and convert it to the right specific type.
019                     * Since all Number subclasses have a String constructor, this will work
020                     * @note: The Translator should take care of this.
021                     **/
022                    try
023                    {
024                            Class type = ClassUtils.primitiveToWrapper(getPropertyDescriptor().getPropertyType());
025                            Constructor cons = type.getConstructor(new Class[]{String.class});
026                            return cons.newInstance(getValue().toString());
027                    }
028                    catch (Exception ex)
029                    {
030                            throw new TrailsRuntimeException(ex);
031                    }
032            }
033    
034            public NumberTranslator getTranslator()
035            {
036                    NumberTranslator numberTranslator = (NumberTranslator) getValidatorTranslatorService().getTranslator(getPropertyDescriptor());
037                    numberTranslator.setOmitZero(true);
038                    return numberTranslator;
039            }
040    
041    
042            @Override
043            public void buildCriterion()
044            {
045                    if (getValue() != null)
046                    {
047                            getCriteria().add(Restrictions.eq(getPropertyDescriptor().getName(), getTypeSpecificValue()));
048                    }
049            }
050    
051    
052    }