Commit ccaa3e2f by Jan Hrabal

2.0

parent 6b52ff0f
......@@ -5,7 +5,7 @@
<groupId>com.jh</groupId>
<artifactId>jh-boot</artifactId>
<version>0.0.1</version>
<version>0.0.2</version>
<packaging>jar</packaging>
<parent>
......
......@@ -14,10 +14,16 @@ import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.hibernate.query.NativeQuery;
import com.jh.boot.web.list.Page;
import com.jh.boot.web.list.PagingInfo;
/**
* TODO comment.
......@@ -129,5 +135,24 @@ public abstract class AbstractHibernateRepository {
}
}
@SuppressWarnings("unchecked")
protected <T> Page<T> pagedCriteria(CriteriaBuilder cb, CriteriaQuery<T> cq, Root<T> root, PagingInfo pagingInfo) {
Query query = entityManager.createQuery(cq);
query.setFirstResult(pagingInfo.getPage() * pagingInfo.getPageSize());
query.setMaxResults(pagingInfo.getPageSize());
List<T> resultList = query.getResultList();
CriteriaQuery<Long> count = cb.createQuery(Long.class);
Root<T> countRoot = count.from(root.getModel());
count.select(cb.count(countRoot));
if (cq.getRestriction() != null) {
count.where(cq.getRestriction());
}
Long rowsCount = entityManager.createQuery(count).getSingleResult();
return new Page<T>(pagingInfo.getPage(), pagingInfo.getPageSize(), rowsCount, resultList, pagingInfo.getField(), pagingInfo.getTrend());
}
}
......@@ -25,6 +25,15 @@ public class SecurityHelper {
}
public static <T extends AppUser> T getUser(Authentication auth, Class<T> clazz) {
if (AppUser.class.isAssignableFrom(clazz)) {
return (T) auth.getPrincipal();
}
return null;
}
public static boolean hasAnyRole(Authentication auth, String...roles) {
AppUser user = auth instanceof AppUserAuthentication ? ((AppUserAuthentication) auth).getUser() : null;
return hasAnyRole(user, roles);
......
package com.jh.boot.web;
import java.util.StringJoiner;
import org.springframework.util.StringUtils;
import com.jh.boot.jpa.AbstractIdEntity;
import com.jh.boot.security.model.AppUser;
public class ConverterUtils {
public static Long id(AbstractIdEntity user) {
return user == null ? null : user.getId();
}
public static String name(AppUser user) {
if (user == null) {
return null;
}
StringJoiner sj = new StringJoiner(" ");
if (StringUtils.hasText(user.getFirstName())) {
sj.add(user.getFirstName());
}
if (StringUtils.hasText(user.getLastName())) {
sj.add(user.getLastName());
}
return sj.toString();
}
}
......@@ -87,6 +87,18 @@ public class Page<T> {
this.pageSize = pageSize;
}
public Page(int page, int pageSize, long totalElements, List<T> content, String sortBy, SortTrend trend) {
super();
this.content = content;
this.sortBy = sortBy;
this.trend = trend;
this.totalElements = (int) totalElements;
this.pageSize = pageSize;
this.page = this.totalElements > 0 ? Math.min(page, this.totalElements / Math.max(pageSize, 1)) : 0;
this.pagesCount = this.totalElements > 0 ? (this.totalElements + pageSize / 2) / this.pageSize : 1;
}
/**
* Gets the content.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment