Commit 440630f5 authored by Matija Obreza's avatar Matija Obreza
Browse files

Service and API endpoint to update dataset file metadata

parent 2d722064
......@@ -228,4 +228,15 @@ public interface DatasetService {
@PreAuthorize("hasRole('ADMINISTRATOR')")
Dataset unpublishDataset(Dataset dataset);
/**
* Update dataset file information
*
* @param dataset the dataset
* @param metadata the updated file metadata
* @return updated dataset
* @throws NoSuchRepositoryFileException
*/
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#dataset, 'write')")
Dataset updateDatasetFile(Dataset dataset, RepositoryFile metadata) throws NoSuchRepositoryFileException;
}
......@@ -287,6 +287,21 @@ public class DatasetServiceImpl implements DatasetService {
return lazyLoad(datasetRepository.save(dataset));
}
@Override
@Transactional
public Dataset updateDatasetFile(Dataset dataset, final RepositoryFile metadata) throws NoSuchRepositoryFileException {
dataset = datasetRepository.findByUuidAndVersion(dataset.getUuid(), dataset.getVersion());
RepositoryFile datasetFile = dataset.getRepositoryFiles().stream().filter(df -> df.getUuid().equals(metadata.getUuid()) && df.getVersion().equals(metadata.getVersion()))
.findFirst().orElse(null);
if (datasetFile == null) {
throw new NotFoundElement("No such dataset file");
}
RepositoryFile updated = fileRepoService.updateMetadata(metadata);
// replace it
dataset.getRepositoryFiles().replaceAll(df -> df.getUuid().equals(datasetFile.getUuid()) ? updated : df);
return lazyLoad(dataset);
}
/**
* {@inheritDoc}
*/
......
......@@ -41,6 +41,7 @@ import org.genesys.catalog.service.filters.DatasetFilter;
import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.NoSuchRepositoryFileException;
import org.genesys.filerepository.model.RepositoryFile;
import org.junit.Test;
import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.dao.DataIntegrityViolationException;
......@@ -340,8 +341,29 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
MockMultipartFile mockMultipartFile2 = new MockMultipartFile("file", "2" + file.getName(), "multipart/form-data", fileInputStream2);
dataset = datasetService.addDatasetFile(dataset, mockMultipartFile2);
assertThat(dataset.getRepositoryFiles(), is(notNullValue()));
assertThat(dataset.getRepositoryFiles().size(), is(2));
assertThat(dataset.getRepositoryFiles().get(0).getVersion(), is(1));
}
@Test
public void testUpdateFile() throws IOException, InvalidRepositoryFileDataException, NotFoundElement, InvalidRepositoryPathException, NoSuchRepositoryFileException {
File file = new File(getClass().getResource("/mcpd20177.csv").getPath());
FileInputStream fileInputStream = new FileInputStream(file);
MockMultipartFile mockMultipartFile = new MockMultipartFile("file", file.getName(), "multipart/form-data", fileInputStream);
Dataset input = buildAndSaveDataset(DATASET_TITLE_1, DATASET_DESCRIPTION_1, partner, false);
input = datasetService.addDatasetFile(input, mockMultipartFile);
RepositoryFile df = input.getRepositoryFiles().get(0);
df.setTitle("Updated file title");
Dataset dataset = datasetService.updateDatasetFile(input, df);
assertThat(dataset.getRepositoryFiles().size(), is(1));
assertThat(dataset.getRepositoryFiles().get(0).getVersion(), is(2));
assertThat(dataset.getRepositoryFiles().get(0).getTitle(), is(df.getTitle()));
}
@Test
public void testRemoveFile() throws IOException, InvalidRepositoryFileDataException, NotFoundElement, InvalidRepositoryPathException, NoSuchRepositoryFileException {
File file = new File(getClass().getResource("/mcpd20177.csv").getPath());
......
......@@ -34,6 +34,7 @@ import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
......@@ -63,6 +64,12 @@ public class DatasetFilesController {
return datasetService.addDatasetFile(datasetService.loadDataset(datasetUuid), inputFile);
}
@PostMapping(value = "/update")
public Dataset updateDatasetFile(@PathVariable("UUID") final UUID datasetUuid, @RequestBody final RepositoryFile metadata) throws NotFoundElement,
InvalidRepositoryFileDataException, InvalidRepositoryPathException, IOException, NoSuchRepositoryFileException {
return datasetService.updateDatasetFile(datasetService.loadDataset(datasetUuid), metadata);
}
@DeleteMapping(value = "/delete/{fileUuid}")
public Dataset removeFileOfDataset(@PathVariable("UUID") final UUID datasetUuid, @PathVariable("fileUuid") final UUID fileUuid) throws NotFoundElement,
InvalidRepositoryFileDataException, InvalidRepositoryPathException, IOException, NoSuchRepositoryFileException {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment