|
@@ -0,0 +1,93 @@
|
|
|
+package com.qizhen.healsphere;
|
|
|
+
|
|
|
+import com.qizhen.healsphere.repository.neo4j.entity.BaseEntity;
|
|
|
+import com.qizhen.healsphere.service.EntityService;
|
|
|
+import com.qizhen.healsphere.service.RelationshipService;
|
|
|
+import com.qizhen.healsphere.web.vo.CreateEntityVO;
|
|
|
+import com.qizhen.healsphere.web.vo.RelationshipVO;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.poi.ss.usermodel.Row;
|
|
|
+import org.apache.poi.ss.usermodel.Sheet;
|
|
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
|
+import org.apache.poi.ss.usermodel.WorkbookFactory;
|
|
|
+import org.junit.Test;
|
|
|
+import org.junit.runner.RunWith;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+import org.springframework.context.annotation.ComponentScan;
|
|
|
+import org.springframework.test.context.junit4.SpringRunner;
|
|
|
+
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 是否空腹及归大类
|
|
|
+ */
|
|
|
+@RunWith(SpringRunner.class)
|
|
|
+@ComponentScan(basePackages = {"com.qizhen.healsphere.model", "com.qizhen.healsphere.repository"})
|
|
|
+@SpringBootTest
|
|
|
+public class SnomedCTFromExcelTest {
|
|
|
+ @Autowired
|
|
|
+ RelationshipService relationshipService;
|
|
|
+ @Autowired
|
|
|
+ EntityService entityService;
|
|
|
+ private static String urlExcelPath = "C:\\Users\\17664\\Desktop\\data2(1).xlsx";
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void WriteFromExcel() throws Exception {
|
|
|
+ getDataFromExecl(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void getDataFromExecl(int index) throws Exception {
|
|
|
+ InputStream fis = new FileInputStream(urlExcelPath);
|
|
|
+ Workbook urlWorkbook = WorkbookFactory.create(fis);
|
|
|
+ Sheet urlSheet = urlWorkbook.getSheetAt(index);
|
|
|
+
|
|
|
+ for (int rowNum = 0; rowNum <= urlSheet.getLastRowNum(); rowNum++) {
|
|
|
+ try {
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+ Row row = urlSheet.getRow(rowNum);
|
|
|
+ saveNeo4j(row.getCell(0).getStringCellValue().trim(), row.getCell(1).getStringCellValue().trim(), row.getCell(2).getStringCellValue().trim(), row.getCell(3).getStringCellValue().trim(), row.getCell(4).getStringCellValue().trim());
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveNeo4j(String startName, String startLabel, String relationShip, String endName, String endLabel) throws Exception {
|
|
|
+ try {
|
|
|
+ if(StringUtils.isEmpty(startName) || StringUtils.isEmpty(startLabel)|| StringUtils.isEmpty(relationShip)|| StringUtils.isEmpty(endName) || StringUtils.isEmpty(endLabel)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ BaseEntity startEntity = createNoExists(startLabel, startName);
|
|
|
+ long startId = startEntity.getId();
|
|
|
+ BaseEntity endEntity = createNoExists(endLabel, endName);
|
|
|
+ Long endId = endEntity.getId();
|
|
|
+ RelationshipVO relationshipVO = new RelationshipVO();
|
|
|
+ relationshipVO.setStartId(startId);
|
|
|
+ relationshipVO.setEndId(endId);
|
|
|
+ relationshipVO.setStartLabel(startLabel);
|
|
|
+ relationshipVO.setEndLabel(endLabel);
|
|
|
+ relationshipVO.setRelationshipType(relationShip);
|
|
|
+ System.out.println(relationshipService.createRelationship(Arrays.asList(relationshipVO)));
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private BaseEntity createNoExists(String labelName, String name) {
|
|
|
+ BaseEntity nodeByName = entityService.findNodeByName(labelName, name);
|
|
|
+ if (Objects.nonNull(nodeByName)) {//节点不存在
|
|
|
+ return nodeByName;
|
|
|
+ }
|
|
|
+ CreateEntityVO createEntity = new CreateEntityVO();
|
|
|
+ createEntity.setName(name);
|
|
|
+ createEntity.setLabel(labelName);
|
|
|
+ return entityService.create(createEntity);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|