Commit 6a5c2305 authored by Matija Obreza's avatar Matija Obreza

Updated API v0 /dataset controller for migration to Catalog

- listDatasets, listAccessionRefs, listMethods (as Descriptors)
- Excel export of old "datasets" drops accessions and creators sheets
parent 8bcc5872
......@@ -27,6 +27,8 @@ import java.util.UUID;
import javax.persistence.EntityManager;
import javax.validation.Valid;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.genesys.catalog.model.Partner;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.filters.DescriptorFilter;
......@@ -61,10 +63,11 @@ import org.springframework.security.acls.domain.BasePermission;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import com.google.common.collect.Lists;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
import org.springframework.validation.annotation.Validated;
/**
* The Class DescriptorServiceImpl.
......@@ -74,6 +77,8 @@ import org.springframework.validation.annotation.Validated;
@Validated
public class DescriptorServiceImpl implements DescriptorService {
private static final Predicate[] EMPTY_PREDICATE_ARRAY = new Predicate[] {};
/** The Constant LOG. */
private static final Logger LOG = LoggerFactory.getLogger(org.genesys.catalog.service.impl.DescriptorServiceImpl.class);
......@@ -125,8 +130,33 @@ public class DescriptorServiceImpl implements DescriptorService {
@Override
@PostFilter("hasRole('ADMINISTRATOR') or filterObject.published or hasPermission(filterObject, 'READ')")
public List<Descriptor> searchMatchingDescriptor(final Descriptor input) {
final Predicate predicate = descriptor.category.in(input.getCategory()).and(descriptor.dataType.in(input.getDataType())).and(descriptor.crop.eq(input.getCrop()))
.and(descriptor.versionTag.eq(input.getVersionTag())).and(descriptor.title.eq(input.getTitle())).and(descriptor.key.eq(input.isKey()));
try {
input.trimStringsToNull();
LOG.debug("searchMatching: {}", new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(input));
} catch (IOException e) {
}
List<Predicate> searches = Lists.newArrayList();
// version + title
searches.add(descriptor.versionTag.eq(input.getVersionTag()).and(descriptor.title.eq(input.getTitle())));
if (StringUtils.isNotBlank(input.getDescription())) {
// description
searches.add(descriptor.description.eq(input.getDescription()));
}
if (StringUtils.isNotBlank(input.getDescription())) {
// title + dataType
searches.add(descriptor.title.eq(input.getTitle()).and(descriptor.dataType.eq(input.getDataType())));
}
if (input.getCategory() != null) {
// title + category
searches.add(descriptor.title.eq(input.getTitle()).and(descriptor.category.eq(input.getCategory())));
}
if (StringUtils.isNotBlank(input.getUom())) {
// uom
searches.add(descriptor.dataType.eq(input.getDataType()).and(descriptor.uom.eq(input.getUom())));
}
final Predicate predicate = descriptor.crop.eq(input.getCrop()).andAnyOf(searches.toArray(EMPTY_PREDICATE_ARRAY));
List<Descriptor> matches = new ArrayList<>();
descriptorRepository.findAll(predicate).forEach(match -> matches.add(lazyLoad(match)));
return matches;
......
/**
* 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.api.v0;
......@@ -26,17 +26,27 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.ValidationException;
import org.apache.commons.lang3.StringUtils;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys.catalog.model.traits.Descriptor.Category;
import org.genesys.catalog.model.traits.Descriptor.DataType;
import org.genesys.catalog.model.vocab.VocabularyTerm;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.api.ModelValidationException;
import org.genesys2.server.api.OAuth2Cleanup;
import org.genesys2.server.api.Pagination;
import org.genesys2.server.exception.AuthorizationException;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionRef;
import org.genesys2.server.model.genesys.Metadata;
import org.genesys2.server.model.genesys.Method;
import org.genesys2.server.model.genesys.TraitCode;
import org.genesys2.server.model.impl.AccessionIdentifier3;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.json.Api0Constants;
......@@ -45,11 +55,15 @@ import org.genesys2.server.service.DatasetService;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.TraitService;
import org.genesys2.server.service.impl.DownloadServiceImpl;
import org.genesys2.server.service.impl.NonUniqueAccessionException;
import org.genesys2.spring.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -58,6 +72,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import net.sf.oval.ConstraintViolation;
......@@ -104,6 +119,99 @@ public class DatasetController extends ApiBaseController {
final List<Metadata> datasets = datasetService.listMyMetadata();
return OAuth2Cleanup.clean(datasets);
}
/**
* List datasets for institute
*
* @param provider the originator
* @param pagination the pagination
* @return the page
*/
@GetMapping("/list/{wiewsCode}")
public Page<Metadata> listDatasets(@PathVariable("wiewsCode") String provider, Pagination pagination) {
return genesysService.listDatasets(provider, pagination.toPageRequest(50, Direction.ASC, "id"));
}
/**
* List accessions.
*
* @param metadataId the metadata id
* @param pagination the pagination
* @return the page
*/
@GetMapping("/{metadataId}/accessions")
public Page<AccessionRef> listAccessions(@PathVariable("metadataId") long metadataId, Pagination pagination) {
return genesysService.listMetadataAccessionRefs(metadataId, pagination.toPageRequest(50, Direction.ASC, "a.id"));
}
/**
* List methods.
*
* @param metadataId the metadata id
* @return the list
*/
@GetMapping("/{metadataId}/descriptors")
public List<Descriptor> listMethods(@PathVariable("metadataId") long metadataId) {
Metadata metadata = genesysService.getMetadata(metadataId);
if (metadata == null) {
throw new NotFoundElement("No Metadata id=" + metadataId);
}
List<Method> methods = genesysService.listMethods(metadata);
return methods.stream().map(method -> {
Descriptor d=new Descriptor();
d.setColumnName(StringUtils.trimToNull(method.getFieldName()));
d.setTitle(StringUtils.trimToNull(method.getParameter().getTitle()));
d.setDescription(StringUtils.trimToNull(method.getMethod()));
d.setUom(StringUtils.trimToNull(method.getUnit()));
d.setCrop(StringUtils.trimToNull(method.getParameter().getCrop().getShortName()));
Category cat = Category.valueOf(DownloadServiceImpl.catalogDescriptorCategory(method.getParameter().getCategory().getName()));
d.setCategory(cat == null ? Category.CHARACTERIZATION : cat);
DataType dt = DataType.valueOf(DownloadServiceImpl.catalogDataType(method.getFieldType()));
d.setDataType(dt == null ? DataType.TEXT : dt);
d.setIntegerOnly(2 == method.getFieldType() ? Boolean.TRUE : 1 == method.getFieldType() ? Boolean.FALSE : null);
d.setVersionTag("genesys1");
if (method.isCoded()) {
List<VocabularyTerm> terms = Lists.newArrayList();
for (final TraitCode traitCode : TraitCode.parseOptions(method.getOptions())) {
VocabularyTerm term = new VocabularyTerm();
term.setCode(StringUtils.trimToNull(traitCode.getCode()));
term.setTitle(StringUtils.trimToNull(traitCode.getValue()));
terms.add(term);
}
if (terms.size() > 0) {
d.setTerms(terms);
d.setDataType(DataType.CODED);
d.setIntegerOnly(true);
int minVal = Integer.MAX_VALUE;
int maxVal = Integer.MIN_VALUE;
boolean onlyInts = true, increasing = true;
for (VocabularyTerm term: terms) {
try {
int val = Integer.parseInt(term.getCode());
if (val > maxVal) {
maxVal = val;
} else {
increasing = false;
}
if (val < minVal) {
minVal = val;
}
} catch (NumberFormatException e) {
onlyInts = false;
}
}
if (onlyInts && increasing) {
d.setDataType(DataType.SCALE);
d.setMinValue((double) minVal);
d.setMaxValue((double) maxVal);
}
}
}
return d;
}).collect(Collectors.toList());
}
/**
* Add a crop
......
......@@ -288,7 +288,7 @@ public class WiewsController extends BaseController {
throw new ResourceNotFoundException();
}
model.addAttribute("faoInstitute", faoInstitute);
model.addAttribute("pagedData", genesysService.listDatasets(faoInstitute, new PageRequest(page - 1, 30)));
model.addAttribute("pagedData", genesysService.listDatasets(faoInstitute.getCode(), new PageRequest(page - 1, 30)));
return "/metadata/index";
}
......
......@@ -33,6 +33,7 @@ import org.genesys2.server.model.genesys.AccessionData;
import org.genesys2.server.model.genesys.AccessionGeo;
import org.genesys2.server.model.genesys.AccessionHistoric;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.model.genesys.AccessionRef;
import org.genesys2.server.model.genesys.AccessionRemark;
import org.genesys2.server.model.genesys.AllAccnames;
import org.genesys2.server.model.genesys.ExperimentTrait;
......@@ -97,8 +98,6 @@ public interface GenesysService {
Page<Metadata> listMetadata(Pageable pageable);
Page<Metadata> listDatasets(FaoInstitute faoInstitute, Pageable pageable);
Metadata getMetadata(long metadataId);
List<Method> listMethods(Metadata metadata);
......@@ -251,4 +250,8 @@ public interface GenesysService {
List<AccessionData> listActiveAndHistoric(List<UUID> accessionUuids);
Page<AccessionRef> listMetadataAccessionRefs(long metadataId, Pageable pageRequest);
Page<Metadata> listDatasets(String instCode, Pageable pageable);
}
......@@ -805,6 +805,11 @@ public class DownloadServiceImpl implements DownloadService {
public void writeXlsxMetadata(Metadata metadata, OutputStream outputStream) throws IOException {
final XSSFWorkbook template = new XSSFWorkbook(getClass().getResourceAsStream("/template/download/CATALOG-DATASET.xlsx"));
template.removeSheetAt(template.getSheetIndex("creators"));
template.removeSheetAt(template.getSheetIndex("accessions"));
template.createSheet("DATA");
template.setSheetOrder("DATA", 0);
final SuperSXSSFWorkbook workbook = new SuperSXSSFWorkbook(template, 50);
WorkbookStyles wbStyles = WorkbookStyles.create(workbook);
......@@ -815,24 +820,26 @@ public class DownloadServiceImpl implements DownloadService {
String crops = metadataMethods.stream().map(method -> method.getParameter().getCrop().getShortName()).distinct().collect(Collectors.joining(";"));
datasetWriteMetadata(metadata, crops, datasetSheet, wbStyles);
final SuperSXSSFSheet accessionsSheet = workbook.getStreamingSheet("accessions");
accessionsSheet.setRowGenerator((sheet) -> {
genesysLowlevelRepository.listMetadataAccessions(metadata.getId(), new RowCallbackHandler() {
int item = 5; // data starts at 5 in the template
@Override
public void processRow(ResultSet rs) throws SQLException {
item++;
final Row nextRow = sheet.createRow(item);
updateCell(nextRow, 1, rs.getString(2));
updateCell(nextRow, 2, rs.getString(3));
updateCell(nextRow, 3, rs.getString(4));
// updateCell(nextRow, 4, accessionRef.getSpecies());
// updateCell(nextRow, 5, accessionRef.getDoi());
}
});
});
// final SuperSXSSFSheet accessionsSheet =
// workbook.getStreamingSheet("accessions");
// accessionsSheet.setRowGenerator((sheet) -> {
// genesysLowlevelRepository.listMetadataAccessions(metadata.getId(), new
// RowCallbackHandler() {
// int item = 5; // data starts at 5 in the template
//
// @Override
// public void processRow(ResultSet rs) throws SQLException {
// item++;
// final Row nextRow = sheet.createRow(item);
//
// updateCell(nextRow, 1, rs.getString(2));
// updateCell(nextRow, 2, rs.getString(3));
// updateCell(nextRow, 3, rs.getString(4));
// // updateCell(nextRow, 4, accessionRef.getSpecies());
// // updateCell(nextRow, 5, accessionRef.getDoi());
// }
// });
// });
final SuperSXSSFSheet descriptorsSheet = workbook.getStreamingSheet("descriptors");
descriptorsSheet.setRowGenerator((sheet) -> {
......@@ -871,7 +878,7 @@ public class DownloadServiceImpl implements DownloadService {
// datasetWriteCreatorsSheet(dataset.getCreators(), sheet);
// });
final SuperSXSSFSheet dataSheet = (SuperSXSSFSheet) workbook.createSheet("DATA");
final SuperSXSSFSheet dataSheet = (SuperSXSSFSheet) workbook.getStreamingSheet("DATA");
dataSheet.setRowGenerator((sheet) -> {
int item = 0;
SXSSFRow header = sheet.createRow(item++);
......@@ -1051,7 +1058,7 @@ public class DownloadServiceImpl implements DownloadService {
}
}
private String catalogDescriptorCategory(String name) {
public static String catalogDescriptorCategory(String name) {
switch (name) {
case "Morphology":
return "CHARACTERIZATION";
......@@ -1086,7 +1093,7 @@ public class DownloadServiceImpl implements DownloadService {
}
}
private String catalogDataType(int fieldType) {
public static String catalogDataType(int fieldType) {
switch (fieldType) {
case 0:
// Coded
......@@ -1113,15 +1120,15 @@ public class DownloadServiceImpl implements DownloadService {
// 4 Title
updateCell(sheet.getRow(row++), col, metadata.getTitle());
// 5 Dataset version
updateCell(sheet.getRow(row++), col, "");
updateCell(sheet.getRow(row++), col, "genesys1");
// 6 Dataset description
updateCell(sheet.getRow(row++), col, metadata.getDescription());
// 7 Dataset URL
updateCellUrl(sheet.getRow(row++), col, "");
updateCellUrl(sheet.getRow(row++), col, "https://www.genesys-pgr.org/data/view/" + metadata.getId());
// 8 Date of registration of the dataset
updateCell(sheet.getRow(row++), col, metadata.getCreatedDate(), wbStyles.dateStyle);
// 9 License
updateCell(sheet.getRow(row++), col, "");
updateCell(sheet.getRow(row++), col, "CC-BY-NC-ND");
// 10 Language of the dataset
updateCell(sheet.getRow(row++), col, "");
// 11 Related resources
......@@ -1133,7 +1140,7 @@ public class DownloadServiceImpl implements DownloadService {
}
// 13 Dataset provider email
updateCellEmail(sheet.getRow(row++), col, "");
updateCellEmail(sheet.getRow(row++), col, "helpdesk@genesys-pgr.org");
// 14 Dataset provider addresss
updateCell(sheet.getRow(row++), col, "");
......
......@@ -53,6 +53,7 @@ import org.genesys2.server.model.genesys.AccessionData;
import org.genesys2.server.model.genesys.AccessionGeo;
import org.genesys2.server.model.genesys.AccessionHistoric;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.model.genesys.AccessionRef;
import org.genesys2.server.model.genesys.AccessionTrait;
import org.genesys2.server.model.genesys.AllAccnames;
import org.genesys2.server.model.genesys.ExperimentAccessionTrait;
......@@ -586,8 +587,8 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
}
@Override
public Page<Metadata> listDatasets(FaoInstitute faoInstitute, Pageable pageable) {
return metadataRepository.findByInstituteCode(faoInstitute.getCode(), pageable);
public Page<Metadata> listDatasets(String instCode, Pageable pageable) {
return metadataRepository.findByInstituteCode(instCode, pageable);
}
@Override
......@@ -603,7 +604,17 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
@Override
public List<Method> listMethods(Metadata metadata) {
final List<Long> x = metadataMethodRepository.listMetadataMethods(metadata);
return x.size() == 0 ? null : methodRepository.findByIds(x);
if (x.size()==0) return Collections.emptyList();
List<Method> methods = methodRepository.findByIds(x);
methods.forEach(method -> {
if (method.getParameter() != null) {
method.getParameter().getId();
if (method.getParameter().getCrop() != null) {
method.getParameter().getCrop().getId();
}
}
});
return methods;
}
@Override
......@@ -612,6 +623,16 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
List<AccessionData> content = listAccessionData(x.getContent());
return new PageImpl<AccessionData>(content, pageable, x.getTotalElements());
}
@Override
public Page<AccessionRef> listMetadataAccessionRefs(long metadataId, Pageable pageable) {
Page<AccessionId> x = accessionTraitRepository.listMetadataAccessions(metadataId, pageable);
return new PageImpl<>(x.getContent().stream().map(aid -> {
AccessionRef aref = new AccessionRef(accessionRepository.getOne(aid.getId()));
aref.setAccession(null);
return aref;
}).collect(Collectors.toList()), pageable, x.getTotalElements());
}
private List<AccessionData> listAccessionData(List<AccessionId> accessionIds) {
List<AccessionData> result = new ArrayList<AccessionData>(accessionIds.size());
......
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