com.green.common.service.BaseService.java Source code

Java tutorial

Introduction

Here is the source code for com.green.common.service.BaseService.java

Source

/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.green.common.service;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Junction;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Lists;
import com.green.modules.sys.entity.Role;
import com.green.modules.sys.entity.User;

/**
 * Service
 * @author ThinkGem
 * @version 2013-05-15
 */
public abstract class BaseService {

    /**
     * 
     */
    protected Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * ?
     * @param user ?UserUtils.getUser()??
     * @param officeAlias ??dc.createAlias("office", "office");
     * @param userAlias ???
     * @return ?
     */
    protected static Junction dataScopeFilter(User user, String officeAlias, String userAlias) {

        // ????
        List<String> dataScope = Lists.newArrayList();
        Junction junction = Restrictions.disjunction();

        // ???
        if (!user.isAdmin()) {
            for (Role r : user.getRoleList()) {
                if (!dataScope.contains(r.getDataScope()) && StringUtils.isNotBlank(officeAlias)) {
                    boolean isDataScopeAll = false;
                    if (Role.DATA_SCOPE_ALL.equals(r.getDataScope())) {
                        isDataScopeAll = true;
                    } else if (Role.DATA_SCOPE_COMPANY_AND_CHILD.equals(r.getDataScope())) {
                        junction.add(Restrictions.eq(officeAlias + ".id", user.getCompany().getId()));
                        junction.add(Restrictions.like(officeAlias + ".parentIds",
                                user.getCompany().getParentIds() + user.getCompany().getId() + ",%"));
                    } else if (Role.DATA_SCOPE_COMPANY.equals(r.getDataScope())) {
                        junction.add(Restrictions.eq(officeAlias + ".id", user.getCompany().getId()));
                        junction.add(Restrictions.and(
                                Restrictions.eq(officeAlias + ".parent.id", user.getCompany().getId()),
                                Restrictions.eq(officeAlias + ".type", "2"))); // ?
                    } else if (Role.DATA_SCOPE_OFFICE_AND_CHILD.equals(r.getDataScope())) {
                        junction.add(Restrictions.eq(officeAlias + ".id", user.getOffice().getId()));
                        junction.add(Restrictions.like(officeAlias + ".parentIds",
                                user.getOffice().getParentIds() + user.getOffice().getId() + ",%"));
                    } else if (Role.DATA_SCOPE_OFFICE.equals(r.getDataScope())) {
                        junction.add(Restrictions.eq(officeAlias + ".id", user.getOffice().getId()));
                    } else if (Role.DATA_SCOPE_CUSTOM.equals(r.getDataScope())) {
                        junction.add(Restrictions.in(officeAlias + ".id", r.getOfficeIdList()));
                    }
                    //else if (Role.DATA_SCOPE_SELF.equals(r.getDataScope())){
                    if (!isDataScopeAll) {
                        if (StringUtils.isNotBlank(userAlias)) {
                            junction.add(Restrictions.eq(userAlias + ".id", user.getId()));
                        } else {
                            junction.add(Restrictions.isNull(officeAlias + ".id"));
                        }
                    } else {
                        // ?????
                        junction = Restrictions.disjunction();
                        break;
                    }
                    dataScope.add(r.getDataScope());
                }
            }
        }
        return junction;
    }

    /**
     * ?
     * @param user ?UserUtils.getUser()??
     * @param officeAlias ??dc.createAlias("office", "office");
     * @param userAlias ???
     * @return ql
     */
    protected static String dataScopeFilterString(User user, String officeAlias, String userAlias) {
        Junction junction = dataScopeFilter(user, officeAlias, userAlias);
        Iterator<Criterion> it = junction.conditions().iterator();
        StringBuilder ql = new StringBuilder();
        ql.append(" and (");
        if (it.hasNext()) {
            ql.append(it.next());
        }
        String[] strField = { ".parentIds like ", ".type=" }; // ????
        while (it.hasNext()) {
            ql.append(" or (");
            String s = it.next().toString();
            for (String field : strField) {
                s = s.replaceAll(field + "(\\w.*)", field + "'$1'");
            }
            ql.append(s).append(")");
        }
        ql.append(")");
        return ql.toString();
    }

    protected List<Long> getIdList(String ids) {
        List<Long> idList = Lists.newArrayList();
        if (StringUtils.isNotBlank(ids)) {
            ids = ids.trim().replace("", ",").replace(" ", ",").replace("", ",");
            String[] arrId = ids.split(",");
            for (String id : arrId) {
                if (id.matches("\\d*")) {
                    idList.add(Long.valueOf(id));
                }
            }
        }
        return idList;
    }
}