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 2879a00161c7cddefb638f24f2cbb42cd547e539..44ac713c04ca19d1fc4f5d63798bcf2d078035d9 100644 --- a/src/main/java/it/inaf/oats/vospace/persistence/NodeDAO.java +++ b/src/main/java/it/inaf/oats/vospace/persistence/NodeDAO.java @@ -204,22 +204,6 @@ public class NodeDAO { return newNode; } - private void setLinkNodeTarget(String nodeVosPath, String target) { - - jdbcTemplate.update(conn -> { - - String sql = "UPDATE node\n" - + "SET target = ?\n" - + "WHERE node_id = id_from_vos_path(?)\n"; - - PreparedStatement ps = conn.prepareStatement(sql); - int i = 0; - ps.setString(++i, target); - ps.setString(++i, nodeVosPath); - return ps; - }); - } - private Node getNodeFromResultSet(ResultSet rs) throws SQLException { Node node = NodeUtils.getTypedNode(rs.getString("type")); @@ -361,7 +345,7 @@ public class NodeDAO { return ps; }); } - + public void copyBranch(String sourceVosPath, String destVosPath) { String destVosParentPath = NodeUtils.getParentPath(destVosPath); @@ -370,9 +354,11 @@ public class NodeDAO { String parentInsert = "INSERT INTO node (node_id, parent_path, parent_relative_path, name, type, location_id, creator_id, group_write, group_read, is_public,\n" + "job_id, tstamp_wrapper_dir, format, async_trans, sticky, accept_views, provide_views, protocols, target)\n"; + // If destination has path '' no prefix, else "destination_path." String ctePathPrefix = "SELECT CASE WHEN path::varchar = '' THEN '' ELSE (path::varchar || '.') END AS prefix\n" + "FROM node WHERE node_id = id_from_vos_path(?)"; + // Calculates also new path, even if it's usually generated by database functions String cteCopiedNodes = "SELECT nextval('node_node_id_seq') AS new_node_id,\n" + "((SELECT prefix FROM path_prefix) || currval('node_node_id_seq'))::ltree AS new_path,\n" + "path, relative_path, parent_path, parent_relative_path, ? AS name,\n" @@ -393,7 +379,7 @@ public class NodeDAO { String parentSelect = "SELECT\n" + "new_node_id, new_parent_path,\n" - + "CASE WHEN nlevel(new_parent_path) = rel_offset THEN ''::ltree ELSE subpath(new_parent_path, rel_offset) END new_parent_relative_path,\n" + + "CASE WHEN nlevel(new_parent_path) <= rel_offset THEN ''::ltree ELSE subpath(new_parent_path, rel_offset) END new_parent_relative_path,\n" + "name, type, location_id, creator_id, group_write, group_read, is_public,\n" + "job_id, tstamp_wrapper_dir, format, async_trans, sticky, accept_views, provide_views, protocols, target\n" + "FROM copied_nodes_paths\n"; @@ -406,15 +392,14 @@ public class NodeDAO { + "copied_nodes_paths AS (" + cteCopiedNodesPaths + ")\n" + parentSelect; - + jdbcTemplate.update(conn -> { PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, destVosParentPath); ps.setString(2, destName); ps.setString(3, sourceVosPath); return ps; - }); - + }); } public boolean isBranchBusy(long parentNodeId) { diff --git a/src/test/java/it/inaf/oats/vospace/persistence/NodeDAOTest.java b/src/test/java/it/inaf/oats/vospace/persistence/NodeDAOTest.java index c93f3512d7e65018199aed457c82ca9b67cb0254..f1c2107422f030e378787a4c1c045859af32210a 100644 --- a/src/test/java/it/inaf/oats/vospace/persistence/NodeDAOTest.java +++ b/src/test/java/it/inaf/oats/vospace/persistence/NodeDAOTest.java @@ -50,6 +50,7 @@ public class NodeDAOTest { ReflectionTestUtils.setField(dao, "authority", AUTHORITY); } + @Test public void testCreateNode() { DataNode dataNode = new DataNode(); @@ -87,12 +88,12 @@ public class NodeDAOTest { @Test public void testListNode() { ContainerNode root = (ContainerNode) dao.listNode("/").get(); - assertEquals(4, root.getNodes().size()); + assertEquals(5, root.getNodes().size()); assertEquals("true", NodeProperties.getNodePropertyByURI(root, NodeProperties.PUBLIC_READ_URI)); assertEquals("0", NodeProperties.getNodePropertyByURI(root, NodeProperties.LENGTH_URI)); - assertEquals("group1 group2", NodeProperties.getNodePropertyByURI(root.getNodes().get(0), NodeProperties.GROUP_READ_URI)); + assertEquals("group1", NodeProperties.getNodePropertyByURI(root.getNodes().get(0), NodeProperties.GROUP_READ_URI)); String bTime = NodeProperties.getNodePropertyByURI(root.getNodes().get(0), NodeProperties.INITIAL_CREATION_TIME_URI); assertTrue(bTime.contains("T")); @@ -284,6 +285,7 @@ public class NodeDAOTest { } + @Test public void testMoveNodeBranch() { // Let's move /test3/m1 to /test3/group1 @@ -319,6 +321,9 @@ public class NodeDAOTest { Optional<Long> optSourceChildId = dao.getNodeId("/test3/m1/m2"); assertTrue(optSourceChildId.isPresent()); + + Optional<Long> optSourceChildLinkId = dao.getNodeId("/test3/m1/link1"); + assertTrue(optSourceChildLinkId.isPresent()); Optional<Long> optDestParentId = dao.getNodeId("/test3/group1"); assertTrue(optDestParentId.isPresent()); @@ -336,8 +341,38 @@ public class NodeDAOTest { Optional<Long> recheckSourceChild = dao.getNodeId("/test3/m1/m2"); assertTrue(recheckSourceChild.isPresent()); + + Optional<Long> resultIdChildLink = dao.getNodeId("/test3/group1/copy_of_m1/link1"); + assertTrue(resultIdChildLink.isPresent()); + + Optional<Long> recheckSourceChildLink = dao.getNodeId("/test3/m1/link1"); + assertTrue(recheckSourceChildLink.isPresent()); } + + @Test + public void testMoveAndCopyIntegrated() { + + assertTrue(dao.getNodeId("/mycontainer").isPresent()); + assertTrue(dao.getNodeId("/mycontainer/destination2").isPresent()); + assertTrue(dao.getNodeId("/mycontainer/destination2/control").isPresent()); + + dao.copyBranch("/mycontainer/destination2/control", "/mycontainer/control"); + + assertTrue(dao.getNodeId("/mycontainer/container1").isPresent()); + + Optional<ShortNodeDescriptor> optSnd = + dao.getShortNodeDescriptor("/mycontainer/destination2", "user3", List.of("group1")); + + dao.moveNodeBranch(dao.getNodeId("/mycontainer/container1").get(), optSnd.get().getDestinationNodeLtreePath()); + + assertTrue(dao.getNodeId("/mycontainer/destination2/container1").isPresent()); + assertTrue(dao.getNodeId("/mycontainer/container1").isEmpty()); + dao.copyBranch("/mycontainer/destination2/container1", "/mycontainer/container1"); + + assertTrue(dao.getNodeId("/mycontainer/container1").isPresent()); + } + @Test public void testRenameNode() { diff --git a/src/test/resources/test-data.sql b/src/test/resources/test-data.sql index 44a025cd80f440faad28393851b4e25892da41f7..3ae4a612a559bf2d4cdfa798e32f386c98f6a0a1 100644 --- a/src/test/resources/test-data.sql +++ b/src/test/resources/test-data.sql @@ -34,6 +34,12 @@ INSERT INTO node (parent_path, parent_relative_path, name, job_id, type, creator INSERT INTO node (parent_path, parent_relative_path, name, async_trans, type, creator_id, is_public, location_id) VALUES ('9', '', 'masynctrans', true, 'container', 'user3', false, 3); -- /test3/masynctrans INSERT INTO node (parent_path, parent_relative_path, name, type, creator_id, is_public, location_id) VALUES ('9', '', 'asyncloc', 'container', 'user3', false, 1); -- /test3/asyncloc INSERT INTO node (parent_path, parent_relative_path, name, type, creator_id, group_write, group_read, is_public, location_id) VALUES ('9', '', 'group1', 'container', 'user3', '{"group1"}', '{"group1"}', false, 3); -- /test3/group1 +INSERT INTO node (parent_path, parent_relative_path, name, type, creator_id, group_write, group_read, is_public, location_id, target) VALUES ('9.10', '', 'link1', 'link', 'user3', '{"group1"}', '{"group1"}', false, 3, 'vos://authority/dummy/link'); -- /test3/m1/link1 + +INSERT INTO node (parent_path, parent_relative_path, name, sticky, type, creator_id, group_write, group_read, is_public, location_id) VALUES ('', NULL, 'mycontainer', true, 'container', 'user3', '{"group1"}', '{"group1"}', false, 3); -- /mycontainer +INSERT INTO node (parent_path, parent_relative_path, name, type, creator_id, group_write, group_read, is_public, location_id) VALUES ('19', '', 'container1', 'container', 'user3', '{"group1"}', '{"group1"}', false, 3); -- /mycontainer/container1 +INSERT INTO node (parent_path, parent_relative_path, name, type, creator_id, group_write, group_read, is_public, location_id) VALUES ('19', '', 'destination2', 'container', 'user3', '{"group1"}', '{"group1"}', false, 3); -- /mycontainer/destination2 +INSERT INTO node (parent_path, parent_relative_path, name, type, creator_id, group_write, group_read, is_public, location_id) VALUES ('19.21', '20', 'control', 'container', 'user3', '{"group1"}', '{"group1"}', false, 3); -- /mycontainer/destination2/control DELETE FROM job;