001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.tide.spring.data;
022    
023    import java.io.Serializable;
024    import java.util.Map;
025    
026    import javax.persistence.EntityManager;
027    import javax.persistence.metamodel.ManagedType;
028    
029    import org.springframework.data.domain.Page;
030    import org.springframework.data.domain.Pageable;
031    import org.springframework.data.jpa.domain.Specification;
032    import org.springframework.data.jpa.repository.support.JpaEntityInformation;
033    import org.springframework.data.jpa.repository.support.JpaEntityInformationSupport;
034    import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
035    
036    
037    public class FilterableJpaRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements FilterableJpaRepository<T, ID> {
038    
039            private JpaEntityInformation<T, ?> entityInformation;
040            private EntityManager entityManager;
041    
042            public FilterableJpaRepositoryImpl(Class<T> domainClass, EntityManager entityManager) {
043                    super(domainClass, entityManager);
044                    
045                    this.entityInformation = JpaEntityInformationSupport.getMetadata(domainClass, entityManager);
046                this.entityManager = entityManager;
047            }
048            
049            @SuppressWarnings("unchecked")
050            public Page<T> findByFilter(Object filter, Pageable pageable) {
051                    if (filter == null)
052                            return findAll(pageable);
053                    
054                    ManagedType<?> filterType = entityManager.getMetamodel().managedType(entityInformation.getJavaType());            
055                    Specification<T> specification = null;
056                    
057                    if (filter.getClass().equals(filterType.getJavaType()))
058                            specification = FilterExampleSpecification.byExample(entityManager.getMetamodel(), filter);             
059                    else if (filter instanceof Map<?, ?>)
060                            specification = FilterMapSpecification.byMap((Map<String, Object>)filter);
061                    else
062                            specification = FilterBeanSpecification.byBean(filter);
063                    
064                    return findAll(specification, pageable);
065            }
066    }