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()));
      }
   }