All Downloads are FREE. Search and download functionalities are using the official Maven repository.

x7.core.bean.DataPermission Maven / Gradle / Ivy

There is a newer version: 2.2.7.RELEASE
Show newest version
package x7.core.bean;

import x7.core.util.BeanUtil;
import x7.core.util.StringUtil;

import java.util.List;
import java.util.Objects;

/**
 * DataPermission, 数据权限, 支持数据库like查找, 或IN; 或搜索,
 * Created by Sim on 2016/5/23.
 */
public interface DataPermission {

    String LIKE_BASE = "DP0";
    String IN_BASE_ZERO = "0";
    String IN_BASE_ONE = "1";

    String getDataPermissionKey();

    void setDataPermissionValue(Object dataPermissionValue);

    Object getDataPermissionValue();

    class Chain {

        public static void beforeHandle(DataPermission dataPermission, Object userDataPermissionValue) {
            DataPermission dp = (DataPermission) dataPermission;
            Object dataPermissionValue = dp.getDataPermissionValue();
            if (Objects.nonNull(dataPermissionValue)) {

                if (dataPermissionValue instanceof List) {
                    List dpList = (List) dataPermissionValue;
                    if (Objects.nonNull(userDataPermissionValue)) {
                        dpList.addAll((List) userDataPermissionValue);
                    }
                }
            } else {
                dp.setDataPermissionValue(userDataPermissionValue);
            }

            dataPermissionValue = dp.getDataPermissionValue();
            if (dataPermissionValue instanceof List){//optimize
                List dpList = (List) dataPermissionValue;
                for (Object obj : dpList){
                    if (obj.toString().equals(IN_BASE_ZERO) || obj.toString().equals(IN_BASE_ONE)){
                        dp.setDataPermissionValue(null);
                        break;
                    }
                }
            }else{
                if (dataPermissionValue.equals(LIKE_BASE)){
                    dp.setDataPermissionValue(null);
                }
            }
        }

        protected static void onBuild(Criteria criteria, Object obj) {
            if (Objects.isNull(obj))
                return;
            if (obj instanceof DataPermission) {
                DataPermission dp = (DataPermission) obj;
                if (Objects.nonNull(dp.getDataPermissionValue())) {
                    criteria.setDataPermission(dp);
                }
            }
        }

        protected static void befroeGetCriteria(CriteriaBuilder builder, Criteria criteria) {

            DataPermission dp = criteria.getDataPermission();
            if (Objects.isNull(dp))
                return;
            final String key = dp.getDataPermissionKey();
            final Object value = dp.getDataPermissionValue();

            if (Objects.isNull(value) || StringUtil.isNullOrEmpty(key))
                return;

            /*
             * DataPermission
             */
            String property = (criteria instanceof Criteria.ResultMapped) ? (BeanUtil.getByFirstLower(criteria.getClz().getSimpleName()) + "." + key) : key;
            if (value instanceof String) {
                String s = (String) value;

                String v = s.endsWith(SqlScript.LIKE_HOLDER) ? s : s + SqlScript.LIKE_HOLDER;
                builder.and().likeRight(property, v);

            } else if (value instanceof List) {

                List dpsList = (List) value;
                if (dpsList.isEmpty())
                    return;
                builder.and().in(property,dpsList);

            }
        }
    }

}