Commit 99de5221 authored by Sara Bertocco's avatar Sara Bertocco
Browse files

Finalized createNode in NodeDAO, fixed isPublic boolean and partially type not null

parent 1e8657bb
Loading
Loading
Loading
Loading
+151 −9
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ import net.ivoa.xml.vospace.v2.Node;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@@ -34,19 +35,66 @@ public class NodeDAO {
        jdbcTemplate = new JdbcTemplate(dataSource);
    }

    public Node createNode(Node myNode) {
   public void createNode(Node myNode) {
        
        // check if parent path exist, else throw HTTP 404 , secondo specifica vospace
        
        /*
            Retrieve the path (ltree) and the relative_path (ltree) of the parent node:
 
            select path, relative_path from node n
            join node_vos_path p on n.node_id = p.node_id
            where p.vos_path = '/path/to/parent’
 
            Then perform the insert using the path and relative_path to fill the parent_path and parent_relative_path columns of the new node.

        */
        
        String nodeURI = myNode.getUri();
        String path = nodeURI.replaceAll("vos://[^/]+", "");
        String parentPath = getParentPath(path);
        
        String sql = "SELECT path, relative_path from "
                + "node n join node_vos_path p on n.node_id = p.node_id "
                + "where p.vos_path = ?";
        
        List<NodePaths> paths = jdbcTemplate.query(conn -> {
              PreparedStatement ps = conn.prepareStatement(sql);
              ps.setString(1, parentPath);
              return ps;
        }, (row, index) -> {
            return getPathsFromResultSet(row);
        });
                   

        if(paths.isEmpty()) {
            throw new IllegalStateException("Unable to find parent node during node creation");
        }     
            
        StringBuilder sb = new StringBuilder();
        sb.append("INSERT INTO ");
        sb.append("NodeProperty");
        sb.append(" (nodeID,propertyURI,propertyValue) SELECT ?, ?, ?");
        sb.append(" WHERE NOT EXISTS (SELECT * FROM NodeProperty");
        sb.append(" WHERE nodeID=? and propertyURI=?)");
        String sqlQuery = sb.toString();
        sb.append("INSERT INTO node");
        sb.append(" (name, busy_state, owner_id, creator_id, group_read, group_write,");
        sb.append(" is_public,  parent_path, type)");
        sb.append(" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ? )");   
        
        jdbcTemplate.update(conn -> {
            PreparedStatement ps = conn.prepareStatement(sb.toString());
            ps.setString(1, getNodeName(myNode));
            ps.setBoolean(2, getIsBusy(myNode));
            ps.setString(3, getProperty(myNode, getPropertyURI("creator")));
            ps.setString(4, getProperty(myNode, getPropertyURI("creator")));
            ps.setArray(5, fromPropertyToArray(getProperty(myNode, getPropertyURI("groupread"))));
            ps.setArray(6, fromPropertyToArray(getProperty(myNode, getPropertyURI("groupwrite"))));
            ps.setBoolean(7, Boolean.valueOf(getProperty(myNode, getPropertyURI("publicread"))));
            //ps.setBoolean(7, true);
            ps.setObject(8, paths.get(0).parentPath, Types.OTHER);
            ps.setString(9, getProperty(myNode, getPropertyURI("type")));
            return ps;
        });
        
        return myNode;
    }
    

    public Optional<Node> 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"
@@ -168,4 +216,98 @@ public class NodeDAO {
    private String getUri(String path) {
        return "vos://" + authority + path;
    }
        

    private NodePaths getPathsFromResultSet(ResultSet rs) throws SQLException {
       
        NodePaths paths = new NodePaths(rs.getString("path"), rs.getString("relative_path"));
        
        return  paths;
      
    }
    
    private String getNodeName(String path) {
        String[] parsedPath = path.split("/");

        return parsedPath[parsedPath.length -1]; 
        
    }
    
 
    
    private String getNodeName(Node myNode) {
        
        String uri = myNode.getUri();
        return getNodeName(uri);
        
    }  

    
    private boolean getIsBusy(Node myNode) {
        
        if (myNode instanceof DataNode) {
            
            DataNode dataNode = (DataNode)myNode;
            return dataNode.isBusy();
        }
        
        return false;
        
    }
    
    
    private String getParentPath(String path) {
        String[] parsedPath = path.split("/");

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < parsedPath.length - 1; i++) {
            sb.append("/").append(parsedPath[i]);
        }

        return sb.toString();
    }
    
    
    private String getProperty(Node node, String uri) {
        for (Property property : node.getProperties()) {
            if (uri.equals(property.getUri())) {
                return property.getValue();
            }
        }
        return null;
    }
    
    private Array fromPropertyToArray(String myProperty) {
        
        if (myProperty == null || myProperty.isBlank()) 
        
            return null;
        
        else {
            
            //myProperty.split(" ")
            return null;        
        }
        
    }
    
    
    private class NodePaths {

        private String path;
        private String parentPath;
        
        public NodePaths(String myPath, String myParentPath) {
            
            this.path = myPath;
            this.parentPath = myParentPath;
        
        }       
        
        public String toString() {
            return  parentPath + " " + path;
        }
    }

}
+11 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ package it.inaf.oats.vospace.persistence;

import javax.sql.DataSource;
import net.ivoa.xml.vospace.v2.ContainerNode;
import net.ivoa.xml.vospace.v2.DataNode;
import net.ivoa.xml.vospace.v2.Node;
import net.ivoa.xml.vospace.v2.Property;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -27,6 +28,16 @@ public class NodeDAOTest {
        dao = new NodeDAO(dataSource);
    }
       
    @Test
    public void testCreateNode() {
        DataNode dataNode = new DataNode();
        
        dataNode.setUri("vos://example.com!vospace/mydata1");
        
        dao.createNode(dataNode);
        
    }

    @Test
    public void testListNode() {
        ContainerNode root = (ContainerNode) dao.listNode("/").get();