Java tutorial
/******************************************************************************* * Copyright (c) 2005, 2014 springside.github.io * * Licensed under the Apache License, Version 2.0 (the "License"); *******************************************************************************/ package org.kee.ssf.service.role; import org.kee.ssf.entity.Role; import org.kee.ssf.repository.RoleDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import org.springside.modules.persistence.DynamicSpecifications; import org.springside.modules.persistence.SearchFilter; import java.util.List; import java.util.Map; // Spring Bean. @Component // public?. @Transactional public class RoleService { private RoleDao roleDao; public Page<Role> get(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType) { PageRequest pageRequest = buildPageRequest(pageNumber, pageSize, sortType); Specification<Role> spec = buildSpecification(searchParams); return roleDao.findAll(spec, pageRequest); } public List<Role> getAll() { return (List<Role>) roleDao.findAll(); } public Role save(Role role) { return roleDao.save(role); } /** * . */ private PageRequest buildPageRequest(int pageNumber, int pagzSize, String sortType) { Sort sort = null; if ("auto".equals(sortType)) { sort = new Sort(Sort.Direction.DESC, "id"); } else if ("title".equals(sortType)) { sort = new Sort(Sort.Direction.ASC, "title"); } return new PageRequest(pageNumber - 1, pagzSize, sort); } /** * ???. */ private Specification<Role> buildSpecification(Map<String, Object> searchParams) { Map<String, SearchFilter> filters = SearchFilter.parse(searchParams); Specification<Role> spec = DynamicSpecifications.bySearchFilter(filters.values(), Role.class); return spec; } @Autowired public void setRoleDao(RoleDao roleDao) { this.roleDao = roleDao; } }