001    package org.trails.component;
002    
003    import org.apache.commons.logging.Log;
004    import org.apache.commons.logging.LogFactory;
005    import org.apache.tapestry.annotations.ComponentClass;
006    import org.apache.tapestry.annotations.InjectObject;
007    import org.apache.tapestry.annotations.Parameter;
008    import org.apache.tapestry.form.IPropertySelectionModel;
009    import org.hibernate.criterion.DetachedCriteria;
010    import org.trails.persistence.HibernatePersistenceService;
011    
012    /**
013     * This guy interacts with persistence service to produce a Select
014     * containing all the elements of the PropertyDescriptor's type.  If
015     * a criteria is specified, it will filter the list by it.
016     */
017    @ComponentClass(allowBody = false, allowInformalParameters = true)
018    public abstract class HibernateAssociationSelect extends AssociationSelect
019    {
020            private static final Log LOG = LogFactory.getLog(HibernateAssociationSelect.class);
021    
022            /**
023             * @todo: remove when the components reuse issue goes away
024             */
025            @InjectObject("service:trails.hibernate.PersistenceService")
026            public abstract HibernatePersistenceService getHibernatePersistenceService();
027    
028            /**
029             * @todo: remove when the components reuse issue goes away
030             */
031            @Override
032            public HibernatePersistenceService getPersistenceService()
033            {
034                    return getHibernatePersistenceService();
035            }
036    
037            @Parameter(required = false)
038            public abstract DetachedCriteria getCriteria();
039    
040            @Override
041            public IPropertySelectionModel buildSelectionModel()
042            {
043                    if (LOG.isDebugEnabled())
044                    {
045                            LOG.debug("Building propertySelectionModel for " + getClassDescriptor().getDisplayName());
046                    }
047    
048                    DetachedCriteria criteria = getCriteria() != null ? getCriteria() : DetachedCriteria.forClass(getClassDescriptor().getType());
049                    IdentifierSelectionModel selectionModel = new IdentifierSelectionModel(
050                            getPersistenceService().getInstances(getClassDescriptor().getType(), criteria),
051                            getClassDescriptor().getIdentifierDescriptor().getName(),
052                            isAllowNone());
053                    selectionModel.setNoneLabel(getNoneLabel());
054                    return selectionModel;
055            }
056    }