Commit 3f87be5b authored by Nicola Fulvio Calabria's avatar Nicola Fulvio Calabria
Browse files

added collections dao

parent 4540f2ba
Loading
Loading
Loading
Loading
+66 −2
Original line number Diff line number Diff line
@@ -6,6 +6,11 @@
package it.inaf.oats.vospace.persistence;

import it.inaf.oats.vospace.persistence.model.NodeCollection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Optional;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
@@ -25,8 +30,67 @@ public class CollectionsDAO {
        jdbcTemplate = new JdbcTemplate(dataSource);
    }

    void insertNewCollection(NodeCollection collection) {
    void createNewCollection(String title, String ownerId) {

        String sql = "INSERT INTO collections (title, owner_id) VALUES (?,?)";

        jdbcTemplate.update(conn -> {
            PreparedStatement ps = conn.prepareStatement(sql);
            int i = 0;
            ps.setString(++i, title);
            ps.setString(++i, ownerId);
            return ps;
        });
    }

    Optional<NodeCollection> getNodeCollectionById(Long id) {
        String sql = "SELECT collection_id, title, owner_id FROM collections\n"
                + "WHERE collection_id = ?";

        Optional<NodeCollection> nc = jdbcTemplate.query(
                conn -> {
                    PreparedStatement ps = conn.prepareStatement(sql);
                    ps.setLong(1, id);
                    return ps;
                },
                rs -> {
                    if (!rs.next()) {
                        return Optional.empty();
                    } else {
                        return Optional.of(getNodeCollectionFromResultset(rs));
                    }

                });

        return nc;

    }
    
    List<NodeCollection> getUserNodeCollections(String userId) {
        String sql = "SELECT collection_id, title, owner_id FROM collections\n"
                + "WHERE owner_id = ?";

        List<NodeCollection> nc = jdbcTemplate.query(
                conn -> {
                    PreparedStatement ps = conn.prepareStatement(sql);
                    ps.setString(1, userId);
                    return ps;
                },
                (row, index) -> {
                    return this.getNodeCollectionFromResultset(row);
                });

        return nc;
    }

    private NodeCollection getNodeCollectionFromResultset(ResultSet rs)
            throws SQLException {
        NodeCollection nc = new NodeCollection(
                rs.getLong("collection_id"),
                rs.getString("title"),
                rs.getString("owner_id")
        );

        return nc;
    }
}
+0 −3
Original line number Diff line number Diff line
@@ -5,7 +5,6 @@
 */
package it.inaf.oats.vospace.persistence;

import it.inaf.oats.vospace.DeleteNodeController;
import it.inaf.oats.vospace.URIUtils;
import it.inaf.oats.vospace.datamodel.NodeProperties;
import it.inaf.oats.vospace.datamodel.NodeUtils;
@@ -32,8 +31,6 @@ import net.ivoa.xml.vospace.v2.DataNode;
import net.ivoa.xml.vospace.v2.LinkNode;
import net.ivoa.xml.vospace.v2.Property;
import net.ivoa.xml.vospace.v2.View;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
+8 −8
Original line number Diff line number Diff line
@@ -12,12 +12,12 @@ package it.inaf.oats.vospace.persistence.model;
public class NodeCollection {
    
    private Long id;
    private String name;
    private String title;
    private String ownerId;
        
    public NodeCollection(Long id, String name, String ownerId) {
    public NodeCollection(Long id, String title, String ownerId) {
        this.id = id;
        this.name = name;
        this.title = title;
        this.ownerId = ownerId;
    }    

@@ -29,12 +29,12 @@ public class NodeCollection {
        this.id = id;
    }

    public String getName() {
        return name;
    public String getTitle() {
        return title;
    }

    public void setName(String name) {
        this.name = name;
    public void setTitle(String title) {
        this.title = title;
    }

    public String getOwnerId() {