ludc
2024-01-08 e1372e2e2f72bb96b55378a2b0fd19195e18fd20
276:web端修改xml属性映射文件,后端接口提交
已修改3个文件
已添加2个文件
340 ■■■■■ 文件已修改
Source/UBCS/ubcs-service-api/ubcs-code-api/src/main/java/com/vci/ubcs/code/entity/GroupMapAttrXML.java 37 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Source/UBCS/ubcs-service-api/ubcs-code-api/src/main/java/com/vci/ubcs/code/vo/GroupMapAttrXMLVO.java 23 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/controller/GroupMapAttrXMLController.java 51 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/IGroupMapAttrXMLService.java 33 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/GroupMapAttrXMLServiceImpl.java 196 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Source/UBCS/ubcs-service-api/ubcs-code-api/src/main/java/com/vci/ubcs/code/entity/GroupMapAttrXML.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,37 @@
package com.vci.ubcs.code.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
 * æ˜ å°„文件
 * @author ludc
 * @date 2024/1/8 15:44
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class GroupMapAttrXML {
    /**
     * å±žæ€§æ˜ å°„文件名称
     */
    private String groupMapAttrName;
    /**
     * å±žæ€§æ˜ å°„文件保存的路径
     */
    private String attrMapPath;
    /**
     * å±žæ€§æ˜ å°„文件内容
     */
    private String groupMapAttrContent;
    /**
     * æ˜¯å¦æ­£åœ¨ä½¿ç”¨
     */
    private Boolean isEnable;
}
Source/UBCS/ubcs-service-api/ubcs-code-api/src/main/java/com/vci/ubcs/code/vo/GroupMapAttrXMLVO.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,23 @@
package com.vci.ubcs.code.vo;
import com.vci.ubcs.code.entity.GroupMapAttrXML;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
 * æ˜ å°„文件
 * @author ludc
 * @date 2024/1/8 15:44
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class GroupMapAttrXMLVO extends GroupMapAttrXML {
    /**
     * ä¿®æ”¹xml文件名称
     */
    private String updateXMLName;
}
Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/controller/GroupMapAttrXMLController.java
@@ -1,18 +1,22 @@
package com.vci.ubcs.code.controller;
import com.vci.ubcs.code.entity.GroupMapAttrXML;
import com.vci.ubcs.code.service.IGroupMapAttrXMLService;
import com.vci.ubcs.code.vo.GroupMapAttrXMLVO;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import oracle.jdbc.proxy.annotation.Post;
import org.apache.ibatis.annotations.Update;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.xml.sax.SAXException;
import javax.annotation.Resource;
import javax.ws.rs.PUT;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.rmi.ServerException;
import java.util.List;
/**
 * é›†å›¢å±žæ€§æ˜ å°„xml配置管理
@@ -27,11 +31,44 @@
    private final IGroupMapAttrXMLService groupMapAttrXMLService;
    @GetMapping("/getGroupMapXML")
    public R<String> getGroupMapXMLInfo(@RequestParam("xmlName") String xmlName) throws ParserConfigurationException, IOException, SAXException {
        return R.data(groupMapAttrXMLService.getGroupMapXMLInfo(xmlName));
    /**
     * èŽ·å–xml文件的详细信息
     * @param groupMapAttrXML
     * @return
     * @throws IOException
     */
    @GetMapping("/detail")
    public R<GroupMapAttrXML> getGroupMapXMLInfo(GroupMapAttrXML groupMapAttrXML) throws ServerException {
        return R.data(groupMapAttrXMLService.getGroupMapXMLInfo(groupMapAttrXML));
    }
    /**
     * èŽ·å–æ‰€æœ‰é…ç½®åœ¨nacos上的xml文件内容,(也就是在启用的xml映射文件)
     * @return
     */
    @GetMapping("getGroupMapXMLList")
    public R<List<GroupMapAttrXML>> getGroupMapXMLList(){
        return R.data(groupMapAttrXMLService.getGroupMapXMLList());
    }
    /**
     * ä¿®æ”¹å±žæ€§æ˜ å°„文件
     * @param groupMapAttrXMLVO
     * @return
     */
    @PutMapping("/updateGroupMapXML")
    public R updateGroupMapXML(@RequestBody GroupMapAttrXMLVO groupMapAttrXMLVO) throws IOException {
        return groupMapAttrXMLService.updateGroupMapXML(groupMapAttrXMLVO);
    }
    /**
     * æ–°å¢žé›†å›¢å±žæ€§æ˜ å°„配置文件
     * @param groupMapAttrXML
     * @return
     */
    @PostMapping("/addGroupMapXML")
    public R addGroupMapXML(@RequestBody GroupMapAttrXML groupMapAttrXML) throws IOException {
        return groupMapAttrXMLService.addGroupMapXML(groupMapAttrXML);
    }
}
Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/IGroupMapAttrXMLService.java
@@ -1,10 +1,15 @@
package com.vci.ubcs.code.service;
import com.vci.ubcs.code.entity.GroupMapAttrXML;
import com.vci.ubcs.code.vo.GroupMapAttrXMLVO;
import org.springblade.core.tool.api.R;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.rmi.ServerException;
import java.util.List;
/**
 * é›†å›¢å±žæ€§æ˜ å°„xml配置管理
@@ -13,6 +18,32 @@
 */
public interface IGroupMapAttrXMLService {
    String getGroupMapXMLInfo(String xmlName) throws ParserConfigurationException, IOException, SAXException;
    /**
     * èŽ·å–xml文件的详细信息
     * @param groupMapAttrXML
     * @return
     * @throws IOException
     */
    GroupMapAttrXML getGroupMapXMLInfo(GroupMapAttrXML groupMapAttrXML) throws ServerException;
    /**
     * èŽ·å–æ‰€æœ‰é…ç½®åœ¨nacos上的xml文件内容,(也就是在启用的xml映射文件)
     * @return
     */
    List<GroupMapAttrXML> getGroupMapXMLList();
    /**
     * ä¿®æ”¹å±žæ€§æ˜ å°„文件
     * @param groupMapAttrXMLVO
     * @return
     */
    R updateGroupMapXML(GroupMapAttrXMLVO groupMapAttrXMLVO) throws IOException;
    /**
     * æ–°å¢žé›†å›¢å±žæ€§æ˜ å°„配置文件
     * @param groupMapAttrXML
     * @return
     */
    R addGroupMapXML(GroupMapAttrXML groupMapAttrXML) throws IOException;
}
Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/GroupMapAttrXMLServiceImpl.java
@@ -1,24 +1,33 @@
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;
@@ -29,18 +38,195 @@
 */
@Service
@Slf4j
public class GroupMapAttrXMLServiceImpl implements IGroupMapAttrXMLService {
public class GroupMapAttrXMLServiceImpl implements IGroupMapAttrXMLService,EnvironmentAware {
    private static final String XML_FILE_PATH = "C:\\data1\\ubcs\\ubcs-server\\xml\\PDM.xml";
    @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 String getGroupMapXMLInfo(String xmlName) throws IOException {
    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 {
            File file = new File(XML_FILE_PATH);
            // å‘文件中写入指定内容
            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文件失败,原因:%s",xmlName,e.getMessage()));
            throw new ServerException(StringUtil.format("读取%s路径下的xml文件失败,原因:%s",xmlPath,e.getMessage()));
        }
    }