Commit 55ccd495 authored by Viacheslav Pavlov's avatar Viacheslav Pavlov Committed by Matija Obreza

WIP: find matchingAccessions for AccessionObservations

parent 51748cf8
......@@ -31,6 +31,8 @@ import java.util.Map;
import java.util.Set;
import java.util.UUID;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQuery;
import org.genesys.amphibian.client.api.DatasetsApi;
import org.genesys.amphibian.client.api.InfoApi;
import org.genesys.amphibian.client.api.PreviewApi;
......@@ -46,8 +48,12 @@ import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys.catalog.model.traits.Descriptor.DataType;
import org.genesys.catalog.model.vocab.VocabularyTerm;
import org.genesys.catalog.persistence.dataset.DatasetRepository;
import org.genesys.catalog.persistence.traits.DescriptorRepository;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.QAccession;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.service.AmphibianService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -76,6 +82,12 @@ public class AmphibianServiceImpl implements AmphibianService, InitializingBean
@Autowired
private DescriptorRepository descriptorRepository;
@Autowired
private AccessionRepository accessionRepository;
@Autowired
private DatasetRepository datasetRepository;
@Override
public void afterPropertiesSet() throws Exception {
if (amphibianClient != null) {
......@@ -329,6 +341,7 @@ public class AmphibianServiceImpl implements AmphibianService, InitializingBean
List<AccessionObservations> res = new ArrayList<>();
DatasetTable table = datasetsApi.getDataset(datasetTableUUID);
Dataset dataset = datasetRepository.findByUuid(datasetTableUUID);
Preview preview = previewApi.getPreview(fileUUID);
long rowCount = preview.getSheets().get(sheet).getRowCount();
......@@ -340,7 +353,6 @@ public class AmphibianServiceImpl implements AmphibianService, InitializingBean
columnsToSelect.put(COLUMN_PREFIX + dataSource.getAcceNumb(), "acceNumb");
columnsToSelect.put(COLUMN_PREFIX + dataSource.getGenus(), "genus");
columnsToSelect.put(COLUMN_PREFIX + dataSource.getInstCode(), "instCode");
columnsToSelect.put(MONGO_COLUMN_ROWINDEX, "row");
Map<String, Integer> traits = dataSource.getTraits();
traits.forEach((key, value) -> columnsToSelect.put(COLUMN_PREFIX + value, key));
......@@ -349,55 +361,75 @@ public class AmphibianServiceImpl implements AmphibianService, InitializingBean
for (int startRow = dataSource.getFirstRow(); startRow <= rowCount; startRow+=BATCH_SIZE) {
List<Object> previewData = getPreviewData(fileUUID, sheet, startRow, BATCH_SIZE, columnsToSelect.keySet().toArray(new String[0]));
for (Object observation : previewData) {
res.add(pushObservationToAmphibian(observation, columnsToSelect, datasetTableUUID, fileUUID, sheet));
res.add(pushObservationToAmphibian(observation, columnsToSelect, datasetTableUUID, fileUUID, dataset));
}
}
return res;
}
private AccessionObservations pushObservationToAmphibian(Object observation, Map<String, String> headers, UUID datasetTableUUID, UUID fileUUID, int sheet) {
private AccessionObservations pushObservationToAmphibian(Object observation, Map<String, String> headers, UUID datasetTableUUID, UUID fileUUID, Dataset dataset) {
AccessionObservations accessionObservations = new AccessionObservations();
accessionObservations.setObservations(new HashMap<>());
Map<String, Object> val = (Map<String, Object>) observation;
Map<String, String> val = (Map<String, String>) observation;
for (String key: val.keySet()) {
switch (headers.get(key)) {
case "doi": {
accessionObservations.doi((String) val.get(key));
accessionObservations.doi(val.get(key));
break;
}
case "acceNumb": {
accessionObservations.accessionNumber((String) val.get(key));
accessionObservations.accessionNumber(val.get(key));
break;
}
case "genus": {
accessionObservations.genus((String) val.get(key));
accessionObservations.genus(val.get(key));
break;
}
case "instCode": {
accessionObservations.instCode((String) val.get(key));
break;
}
case "row": {
accessionObservations.instCode(val.get(key));
break;
}
default: { // key column linked to descriptor
accessionObservations.putObservationsItem(headers.get(key), new ArrayList<>()); // TODO work with case if already exists ( descr from another file)
accessionObservations.putObservationsItem(headers.get(key), new ArrayList<>()); // TODO work with case if already exists (another file)
Observation observationItem = new Observation();
observationItem.addVItem(val.get(key)); // Value TODO parse if array
observationItem.addVItem(val.get(key)); // Value TODO if array
observationItem.c(Integer.valueOf(key.substring(key.indexOf("C") + 1))); // Column
observationItem.d(datasetTableUUID); // dataset
observationItem.f(fileUUID); // file
observationItem.r((Integer) val.get(MONGO_COLUMN_ROWINDEX)); // row
observationItem.s(sheet); // sheet
accessionObservations.getObservations().get(headers.get(key)).add(observationItem);
}
}
}
findMatchingAccession(accessionObservations, dataset);
return datasetsApi.addObservations(datasetTableUUID, accessionObservations).get(0);
}
private void findMatchingAccession(AccessionObservations observations, Dataset dataset) {
if(observations.getInstCode() == null) {
if (dataset.getOwner().getInstitutes().size() == 1) {
observations.instCode(dataset.getOwner().getInstitutes().get(0).getCode());
}
}
JPAQuery query = new JPAQuery();
BooleanExpression whereClause = QAccession.accession.accessionNumber.eq(observations.getAccessionNumber()).and(QAccession.accession.genus.eq(observations.getGenus())).and(QAccession.accession.institute.code.eq(observations.getInstCode()));
if (observations.getDoi() != null) {
whereClause = whereClause.or(QAccession.accession.doi.eq(observations.getDoi()));
}
query.where(whereClause);
List<Accession> accessions = (List<Accession>) accessionRepository.findAll(whereClause);
if (accessions.size() == 1) {
observations.setAccession(accessions.get(0).getUuid());
}
}
@Override
@PreAuthorize("#dataset.published or hasRole('ADMINISTRATOR') or hasPermission(#dataset, 'read')")
public DatasetTable getDataset(Dataset dataset) throws AmphibianException {
......
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