父子节点多级关系导入时的处理

本文以Excel形式导入:
在Excel中初始化需要导入的数据,子节点名称比父节点名称前多2个空格,代表父子关系
导入节点的父子关系图
将Excel导入,在后台解析,代码如下:

public List<ModelNode> covertSheetToList(Sheet sheet, String modelId, String nodeId) {
		List<ModelNode> nodeList = new ArrayList<ModelNode>();
		String[] ids = new String[50];
		ids[0] = nodeId;
		//行数
		int rowNum = sheet.getPhysicalNumberOfRows();
		//i=1 跳过第一行
		String id = "";
		int index = -1;
		for(int i = 1;i<rowNum;i++){
			Row row = sheet.getRow(i);
			if(null == row){
				continue;
			}
			ModelNode node = new ModelNode();
			String nodeName = getCellValue(row.getCell(0));
			String weight = getCellValue(row.getCell(1));   //需要验证权重的格式
			if("".equals(nodeName.trim()) || "".equals(weight.trim())){
				continue;
			}
			node.setName(nodeName);
			node.setWeight(cureWeight(weight));
			node.setModelId(modelId);
			node.setCreateTime(String.valueOf(System.currentTimeMillis()));
			
			index = getFirstCharIndex(nodeName);
			if(null == ids[index] || index % 2 != 0){  //把一些父节点为空或空格数量不为2的倍数的不导入
				continue;
			}
			for(int j = index+1;j<ids.length;j++){
				ids[j] = null;
			}
			node.setParentId(ids[index]);
			id = IdGenerator.uuid32();
			node.setId(id);
			ids[index+2] = id;
			/*if(nodeName.startsWith(" ")){
				if(!firstLevel){
					continue;
				}
				if(index == getFirstCharIndex(nodeName)-1){
					node.setParentId(pId);
					id = IdGenerator.uuid32();
					node.setId(id);
				}else{
					int index1 = getFirstCharIndex(nodeName)-1;
					if(index1 - index != 2){
						continue;
					}else{
						index = index1;
					}
					if(index % 2 != 1){   //空格的个数不是偶数,为非法节点,不进行导入
						continue;
					}
					pId = id;
					node.setParentId(pId);
					id = IdGenerator.uuid32();
					node.setId(id);
				}
			}else{
				firstLevel = true;
				index = getFirstCharIndex(nodeName)-1;
				pId = id = IdGenerator.uuid32();
				node.setId(pId);
				node.setParentId(nodeId);
			}*/
			nodeList.add(node);
		}
		return nodeList;
	}

导入后的效果如下:

节点导入后的效果

猜你喜欢

转载自blog.csdn.net/juligang320/article/details/82886524