Commit e60aa06f by qiuchaofei

1,添加上传文件,2对关系进行排序

parent 1ff1be19
...@@ -5,7 +5,10 @@ import com.ulisesbocchio.jasyptspringboot.environment.StandardEncryptableEnviron ...@@ -5,7 +5,10 @@ import com.ulisesbocchio.jasyptspringboot.environment.StandardEncryptableEnviron
import org.jasypt.encryption.StringEncryptor; import org.jasypt.encryption.StringEncryptor;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.EnableScheduling;
@EnableScheduling @EnableScheduling
...@@ -32,5 +35,15 @@ public class MetadataRelationApplication { ...@@ -32,5 +35,15 @@ public class MetadataRelationApplication {
} }
}; };
} }
@Bean
public TomcatServletWebServerFactory tomcatServletWebServerFactory (){
// 修改内置的 tomcat 容器配置
TomcatServletWebServerFactory tomcatServlet = new TomcatServletWebServerFactory();
tomcatServlet .addConnectorCustomizers(
(TomcatConnectorCustomizer) connector -> connector.setProperty("relaxedQueryChars", "[]{}")
);
return tomcatServlet ;
}
} }
...@@ -10,7 +10,9 @@ import org.slf4j.LoggerFactory; ...@@ -10,7 +10,9 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
...@@ -149,4 +151,15 @@ public class DataRelationAnalyController { ...@@ -149,4 +151,15 @@ public class DataRelationAnalyController {
return ""; return "";
} }
//解析excel,存入neo4j
@ApiOperation(tags = "", value = "从excel生成neo4j的节点与关系。")
@RequestMapping(path = "/uploadExcelToNeo4j", method = RequestMethod.POST)
public String uploadExcelToNeo4j(@RequestParam(value = "excelFile") MultipartFile file) {
logger.info("上传excel文件节点与关系:"+file.getOriginalFilename() );
excel2Neo4jService.uploadExcel(file) ;
logger.info("上传excel文件节点与关系完成。" );
return "";
}
} }
package com.keymobile.metadata.metadataRelation.service; package com.keymobile.metadata.metadataRelation.service;
import org.springframework.web.multipart.MultipartFile;
public interface Excel2Neo4jService { public interface Excel2Neo4jService {
String generalRelationFromExcel(String filePath); String generalRelationFromExcel(String filePath);
// fileName, file
String uploadExcel(MultipartFile file) ;
} }
...@@ -13,11 +13,9 @@ import org.neo4j.driver.v1.Session; ...@@ -13,11 +13,9 @@ import org.neo4j.driver.v1.Session;
import org.neo4j.driver.v1.StatementResult; import org.neo4j.driver.v1.StatementResult;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File; import java.io.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
...@@ -31,8 +29,27 @@ public class Excel2Neo4jServiceImpl implements Excel2Neo4jService { ...@@ -31,8 +29,27 @@ public class Excel2Neo4jServiceImpl implements Excel2Neo4jService {
private Session neo4jSession; private Session neo4jSession;
@Override @Override
public String generalRelationFromExcel(String filePath) { public String uploadExcel(MultipartFile file) {
File file = new File(filePath); String fileName = file.getOriginalFilename();
try {
InputStream inputStream = file.getInputStream();
Workbook workbook;
if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
workbook = new XSSFWorkbook(inputStream);
} else {
workbook = new HSSFWorkbook(inputStream);
}
generalRelationFromWorkBook(workbook);
} catch (IOException e) {
e.printStackTrace();
return "failed";
}
return "OK";
}
private String generalRelationFromWorkBook(Workbook workbook){
//先删除数据 //先删除数据
String deleteCypher= "MATCH (n:softplatform) detach delete n"; String deleteCypher= "MATCH (n:softplatform) detach delete n";
...@@ -40,138 +57,112 @@ public class Excel2Neo4jServiceImpl implements Excel2Neo4jService { ...@@ -40,138 +57,112 @@ public class Excel2Neo4jServiceImpl implements Excel2Neo4jService {
List<SystemNode> systemNodeList = new ArrayList<>(); List<SystemNode> systemNodeList = new ArrayList<>();
List<SystemRelation> systemRelationList = new ArrayList<>(); List<SystemRelation> systemRelationList = new ArrayList<>();
FileInputStream is = null; Map<String,String> colorMap = new HashMap<>();
Workbook workbook = null;
try {
String fileName = file.getName(); Sheet yanseSHeet = workbook.getSheet("形式颜色");
// 创建Workbook工作薄对象,表示整个excel {
// 获取excel文件的io流 int firstRowNum = yanseSHeet.getFirstRowNum();
is = new FileInputStream(file); int lastRowNum = yanseSHeet.getLastRowNum();
// 根据文件后缀名不同(xls和xlsx)获得不同的Workbook实现类对象 for (int rowNum = firstRowNum + 1; rowNum <= lastRowNum; rowNum++) {
if (fileName.endsWith(".xlsx")) { // 获得当前行
// 2007 及2007以上 Row row = yanseSHeet.getRow(rowNum);
workbook = new XSSFWorkbook(is); if (row == null) {
} continue;
}
Map<String,String> colorMap = new HashMap<>(); // 获得当前行的开始列
int firstCellNum = row.getFirstCellNum();
// 第一个sheet是 node,先解析入neo4j。 // 获得当前行的列数
// 第二个sheet 是 关系, int lastCellNum = row.getLastCellNum();
if (workbook != null) { String[] cells = new String[row.getLastCellNum()];
// 循环当前行
Sheet yanseSHeet = workbook.getSheet("形式颜色"); String typeName = getCellValue(row.getCell(1));
{ String color = getCellValue(row.getCell(2));
int firstRowNum = yanseSHeet.getFirstRowNum(); colorMap.put(typeName,color);
int lastRowNum = yanseSHeet.getLastRowNum();
for (int rowNum = firstRowNum + 1; rowNum <= lastRowNum; rowNum++) {
// 获得当前行
Row row = yanseSHeet.getRow(rowNum);
if (row == null) {
continue;
}
// 获得当前行的开始列
int firstCellNum = row.getFirstCellNum();
// 获得当前行的列数
int lastCellNum = row.getLastCellNum();
String[] cells = new String[row.getLastCellNum()];
// 循环当前行
String typeName = getCellValue(row.getCell(1));
String color = getCellValue(row.getCell(2));
colorMap.put(typeName,color);
} }
} }
Sheet jiedianSHeet = workbook.getSheet("系统节点"); Sheet jiedianSHeet = workbook.getSheet("系统节点");
{ {
// 获得当前sheet的开始行 // 获得当前sheet的开始行
int firstRowNum = jiedianSHeet.getFirstRowNum(); int firstRowNum = jiedianSHeet.getFirstRowNum();
// 获得当前sheet的结束行 // 获得当前sheet的结束行
int lastRowNum = jiedianSHeet.getLastRowNum(); int lastRowNum = jiedianSHeet.getLastRowNum();
// 循环除第一行外的所有行 // 循环除第一行外的所有行
for (int rowNum = firstRowNum + 1; rowNum <= lastRowNum; rowNum++) { for (int rowNum = firstRowNum + 1; rowNum <= lastRowNum; rowNum++) {
// 获得当前行 // 获得当前行
Row row = jiedianSHeet.getRow(rowNum); Row row = jiedianSHeet.getRow(rowNum);
if (row == null) { if (row == null) {
continue; continue;
}
// 获得当前行的开始列
int firstCellNum = row.getFirstCellNum();
// 获得当前行的列数
int lastCellNum = row.getLastCellNum();
String[] cells = new String[row.getLastCellNum()];
// 循环当前行
Map<String,String> nodeAttribute = new HashMap<>();
String systemName = "";
SystemNode systemNode = new SystemNode();
for (int cellNum = firstCellNum; cellNum < lastCellNum; cellNum++) {
Cell cell = row.getCell(cellNum);
String value = getCellValue(cell);
if(cellNum == 1 ){
systemName = getCellValue(cell);
systemNode.setName(systemName);
}else if(cellNum ==2 ){
nodeAttribute.put("内部",getCellValue(cell));
systemNode.getNodeAttributeMap().put("内外部系统",getCellValue(cell));
}else if(cellNum ==3 ){
systemNode.getNodeAttributeMap().put("行",getCellValue(cell));
}else if(cellNum ==4 ){
systemNode.getNodeAttributeMap().put("列",getCellValue(cell));;
}
}
systemNodeList.add(systemNode);
}
} }
// 获得当前行的开始列
Sheet guanxiSHeet = workbook.getSheet("关系"); int firstCellNum = row.getFirstCellNum();
{ // 获得当前行的列数
// 获得当前sheet的开始行 int lastCellNum = row.getLastCellNum();
int firstRowNum = guanxiSHeet.getFirstRowNum(); String[] cells = new String[row.getLastCellNum()];
// 获得当前sheet的结束行 // 循环当前行
int lastRowNum = guanxiSHeet.getLastRowNum(); Map<String,String> nodeAttribute = new HashMap<>();
// 循环除第一行外的所有行 String systemName = "";
for (int rowNum = firstRowNum + 1; rowNum <= lastRowNum; rowNum++) { SystemNode systemNode = new SystemNode();
// 获得当前行 for (int cellNum = firstCellNum; cellNum < lastCellNum; cellNum++) {
Row row = guanxiSHeet.getRow(rowNum); Cell cell = row.getCell(cellNum);
if (row == null) { String value = getCellValue(cell);
continue; if(cellNum == 1 ){
} systemName = getCellValue(cell);
// 获得当前行的开始列 systemNode.setName(systemName);
int firstCellNum = row.getFirstCellNum(); }else if(cellNum ==2 ){
// 获得当前行的列数 nodeAttribute.put("内部",getCellValue(cell));
int lastCellNum = row.getLastCellNum(); systemNode.getNodeAttributeMap().put("内外部系统",getCellValue(cell));
String[] cells = new String[row.getLastCellNum()]; }else if(cellNum ==3 ){
// 循环当前行 systemNode.getNodeAttributeMap().put("行",getCellValue(cell));
SystemRelation systemRelation = new SystemRelation(); }else if(cellNum ==4 ){
String fromId = getCellValue(row.getCell(1)); systemNode.getNodeAttributeMap().put("列",getCellValue(cell));;
String toId = getCellValue(row.getCell(2));
systemRelation.setFrom(fromId);
systemRelation.setTo(toId);
String xingshi = getCellValue(row.getCell(3));
systemRelation.getAttributeMap().put("形式",xingshi);
String color = colorMap.get(xingshi);
systemRelation.getAttributeMap().put("颜色",color);
systemRelation.getAttributeMap().put("协议",getCellValue(row.getCell(4)));
systemRelation.getAttributeMap().put("业务类别",getCellValue(row.getCell(5)));
systemRelation.getAttributeMap().put("数据结构",getCellValue(row.getCell(6)));
systemRelation.getAttributeMap().put("业务说明",getCellValue(row.getCell(7)));
systemRelationList.add(systemRelation);
} }
} }
systemNodeList.add(systemNode);
} }
}catch (Exception e){
e.printStackTrace();
} }
finally {
try { Sheet guanxiSHeet = workbook.getSheet("关系");
is.close(); {
} catch (IOException e) { // 获得当前sheet的开始行
System.out.println(e.toString()); int firstRowNum = guanxiSHeet.getFirstRowNum();
// 获得当前sheet的结束行
int lastRowNum = guanxiSHeet.getLastRowNum();
// 循环除第一行外的所有行
for (int rowNum = firstRowNum + 1; rowNum <= lastRowNum; rowNum++) {
// 获得当前行
Row row = guanxiSHeet.getRow(rowNum);
if (row == null) {
continue;
}
// 获得当前行的开始列
int firstCellNum = row.getFirstCellNum();
// 获得当前行的列数
int lastCellNum = row.getLastCellNum();
String[] cells = new String[row.getLastCellNum()];
// 循环当前行
SystemRelation systemRelation = new SystemRelation();
String xuhao = getCellValue(row.getCell(0));
String fromId = getCellValue(row.getCell(1));
String toId = getCellValue(row.getCell(2));
systemRelation.setFrom(fromId);
systemRelation.setTo(toId);
String xingshi = getCellValue(row.getCell(3));
systemRelation.getAttributeMap().put("形式",xingshi);
String color = colorMap.get(xingshi);
systemRelation.getAttributeMap().put("颜色",color);
systemRelation.getAttributeMap().put("序号",xuhao);
systemRelation.getAttributeMap().put("协议",getCellValue(row.getCell(4)));
systemRelation.getAttributeMap().put("业务类别",getCellValue(row.getCell(5)));
systemRelation.getAttributeMap().put("数据结构",getCellValue(row.getCell(6)));
systemRelation.getAttributeMap().put("业务说明",getCellValue(row.getCell(7)));
systemRelationList.add(systemRelation);
} }
} }
for(SystemNode systemNode:systemNodeList){ for(SystemNode systemNode:systemNodeList){
...@@ -196,6 +187,7 @@ public class Excel2Neo4jServiceImpl implements Excel2Neo4jService { ...@@ -196,6 +187,7 @@ public class Excel2Neo4jServiceImpl implements Excel2Neo4jService {
String fromId = relation.getFrom(); String fromId = relation.getFrom();
String toId = relation.getTo(); String toId = relation.getTo();
String xuhao = relation.getAttributeMap().get("序号");
String type = relation.getAttributeMap().get("形式"); String type = relation.getAttributeMap().get("形式");
String yanse = relation.getAttributeMap().get("颜色"); String yanse = relation.getAttributeMap().get("颜色");
String xieyi = relation.getAttributeMap().get("协议"); String xieyi = relation.getAttributeMap().get("协议");
...@@ -205,14 +197,48 @@ public class Excel2Neo4jServiceImpl implements Excel2Neo4jService { ...@@ -205,14 +197,48 @@ public class Excel2Neo4jServiceImpl implements Excel2Neo4jService {
String addRelationCypher = " match( a:softplatform {name:\"" + fromId + "\"} ),(b:softplatform {name:\"" + toId + "\"} ) " + String addRelationCypher = " match( a:softplatform {name:\"" + fromId + "\"} ),(b:softplatform {name:\"" + toId + "\"} ) " +
" merge (a)-[r:"+type+"]-> (b) " + " merge (a)-[r:"+type+"]-> (b) " +
"set r.协议=\""+ xieyi + "\" , r.颜色=\""+ yanse +"\" , r.业务类别 = \""+zubie+"\" ,r.数据结构= \""+shujujiegou+"\",r.业务说明= \""+yewushuoming+"\" " + "set r.协议=\""+ xieyi + "\" ,r.序号= \""+ xuhao +"\" ,r.颜色=\""+ yanse +"\" , r.业务类别 = \""+zubie+"\" ,r.数据结构= \""+shujujiegou+"\",r.业务说明= \""+yewushuoming+"\" " +
" return r "; " return r ";
neo4jSession.run(addRelationCypher); neo4jSession.run(addRelationCypher);
} }
return "";
}
return ""; @Override
public String generalRelationFromExcel(String filePath) {
File file = new File(filePath);
FileInputStream is = null;
Workbook workbook = null;
try {
String fileName = file.getName();
// 创建Workbook工作薄对象,表示整个excel
// 获取excel文件的io流
is = new FileInputStream(file);
// 根据文件后缀名不同(xls和xlsx)获得不同的Workbook实现类对象
if (fileName.endsWith(".xlsx")) {
// 2007 及2007以上
workbook = new XSSFWorkbook(is);
}
generalRelationFromWorkBook(workbook);
}catch (Exception e){
e.printStackTrace();
return "failed";
}
finally {
try {
is.close();
} catch (IOException e) {
System.out.println(e.toString());
}
}
return "OK";
} }
public static String getCellValue(Cell cell) { public static String getCellValue(Cell cell) {
String cellValue = ""; String cellValue = "";
if (cell == null) { if (cell == null) {
......
...@@ -563,7 +563,7 @@ public class MetadataServiceImpl implements IMetadataService { ...@@ -563,7 +563,7 @@ public class MetadataServiceImpl implements IMetadataService {
Map<Long, ReturnNode> nodesMap = new HashMap<>(); Map<Long, ReturnNode> nodesMap = new HashMap<>();
Map<String, MetaModel> metaModelMap = new HashMap<>(); Map<String, MetaModel> metaModelMap = new HashMap<>();
Map<String ,ReturnEdge> returnEdgeMap = new HashMap<>(); Map<String ,ReturnEdge> returnEdgeMap = new TreeMap<>();
Map<String,String> colorMap = new HashMap<>(); Map<String,String> colorMap = new HashMap<>();
while (statementResult.hasNext()) { while (statementResult.hasNext()) {
...@@ -606,7 +606,7 @@ public class MetadataServiceImpl implements IMetadataService { ...@@ -606,7 +606,7 @@ public class MetadataServiceImpl implements IMetadataService {
* asMap 相当于 节点的properties属性信息 * asMap 相当于 节点的properties属性信息
*/ */
// relationship.id(); // relationship.id();
// String xuhao =""+ relationship.asMap().get("序号");
String xieyi =""+ relationship.asMap().get("协议"); String xieyi =""+ relationship.asMap().get("协议");
String yanse =""+ relationship.asMap().get("颜色"); String yanse =""+ relationship.asMap().get("颜色");
String yewuleibie =""+ relationship.asMap().get("业务类别"); String yewuleibie =""+ relationship.asMap().get("业务类别");
...@@ -636,7 +636,7 @@ public class MetadataServiceImpl implements IMetadataService { ...@@ -636,7 +636,7 @@ public class MetadataServiceImpl implements IMetadataService {
edge.getAttributeMaps().put("业务说明",yewushuoming); edge.getAttributeMaps().put("业务说明",yewushuoming);
// //
returnEdgeMap .put(edgeId,edge ); returnEdgeMap .put(xuhao,edge );
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment