Newer
Older
/*
* This file is part of vospace-file-service
* Copyright (C) 2021 Istituto Nazionale di Astrofisica
* SPDX-License-Identifier: GPL-3.0-or-later
*/
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package it.inaf.ia2.transfer.persistence;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class ListOfFilesDAO {
private final JdbcTemplate jdbcTemplate;
@Autowired
public ListOfFilesDAO(DataSource fileCatalogDatasource) {
this.jdbcTemplate = new JdbcTemplate(fileCatalogDatasource);
}
public void createList(String vosListNodePath, List<String> vosPaths) {
List<NodeIdPath> idPaths = getAllNodeIdPaths(vosListNodePath, vosPaths);
if (idPaths.size() != (vosPaths.size() + 1)) {
throw new IllegalStateException("Unable to retrieve some file identifiers from paths");
}
Integer listNodeId = null;
List<Integer> nodeIds = new ArrayList<>();
for (NodeIdPath idPath : idPaths) {
if (vosListNodePath.equals(idPath.nodePath)) {
listNodeId = idPath.nodeId;
} else {
nodeIds.add(idPath.nodeId);
}
}
if (listNodeId == null) {
throw new IllegalStateException("List node id not found for path " + vosListNodePath);
}
createList(listNodeId, nodeIds);
}
private List<NodeIdPath> getAllNodeIdPaths(String vosListNodePath, List<String> vosPaths) {
List<String> allPaths = new ArrayList<>(vosPaths);
allPaths.add(vosListNodePath);
String sql = "SELECT node_id, vos_path FROM node_vos_path WHERE vos_path IN ("
+ String.join(",", Collections.nCopies(allPaths.size(), "?")) + ")";
return jdbcTemplate.query(conn -> {
PreparedStatement ps = conn.prepareStatement(sql);
int i = 0;
for (String path : allPaths) {
ps.setString(++i, path);
}
return ps;
}, (row, index) -> {
NodeIdPath nodeIdPath = new NodeIdPath();
nodeIdPath.nodeId = row.getInt("node_id");
nodeIdPath.nodePath = row.getString("vos_path");
return nodeIdPath;
});
}
private void createList(int listNodeId, List<Integer> nodes) {
String sql = "INSERT INTO list_of_files (list_node_id, node_id) VALUES "
+ String.join(",", Collections.nCopies(nodes.size(), "(?, ?)"));
jdbcTemplate.update(conn -> {
PreparedStatement ps = conn.prepareStatement(sql);
int i = 0;
for (int nodeId : nodes) {
ps.setInt(++i, listNodeId);
ps.setInt(++i, nodeId);
}
return ps;
});
}
static class NodeIdPath {
int nodeId;
String nodePath;
}
}