diff --git a/src/main/java/it/inaf/oats/vospace/persistence/NodeDAO.java b/src/main/java/it/inaf/oats/vospace/persistence/NodeDAO.java index 730a9bd418e802b55b01e5be04b7cad10257c4fb..11d8ad5b2fdb7bdd8c8c873f94d3e38c4fbdcc00 100644 --- a/src/main/java/it/inaf/oats/vospace/persistence/NodeDAO.java +++ b/src/main/java/it/inaf/oats/vospace/persistence/NodeDAO.java @@ -41,21 +41,24 @@ public class NodeDAO { String path = nodeURI.replaceAll("vos://[^/]+", ""); String parentPath = getParentPath(path); - String sql = "SELECT path, relative_path from " + String sql = "SELECT path from " + "node n join node_vos_path p on n.node_id = p.node_id " + "where p.vos_path = ?"; - List paths = jdbcTemplate.query(conn -> { + List ltreeParentPaths = jdbcTemplate.query(conn -> { PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, parentPath); return ps; }, (row, index) -> { - return getPathsFromResultSet(row); + return row.getString("path"); }); - if (paths.isEmpty()) { + if (ltreeParentPaths.isEmpty()) { throw new IllegalStateException("Unable to find parent node during node creation"); } + if (ltreeParentPaths.size() > 1) { + throw new IllegalStateException("Multiple ltree parent paths found for " + parentPath); + } StringBuilder sb = new StringBuilder(); sb.append("INSERT INTO node"); @@ -72,7 +75,7 @@ public class NodeDAO { ps.setArray(5, fromPropertyToArray(ps, getProperty(myNode, getPropertyURI("groupread")))); ps.setArray(6, fromPropertyToArray(ps, getProperty(myNode, getPropertyURI("groupwrite")))); ps.setBoolean(7, Boolean.valueOf(getProperty(myNode, getPropertyURI("publicread")))); - ps.setObject(8, paths.get(0).parentPath, Types.OTHER); + ps.setObject(8, ltreeParentPaths.get(0), Types.OTHER); ps.setObject(9, getDbNodeType(myNode), Types.OTHER); return ps; }); @@ -81,7 +84,7 @@ public class NodeDAO { public Optional listNode(String path) { - String sql = "SELECT os.vos_path, n.node_id, type, async_trans, busy_state, owner_id, group_read, group_write, is_public, content_length, created_on, last_modified from node n\n" + String sql = "SELECT os.vos_path, n.node_id, type, async_trans, busy_state, creator_id, group_read, group_write, is_public, content_length, created_on, last_modified from node n\n" + "JOIN node_vos_path os ON n.node_id = os.node_id\n" + "WHERE n.path ~ (" + getFirstLevelChildrenSelector(path) + ")::lquery\n" + "OR os.vos_path = ? ORDER BY vos_path"; @@ -141,6 +144,9 @@ public class NodeDAO { addProperty(getPropertyURI("btime"), rs.getString("created_on"), properties); + addProperty(getPropertyURI("creator"), rs.getString("creator_id"), + properties); + addProperty(getPropertyURI("mtime"), rs.getString("last_modified"), properties); @@ -228,13 +234,22 @@ public class NodeDAO { return false; } + // Copied from CreateNodeController: to be moved in a common utility class private String getParentPath(String path) { - String[] parsedPath = path.split("/"); + String[] parsedPath = path.split("[/]+"); + + if (parsedPath.length < 2 || !parsedPath[0].isEmpty()) { + throw new IllegalArgumentException(); + } StringBuilder sb = new StringBuilder(); + sb.append("/"); - for (int i = 0; i < parsedPath.length - 1; i++) { - sb.append("/").append(parsedPath[i]); + for (int i = 1; i < parsedPath.length - 1; i++) { + sb.append(parsedPath[i]); + if (i < parsedPath.length - 2) { + sb.append("/"); + } } return sb.toString();