Newer
Older
import it.inaf.oats.vospace.datamodel.NodeProperties;
import it.inaf.oats.vospace.datamodel.NodeUtils;
import it.inaf.oats.vospace.datamodel.NodeUtils;
import it.inaf.oats.vospace.exception.InternalFaultException;
import it.inaf.oats.vospace.exception.NodeNotFoundException;
import it.inaf.oats.vospace.exception.PermissionDeniedException;
Sonia Zorba
committed
import java.sql.Array;
import net.ivoa.xml.vospace.v2.Node;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
Sara Bertocco
committed
import java.sql.Types;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Function;
import java.util.stream.Collectors;
import net.ivoa.xml.vospace.v2.ContainerNode;
import net.ivoa.xml.vospace.v2.DataNode;
import net.ivoa.xml.vospace.v2.Property;
import net.ivoa.xml.vospace.v2.StructuredDataNode;
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;
import org.springframework.stereotype.Repository;
@Repository
public class NodeDAO {
private static final Logger LOG = LoggerFactory.getLogger(DeleteNodeController.class);
@Value("${vospace-authority}")
private String authority;
private final JdbcTemplate jdbcTemplate;
@Autowired
public NodeDAO(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}
Sara Bertocco
committed
String nodeURI = myNode.getUri();
String path = nodeURI.replaceAll("vos://[^/]+", "");
Sara Bertocco
committed
throw new IllegalStateException("Unable to find parent node during node creation");
throw new IllegalStateException("Multiple ltree parent paths found for " + parentPath);
}
Sara Bertocco
committed
sb.append("INSERT INTO node");
sb.append(" (name, busy_state, owner_id, creator_id, group_read, group_write,");
sb.append(" is_public, parent_path, parent_relative_path, type, accept_views, provide_views)");
sb.append(" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
Sara Bertocco
committed
jdbcTemplate.update(conn -> {
PreparedStatement ps = conn.prepareStatement(sb.toString());
ps.setString(++i, NodeUtils.getNodeName(myNode));
ps.setBoolean(++i, NodeUtils.getIsBusy(myNode));
ps.setString(++i, NodeProperties.getStandardNodePropertyByName(myNode, "creator"));
ps.setString(++i, NodeProperties.getStandardNodePropertyByName(myNode, "creator"));
ps.setArray(++i, fromPropertyToArray(ps, NodeProperties.getStandardNodePropertyByName(myNode, "groupread")));
ps.setArray(++i, fromPropertyToArray(ps, NodeProperties.getStandardNodePropertyByName(myNode, "groupwrite")));
ps.setBoolean(++i, Boolean.valueOf(NodeProperties.getStandardNodePropertyByName(myNode, "publicread")));
ps.setObject(++i, paths.get(0).path, Types.OTHER);
ps.setObject(++i, paths.get(0).relativePath, Types.OTHER);
ps.setObject(++i, NodeUtils.getDbNodeType(myNode), Types.OTHER);
ps.setObject(++i, fromViewsToArray(ps, myNode, d -> d.getAccepts()), Types.OTHER);
ps.setObject(++i, fromViewsToArray(ps, myNode, d -> d.getProvides()), Types.OTHER);
Sara Bertocco
committed
return ps;
});
public Optional<Node> listNode(String path) {
String sql = "SELECT os.vos_path, n.node_id, type, async_trans, sticky, busy_state, creator_id, group_read, group_write,\n"
+ "is_public, content_length, created_on, last_modified, accept_views, provide_views\n"
+ "FROM node n\n"
+ "JOIN node_vos_path os ON n.node_id = os.node_id\n"
+ "WHERE n.path ~ (" + getFirstLevelChildrenSelector(path) + ")::lquery\n"
+ "OR os.vos_path = ? ORDER BY vos_path";
List<Node> parentAndChildren = jdbcTemplate.query(conn -> {
ps.setString(1, path);
ps.setString(2, path);
if (parentAndChildren.isEmpty()) {
return Optional.empty();
}
// 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));
}
}
String vosPath = NodeUtils.getVosPath(newNode);
// List<NodePaths> paths = getNodePathsFromDB(nodeURI);
// This method cannot be used to modify the accepts or provides list of Views for the Node.
// Only DataNodes has Views (see VOSpace Data Model
// Check if trying to change views getting Views (if present) of the modified node
if (newNode instanceof DataNode) {
DataNode dataNode = (DataNode)newNode;
List<View> requestedAcceptedViews = dataNode.getAccepts();
List<View> savedAcceptedViews = getAcceptedViewsFromDB(vosPath);
// Get the Views of the saved node
List<View> requestedProvidedViews = dataNode.getProvides();
List<View> savedProvidedViews = getProvidedViewsFromDB(vosPath);
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
if (!requestedAcceptedViews.isEmpty()) {
//se sono non nulle, devo fare i controlli, altrimenti di sicuro l'utente non sta chiedendo di cambiarle
// Check if requestedAcceptedViews are different from the already stored
if (!checkIfViewsAreEquals(requestedAcceptedViews,savedAcceptedViews)) {
LOG.debug("setNode trying to modify accepted Views.");
throw new PermissionDeniedException("Trying to modify accepted views");
}
}
if (!requestedProvidedViews.isEmpty()) {
//se sono non nulle, devo fare i controlli, altrimenti di sicuro l'utente non sta chiedendo di cambiarle
// Check if requestedAcceptedViews are different from the already stored
if (!checkIfViewsAreEquals(requestedProvidedViews,savedProvidedViews)) {
LOG.debug("setNode trying to modify provided Views.");
throw new PermissionDeniedException("Trying to modify provided views");
}
}
}
StringBuilder sb = new StringBuilder();
sb.append("UPDATE node");
sb.append(" SET owner_id = ?, group_read = ?, group_write = ?, is_public = ? " );
sb.append(" FROM node_vos_path p WHERE p.vos_path = ? AND p.node_id = node.node_id ");
jdbcTemplate.update(conn -> {
PreparedStatement ps = conn.prepareStatement(sb.toString());
int i = 0;
ps.setString(++i, NodeProperties.getNodePropertyByURI(newNode, NodeProperties.CREATOR_URI));
ps.setArray(++i, fromPropertyToArray(ps, NodeProperties.getNodePropertyByURI(newNode, NodeProperties.GROUP_READ_URI)));
ps.setArray(++i, fromPropertyToArray(ps, NodeProperties.getNodePropertyByURI(newNode, NodeProperties.GROUP_WRITE_URI)));
ps.setBoolean(++i, Boolean.valueOf(NodeProperties.getNodePropertyByURI(newNode, NodeProperties.PUBLIC_READ_URI)));
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
}
private Optional<Node> getNode(String path) {
String sql = "SELECT node_id, type, async_trans, sticky, busy_state, creator_id, group_read, group_write,\n"
+ "is_public, content_length, created_on, last_modified, accept_views, provide_views \n"
+ "FROM node WHERE path = ?";
List<Node> nodes = jdbcTemplate.query(conn -> {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, path);
return ps;
}, (row, index) -> {
return getNodeFromResultSet(row);
});
if (nodes.isEmpty()) {
return Optional.empty();
}
// Query returns the node (can be container or data)
Node node = nodes.get(0);
return Optional.of(node);
}
private String getFirstLevelChildrenSelector(String path) {
String select = "(SELECT path FROM node WHERE node_id = (SELECT node_id FROM node_vos_path WHERE vos_path = ?))::varchar || '";
if (!"/".equals(path)) {
select += ".";
}
select += "*{1}'";
return select;
Nicola Fulvio Calabria
committed
private String getAllLevelsChildrenSelector(String path) {
String select = "(SELECT path FROM node WHERE node_id = (SELECT node_id FROM node_vos_path WHERE vos_path = ?))::varchar || '";
if (!"/".equals(path)) {
select += ".";
}
select += "*{1,}'";
return select;
}
private Node getNodeFromResultSet(ResultSet rs) throws SQLException {
Sonia Zorba
committed
if (node instanceof DataNode) {
DataNode dataNode = (DataNode) node;
dataNode.setBusy(rs.getBoolean("busy_state"));
dataNode.setAccepts(getViews(rs.getArray("accept_views")));
dataNode.setProvides(getViews(rs.getArray("provide_views")));
Sonia Zorba
committed
}
node.setUri(getUri(rs.getString("vos_path")));
List<Property> properties = new ArrayList<>();
Sonia Zorba
committed
addProperty(NodeProperties.LENGTH_URI, rs.getString("content_length"),
properties);
Sonia Zorba
committed
addProperty(NodeProperties.INITIAL_CREATION_TIME_URI, rs.getString("created_on"),
properties);
Sonia Zorba
committed
addProperty(NodeProperties.CREATOR_URI, rs.getString("creator_id"),
addProperty(NodeProperties.MODIFICATION_TIME_URI, rs.getString("last_modified"),
properties);
Sonia Zorba
committed
addProperty(NodeProperties.GROUP_READ_URI, getGroupsString(rs, "group_read"),
properties);
Sonia Zorba
committed
addProperty(NodeProperties.GROUP_WRITE_URI, getGroupsString(rs, "group_write"),
properties);
Sonia Zorba
committed
addProperty(NodeProperties.PUBLIC_READ_URI, String.valueOf(rs.getBoolean("is_public")),
properties);
Sonia Zorba
committed
addProperty("urn:async_trans", String.valueOf(rs.getBoolean("async_trans")),
properties);
addProperty("urn:sticky", String.valueOf(rs.getBoolean("sticky")), properties);
Sonia Zorba
committed
node.setProperties(properties);
Nicola Fulvio Calabria
committed
public void deleteNode(String path) {
int nodesWithPath = countNodesWithPath(path);
Nicola Fulvio Calabria
committed
if (nodesWithPath == 0) {
throw new IllegalStateException("Node at path "
Nicola Fulvio Calabria
committed
+ path + " not found");
}
Nicola Fulvio Calabria
committed
if (nodesWithPath > 1) {
throw new IllegalStateException("Multiple nodes at path " + path);
}
Nicola Fulvio Calabria
committed
String insertSql = "INSERT INTO deleted_node "
+ "(node_id, parent_path, parent_relative_path, "
+ "name, os_name, tstamp_wrapper_dir, type, location_id, format, "
+ "async_trans, busy_state, owner_id, creator_id, group_read, "
+ "group_write, is_public, delta, content_type, content_encoding, "
+ "content_length, content_md5, created_on, last_modified, "
+ "accept_views, provide_views, protocols, sticky)\n";
Nicola Fulvio Calabria
committed
String deleteSql = "DELETE \n"
+ "FROM node n\n"
+ "USING node_vos_path os\n"
+ "WHERE n.node_id = os.node_id AND\n"
+ "(n.path ~ (" + getAllLevelsChildrenSelector(path) + ")::lquery\n"
+ "OR os.vos_path = ?) RETURNING\n"
+ "n.node_id, parent_path, parent_relative_path, "
+ "name, os_name, tstamp_wrapper_dir, type, location_id, format, "
+ "async_trans, busy_state, owner_id, creator_id, group_read, "
+ "group_write, is_public, delta, content_type, content_encoding, "
+ "content_length, content_md5, created_on, last_modified, "
+ "accept_views, provide_views, protocols, sticky\n";
String withSql = "WITH del AS (" + deleteSql + ")";
String sql = withSql + insertSql + "SELECT * FROM del\n";
jdbcTemplate.update(sql, path, path);
Nicola Fulvio Calabria
committed
}
// utility method for deleteNode
Nicola Fulvio Calabria
committed
public int countNodesWithPath(String path) {
String sql = "SELECT COUNT(*) from "
+ "node_vos_path p "
+ "where p.vos_path = ?";
Nicola Fulvio Calabria
committed
Object[] args = {path};
int[] types = {Types.VARCHAR};
Nicola Fulvio Calabria
committed
return jdbcTemplate.queryForObject(sql, args, types, Integer.class);
}
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
public String getNodeOsName(String vosPath) {
String sql = "SELECT \n"
+ "(CASE WHEN os_name IS NOT NULL THEN os_name ELSE name END) AS os_name\n"
+ "FROM node n\n"
+ "JOIN node_vos_path p ON n.node_id = p.node_id\n"
+ "WHERE p.vos_path = ?";
Object[] args = {vosPath};
int[] types = {Types.VARCHAR};
return jdbcTemplate.queryForObject(sql, args, types, String.class);
}
public void setNodeLocation(String vosPath, int locationId, String nodeOsName) {
String sql = "UPDATE node SET location_id = ?, os_name = ? WHERE node_id = "
+ "(SELECT node_id FROM node_vos_path WHERE vos_path = ?)";
int updated = jdbcTemplate.update(sql, ps -> {
ps.setInt(1, locationId);
ps.setString(2, nodeOsName);
ps.setString(3, vosPath);
});
if (updated != 1) {
throw new InternalFaultException("Unable to set node location for path " + vosPath);
}
}
Sonia Zorba
committed
private String getGroupsString(ResultSet rs, String column) throws SQLException {
Array array = rs.getArray(column);
if (array == null) {
return null;
}
return String.join(" ", (String[]) array.getArray());
}
// If value is null does nothing
private void addProperty(String uri, String value, List<Property> list) {
if (value != null) {
Property prop = new Property();
prop.setUri(uri);
prop.setValue(value);
list.add(prop);
}
}
private String getUri(String path) {
return "vos://" + authority + path;
}
Sara Bertocco
committed
private NodePaths getPathsFromResultSet(ResultSet rs) throws SQLException {
NodePaths paths = new NodePaths(rs.getString("path"), rs.getString("relative_path"));
Sara Bertocco
committed
}
private Array fromPropertyToArray(PreparedStatement ps, String myProperty) throws SQLException {
if (myProperty == null || myProperty.isBlank()) {
Sara Bertocco
committed
return null;
} else {
return ps.getConnection().createArrayOf("varchar", myProperty.split(" "));
Sara Bertocco
committed
}
private Array fromViewsToArray(PreparedStatement ps, Node node, Function<DataNode, List<View>> viewsExtractor) throws SQLException {
if (node instanceof DataNode) {
DataNode dataNode = (DataNode) node;
List<View> views = viewsExtractor.apply(dataNode);
if (views != null && !views.isEmpty()) {
Object[] array = views.stream().map(v -> v.getUri()).toArray();
return ps.getConnection().createArrayOf("varchar", array);
}
}
return null;
}
if (array == null) {
return null;
}
try {
return Arrays.stream((String[]) array.getArray())
.map(uri -> {
View view = new View();
view.setUri(uri);
return view;
})
.collect(Collectors.toList());
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
private boolean checkIfViewsAreEquals(List<View> viewsA, List<View> viewsB) {
boolean viewsAreEqual = true;
if ((viewsA == null || viewsA.isEmpty()) &&
(viewsB == null || viewsB.isEmpty()))
return true;
if ((viewsA == null || viewsA.isEmpty()) ||
(viewsB == null || viewsB.isEmpty()))
return false;
if( viewsA.size() != viewsB.size())
return false;
for(int i = 0; i < viewsA.size(); i++) {
String viewUriA = viewsA.get(i).getUri();
boolean found = false;
for(int j = 0; j < viewsB.size(); j++) {
String viewUriB = viewsB.get(i).getUri();
if (viewUriA.compareTo(viewUriB) == 0) {
found = true;
}
}
if(found == false) {
viewsAreEqual = false;
break;
}
}
return viewsAreEqual;
}
private List<View> getAcceptedViewsFromDB(String nodePath) {
return getViewsFromDB(nodePath, "accept_views");
}
private List<View> getProvidedViewsFromDB(String nodePath) {
return getViewsFromDB(nodePath, "provide_views");
}
private List<View> getViewsFromDB(String nodePath, String viewType) {
String sql = "SELECT accept_views FROM node WHERE node_id = "
+ "(SELECT node_id FROM node_vos_path WHERE vos_path = ?)";
Array results = jdbcTemplate.query(sql, ps -> {
ps.setString(1, nodePath);
}, (rs) -> {
if(rs.next())
return rs.getArray("accept_views");
else
return null;
});
return getViews(results);
}
private List<NodePaths> getNodePathsFromDB(String nodeURI) {
//String nodeURI = myNode.getUri();
String path = nodeURI.replaceAll("vos://[^/]+", "");
String parentPath = NodeUtils.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);
});
return paths;
Sara Bertocco
committed
private class NodePaths {
private final String path;
private final String relativePath;
public NodePaths(String myPath, String myRelativePath) {
Sara Bertocco
committed
this.path = myPath;
Sara Bertocco
committed
public String toString() {
Sara Bertocco
committed
}
public String getPath() {
return this.path;
}
public String getRelativePath() {
return this.relativePath;
}
Sara Bertocco
committed
}