Commit 0c66b196 authored by Matija Obreza's avatar Matija Obreza

Merge branch '345-catalog-dataset-metadata-for-download' into 'master'

Resolve "Catalog dataset metadata for download"

Closes #345

See merge request genesys-pgr/genesys-server!249
parents 65ad6bb3 642febc2
......@@ -17,6 +17,7 @@ package org.genesys.catalog.service.impl;
import static org.genesys.catalog.model.dataset.QDataset.dataset;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
......@@ -52,6 +53,7 @@ import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.security.SecurityUtils;
import org.genesys2.server.service.DownloadService;
import org.genesys2.util.JPAUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
......@@ -97,8 +99,13 @@ public class DatasetServiceImpl implements DatasetService {
@Autowired
private SecurityUtils securityUtils;
/** The accessions repository. */
@Autowired
private AccessionRepository accessionRepository;
/** The download service. */
@Autowired
private DownloadService downloadService;
/** The file repository path. */
@Value("${file.repository.datasets.folder}")
......@@ -459,7 +466,7 @@ public class DatasetServiceImpl implements DatasetService {
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR')")
public Dataset approveDataset(final Dataset dataset) {
final Dataset loaded = datasetRepository.findByUuidAndVersion(dataset.getUuid(), dataset.getVersion());
Dataset loaded = datasetRepository.findByUuidAndVersion(dataset.getUuid(), dataset.getVersion());
if (loaded == null) {
throw new NotFoundElement("No dataset with specified uuid and version");
}
......@@ -472,6 +479,31 @@ public class DatasetServiceImpl implements DatasetService {
loaded.setState(PublishState.PUBLISHED);
// generate file with metadata and add it to the dataset
try (final ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
downloadService.writeXlsxDatasetMetadata(loaded, bos);
final String fileName = "Metadata.xlsx";
final String contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
try {
final RepositoryFile datasetFile = repositoryService.getFile(Paths.get(datasetRepositoryPath, loaded.getUuid().toString()), fileName);
repositoryService.updateBytes(datasetFile, contentType, bos.toByteArray());
} catch (final NoSuchRepositoryFileException | InvalidRepositoryPathException e) {
try {
RepositoryFile metadata = new RepositoryFile();
metadata.setTitle("Dataset metadata");
metadata.setDescription("Auto-generated by Genesys");
final RepositoryFile addedFile = repositoryService.addFile(Paths.get(datasetRepositoryPath, loaded.getUuid().toString()).toAbsolutePath(),
fileName, contentType, bos.toByteArray(), metadata);
loaded.getRepositoryFiles().add(addedFile);
loaded = lazyLoad(datasetRepository.save(loaded));
} catch (final InvalidRepositoryPathException | InvalidRepositoryFileDataException e1) {
LOG.warn("Error while adding metadata file.", e1);
}
}
} catch (final IOException e) {
LOG.warn("Error while generating metadata file.", e);
}
{
// Relax permissions on dataset files: allow USERS and ANONYMOUS to read the
// files
......
/**
* Copyright 2014 Global Crop Diversity Trust
/*
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -12,7 +12,7 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
*/
package org.genesys2.server.service;
......@@ -21,6 +21,7 @@ import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.List;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
......@@ -38,6 +39,8 @@ public interface DownloadService {
void writeXlsxDescriptor(List<Descriptor> descriptors, OutputStream outputStream) throws IOException;
void writeXlsxDatasetMetadata(Dataset dataset, OutputStream outputStream) throws IOException;
void writeCsvLastUpdated(List<Object[]> lastUpdatedStatistics, OutputStreamWriter outputStream);
}
......@@ -24,8 +24,8 @@ import org.genesys.catalog.exceptions.NotFoundElement;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys2.server.model.PublishState;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.transaction.annotation.Transactional;
public class DatasetPublishingTest extends AbstractDatasetServiceTest {
......@@ -86,6 +86,7 @@ public class DatasetPublishingTest extends AbstractDatasetServiceTest {
}
@Test(expected = InvalidApiUsageException.class)
@WithMockUser(roles = "ADMINISTRATOR")
public void testReviewDatasetFailIfPublished() {
assertThat(savedDataset.getState(), is(PublishState.DRAFT));
final Dataset loaded = datasetRepository.findByUuid(savedDataset.getUuid());
......@@ -108,8 +109,6 @@ public class DatasetPublishingTest extends AbstractDatasetServiceTest {
assertThat(loaded, not(nullValue()));
}
//FIXME Remove ignore as soon as frontend clients start supporting PublishState
@Ignore
@Test(expected = InvalidApiUsageException.class)
public void testApproveDatasetFailIfDrafted() {
assertThat(savedDataset.getState(), is(PublishState.DRAFT));
......@@ -121,6 +120,7 @@ public class DatasetPublishingTest extends AbstractDatasetServiceTest {
}
@Test(expected = InvalidApiUsageException.class)
@WithMockUser(roles = "ADMINISTRATOR")
public void testApproveDatasetFailWhenPublished() {
assertThat(savedDataset.getState(), is(PublishState.DRAFT));
final Dataset loaded = datasetRepository.findByUuid(savedDataset.getUuid());
......@@ -135,6 +135,7 @@ public class DatasetPublishingTest extends AbstractDatasetServiceTest {
}
@Test
@WithMockUser(roles = "ADMINISTRATOR")
public void testUnpublishDataset() {
assertThat(savedDataset.getState(), is(PublishState.DRAFT));
final Dataset loaded = datasetRepository.findByUuid(savedDataset.getUuid());
......
Markdown is supported
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