package org.datavaultplatform.broker.services;

import java.util.Date;
import org.datavaultplatform.common.model.User;
import org.datavaultplatform.common.model.Deposit;
import org.datavaultplatform.common.model.Vault;
import org.datavaultplatform.common.model.dao.DepositDAO;

import java.util.UUID;
import java.util.List;

public class DepositsService {

    private DepositDAO depositDAO;
    
    public List<Deposit> getDeposits(String sort) {
        return depositDAO.list(sort);
    }
    
    public void addDeposit(Vault vault,
                           Deposit deposit,
                           String shortPath,
                           String origin,
                           String archiveDeviceID) {
        
        Date d = new Date();
        deposit.setCreationTime(d);
        
        deposit.setVault(vault);
        deposit.setStatus(Deposit.Status.NOT_STARTED);
        
        // Set display values for the deposit path/origin
        deposit.setShortFilePath(shortPath);
        deposit.setFileOrigin(origin);
        
        // Generate a new UUID for this Bag.
        deposit.setBagId(UUID.randomUUID().toString());
        
        // Link the deposit to the archive store
        deposit.setArchiveDevice(archiveDeviceID);
        
        depositDAO.save(deposit);
    }
    
    public void updateDeposit(Deposit deposit) {
        depositDAO.update(deposit);
    }
    
    public Deposit getDeposit(String depositID) {
        return depositDAO.findById(depositID);
    }
    
    public void setDepositDAO(DepositDAO depositDAO) { this.depositDAO = depositDAO; }

    public int count() { return depositDAO.count(); }

    public int queueCount() { return depositDAO.queueCount(); }

    public int inProgressCount() { return depositDAO.inProgressCount(); }

    public List<Deposit> inProgress() { return depositDAO.inProgress(); }

    public List<Deposit> search(String query, String sort) { return this.depositDAO.search(query, sort); }

    public Long size() { return depositDAO.size(); }
    
    // Get the specified Deposit object and validate it against the current User and Vault
    public Deposit getUserDeposit(User user, String depositID) throws Exception {
        
        Deposit deposit = getDeposit(depositID);
        
        if (deposit == null) {
            throw new Exception("Invalid Deposit ID");
        }
        
        Vault vault = deposit.getVault();

        if (!vault.equals(deposit.getVault())) {
            throw new Exception("Invalid Vault ID");
        }

        return deposit;
    }
}


@@ -2425,16 +2425,24 @@
 ault();%0A
+        
 %0A       
@@ -2450,40 +2450,141 @@
 if (
-!
 vault
-.equals(deposit.getVault()
+ == null) %7B%0A            throw new Exception(%22Vault does not exist%22);%0A        %7D%0A        %0A        if (!vault.getUser().equals(user
 )) %7B
@@ -2621,24 +2621,21 @@
 on(%22
-Invalid Vault ID
+Access denied
 %22);%0A

Add user check for deposit properties