ludc
2024-02-29 8ef9e366be48dc5e8e52617ea8ed48b37a0e1f74
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package com.vci.ubcs.code.service.impl;
 
import com.vci.ubcs.code.entity.GroupMapAttrXML;
import com.vci.ubcs.code.service.IGroupMapAttrXMLService;
import com.vci.ubcs.code.util.gennerAttrMapUtil;
import com.vci.ubcs.code.vo.GroupMapAttrXMLVO;
import com.vci.ubcs.code.vo.webserviceModel.attrmap.ClsfAttrMappingDO;
import com.vci.ubcs.code.vo.webserviceModel.attrmap.LibraryClsfDO;
import com.vci.ubcs.code.vo.webserviceModel.attrmap.LibraryDO;
import com.vci.ubcs.code.webService.config.AttributeMapConfig;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.rmi.ServerException;
import java.security.acl.Group;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 集团属性映射xml配置管理
 * @author ludc
 * @date 2024/1/8 9:40
 */
@Service
@Slf4j
public class GroupMapAttrXMLServiceImpl implements IGroupMapAttrXMLService,EnvironmentAware {
 
    @Autowired
    private AttributeMapConfig attributeMapConfig;
 
    @Value("${code.universalinterface.attrconfig.attrmap_parent_path:/data1/ubcs/ubcs-server/xml}")
    private String ATTRMAP_PARENT_PATH;
 
    /**
     * 当前操作系统,是否为windows系统
     */
    private String separator = "\\";
 
    /**
     * 根据当前运行的环境,匹配分隔符
     * @param environment
     */
    @Override
    public void setEnvironment(Environment environment) {
        String os = environment.getProperty("os.name").toLowerCase();
        if (!os.contains("win")) {
            this.separator = "/";
        }
    }
 
    /**
     * 获取xml文件的详细信息
     * @param groupMapAttrXML
     * @return
     * @throws IOException
     */
    @Override
    public GroupMapAttrXML getGroupMapXMLInfo(GroupMapAttrXML groupMapAttrXML) throws ServerException {
        if(Func.isEmpty(groupMapAttrXML.getAttrMapPath())){
            throw new ServiceException("属性映射文件路径不能为空!");
        }
        File file = new File(groupMapAttrXML.getAttrMapPath());
        Map<String, String> stringStringMap = attributeMapConfig.getSystem_attrmap();
        GroupMapAttrXML data = new GroupMapAttrXML();
        if (file.exists() && file.isFile()) {
            data.setGroupMapAttrName(file.getName());
            data.setAttrMapPath(file.getPath());
            if(!stringStringMap.isEmpty() && stringStringMap.containsKey(file.getName().replace(".xml",""))){
                groupMapAttrXML.setIsEnable(true);
            }else {
                groupMapAttrXML.setIsEnable(false);
            }
            data.setGroupMapAttrContent(getXMLContent(file.getPath()));
        }
        return data;
    }
 
    /**
     * 获取所有配置在nacos上的xml文件内容,(也就是在启用的xml映射文件)
     * @return
     */
    @Override
    public List<GroupMapAttrXML> getGroupMapXMLList() {
        // 获取到已在nacos上配置的的xml映射文件
        Map<String, String> stringStringMap = attributeMapConfig.getSystem_attrmap();
        List<GroupMapAttrXML> groupMapAttrXMLList = new ArrayList<>();
        // 获取到父路径下的文件信息
        File fileDir = new File(ATTRMAP_PARENT_PATH);
        File[] childDir = fileDir.listFiles();
        if(Func.isNotEmpty(childDir) && childDir.length > 0){
            Arrays.stream(childDir).forEach(file->{
                GroupMapAttrXML groupMapAttrXML = new GroupMapAttrXML();
                if(!stringStringMap.isEmpty() && stringStringMap.containsKey(file.getName().replace(".xml",""))){
                    groupMapAttrXML.setIsEnable(true);
                }else {
                    groupMapAttrXML.setIsEnable(false);
                }
                groupMapAttrXML.setGroupMapAttrName(file.getName());
                groupMapAttrXML.setAttrMapPath(file.getPath());
                try {
                    groupMapAttrXML.setGroupMapAttrContent(getXMLContent(file.getPath()));
                } catch (ServerException e) {
                    throw new ServiceException(e.getMessage());
                }
                groupMapAttrXMLList.add(groupMapAttrXML);
            });
        }
        return groupMapAttrXMLList;
    }
 
    /**
     * 修改属性映射文件
     * @param groupMapAttrXMLVO
     * @return
     */
    public R updateGroupMapXML(GroupMapAttrXMLVO groupMapAttrXMLVO) throws IOException {
        if(Func.isEmpty(groupMapAttrXMLVO.getAttrMapPath())){
            throw new ServiceException("属性映射文件路径不能为空!");
        }
        File file = new File(groupMapAttrXMLVO.getAttrMapPath());
        // 文件名相同,就只需要修改内容
        if(groupMapAttrXMLVO.getGroupMapAttrName().equals(groupMapAttrXMLVO.getUpdateXMLName())){
            if(!file.exists()){
                return R.fail("被修改的"+groupMapAttrXMLVO.getGroupMapAttrName()+"文件不存在!");
            }
            FileWriter writer = null;
            try{
                writer = new FileWriter(file);
                writer.write(groupMapAttrXMLVO.getGroupMapAttrContent());
            }catch (IOException e) {
                e.printStackTrace();
                throw new ServerException("文件内容修改失败,原因:"+e.getMessage());
            }finally {
                writer.close();
            }
        }else{
            //修改了文件名,就需要将文件名和内容进行修改
            File newFile = new File(groupMapAttrXMLVO.getAttrMapPath().replace(groupMapAttrXMLVO.getGroupMapAttrName(),groupMapAttrXMLVO.getUpdateXMLName()));
            FileWriter writer = null;
            try {
                writer = new FileWriter(newFile);
                writer.write(groupMapAttrXMLVO.getGroupMapAttrContent());
            } catch (IOException e) {
                e.printStackTrace();
                throw new ServerException("属性映射文件修改失败,原因:"+e.getMessage());
            }finally {
                writer.close();
            }
            if (!file.delete()) {
                R.fail("属性映射文件名修改失败,请检查文件名是否重复!");
            }
        }
        Map<String, String> stringStringMap = attributeMapConfig.getSystem_attrmap();
        if (!stringStringMap.containsKey(groupMapAttrXMLVO.getUpdateXMLName().replace(".xml",""))) {
            return R.success("修改成功,修改属性映射文件名之后,未在nacos上找到相关配置,请及时更新nacos上的配置!");
        }
        return R.success("属性映射文件修改成功!");
    }
 
    @Override
    public R addGroupMapXML(GroupMapAttrXML groupMapAttrXML) throws IOException {
        if(Func.isEmpty(groupMapAttrXML.getGroupMapAttrName())){
            throw new ServiceException("属性映射文件名称不能为空!");
        }
        // 创建一个新文件
        File file = new File(ATTRMAP_PARENT_PATH);
        if(!file.exists()){
            return R.fail("nacos上xml属性映射文件,父路径配置有误,请检查!");
        }
        File[] files = file.listFiles();
        if(files.length>0){
            List<File> repeatNameFile = Arrays.stream(files).filter(item -> {
                if (item.getName().equals(groupMapAttrXML.getGroupMapAttrName())) {
                    return true;
                }
                return false;
            }).collect(Collectors.toList());
            if (!repeatNameFile.isEmpty()) {
                return R.fail("新增的属性映射xml文件名已存在!");
            }
        }
        File addXMLFile = new File(ATTRMAP_PARENT_PATH + separator + groupMapAttrXML.getGroupMapAttrName());
        FileWriter writer = null;
        try {
            // 向文件中写入指定内容
            writer = new FileWriter(addXMLFile);
            writer.write(groupMapAttrXML.getGroupMapAttrContent());
        } catch (IOException e) {
            e.printStackTrace();
            throw new ServerException("文件内容写入失败,原因:"+e.getMessage());
        }finally {
            writer.close();
        }
        Map<String, String> stringStringMap = attributeMapConfig.getSystem_attrmap();
        if (!stringStringMap.containsKey(groupMapAttrXML.getGroupMapAttrName().replace(".xml",""))) {
            groupMapAttrXML.setIsEnable(false);
            return R.success("新增成功,但新增的属性映射文件名,未在nacos上找到相关配置,请及时更新nacos上的配置!");
        }else {
            groupMapAttrXML.setIsEnable(true);
            return R.success("新增成功!");
        }
    }
 
    /**
     * 获取xml文件中内容
     * @return
     */
    private String getXMLContent(String xmlPath) throws ServerException {
        try {
            File file = new File(xmlPath);
            byte[] bytes = Files.readAllBytes(Paths.get(file.toURI()));
            return new String(bytes);
        } catch (Exception e) {
            throw new ServerException(StringUtil.format("读取%s路径下的xml文件失败,原因:%s",xmlPath,e.getMessage()));
        }
    }
 
}