kimi
2020-03-19 807e2c7a2ca8283ba6d6f764c83320ad5e023349
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
146
package com.highdatas.mdm.util;
 
import com.highdatas.mdm.process.canvas.ProcessDiagramGenerator;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
 
public class WorkflowUtils {
 
 
    public static void main(String[] args) {
        System.out.println(toFirstChar("汉字转换为拼音").toUpperCase()); //转为首字母大写
        //System.out.println(toPinyin("汉字转换为拼音"));
    }
    private static Logger logger = LoggerFactory.getLogger(WorkflowUtils.class);
 
    /**
     * 转换流程节点类型为中文说明
     *
     * @param type 英文名称
     * @return 翻译后的中文名称
     */
    public static String parseToZhType(String type) {
        Map<String, String> types = new HashMap<String, String>();
        types.put("userTask", "用户任务");
        types.put("serviceTask", "系统任务");
        types.put("startEvent", "开始节点");
        types.put("endEvent", "结束节点");
        types.put("exclusiveGateway", "条件判断节点(系统自动根据条件处理)");
        types.put("inclusiveGateway", "并行处理任务");
        types.put("callActivity", "子流程");
        return types.get(type) == null ? type : types.get(type);
    }
 
    /**
     * 导出图片文件到硬盘
     *
     * @return 文件的全路径
     */
    public static String exportDiagramToFile(RepositoryService repositoryService, ProcessDefinition processDefinition, String exportDir) throws IOException {
//        String diagramResourceName = processDefinition.getDiagramResourceName();
//        String key = processDefinition.getKey();
//        int version = processDefinition.getVersion();
        String diagramPath = exportDir;
        BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinition.getId());
        ProcessEngineConfiguration engineConfiguration = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
        ProcessDiagramGenerator diagramGenerator = new ProcessDiagramGenerator();
        InputStream resourceAsStream = diagramGenerator.generateDiagram(bpmnModel, "png",  "WenQuanYi Micro Hei", "WenQuanYi Micro Hei",null, null,0);
        byte[] b = new byte[resourceAsStream.available()];
        
        resourceAsStream.read(b, 0, b.length);
 
        // create file if not exist
//        String diagramDir = exportDir + "/" + key + "/" + version;
//        File diagramDirFile = new File(diagramDir);
//        if (!diagramDirFile.exists()) {
//            diagramDirFile.mkdirs();
//        }
//        diagramPath = exportDir + "/" +11 +".png";
        File file = new File(diagramPath);
 
        if (file.exists()) {
 
            logger.debug("diagram exist, ignore... : {}", diagramPath);
            return diagramPath;
        } else {
            File parentFile = file.getParentFile();
            if (!parentFile.exists()) {
                parentFile.createNewFile();
            }
            file.createNewFile();
        }
 
        logger.debug("export diagram to : {}", diagramPath);
 
        // wirte bytes to file
        FileUtils.writeByteArrayToFile(file, b);
        return diagramPath;
    }
 
 
    /**
     * 获取字符串拼音的第一个字母
     * @param chinese
     * @return
     */
    public static String toFirstChar(String chinese){
        String pinyinStr = "";
        char[] newChar = chinese.toCharArray();  //转为单个字符
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < newChar.length; i++) {
            if (newChar[i] > 128) {
                try {
                    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0].charAt(0);
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            }else{
                pinyinStr += newChar[i];
            }
        }
        return pinyinStr;
    }
 
    /**
     * 汉字转为拼音
     * @param chinese
     * @return
     */
    public static String toPinyin(String chinese){
        String pinyinStr = "";
        char[] newChar = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < newChar.length; i++) {
            if (newChar[i] > 128) {
                try {
                    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            }else{
                pinyinStr += newChar[i];
            }
        }
        return pinyinStr;
    }
}