public Map<String, Set> doubleNodes(Neo4jNode node1, Neo4jNode node2, String type, int step) throws SQLException { Map<String, Set> map = new HashMap<String, Set>(); Set<Neo4jNode> nodes = new HashSet<Neo4jNode>(); Set<Neo4jRelationship> relationships = new HashSet<Neo4jRelationship>(); if (!type.equals("")) type = ":" + type; StringBuffer sb = new StringBuffer("start "); if (node1.getNode_type().equals("1")) { sb.append("a=node:nodes(node={1}),"); } else { sb.append("a=node:nodes(regno={1}),"); } if (node2.getNode_type().equals("1")) { sb.append("b=node:nodes(node={2}) "); } else { sb.append("b=node:nodes(regno={2}) "); } sb.append("match p=allShortestPaths(a-["); // 全部最短路径 sb.append(type); sb.append("*.."); sb.append(step); sb.append("]-b) return nodes(p),relationships(p),length(p) limit "); // 2点之间存在多条最短路径 int[] arrays = {1, 5 - step}; // 多节点5层以下取多条路径 5层以上取最短路径 int limit = Math.max(arrays[0], arrays[1]); sb.append(limit); conn = getConn(); PreparedStatement psmt = conn.prepareStatement(sb.toString()); psmt.setString(1, node1.getNode()); psmt.setString(2, node2.getNode()); psmt.setQueryTimeout(Integer.parseInt(queryTimeOut)); ResultSet rs = psmt.executeQuery(); while (rs.next()) { nodes.addAll(jsonNode(rs.getString(1))); // Iterator<Neo4jNode> it=nodes.iterator(); // while(it.hasNext()){ // Neo4jNode i=it.next(); // System.out.println(i.getInv_name()+"="+i.getNode()); // } if (rs.getInt(3) == 1) { relationships.addAll(exBenifit(node1, node2, type)); } else { relationships.addAll(jsonRelationship(rs.getString(2))); } } map.put("nodes", nodes); map.put("relationships", relationships); return map; }
@Override public Boolean nodeIsExist(Neo4jNode node) throws SQLException { // TODO Auto-generated method stub StringBuffer sb = new StringBuffer(); sb.append("start a=node:nodes("); if ("1".equals(node.getNode_type())) { sb.append("node"); } else { sb.append("regno"); } sb.append("="); sb.append("{1}"); sb.append(") return a"); conn = getConn(); PreparedStatement psmt = conn.prepareStatement(sb.toString()); psmt.setString(1, node.getNode()); ResultSet rs = psmt.executeQuery(); if (rs.next()) { return true; } else { return false; } // rs.next(); // if (rs.getString(1).length()!=0) // return false; // else return true; }
private Set<Neo4jRelationship> exBenifit(Neo4jNode node1, Neo4jNode node2, String type) throws SQLException { // TODO Auto-generated method stub Set<Neo4jRelationship> relationships = new HashSet<Neo4jRelationship>(); StringBuffer sb = new StringBuffer("start "); sb.append("a=node:nodes(node={1}),"); sb.append("b=node:nodes(node={2}) "); sb.append("match p=a-["); sb.append(type); sb.append("]-b return relationships(p)"); conn = getConn(); PreparedStatement psmt = conn.prepareStatement(sb.toString()); psmt.setString(1, node1.getNode()); psmt.setString(2, node2.getNode()); psmt.setQueryTimeout(Integer.parseInt(queryTimeOut)); ResultSet rs = psmt.executeQuery(); while (rs.next()) { relationships.addAll(jsonRelationship(rs.getString(1))); } return relationships; }
public Map<String, Set> single(Neo4jNode node1, int depth, String type, RelationQuery query) throws SQLException { Map<String, Set> map = new HashMap<String, Set>(); Set<Neo4jNode> nodes = new HashSet<Neo4jNode>(); Set<Neo4jRelationship> relationships = new HashSet<Neo4jRelationship>(); if (!type.equals("")) type = ":" + type; StringBuffer sb = new StringBuffer("start "); sb.append("a=node:nodes("); if ("1".equals(node1.getNode_type())) { sb.append("node"); } else { sb.append("regno"); } sb.append("={1}"); sb.append(") "); sb.append("match p=a"); if (StringUtils.isNotNullOrEmpty(query.getFoward())) { if ("down".equals(query.getFoward())) { sb.append("<"); } } sb.append(" -[ "); sb.append(type); sb.append("*.."); sb.append(depth); sb.append("]-"); if (StringUtils.isNotNullOrEmpty(query.getFoward())) { if ("up".equals(query.getFoward())) { sb.append(">"); } } sb.append(" b "); sb.append(" where 1=1 "); if (StringUtils.isNotNullOrEmpty(query.getEndStatus()) && query.getEndStatus().size() > 0) { sb.append("and all(x in nodes(p) where x.entstatus='' "); sb.append(" or "); for (int i = 0; i < query.getEndStatus().size(); i++) { sb.append(" x.entstatus='" + query.getEndStatus().get(i) + "'"); if (i != (query.getEndStatus().size() - 1)) { sb.append(" or "); } } sb.append(") "); } // all(x in nodes(p) where x.entstatus='') if (StringUtils.isNotNullOrEmpty(query.getFoward())) { // 如果是只查询股东 需要包括人员 // entstatus='' if ("down".equals(query.getFoward())) { sb.append(" or b.entstatus='' "); } } sb.append(" return nodes(p),relationships(p) order by length(p) "); sb.append(" limit "); sb.append(nodeLimit); // "start a=node:nodes(node={1}) " // + "match p=a-["+type+"*.."+depth+"]-b " // + "return nodes(p),relationships(p) LIMIT 100" conn = getConn(); PreparedStatement psmt = conn.prepareStatement(sb.toString()); psmt.setQueryTimeout(Integer.parseInt(queryTimeOut)); psmt.setString(1, node1.getNode()); // psmt.setInt(2, depth); // psmt.setString(3, type); ResultSet rs = psmt.executeQuery(); while (rs.next()) { nodes.addAll(jsonNode(rs.getString(1))); // Iterator<Neo4jNode> it=nodes.iterator(); // while(it.hasNext()){ // Neo4jNode i=it.next(); // System.out.println(i.getInv_name()+"="+i.getNode()); // } relationships.addAll(jsonRelationship(rs.getString(2))); } map.put("nodes", nodes); map.put("relationships", relationships); return map; }