Java tutorial
/******************************************************************************* * Copyright (c) 2005, 2014 springside.github.io * * Licensed under the Apache License, Version 2.0 (the "License"); *******************************************************************************/ package org.kee.ssf.service.module; import org.kee.ssf.entity.Module; import org.kee.ssf.repository.ModuleDao; 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 ModuleService { private ModuleDao moduleDao; public Page<Module> getModule(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType) { PageRequest pageRequest = buildPageRequest(pageNumber, pageSize, sortType); Specification<Module> spec = buildSpecification(searchParams); return moduleDao.findAll(spec, pageRequest); } public List<Module> getAll() { return (List<Module>) moduleDao.findAll(); } public Module save(Module module) { Module pModule = moduleDao.findOne(Long.valueOf(module.getPid())); module.setFullCode(pModule.getFullCode() + ":" + module.getCode()); return moduleDao.save(module); } /** * . */ 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<Module> buildSpecification(Map<String, Object> searchParams) { Map<String, SearchFilter> filters = SearchFilter.parse(searchParams); Specification<Module> spec = DynamicSpecifications.bySearchFilter(filters.values(), Module.class); return spec; } @Autowired public void setModuleDao(ModuleDao moduleDao) { this.moduleDao = moduleDao; } }