kimi
2020-05-18 c8aee7b9bfd79cfd741d7e5692520f4f51a31a86
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.highdatas.mdm.service.impl;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.highdatas.mdm.pojo.Result;
import com.highdatas.mdm.service.ModelEditorService;
import com.highdatas.mdm.util.Constant;
import com.highdatas.mdm.util.DbUtils;
import org.activiti.editor.constants.ModelDataJsonConstants;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Model;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Map;
 
/**
 * @author kimi
 * @description
 * @date 2019-12-11 14:41
 */
 
 
@Service
public class ModelEditorServiceImpl  implements ModelEditorService, ModelDataJsonConstants {
 
    protected static final Logger LOGGER = LoggerFactory.getLogger(ModelEditorServiceImpl.class);
 
    @Autowired
    private RepositoryService repositoryService;
 
    @Autowired
    private ObjectMapper objectMapper;
 
 
 
    @Override
    public ObjectNode getEditorJson(String modelId) {
        ObjectNode modelNode = null;
 
        Model model = repositoryService.getModel(modelId);
 
        if (model != null) try {
            if (StringUtils.isNotEmpty(model.getMetaInfo())) {
                modelNode = (ObjectNode) objectMapper.readTree(model.getMetaInfo());
            } else {
                modelNode = objectMapper.createObjectNode();
                modelNode.put(MODEL_NAME, model.getName());
            }
            modelNode.put(MODEL_ID, model.getId());
            byte[] modelEditorSource = repositoryService.getModelEditorSource(model.getId());
            String s = new String(modelEditorSource, "utf-8");
            JSONObject editorJsonNode = JSONObject.parseObject(s);
            JSONArray childShapes = editorJsonNode.getJSONArray("childShapes");
            for (Object childShape : childShapes) {
                JSONObject nextJsonNode = (JSONObject) childShape;
                JSONObject stencil = nextJsonNode.getJSONObject("stencil");
                String nodeType = stencil.getString(Constant.ID);
 
                if (!nodeType.equalsIgnoreCase("SequenceFlow")) {
                    continue;
                }
 
                JSONObject properties = nextJsonNode.getJSONObject("properties");
                String etlCondition = properties.getString("conditionsequenceflow");
                etlCondition = etlCondition.replace("&&", "++");
                properties.put("conditionsequenceflow", etlCondition);
            }
            JsonNode jsonNode = objectMapper.readTree(editorJsonNode.toJSONString());
            modelNode.put("model", jsonNode);
 
        } catch (Exception e) {
            LOGGER.error("Error creating model JSON", e);
            throw new ActivitiException("Error creating model JSON", e);
        }
        else {
 
        }
        return modelNode;
    }
 
    @Override
    public Result saveModel(String modelId, Map<String, String> values) {
        return null;
    }
 
    @Override
    public String getStencilset() {
        InputStream stencilsetStream = this.getClass().getClassLoader().getResourceAsStream("static/process/stencilset_cn.json");
        try {
            return IOUtils.toString(stencilsetStream, "utf-8");
        } catch (Exception e) {
            throw new ActivitiException("Error while loading stencil set", e);
        }
    }
 
    @Override
    public String createModel() {
        //初始化一个空模型
        Model model = repositoryService.newModel();
 
        //设置一些默认信息
        String name = "new-process";
        String description = "";
        int revision = 1;
        String key = DbUtils.getRandomString(8);
 
        ObjectNode modelNode = objectMapper.createObjectNode();
        modelNode.put(ModelDataJsonConstants.MODEL_NAME, name);
        modelNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, description);
        modelNode.put(ModelDataJsonConstants.MODEL_REVISION, revision);
 
        model.setName(name);
        model.setKey(key);
        model.setMetaInfo(modelNode.toString());
 
        repositoryService.saveModel(model);
        String id = model.getId();
 
        //完善ModelEditorSource
        ObjectNode editorNode = objectMapper.createObjectNode();
        editorNode.put("id", "canvas");
        editorNode.put("resourceId", "canvas");
        ObjectNode stencilSetNode = objectMapper.createObjectNode();
        stencilSetNode.put("namespace",
                "http://b3mn.org/stencilset/bpmn2.0#");
        editorNode.put("stencilset", stencilSetNode);
        try {
            repositoryService.addModelEditorSource(id,editorNode.toString().getBytes("utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return model.getId();
    }
}