Package com.alibaba.tamper.core.config

Examples of com.alibaba.tamper.core.config.BeanMappingField


        NodeList nodeList = node.getChildNodes();
        List<BeanMappingField> beanFields = new ArrayList<BeanMappingField>(10);
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node clildNode = nodeList.item(i);
            if ("field-mapping".equals(clildNode.getNodeName())) {
                BeanMappingField field = parseField(clildNode);
                BeanMappingBehavior fieldbehavior = BeanMappingBehaviorParse.parse(clildNode, beanbeHavior);// 解析下behaivor参数
                field.setBehavior(fieldbehavior);
                if (fieldbehavior.isMappingEmptyStrings() == false || fieldbehavior.isMappingNullValue() == false) {
                    config.setBatch(false);// 强制设置为false,因为batch处理无法过滤null/empty value不做set处理
                }
                beanFields.add(field);
            }
View Full Code Here


        config.setBeanFields(beanFields);
        return config;
    }

    public static BeanMappingField parseField(Node node) {
        BeanMappingField beanField = new BeanMappingField();
        Node srcNameNode = node.getAttributes().getNamedItem("srcName");
        Node srcClassNode = node.getAttributes().getNamedItem("srcClass");
        Node srcLocatorNode = node.getAttributes().getNamedItem("srcLocatorClass");
        Node srcComponentNode = node.getAttributes().getNamedItem("srcComponentClass");

        Node targetNameNode = node.getAttributes().getNamedItem("targetName");
        Node targetClassNode = node.getAttributes().getNamedItem("targetClass");
        Node targetLocatorNode = node.getAttributes().getNamedItem("targetLocatorClass");
        Node targetComponentNode = node.getAttributes().getNamedItem("targetComponentClass");
        Node defaultValueNode = node.getAttributes().getNamedItem("defaultValue");
        Node convertorNode = node.getAttributes().getNamedItem("convertor");
        Node scriptNode = node.getAttributes().getNamedItem("script");

        if (scriptNode == null && srcNameNode == null) {
            throw new BeanMappingException("srcName or script is requied");
        }
        if (targetNameNode == null) {
            throw new BeanMappingException("targetName is requied");
        }

        if (srcNameNode != null) {
            beanField.getSrcField().setName(srcNameNode.getNodeValue());
        }

        if (srcClassNode != null) {
            beanField.getSrcField().setClazz(ReflectionHelper.forName(srcClassNode.getNodeValue()));
        }
        if (srcLocatorNode != null) {
            beanField.getSrcField().setLocatorClass(ReflectionHelper.forName(srcLocatorNode.getNodeValue()));
        }

        if (srcComponentNode != null) {
            beanField.getSrcField().addComponentClasses(ReflectionHelper.forName(srcComponentNode.getNodeValue()));
        }

        if (targetNameNode != null) {
            beanField.getTargetField().setName(targetNameNode.getNodeValue());
        }

        if (targetClassNode != null) {
            beanField.getTargetField().setClazz(ReflectionHelper.forName(targetClassNode.getNodeValue()));
        }

        if (targetLocatorNode != null) {
            beanField.getTargetField().setLocatorClass(ReflectionHelper.forName(targetLocatorNode.getNodeValue()));
        }

        if (targetComponentNode != null) {
            beanField.getTargetField().addComponentClasses(ReflectionHelper.forName(targetComponentNode.getNodeValue()));
        }

        if (defaultValueNode != null) {
            beanField.setDefaultValue(defaultValueNode.getNodeValue());
        }
        if (convertorNode != null) {
            beanField.setConvertor(convertorNode.getNodeValue());
        }
        if (scriptNode != null) {
            beanField.setScript(scriptNode.getNodeValue());
        }
        // 处理下mapping
        Node mappingNode = node.getAttributes().getNamedItem("mapping");
        if (mappingNode != null) {
            beanField.setMapping(Boolean.valueOf(mappingNode.getNodeValue()));
        }

        // 处理下nest name
        Node nestNode = node.getAttributes().getNamedItem("nest");
        if (nestNode != null) {
            beanField.setNestName(nestNode.getNodeValue());
        }

        return beanField;
    }
View Full Code Here

            if (srcPd == null) {// 没有匹配属性
                continue;
            }

            if (targetPd.getWriteMethod() != null && srcPd.getReadMethod() != null) {
                BeanMappingField field = new BeanMappingField();
                BeanMappingFieldAttributes srcFieldAttribute = new BeanMappingFieldAttributes();
                srcFieldAttribute.setName(property);
                srcFieldAttribute.setClazz(srcPd.getPropertyType());
                BeanMappingFieldAttributes targetFieldAttribute = new BeanMappingFieldAttributes();
                targetFieldAttribute.setName(property);
                targetFieldAttribute.setClazz(targetPd.getPropertyType());
                // 添加记录
                field.setSrcField(srcFieldAttribute);
                field.setTargetField(targetFieldAttribute);
                field.setBehavior(globalBehavior);
                fields.add(field);
            }

        }
        object.setBeanFields(fields);
View Full Code Here

        object.setBatch(true);
        List<BeanMappingField> fields = new ArrayList<BeanMappingField>();
        BeanMappingBehavior globalBehavior = BeanMappingConfigHelper.getInstance().getGlobalBehavior();
        for (PropertyDescriptor targetPd : targetPds) {
            if (targetPd.getWriteMethod() != null && targetPd.getReadMethod() != null) {
                BeanMappingField field = new BeanMappingField();
                BeanMappingFieldAttributes srcFieldAttribute = new BeanMappingFieldAttributes();
                srcFieldAttribute.setName(targetPd.getName());
                srcFieldAttribute.setClazz(targetPd.getPropertyType());
                BeanMappingFieldAttributes targetFieldAttribute = new BeanMappingFieldAttributes();
                targetFieldAttribute.setName(targetPd.getName());
                targetFieldAttribute.setClazz(targetPd.getPropertyType());
                field.setSrcField(srcFieldAttribute);
                field.setTargetField(targetFieldAttribute);
                field.setBehavior(globalBehavior);// 设置为global
                fields.add(field);
            }
        }
        object.setBeanFields(fields);
        object.setBehavior(globalBehavior);// 设置为global
View Full Code Here

        newObject.setTargetKey(object.getTargetKey());
        newObject.setBehavior(object.getBehavior());
        List<BeanMappingField> fields = newObject.getBeanFields();
        for (BeanMappingField field : object.getBeanFields()) {
            // 倒转一下
            BeanMappingField newField = new BeanMappingField();
            BeanMappingFieldAttributes srcFieldAttribute = new BeanMappingFieldAttributes();
            srcFieldAttribute.setName(field.getTargetField().getName());
            srcFieldAttribute.setClazz(field.getTargetField().getClazz());
            BeanMappingFieldAttributes targetFieldAttribute = new BeanMappingFieldAttributes();
            targetFieldAttribute.setName(field.getSrcField().getName());
            targetFieldAttribute.setClazz(field.getSrcField().getClazz());

            newField.setSrcField(srcFieldAttribute);
            newField.setTargetField(targetFieldAttribute);
            newField.setDefaultValue(field.getDefaultValue());
            newField.setMapping(field.isMapping());
            newField.setBehavior(field.getBehavior());
            if (StringUtils.isNotEmpty(field.getConvertor()) || StringUtils.isNotEmpty(field.getScript())) {
                object.setReversable(false);// 强制设置为false
                return null;
            }
            fields.add(newField);
View Full Code Here

            Object[] batchValues = getBatchExecutor.gets(param.getSrcRef());
            holder = new BatchObjectHolder(batchValues);
        }
        List<BeanMappingField> beanFields = config.getBeanFields();
        for (int i = 0, size = beanFields.size(); i < size; i++) {
            BeanMappingField beanField = beanFields.get(i);
            if (beanField.isMapping()) {
                doBeanMapping(param, beanField, holder);
            } else {
                doFieldMapping(param, beanField, holder);
            }
        }
View Full Code Here

public class DefaultValueValueProcess implements ValueProcess {

    @Override
    public Object process(Object value, ValueProcessInvocation invocation) throws BeanMappingException {
        // 处理下自己的业务
        BeanMappingField currentField = invocation.getContext().getCurrentField();
        if (value == null && StringUtils.isNotEmpty(currentField.getDefaultValue())
            && currentField.isMapping() == false) {
            if (currentField.getSrcField().getClazz() != null) {// 有指定对应的SrcClass
                Convertor convertor = ConvertorHelper.getInstance().getConvertor(String.class,
                                                                                 currentField.getSrcField().getClazz());
                if (convertor != null) {
                    // 先对String字符串进行一次转化
                    value = convertor.convert(currentField.getDefaultValue(), currentField.getSrcField().getClazz());
                }
            }

            if (value == null) {
                // 不存在对默认值处理的convertor,不予处理,后续尝试下自定义的convertor
                value = currentField.getDefaultValue();
            }
        }

        return invocation.proceed(value);
    }
View Full Code Here

*/
public class ScriptValueProcess implements ValueProcess {

    @Override
    public Object process(Object value, ValueProcessInvocation invocation) throws BeanMappingException {
        BeanMappingField currentField = invocation.getContext().getCurrentField();
        if (StringUtils.isNotEmpty(currentField.getScript())) {
            BeanMappingObject beanObject = invocation.getContext().getBeanObject();

            Map param = (Map) ContextObjectHolder.getInstance().get(ContextObjectHolder.SCRIPT_CONTEXT);// 使用第一次记录的script_context
            if (param == null) {
                param = new HashMap();
                param.put(beanObject.getSrcKey(), invocation.getContext().getParam().getSrcRef());
                param.put(beanObject.getTargetKey(), invocation.getContext().getParam().getTargetRef());
                ContextObjectHolder.getInstance().put(ContextObjectHolder.SCRIPT_CONTEXT, param);
            }

            Map custom = invocation.getContext().getCustom();
            if (custom != null && custom.containsKey(ContextObjectHolder.SCRIPT_CONTEXT)) {
                Map newParam = (Map) custom.get(ContextObjectHolder.SCRIPT_CONTEXT);
                param.putAll(newParam);
            }

            // 进行值转化处理
            ScriptExecutor scriptExecutor = ScriptHelper.getInstance().getScriptExecutor();
            value = scriptExecutor.evaluate(param, currentField.getScript());
        }

        // 继续走到下一步处理
        return invocation.proceed(value);

View Full Code Here

    private final static Logger logger = LoggerFactory.getLogger(ConvertorValueProcess.class);

    @Override
    public Object process(Object value, ValueProcessInvocation invocation) throws BeanMappingException {
        if (value != null && invocation.getContext().getCurrentField().isMapping() == false) {
            BeanMappingField currentField = invocation.getContext().getCurrentField();
            String customConvertorName = currentField.getConvertor();
            Convertor convertor = currentField.getConvertorRef();// 取上一次实例化后的convertor对象

            if (convertor == null && currentField.getConvertorClass() != null) {
                // 进行自定义convertor初始化
                Class clazz = currentField.getConvertorClass();
                convertor = (Convertor) ReflectionHelper.newInstance(clazz);
                currentField.setConvertorRef(convertor);
            }

            if (StringUtils.isNotEmpty(customConvertorName)) { // 判断是否有自定义的convertor
                convertor = ConvertorHelper.getInstance().getConvertor(customConvertorName);
            } else if (convertor == null) {
                // srcClass针对直接使用script的情况,会出现为空,这时候需要依赖value.getClass进行转化
                // 优先不选择使用value.getClass()的原因:原生类型会返回对应的Object类型,导出会出现不必要的convertor转化
                Class srcClass = currentField.getSrcField().getClazz();
                if (srcClass == null || srcClass.isPrimitive() == false) {
                    srcClass = value.getClass();
                }
                Class targetClass = currentField.getTargetField().getClazz();
                if (targetClass != null) {
                    // targetClass可能存在为空,比如这里的Value配置了DefaultValue,在MapSetExecutor解析时会无法识别TargetClass
                    // 无法识别后,就不做转化
                    convertor = ConvertorHelper.getInstance().getConvertor(srcClass, targetClass);

                    if (convertor == null && !targetClass.isAssignableFrom(srcClass) && logger.isWarnEnabled()) {
                        // 记录下日志
                        StringBuilder builder = new StringBuilder();
                        builder.append("srcName[" + currentField.getSrcField().getName());
                        builder.append("],srcClass[" + ObjectUtils.toString(srcClass, "null"));
                        builder.append("],targetName[" + currentField.getTargetField().getName());
                        builder.append("],targetClass[" + ObjectUtils.toString(targetClass, "null") + "]");
                        logger.warn(builder.toString() + " convertor is null!");
                    }
                }
            }

            if (convertor != null && currentField.getTargetField().getClazz() != null) {
                List<Class> componentClasses = currentField.getTargetField().getComponentClasses();
                Class[] array = null;
                if (componentClasses != null && componentClasses.size() > 0) {
                    array = componentClasses.toArray(new Class[componentClasses.size()]);
                }
                if (array != null && convertor instanceof CollectionConvertor) {
                    // 进行嵌套对象处理
                    value = ((CollectionConvertor) convertor).convertCollection(
                                                                                currentField,
                                                                                value,
                                                                                currentField.getTargetField().getClazz(),
                                                                                array);
                } else {
                    value = convertor.convert(value, currentField.getTargetField().getClazz());
                }
            }
        }

        // 继续下一步的调用
View Full Code Here

* @author jianghang 2011-6-22 上午09:50:16
*/
public class BehaviorValueProcess implements ValueProcess {

    public Object process(Object value, ValueProcessInvocation invocation) throws BeanMappingException {
        BeanMappingField field = invocation.getContext().getCurrentField();
        BeanMappingBehavior behavior = field.getBehavior(); // 每个属性mapping都会有一个behavior,允许覆盖上层

        // 判断一下value的null 情况
        if (value == null && behavior.isMappingNullValue() == false) {
            return value;
        }
View Full Code Here

TOP

Related Classes of com.alibaba.tamper.core.config.BeanMappingField

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.