org.kee.ssf.service.sys.PrivilegeService.java Source code

Java tutorial

Introduction

Here is the source code for org.kee.ssf.service.sys.PrivilegeService.java

Source

/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package org.kee.ssf.service.sys;

import org.kee.ssf.entity.Privilege;
import org.kee.ssf.entity.Role;
import org.kee.ssf.repository.PrivilegeDao;
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 PrivilegeService {

    private PrivilegeDao privilegeDao;

    public Page<Privilege> get(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType) {
        PageRequest pageRequest = buildPageRequest(pageNumber, pageSize, sortType);
        Specification<Privilege> spec = buildSpecification(searchParams);

        return privilegeDao.findAll(spec, pageRequest);
    }

    /**
     * .
     */
    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<Privilege> buildSpecification(Map<String, Object> searchParams) {
        Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
        Specification<Privilege> spec = DynamicSpecifications.bySearchFilter(filters.values(), Privilege.class);
        return spec;
    }

    @Autowired
    public void setPrivilegeDao(PrivilegeDao privilegeDao) {
        this.privilegeDao = privilegeDao;
    }
}