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