Skip to content
NodeDAO.java 3.43 KiB
Newer Older
Sara Bertocco's avatar
Sara Bertocco committed
package it.inaf.oats.vospace.persistence;

import net.ivoa.xml.vospace.v2.Node;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import net.ivoa.xml.vospace.v2.ContainerNode;
import net.ivoa.xml.vospace.v2.DataNode;
Sara Bertocco's avatar
Sara Bertocco committed
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
Sara Bertocco's avatar
Sara Bertocco committed
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/**
 *
 * @author bertocco
 */
@Repository
public class NodeDAO {

    @Value("${vospace-authority}")
    private String authority;
Sara Bertocco's avatar
Sara Bertocco committed

    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public NodeDAO(DataSource dataSource) {
        jdbcTemplate = new JdbcTemplate(dataSource);
    }

    public Node createNode(Node myNode) {
Sara Bertocco's avatar
Sara Bertocco committed
        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();
Sara Bertocco's avatar
Sara Bertocco committed
        return myNode;
    }

    public Node listNode(String path) {
Sara Bertocco's avatar
Sara Bertocco committed

        String sql = "SELECT os.os_path, n.node_id, type, async_trans, owner_id, group_read, group_write, is_public, content_length, created_on, last_modified from node n\n"
                + "JOIN node_os_path os ON n.node_id = os.node_id\n"
                + "WHERE n.path ~ (" + getFirstLevelChildrenSelector(path) + ")::lquery\n"
                + "OR os.os_path = ? ORDER BY os_path";
Sara Bertocco's avatar
Sara Bertocco committed

        List<Node> parentAndChildren = jdbcTemplate.query(conn -> {
Sara Bertocco's avatar
Sara Bertocco committed
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, path);
            ps.setString(2, path);
Sara Bertocco's avatar
Sara Bertocco committed
            return ps;
        }, (row, index) -> {
            return getNodeFromResultSet(row);
Sara Bertocco's avatar
Sara Bertocco committed
        });

        // Query returns parent as first node
        Node node = parentAndChildren.get(0);
        
        // Fill children
        if (node instanceof ContainerNode && parentAndChildren.size() > 1) {
            ContainerNode parent = (ContainerNode) node;
            for (int i = 1; i < parentAndChildren.size(); i++) {
                parent.getNodes().add(parentAndChildren.get(i));
            }
        }

        return node;
    }

    private String getFirstLevelChildrenSelector(String path) {
        String select = "(SELECT path FROM node WHERE node_id = (SELECT node_id FROM node_os_path WHERE os_path = ?))::varchar || '";

        if (!"/".equals(path)) {
            select += ".";
        }
        select += "*{1}'";
        return select;
Sara Bertocco's avatar
Sara Bertocco committed
    }

    private Node getNodeFromResultSet(ResultSet rs) throws SQLException {

        Node node = getTypedNode(rs.getString("type"));
        node.setUri(getUri(rs.getString("os_path")));

        return node;
    }

    private Node getTypedNode(String type) {
        Node node;
        switch (type) {
            case "container":
                node = new ContainerNode();
                break;
            case "data":
                node = new DataNode();
                break;
            default:
                throw new UnsupportedOperationException("Node type " + type + " not supported yet");
        }
        return node;
    }

    private String getUri(String path) {
        return "vos://" + authority + path;
    }
Sara Bertocco's avatar
Sara Bertocco committed
}