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

本文以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;
	}

导入后的效果如下:

节点导入后的效果

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

行云的逆袭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值