package org.springblade.flow.engine.custom.xml.impl;


import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

import org.flowable.bpmn.converter.ManualTaskXMLConverter;
import org.flowable.bpmn.converter.util.BpmnXMLUtil;
import org.flowable.bpmn.model.BaseElement;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.bpmn.model.ManualTask;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author:LoveMyOrange
 * @Description: 如果只是取出扩展属性 输出 那么下面代码写法没有问题, 如果 调用完super 还想添加扩展属性,那么表面是正确的,实际上是完全错误
 * 原因: SAX解析机制
 * @Date:Created in 2021/12/17 14:21
 */
@Slf4j
public class CustomManualTaskXMLConverter extends ManualTaskXMLConverter {
    @Override
    protected BaseElement convertXMLToElement(XMLStreamReader xtr, BpmnModel model) throws Exception {
        ManualTask manualTask = new ManualTask();
        BpmnXMLUtil.addXMLLocation(manualTask, xtr);
        BpmnXMLUtil.addCustomAttributes(xtr, manualTask, defaultElementAttributes,defaultActivityAttributes);
        parseChildElements(getXMLElementName(), manualTask, model, xtr);
        return manualTask;
    }

    /**
     * 将扩展属性给添加上去
     * @param element
     * @param model
     * @param xtw
     * @throws Exception
     */
    @Override
    protected void writeAdditionalAttributes(BaseElement element, BpmnModel model, XMLStreamWriter xtw) throws Exception {
        ManualTask manualTask = (ManualTask) element;
        // write custom attributes
        BpmnXMLUtil.writeCustomAttributes(manualTask.getAttributes().values(), xtw, defaultElementAttributes,
                defaultActivityAttributes);
    }
}
