Commit ff400445 by Jan Hrabal

x

parent 514b0131
package com.jh.boot.jpa;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
......@@ -14,21 +11,7 @@ import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import org.hibernate.Criteria;
import org.hibernate.criterion.Conjunction;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.query.NativeQuery;
import org.hibernate.transform.Transformers;
import com.jh.boot.web.list.Page;
import com.jh.boot.web.list.PagingInfo;
import com.jh.boot.web.list.SortTrend;
import com.jh.boot.web.list.SortingInfo;
/**
......@@ -69,104 +52,6 @@ public abstract class AbstractHibernateRepository {
}
}
/**
* TODO comment
*
*
* @param tokens the tokens
* @param fields the fields
* @return the conjunction
*/
protected Conjunction fulltextCriteria(List<String> tokens, String...fields) {
Objects.requireNonNull(tokens, "Tokens must not be null");
Objects.requireNonNull(fields, "Fields must not be null");
Conjunction and = Restrictions.conjunction();
for (String t : tokens) {
Disjunction d = Restrictions.disjunction();
for (String f : fields) {
d.add(Restrictions.ilike(f, t, MatchMode.ANYWHERE));
}
and.add(d);
}
return and;
}
/**
* TODO comment.
*
* @param c the c
* @param clazz the clazz
* @param properties the properties
*/
protected void fields(Criteria c, Class<?> clazz, String...properties) {
Objects.requireNonNull(c, "Criteria must not be null");
Objects.requireNonNull(clazz, "Class must not be null");
if (properties == null || properties.length == 0) {
return;
}
ProjectionList pl = Projections.projectionList();
for (String p : properties) {
pl.add(Projections.alias(Projections.property(p), p));
}
c.setProjection(pl);
c.setResultTransformer(Transformers.aliasToBean(clazz));
}
/**
* TODO comment.
*
* @param <T> the generic type
* @param c the c
* @param pagingInfo the paging info
* @return the page
*/
@SuppressWarnings("unchecked")
protected <T> Page<T> pagedResult(Criteria c, PagingInfo pagingInfo) {
if (pagingInfo == null) {
return new Page<>(c.list());
} else if (pagingInfo.getPageSize() < 1) {
return new Page<>(Collections.emptyList());
}
int pageSize = pagingInfo.getPageSize();
int count = ((Number) c.setProjection(Projections.rowCount()).uniqueResult()).intValue();
//count pages count
int pagesCount = count / pageSize + ( count % pageSize == 0 ? 0 : 1);
//limit results
c.setFirstResult(pageSize * pagingInfo.getPage());
c.setMaxResults(pageSize);
//reset criteria https://stackoverflow.com/a/1472958
c.setProjection(null);
c.setResultTransformer(Criteria.ROOT_ENTITY);
sortedCriteria(c, pagingInfo);
return new Page<>(pagingInfo.getPage(), pagesCount, pagingInfo.getPageSize(), count, c.list(), pagingInfo.getField(), pagingInfo.getTrend());
}
/**
* Sorted criteria.
*
* @param c the c
* @param sortingInfo the sorting info
* @return the criteria
*/
protected Criteria sortedCriteria(Criteria c, SortingInfo sortingInfo) {
if (sortingInfo != null && sortingInfo.getField() != null) {
c.addOrder(sortingInfo.getTrend() == SortTrend.DESCENDING ? Order.desc(sortingInfo.getField()) : Order.asc(sortingInfo.getField()));
}
return c;
}
public <ID, T> T findById(Class<T> clazz, ID id) {
return entityManager.find(clazz, id);
......@@ -270,9 +155,4 @@ public abstract class AbstractHibernateRepository {
}
// @SuppressWarnings("unchecked")
// public <T extends AbstractIdEntity> T merge(T entity) {
// return (T) getSession().merge(entity);
// }
}
package com.jh.boot.web.error;
import java.util.Collection;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
/**
* The Class ValidationFailedException.
*/
@ResponseStatus(value = HttpStatus.FORBIDDEN)
public class SecurityViolationException extends RestApiException {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 1L;
/**
* Instantiates a new validation failed exception.
*
* @param errors the errors
*/
public SecurityViolationException(Collection<ErrorMessage> errors) {
super(HttpStatus.FORBIDDEN, errors);
}
/**
* Instantiates a new validation failed exception.
*
* @param errors the errors
*/
public SecurityViolationException(ErrorMessage... errors) {
super(HttpStatus.FORBIDDEN, errors);
}
}
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