/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jeeplus.modules.flowable.service.converter.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.jeeplus.common.utils.SpringContextHolder;
import com.jeeplus.modules.extension.entity.FormSetting;
import com.jeeplus.modules.extension.service.FormSettingService;
import org.apache.commons.lang3.StringUtils;
import org.flowable.bpmn.model.CustomProperty;
import org.flowable.bpmn.model.ExtensionAttribute;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.bpmn.model.StartEvent;
import org.flowable.editor.language.json.converter.BpmnJsonConverterUtil;
import org.flowable.editor.language.json.converter.StartEventJsonConverter;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author liugaofeng
 */
public class FlowStartEventJsonConverter extends StartEventJsonConverter{
    @Override
    protected FlowElement convertJsonToElement(JsonNode elementNode, JsonNode modelNode, Map<String, JsonNode> shapeMap) {
        StartEvent startEvent = (StartEvent)super.convertJsonToElement(elementNode, modelNode, shapeMap);
        String procDefId = getProperty("process_id", modelNode).asText();
        String taskDefId = getProperty("overrideid", elementNode).asText();
        FormSetting formSetting = SpringContextHolder.getBean(FormSettingService.class).queryByDefIdAndTaskId(procDefId, taskDefId);
        if(formSetting != null ){
            String readOnly = formSetting.getFormReadOnly();
            ExtensionAttribute attribute = new ExtensionAttribute();
            attribute.setName("flowable:formReadOnly");
            attribute.setValue(readOnly);
            startEvent.addAttribute(attribute);
            String formType = formSetting.getFormType() == null? "":formSetting.getFormType();
            ExtensionAttribute attribute2 = new ExtensionAttribute();
            attribute2.setName("flowable:formType");
            attribute2.setValue(formType);
            startEvent.addAttribute(attribute2);
        }
        return startEvent;
    }

    /**
     * 创建自定义属性
     *
     * @param propertyName  属性名称
     * @param propertyValue 属性值
     */
    private CustomProperty createProperty(String propertyName, String propertyValue) {
        CustomProperty customProperty = new CustomProperty();
        customProperty.setId(propertyName);
        customProperty.setName(propertyName);
        customProperty.setSimpleValue(propertyValue);
        return customProperty;
    }

}
