Skip to content
MoveService.java 5.32 KiB
Newer Older
Sonia Zorba's avatar
Sonia Zorba committed
/*
 * This file is part of vospace-rest
 * Copyright (C) 2021 Istituto Nazionale di Astrofisica
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
package it.inaf.oats.vospace;

import it.inaf.ia2.aa.data.User;
import it.inaf.oats.vospace.datamodel.NodeUtils;
import it.inaf.oats.vospace.exception.InternalFaultException;
import it.inaf.oats.vospace.exception.NodeBusyException;
import it.inaf.oats.vospace.exception.NodeNotFoundException;
import it.inaf.oats.vospace.exception.PermissionDeniedException;
import it.inaf.oats.vospace.persistence.NodeDAO;
import java.util.List;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest;
import net.ivoa.xml.vospace.v2.ContainerNode;
import net.ivoa.xml.vospace.v2.Transfer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
Sonia Zorba's avatar
Sonia Zorba committed
import org.springframework.dao.CannotSerializeTransactionException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
Sonia Zorba's avatar
Sonia Zorba committed
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
@EnableTransactionManagement
public class MoveService {

    @Autowired
    private NodeDAO nodeDao;

    @Value("${vospace-authority}")
    private String authority;

    private CreateNodeController createNodeController;
    @Autowired
    private HttpServletRequest servletRequest;
    
Sonia Zorba's avatar
Sonia Zorba committed
    @Transactional(rollbackFor = { Exception.class }, isolation = Isolation.REPEATABLE_READ)
    public void processMoveJob(Transfer transfer) {
        // Get Source Vos Path
        String sourcePath = transfer.getTarget().substring("vos://".length() + authority.length());
        // Get Destination Vos Path (it's in transfer direction)
        String destinationPath = transfer.getDirection().substring("vos://".length() + authority.length());

        // Extract User permissions from servlet request
        User user = (User) servletRequest.getUserPrincipal();

        // Generic common validation for move process job paths
        this.validatePath(sourcePath);
        this.validatePath(destinationPath);

        if (sourcePath.equals(destinationPath)) {
Sonia Zorba's avatar
Sonia Zorba committed
        try {
            // Get source node
            Optional<Long> sourceIdOpt = nodeDao.getNodeId(sourcePath);
            long sourceId = sourceIdOpt.orElseThrow(() -> new NodeNotFoundException(sourcePath));
Sonia Zorba's avatar
Sonia Zorba committed
            if (nodeDao.isBranchBusy(sourceId)) {
                throw new NodeBusyException(sourcePath);
            }
Sonia Zorba's avatar
Sonia Zorba committed
            if (!nodeDao.isBranchWritable(sourceId, user.getName(), user.getGroups())) {
                throw new PermissionDeniedException(sourcePath);
            }
Sonia Zorba's avatar
Sonia Zorba committed
            Optional<String> destLtreePath = nodeDao.getLtreePath(destinationPath);
Sonia Zorba's avatar
Sonia Zorba committed
            String parentDestLtreePath = null;
            if (destLtreePath.isPresent()) {
                // When the destination is an existing ContainerNode, the source SHALL be placed under it (i.e., within the container)
                // TODO: check that this is a ContainerNode using a simple select, otherwise throw an error
                // maybe we could select the type together with the ltree returned by the previous query
                parentDestLtreePath = destLtreePath.get();
Sonia Zorba's avatar
Sonia Zorba committed
                // Compare source and destination paths parents and see if it's just a rename        
                if (NodeUtils.getParentPath(sourcePath)
                        .equals(NodeUtils.getParentPath(destinationPath))) {

                    nodeDao.renameNode(sourceId, NodeUtils.getLastPathElement(destinationPath));
                } else {
                    // TODO (if we want this): modify the return type of createDestination to obtain an ltree path
                    //parentDestLtree = this.createDestination(NodeUtils.getParentPath(destinationPath), user);
                }
Sonia Zorba's avatar
Sonia Zorba committed
            if (parentDestLtreePath != null) {
                nodeDao.moveNodeBranch(sourceId, parentDestLtreePath);
            }
        } catch (CannotSerializeTransactionException ex) {
            // Concurrent transactions attempted to modify this set of nodes            
            throw new NodeBusyException(sourcePath);
    }

    private void validatePath(String path) {        
        if (path.equals("/")) {            
            throw new IllegalArgumentException("Cannot move root node or to root node");
        }
    }

    private boolean checkNodeExistence(String path) {
        Optional<Long> optNodeId = nodeDao.getNodeId(path);
        return optNodeId.isPresent();
    }
    // Returns node id of created destination
    private Long createDestination(String path, User user) {
        List<String> components = NodeUtils.subPathComponents(path);

        for (int i = 0; i < components.size(); i++) {
            if (!this.checkNodeExistence(components.get(i))) {
                ContainerNode node = new ContainerNode();
                node.setUri("vos://" + this.authority + components.get(i));
                createNodeController.createNode(node, user);                
            }
        }
        return nodeDao.getNodeId(path).orElseThrow(()-> 
                new InternalFaultException("Unable to create destination at path: "+path));
            
    }