Commit e60aa06f by qiuchaofei

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

parent 1ff1be19
......@@ -5,7 +5,10 @@ import com.ulisesbocchio.jasyptspringboot.environment.StandardEncryptableEnviron
import org.jasypt.encryption.StringEncryptor;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableScheduling
......@@ -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;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
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.multipart.MultipartFile;
import java.util.HashMap;
import java.util.List;
......@@ -149,4 +151,15 @@ public class DataRelationAnalyController {
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;
import org.springframework.web.multipart.MultipartFile;
public interface Excel2Neo4jService {
String generalRelationFromExcel(String filePath);
// fileName, file
String uploadExcel(MultipartFile file) ;
}
......@@ -13,11 +13,9 @@ import org.neo4j.driver.v1.Session;
import org.neo4j.driver.v1.StatementResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
......@@ -31,8 +29,27 @@ public class Excel2Neo4jServiceImpl implements Excel2Neo4jService {
private Session neo4jSession;
@Override
public String generalRelationFromExcel(String filePath) {
File file = new File(filePath);
public String uploadExcel(MultipartFile file) {
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";
......@@ -40,138 +57,112 @@ public class Excel2Neo4jServiceImpl implements Excel2Neo4jService {
List<SystemNode> systemNodeList = new ArrayList<>();
List<SystemRelation> systemRelationList = new ArrayList<>();
FileInputStream is = null;
Workbook workbook = null;
try {
Map<String,String> colorMap = new HashMap<>();
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);
}
Map<String,String> colorMap = new HashMap<>();
// 第一个sheet是 node,先解析入neo4j。
// 第二个sheet 是 关系,
if (workbook != null) {
Sheet yanseSHeet = workbook.getSheet("形式颜色");
{
int firstRowNum = yanseSHeet.getFirstRowNum();
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 yanseSHeet = workbook.getSheet("形式颜色");
{
int firstRowNum = yanseSHeet.getFirstRowNum();
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的开始行
int firstRowNum = jiedianSHeet.getFirstRowNum();
// 获得当前sheet的结束行
int lastRowNum = jiedianSHeet.getLastRowNum();
// 循环除第一行外的所有行
for (int rowNum = firstRowNum + 1; rowNum <= lastRowNum; rowNum++) {
// 获得当前行
Row row = jiedianSHeet.getRow(rowNum);
if (row == null) {
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 jiedianSHeet = workbook.getSheet("系统节点");
{
// 获得当前sheet的开始行
int firstRowNum = jiedianSHeet.getFirstRowNum();
// 获得当前sheet的结束行
int lastRowNum = jiedianSHeet.getLastRowNum();
// 循环除第一行外的所有行
for (int rowNum = firstRowNum + 1; rowNum <= lastRowNum; rowNum++) {
// 获得当前行
Row row = jiedianSHeet.getRow(rowNum);
if (row == null) {
continue;
}
Sheet guanxiSHeet = workbook.getSheet("关系");
{
// 获得当前sheet的开始行
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 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("协议",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);
// 获得当前行的开始列
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);
}
}catch (Exception e){
e.printStackTrace();
}
finally {
try {
is.close();
} catch (IOException e) {
System.out.println(e.toString());
Sheet guanxiSHeet = workbook.getSheet("关系");
{
// 获得当前sheet的开始行
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){
......@@ -196,6 +187,7 @@ public class Excel2Neo4jServiceImpl implements Excel2Neo4jService {
String fromId = relation.getFrom();
String toId = relation.getTo();
String xuhao = relation.getAttributeMap().get("序号");
String type = relation.getAttributeMap().get("形式");
String yanse = relation.getAttributeMap().get("颜色");
String xieyi = relation.getAttributeMap().get("协议");
......@@ -205,14 +197,48 @@ public class Excel2Neo4jServiceImpl implements Excel2Neo4jService {
String addRelationCypher = " match( a:softplatform {name:\"" + fromId + "\"} ),(b:softplatform {name:\"" + toId + "\"} ) " +
" 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 ";
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) {
String cellValue = "";
if (cell == null) {
......
......@@ -563,7 +563,7 @@ public class MetadataServiceImpl implements IMetadataService {
Map<Long, ReturnNode> nodesMap = 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<>();
while (statementResult.hasNext()) {
......@@ -606,7 +606,7 @@ public class MetadataServiceImpl implements IMetadataService {
* asMap 相当于 节点的properties属性信息
*/
// relationship.id();
//
String xuhao =""+ relationship.asMap().get("序号");
String xieyi =""+ relationship.asMap().get("协议");
String yanse =""+ relationship.asMap().get("颜色");
String yewuleibie =""+ relationship.asMap().get("业务类别");
......@@ -636,7 +636,7 @@ public class MetadataServiceImpl implements IMetadataService {
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