001    /**
002     *   GRANITE DATA SERVICES
003     *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004     *
005     *   This file is part of the Granite Data Services Platform.
006     *
007     *   Granite Data Services is free software; you can redistribute it and/or
008     *   modify it under the terms of the GNU Lesser General Public
009     *   License as published by the Free Software Foundation; either
010     *   version 2.1 of the License, or (at your option) any later version.
011     *
012     *   Granite Data Services is distributed in the hope that it will be useful,
013     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015     *   General Public License for more details.
016     *
017     *   You should have received a copy of the GNU Lesser General Public
018     *   License along with this library; if not, write to the Free Software
019     *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020     *   USA, or see <http://www.gnu.org/licenses/>.
021     */
022    package org.granite.tide.spring.data;
023    
024    import java.io.Serializable;
025    
026    import javax.persistence.EntityManager;
027    
028    import org.springframework.data.jpa.repository.JpaRepository;
029    import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
030    import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean;
031    import org.springframework.data.repository.core.RepositoryMetadata;
032    import org.springframework.data.repository.core.support.RepositoryFactorySupport;
033    
034    public class FilterableJpaRepositoryFactoryBean<R extends JpaRepository<T, I>, T, I extends Serializable> extends JpaRepositoryFactoryBean<R, T, I> {
035    
036            @Override
037            protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
038                    return new FilterableJpaRepositoryFactory<T, I>(entityManager);
039            }
040            
041            private static class FilterableJpaRepositoryFactory<T, I extends Serializable> extends JpaRepositoryFactory {
042    
043                    private EntityManager entityManager;
044    
045                    public FilterableJpaRepositoryFactory(EntityManager entityManager) {
046                            super(entityManager);
047    
048                            this.entityManager = entityManager;
049                    }
050                    
051                    @SuppressWarnings("unchecked")
052                    @Override
053                    protected Object getTargetRepository(RepositoryMetadata metadata) {
054                            return new FilterableJpaRepositoryImpl<T, I>((Class<T>)metadata.getDomainType(), entityManager);
055                    }
056    
057                    @Override
058                    protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
059                            return FilterableJpaRepository.class;
060                    }
061            }
062    }