Search in sources :

Example 1 with IgnoreCaseSet

use of org.sagacity.sqltoy.model.IgnoreCaseSet in project sagacity-sqltoy by chenrenfei.

the class SqltoyUnifyFieldsHandler method forceUpdateFields.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.sagacity.sqltoy.plugins.IUnifyFieldsHandler#forceUpdateFields()
	 */
@Override
public IgnoreCaseSet forceUpdateFields() {
    // 最后修改时间作为必须修改字段
    IgnoreCaseSet forceUpdateFields = new IgnoreCaseSet();
    forceUpdateFields.add("updateTime");
    return forceUpdateFields;
}
Also used : IgnoreCaseSet(org.sagacity.sqltoy.model.IgnoreCaseSet)

Example 2 with IgnoreCaseSet

use of org.sagacity.sqltoy.model.IgnoreCaseSet in project sagacity-sqltoy by chenrenfei.

the class DialectUtils method getUpdateReflectHandler.

/**
 * @todo 构造修改记录参数反射赋值处理器
 * @param preHandler
 * @param forceUpdateProps
 * @param unifyFieldsHandler
 * @return
 */
public static ReflectPropsHandler getUpdateReflectHandler(final ReflectPropsHandler preHandler, String[] forceUpdateProps, IUnifyFieldsHandler unifyFieldsHandler) {
    if (unifyFieldsHandler == null) {
        return preHandler;
    }
    final Map<String, Object> keyValues = unifyFieldsHandler.updateUnifyFields();
    if (keyValues == null || keyValues.isEmpty()) {
        return preHandler;
    }
    // update操作强制更新字段优先
    final Set<String> forceSet = new HashSet<String>();
    if (forceUpdateProps != null && forceUpdateProps.length > 0) {
        for (String field : forceUpdateProps) {
            forceSet.add(field.toLowerCase().replace("_", ""));
        }
    }
    // 强制修改字段赋值
    IgnoreCaseSet tmpSet = unifyFieldsHandler.forceUpdateFields();
    final IgnoreCaseSet forceUpdateFields = (tmpSet == null) ? new IgnoreCaseSet() : tmpSet;
    ReflectPropsHandler handler = new ReflectPropsHandler() {

        @Override
        public void process() {
            if (preHandler != null) {
                preHandler.setPropertyIndexMap(this.getPropertyIndexMap());
                preHandler.setRowIndex(this.getRowIndex());
                preHandler.setRowData(this.getRowData());
                preHandler.process();
            }
            // 修改操作
            for (Map.Entry<String, Object> entry : keyValues.entrySet()) {
                // 统一修改字段不在强制更新字段范围内
                if (!forceSet.contains(entry.getKey().toLowerCase())) {
                    if (StringUtil.isBlank(this.getValue(entry.getKey())) || forceUpdateFields.contains(entry.getKey())) {
                        this.setValue(entry.getKey(), entry.getValue());
                    }
                }
            }
        }
    };
    return handler;
}
Also used : ReflectPropsHandler(org.sagacity.sqltoy.callback.ReflectPropsHandler) Map(java.util.Map) HashMap(java.util.HashMap) IgnoreKeyCaseMap(org.sagacity.sqltoy.model.IgnoreKeyCaseMap) IgnoreCaseSet(org.sagacity.sqltoy.model.IgnoreCaseSet) HashSet(java.util.HashSet)

Example 3 with IgnoreCaseSet

use of org.sagacity.sqltoy.model.IgnoreCaseSet in project sagacity-sqltoy by chenrenfei.

the class EntityManager method parseSecureConfig.

/**
 * @todo 解析加解密配置
 * @param entityMeta
 * @param entityClass
 */
private void parseSecureConfig(EntityMeta entityMeta, Class entityClass) {
    Class classType = entityClass;
    SecureConfig secureConfig = null;
    // 增加递归对父类检测
    while (classType != null && !classType.equals(Object.class)) {
        secureConfig = (SecureConfig) classType.getAnnotation(SecureConfig.class);
        if (secureConfig != null) {
            break;
        }
        classType = classType.getSuperclass();
    }
    // 不存在加解密配置
    if (secureConfig == null) {
        return;
    }
    Secure[] secures = secureConfig.secures();
    if (secures != null && secures.length > 0) {
        IgnoreCaseSet secureColumns = new IgnoreCaseSet();
        String field;
        FieldMeta fieldMeta;
        for (Secure secure : secures) {
            field = secure.field();
            fieldMeta = entityMeta.getFieldMeta(field);
            if (fieldMeta != null) {
                // 加密
                if (secure.secureType().equals(SecureType.ENCRYPT)) {
                    secureColumns.add(fieldMeta.getColumnName());
                    entityMeta.addSecureField(new FieldSecureConfig(field, SecureType.ENCRYPT, null, null, 0, 0, 0));
                } else {
                    // 依据加密字段进行脱敏保存
                    entityMeta.addSecureField(new FieldSecureConfig(field, secure.secureType(), secure.sourceField(), secure.maskCode(), secure.headSize(), secure.tailSize(), secure.maskRate()));
                }
            }
        }
        // 加密字段
        if (!secureColumns.isEmpty()) {
            entityMeta.setSecureColumns(secureColumns);
        }
    }
}
Also used : FieldSecureConfig(org.sagacity.sqltoy.config.model.FieldSecureConfig) Secure(org.sagacity.sqltoy.config.annotation.Secure) FieldMeta(org.sagacity.sqltoy.config.model.FieldMeta) SecureConfig(org.sagacity.sqltoy.config.annotation.SecureConfig) FieldSecureConfig(org.sagacity.sqltoy.config.model.FieldSecureConfig) IgnoreCaseSet(org.sagacity.sqltoy.model.IgnoreCaseSet)

Example 4 with IgnoreCaseSet

use of org.sagacity.sqltoy.model.IgnoreCaseSet in project sagacity-sqltoy by chenrenfei.

the class DialectUtils method getAddReflectHandler.

/**
 * @todo 构造新增记录参数反射赋值处理器
 * @param preHandler
 * @param unifyFieldsHandler
 * @return
 */
public static ReflectPropsHandler getAddReflectHandler(final ReflectPropsHandler preHandler, IUnifyFieldsHandler unifyFieldsHandler) {
    if (unifyFieldsHandler == null) {
        return preHandler;
    }
    final Map<String, Object> keyValues = unifyFieldsHandler.createUnifyFields();
    if (keyValues == null || keyValues.isEmpty()) {
        return preHandler;
    }
    // 强制修改字段赋值
    IgnoreCaseSet tmpSet = unifyFieldsHandler.forceUpdateFields();
    final IgnoreCaseSet forceUpdateFields = (tmpSet == null) ? new IgnoreCaseSet() : tmpSet;
    ReflectPropsHandler handler = new ReflectPropsHandler() {

        @Override
        public void process() {
            if (preHandler != null) {
                preHandler.setPropertyIndexMap(this.getPropertyIndexMap());
                preHandler.setRowIndex(this.getRowIndex());
                preHandler.setRowData(this.getRowData());
                preHandler.process();
            }
            for (Map.Entry<String, Object> entry : keyValues.entrySet()) {
                if (StringUtil.isBlank(this.getValue(entry.getKey())) || forceUpdateFields.contains(entry.getKey())) {
                    this.setValue(entry.getKey(), entry.getValue());
                }
            }
        }
    };
    return handler;
}
Also used : ReflectPropsHandler(org.sagacity.sqltoy.callback.ReflectPropsHandler) Map(java.util.Map) HashMap(java.util.HashMap) IgnoreKeyCaseMap(org.sagacity.sqltoy.model.IgnoreKeyCaseMap) IgnoreCaseSet(org.sagacity.sqltoy.model.IgnoreCaseSet)

Example 5 with IgnoreCaseSet

use of org.sagacity.sqltoy.model.IgnoreCaseSet in project sagacity-sqltoy by chenrenfei.

the class DialectUtils method getSaveOrUpdateReflectHandler.

/**
 * @todo 构造创建和修改记录时的反射
 * @param idFields
 * @param prepHandler
 * @param forceUpdateProps
 * @param unifyFieldsHandler
 * @return
 */
public static ReflectPropsHandler getSaveOrUpdateReflectHandler(final String[] idFields, final ReflectPropsHandler prepHandler, String[] forceUpdateProps, IUnifyFieldsHandler unifyFieldsHandler) {
    if (unifyFieldsHandler == null) {
        return prepHandler;
    }
    final Map<String, Object> addKeyValues = unifyFieldsHandler.createUnifyFields();
    final Map<String, Object> updateKeyValues = unifyFieldsHandler.updateUnifyFields();
    if ((addKeyValues == null || addKeyValues.isEmpty()) && (updateKeyValues == null || updateKeyValues.isEmpty())) {
        return prepHandler;
    }
    // update操作强制更新字段优先
    final Set<String> forceSet = new HashSet<String>();
    if (forceUpdateProps != null && forceUpdateProps.length > 0) {
        for (String field : forceUpdateProps) {
            forceSet.add(field.toLowerCase().replace("_", ""));
        }
    }
    // 强制修改字段赋值
    IgnoreCaseSet tmpSet = unifyFieldsHandler.forceUpdateFields();
    final IgnoreCaseSet forceUpdateFields = (tmpSet == null) ? new IgnoreCaseSet() : tmpSet;
    final int idLength = (idFields == null) ? 0 : idFields.length;
    // 构造一个新的包含update和save 的字段处理
    ReflectPropsHandler handler = new ReflectPropsHandler() {

        @Override
        public void process() {
            if (prepHandler != null) {
                prepHandler.setPropertyIndexMap(this.getPropertyIndexMap());
                prepHandler.setRowIndex(this.getRowIndex());
                prepHandler.setRowData(this.getRowData());
                prepHandler.process();
            }
            // 主键为空表示save操作
            if (idLength > 0 && this.getValue(idFields[0]) == null) {
                for (Map.Entry<String, Object> entry : addKeyValues.entrySet()) {
                    if (StringUtil.isBlank(this.getValue(entry.getKey()))) {
                        this.setValue(entry.getKey(), entry.getValue());
                    }
                }
            }
            // 修改属性值
            for (Map.Entry<String, Object> entry : updateKeyValues.entrySet()) {
                // 统一修改字段不在强制更新字段范围内
                if (!forceSet.contains(entry.getKey().toLowerCase())) {
                    if (StringUtil.isBlank(this.getValue(entry.getKey())) || forceUpdateFields.contains(entry.getKey())) {
                        this.setValue(entry.getKey(), entry.getValue());
                    }
                }
            }
        }
    };
    return handler;
}
Also used : ReflectPropsHandler(org.sagacity.sqltoy.callback.ReflectPropsHandler) Map(java.util.Map) HashMap(java.util.HashMap) IgnoreKeyCaseMap(org.sagacity.sqltoy.model.IgnoreKeyCaseMap) IgnoreCaseSet(org.sagacity.sqltoy.model.IgnoreCaseSet) HashSet(java.util.HashSet)

Aggregations

IgnoreCaseSet (org.sagacity.sqltoy.model.IgnoreCaseSet)7 HashMap (java.util.HashMap)4 Map (java.util.Map)3 ReflectPropsHandler (org.sagacity.sqltoy.callback.ReflectPropsHandler)3 IgnoreKeyCaseMap (org.sagacity.sqltoy.model.IgnoreKeyCaseMap)3 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 DecryptHandler (org.sagacity.sqltoy.callback.DecryptHandler)1 Secure (org.sagacity.sqltoy.config.annotation.Secure)1 SecureConfig (org.sagacity.sqltoy.config.annotation.SecureConfig)1 FieldMeta (org.sagacity.sqltoy.config.model.FieldMeta)1 FieldSecureConfig (org.sagacity.sqltoy.config.model.FieldSecureConfig)1 DataAccessException (org.sagacity.sqltoy.exception.DataAccessException)1 QueryResult (org.sagacity.sqltoy.model.QueryResult)1 Element (org.w3c.dom.Element)1