Last active
April 10, 2017 19:40
-
-
Save guilherfp/5edb4a1f874903cf7b894bdf32982857 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.Serializable; | |
import java.lang.reflect.Field; | |
import java.util.List; | |
import java.util.Optional; | |
import java.util.function.Supplier; | |
import java.util.stream.Collectors; | |
import java.util.stream.Stream; | |
import org.hibernate.Criteria; | |
import org.hibernate.Query; | |
import org.hibernate.SQLQuery; | |
import org.hibernate.Session; | |
import org.hibernate.SessionFactory; | |
import org.hibernate.criterion.Projections; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.data.domain.Page; | |
import org.springframework.data.domain.Pageable; | |
import org.springframework.data.domain.Sort.Order; | |
import org.springframework.data.repository.support.PageableExecutionUtils; | |
import org.springframework.data.repository.support.PageableExecutionUtils.TotalSupplier; | |
import fgw.fin.domain.catergoria.Categoria; | |
import fgw.fin.domain.conta.Conta; | |
import fgw.fin.domain.criterios.Criterios; | |
import fgw.fin.infra.shared.ReflectionUtils; | |
/** | |
* @author guilherme.pacheco | |
*/ | |
public class HibernateRepository<T> { | |
private final SessionFactory sessionFactory; | |
private final List<String> fields; | |
private final Class<T> clazz; | |
@Autowired | |
public HibernateRepository(Class<T> clazz, SessionFactory sessionFactory) { | |
this.sessionFactory = sessionFactory; | |
fields = fields(clazz); | |
this.clazz = clazz; | |
} | |
private List<String> fields(Class<?> clazz) { | |
Stream<Field> stream = ReflectionUtils.allFields(clazz).stream(); | |
return stream.map(Field::getName).collect(Collectors.toList()); | |
} | |
public Session session() { | |
return sessionFactory.getCurrentSession(); | |
} | |
public Criteria criteria() { | |
return session().createCriteria(clazz); | |
} | |
public T byId(Serializable id) { | |
return session().byId(clazz).load(id); | |
} | |
public void save(T obj) { | |
session().save(obj); | |
} | |
public void delete(Categoria categoria) { | |
session().delete(categoria); | |
} | |
public void saveFlush(T obj) { | |
Session session = session(); | |
session.save(obj); | |
session.flush(); | |
} | |
public void saveOrUpdate(Conta conta) { | |
session().saveOrUpdate(conta); | |
} | |
public List<T> list() { | |
return list(criteria()); | |
} | |
@SuppressWarnings("unchecked") | |
private List<T> list(Criteria criteria) { | |
return criteria.list(); | |
} | |
public Query query(String queryString) { | |
return sessionFactory.getCurrentSession().createQuery(queryString); | |
} | |
public Page<T> page(Pageable pageable) { | |
return page(pageable, list(pageable), () -> count(criteria())); | |
} | |
public Page<T> page(Pageable pageable, Supplier<Criteria> function) { | |
return page(pageable, list(pageable, function.get()), () -> count(function.get())); | |
} | |
private Page<T> page(Pageable pageable, List<T> list, TotalSupplier count) { | |
return PageableExecutionUtils.getPage(list, pageable, count); | |
} | |
public SQLQuery sql(String sql) { | |
return session().createSQLQuery(sql); | |
} | |
private List<T> list(Pageable pageable) { | |
return list(pageable, criteria()); | |
} | |
@SuppressWarnings("unchecked") | |
private List<T> list(Pageable pageable, Criteria criteria) { | |
criteria.setFirstResult(pageable.getOffset()); | |
criteria.setMaxResults(pageable.getPageSize()); | |
checkSort(pageable, criteria); | |
return criteria.list(); | |
} | |
private void checkSort(Pageable pageable, final Criteria criteria) { | |
if (pageable.getSort() != null) { | |
pageable.getSort().forEach(order -> convert(order).ifPresent(criteria::addOrder)); | |
} | |
} | |
private Optional<org.hibernate.criterion.Order> convert(Order order) { | |
if (fields.contains(order.getProperty())) { | |
if (order.isAscending()) { | |
return Optional.of(org.hibernate.criterion.Order.asc(order.getProperty())); | |
} else { | |
return Optional.of(org.hibernate.criterion.Order.desc(order.getProperty())); | |
} | |
} | |
return Optional.empty(); | |
} | |
private long count(Criteria criteria) { | |
return ((Long) criteria.setProjection(Projections.rowCount()).uniqueResult()).longValue(); | |
} | |
@SuppressWarnings("unchecked") | |
public List<T> list(Criterios criterios) { | |
Criteria criteria = criteria(); | |
criteria.add(CriteriosHibernateParser.parse(criterios)); | |
return criteria.list(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Field; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
/** | |
* @author guilherme.pacheco | |
*/ | |
public final class ReflectionUtils { | |
private ReflectionUtils() { | |
super(); | |
} | |
public static List<Field> allFields(Class<?> clazz) { | |
List<Field> fields = new ArrayList<>(); | |
if (clazz != null) { | |
fields.addAll(Arrays.asList(clazz.getDeclaredFields())); | |
fields.addAll(allFields(clazz.getSuperclass())); | |
} | |
return fields; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment