Commit 6ba4b18c authored by Maxym Borodenko's avatar Maxym Borodenko Committed by Matija Obreza
Browse files

Catalog dataset metadata for download

- including tests
parent 65ad6bb3
......@@ -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,28 @@ 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 = "genesys_catalog_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 {
final RepositoryFile addedFile = repositoryService.addFile(Paths.get(datasetRepositoryPath, loaded.getUuid().toString()).toAbsolutePath(),
fileName, contentType, bos.toByteArray(), null);
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);
}
/**
* 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.impl;
......@@ -39,7 +39,12 @@ import org.apache.poi.xssf.streaming.SXSSFWorkbook;
// NOTE Excel 2016 has a limit of 66,530 hyperlinks
// import org.apache.poi.xssf.usermodel.XSSFHyperlink;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.genesys.catalog.model.dataset.AccessionIdentifier;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.dataset.DatasetCreator;
import org.genesys.catalog.model.dataset.DatasetLocation;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys.catalog.model.vocab.VocabularyTerm;
import org.genesys2.server.model.genesys.AccessionAlias;
import org.genesys2.server.model.genesys.AccessionCollect;
import org.genesys2.server.model.genesys.AccessionData;
......@@ -71,6 +76,7 @@ import org.springframework.transaction.annotation.Transactional;
import com.opencsv.CSVWriter;
@Service
@Transactional(readOnly = true)
public class DownloadServiceImpl implements DownloadService {
public static final Logger LOG = LoggerFactory.getLogger(DownloadServiceImpl.class);
......@@ -131,7 +137,6 @@ public class DownloadServiceImpl implements DownloadService {
private HashMap<String, Method> pdciMethods;
@Override
@Transactional(readOnly = true)
public void writeXlsxMCPD(final AppliedFilters filters, final OutputStream outputStream) throws IOException {
XSSFWorkbook template = new XSSFWorkbook(getClass().getResourceAsStream("/template/download/MCPD.xlsx"));
......@@ -377,7 +382,127 @@ public class DownloadServiceImpl implements DownloadService {
}
@Override
@Transactional(readOnly = true)
public void writeXlsxDatasetMetadata(final Dataset dataset, final OutputStream outputStream) throws IOException {
final XSSFWorkbook template = new XSSFWorkbook(getClass().getResourceAsStream("/template/download/genesys_catalog_metadata.xlsx"));
final SXSSFWorkbook workbook = new SXSSFWorkbook(template, -1);
final Sheet accessionsSheet = workbook.getSheet("accessions");
writeAccessionsMetadata(dataset.getAccessionIdentifiers(), accessionsSheet);
((SXSSFSheet) accessionsSheet).flushRows();
final Sheet descriptorsSheet = workbook.getSheet("descriptors");
writeDescriptorsMetadata(dataset.getDescriptors(), descriptorsSheet);
((SXSSFSheet) descriptorsSheet).flushRows();
final Sheet locationsSheet = workbook.getSheet("location_timing");
writeLocationsMetadata(dataset.getLocations(), locationsSheet);
((SXSSFSheet) locationsSheet).flushRows();
final Sheet creatorsSheet = workbook.getSheet("creators");
writeCreatorsMetadata(dataset.getCreators(), creatorsSheet);
((SXSSFSheet) creatorsSheet).flushRows();
workbook.write(outputStream);
workbook.dispose();
outputStream.flush();
}
private void writeDescriptorsMetadata(final List<Descriptor> descriptors, final Sheet sheet) {
int item = 5;
final String NEW_LINE = "\n";
for (final Descriptor descriptor: descriptors) {
item ++;
final Row nextRow = sheet.createRow(item);
createCell(nextRow, 1, descriptor.getUuid().toString());
createCell(nextRow, 2, descriptor.getVersion().toString());
createCell(nextRow, 3, descriptor.getCrop());
createCell(nextRow, 4, descriptor.getVersionTag());
createCell(nextRow, 5, descriptor.getTitle());
createCell(nextRow, 6, descriptor.getCategory().name());
createCell(nextRow, 7, descriptor.getDescription());
createCell(nextRow, 8, descriptor.getDataType().name());
createCell(nextRow, 9, descriptor.getIntegerOnly());
createCell(nextRow, 10, descriptor.getMinValue());
createCell(nextRow, 11, descriptor.getMaxValue());
final String vocabularyId = descriptor.getVocabulary() == null ? "" : descriptor.getVocabulary().getId().toString();
createCell(nextRow, 12, vocabularyId);
final StringBuilder codes = new StringBuilder();
final StringBuilder codeTitles = new StringBuilder();
final StringBuilder codeDescriptions = new StringBuilder();
for (final VocabularyTerm term: descriptor.getTerms()) {
codes.append(NEW_LINE).append(term.getCode());
codeTitles.append(NEW_LINE).append(term.getTitle());
String description = term.getDescription();
codeDescriptions.append(NEW_LINE).append(description != null ? description.replace(NEW_LINE, "; ") : "");
}
createCell(nextRow, 13, codes.toString().replaceFirst(NEW_LINE, ""));
createCell(nextRow, 14, codeTitles.toString().replaceFirst(NEW_LINE, ""));
createCell(nextRow, 15, codeDescriptions.toString().replaceFirst(NEW_LINE, ""));
createCell(nextRow, 16, descriptor.getColumnName());
createCell(nextRow, 17, descriptor.getUom());
}
}
private void writeLocationsMetadata(final List<DatasetLocation> locations, final Sheet sheet) {
int item = 5;
for (final DatasetLocation location: locations) {
item ++;
final Row nextRow = sheet.createRow(item);
createCell(nextRow, 1, location.getUuid().toString());
createCell(nextRow, 2, location.getVersion());
createCell(nextRow, 3, location.getUserCountry());
createCell(nextRow, 4, location.getStateProvince());
createCell(nextRow, 5, location.getVerbatimLocality());
createCell(nextRow, 6, location.getDecimalLongitude());
createCell(nextRow, 7, location.getDecimalLatitude());
createCell(nextRow, 8, location.getStartDate());
createCell(nextRow, 9, location.getEndDate());
}
}
private void writeCreatorsMetadata(final List<DatasetCreator> creators, final Sheet sheet) {
int item = 5;
for (final DatasetCreator creator: creators) {
item ++;
final Row nextRow = sheet.createRow(item);
createCell(nextRow, 1, creator.getUuid().toString());
createCell(nextRow, 2, creator.getVersion());
createCell(nextRow, 3, creator.getFullName());
createCell(nextRow, 4, creator.getRole().name());
createCell(nextRow, 5, creator.getInstitutionalAffiliation());
createCell(nextRow, 6, creator.getEmail());
createCell(nextRow, 7, creator.getPhoneNumber());
createCell(nextRow, 8, creator.getFax());
createCell(nextRow, 9, creator.getInstituteAddress());
}
}
private void writeAccessionsMetadata(final Set<AccessionIdentifier> identifiers, final Sheet sheet) {
int item = 5;
for (final AccessionIdentifier accessionIdentifier: identifiers) {
item ++;
final Row nextRow = sheet.createRow(item);
createCell(nextRow, 1, accessionIdentifier.getInstCode());
createCell(nextRow, 2, accessionIdentifier.getAcceNumb());
createCell(nextRow, 3, accessionIdentifier.getGenus());
createCell(nextRow, 4, accessionIdentifier.getSpecies());
createCell(nextRow, 5, accessionIdentifier.getDoi());
}
}
@Override
public void writeXlsxDescriptor(final List<Descriptor> descriptors, final OutputStream outputStream) throws IOException {
XSSFWorkbook template = new XSSFWorkbook(getClass().getResourceAsStream("/template/download/DESCRIPTORS.xlsx"));
SXSSFWorkbook workbook = new SXSSFWorkbook(template, -1);
......@@ -424,7 +549,6 @@ public class DownloadServiceImpl implements DownloadService {
}
@Override
@Transactional(readOnly = true)
public void writeXlsxPDCI(final AppliedFilters filters, final OutputStream outputStream) throws IOException {
XSSFWorkbook template = new XSSFWorkbook(getClass().getResourceAsStream("/template/download/PDCI.xlsx"));
......@@ -490,7 +614,6 @@ public class DownloadServiceImpl implements DownloadService {
}
@Override
@Transactional(readOnly = true)
public void writeCsvLastUpdated(final List<Object[]> lastUpdatedStatistics, final OutputStreamWriter outputStream) {
try (CSVWriter writer = new CSVWriter(outputStream, '\t', '"', '\\', "\n")) {
......
......@@ -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