Commit ea700787 authored by Matija Obreza's avatar Matija Obreza

Updated DatasetTableApi and PreviewApi

- Preview only selected fields
- Append and get AccessionObservations
parent bb5657a3
......@@ -168,9 +168,46 @@ paths:
in: query
description: Number of rows to return
required: false
type: integer
format: int64
responses:
'200':
description: OK
schema:
type: array
items:
$ref: '#/definitions/AccessionObservations'
'401':
description: Unauthorized
'403':
description: Forbidden
'404':
description: Not Found
deprecated: false
'/api/v0/datasets/{uuid}/add':
post:
tags:
- datasets
summary: 'Add observations, returns result of update'
description: Add accession observations
operationId: addObservations
consumes:
- application/json
produces:
- '*/*'
parameters:
- name: uuid
in: path
description: Table name
required: true
type: string
default: '50'
allowEmptyValue: false
format: uuid
- in: body
name: observations
description: New accession observations
required: true
schema:
$ref: '#/definitions/AccessionObservations'
responses:
'200':
description: OK
......@@ -178,6 +215,8 @@ paths:
type: array
items:
$ref: '#/definitions/AccessionObservations'
'201':
description: Created
'401':
description: Unauthorized
'403':
......@@ -324,6 +363,15 @@ paths:
type: integer
default: 50
allowEmptyValue: false
- name: fields
in: query
description: Specify fields to return
required: false
type: array
items:
type: string
collectionFormat: multi
allowEmptyValue: true
responses:
'200':
description: OK
......@@ -339,13 +387,77 @@ paths:
description: Not Found
deprecated: false
definitions:
Observation:
type: object
properties:
v:
title: values
description: List of values
type: array
items:
type: object
e:
title: errors
description: List of errors
type: array
items:
type: string
d:
title: dataset
description: Source dataset UUID
type: string
format: uuid
f:
title: source
description: Source dataset file UUID
type: string
format: uuid
s:
title: sheet
description: Sheet index in the source dataset file
type: integer
r:
title: row
description: Row index in the source sheet
type: integer
c:
title: column
description: Column index in the source sheet
type: integer
title: Observation
ObservationSource:
type: object
properties:
d:
title: dataset
description: Source dataset UUID
type: string
format: uuid
f:
title: source
description: Source dataset file UUID
type: string
format: uuid
s:
title: sheet
description: Sheet index in the source dataset file
type: integer
r:
title: row
description: Row index in the source sheet
type: integer
c:
title: column
description: Column index in the source sheet
type: integer
title: ObservationSource
AccessionObservations:
type: object
properties:
a:
accession:
type: string
format: uuid
acceNumb:
accessionNumber:
type: string
doi:
type: string
......
......@@ -101,13 +101,15 @@ public class PreviewApi {
public List<?> getData(@ApiParam(value = "Your reference UUID", required = true) @PathVariable UUID uuid, // uuid
@ApiParam(value = "Sheet index", required = true) @PathVariable long sheet, // sheet index
@ApiParam(value = "Index of the first row", required = true) @PathVariable long startRow, // start row
@ApiParam(value = "Number of rows to return", required = false, format="int32", type="integer") @RequestParam(name = "count", required = false, defaultValue = "50") Optional<Integer> count) {
@ApiParam(value = "Number of rows to return", required = false, format="int32", type="integer") @RequestParam(name = "count", required = false, defaultValue = "50") Optional<Integer> count, // max rows
@ApiParam(value = "Specify fields to return", required = false) @RequestParam(required=false) String[] fields // selected fields
) {
Preview preview = previewService.getPreview(uuid);
if (preview == null) {
throw new NoSuchThingException("No preview for reference UUID=" + uuid);
}
return previewService.getData(uuid, sheet, startRow, count.orElse(50));
return previewService.getData(uuid, sheet, startRow, count.orElse(50), fields);
}
}
......@@ -15,19 +15,30 @@
*/
package org.genesys.amphibian.api.v0.genesys;
import static org.springframework.data.mongodb.core.query.Criteria.*;
import static org.springframework.data.mongodb.core.query.Query.*;
import static org.springframework.data.mongodb.core.query.Update.*;
import java.util.List;
import java.util.Optional;
import org.bson.Document;
import org.genesys.amphibian.api.Constants;
import org.genesys.amphibian.api.v0.TablesApi;
import org.genesys.amphibian.model.DatasetTable;
import org.genesys.amphibian.model.Table;
import org.genesys.amphibian.model.genesys.AccessionObservations;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
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;
import com.mongodb.client.result.UpdateResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
......@@ -48,18 +59,34 @@ public class DatasetTableApi extends TablesApi<DatasetTable> {
@Override
@ApiOperation(nickname = "getDataset", notes = "Get dataset information", value = "...")
@ApiResponses({ @ApiResponse(code = 200, message = "OK", response = DatasetTable.class) })
public Table get(@ApiParam(value = "Table key", required = true, format="uuid") @PathVariable(name = "name") String tableKey) {
public Table get(@ApiParam(value = "Table key", required = true, format = "uuid") @PathVariable(name = "name") String tableKey) {
return super.get(tableKey);
}
@Override
@ApiOperation(nickname = "getObservations", notes = "Get accession observations", value = "Retrieve the list of accession observations in this table")
@ApiResponses({ @ApiResponse(code = 200, message = "OK", response = AccessionObservations.class, responseContainer="List") })
public List<?> getData(@ApiParam(value = "Table name", required = true, format="uuid") @PathVariable(name="name") String tableName, // tableName
@ApiResponses({ @ApiResponse(code = 200, message = "OK", response = AccessionObservations.class, responseContainer = "List") })
public List<?> getData(@ApiParam(value = "Table name", required = true, format = "uuid") @PathVariable(name = "name") String tableKey, // tableKey
@ApiParam(value = "Index of the first row", required = true) @PathVariable long startRow, // start row
@ApiParam(value = "Number of rows to return", required = false, type="integer") @RequestParam(name = "count", required = false, defaultValue = "50") Optional<Integer> count) {
@ApiParam(value = "Number of rows to return", required = false, type = "integer") @RequestParam(name = "count", required = false, defaultValue = "50") Optional<Integer> count) {
return super.getData(tableName, startRow, count);
return super.getData(tableKey, startRow, count);
}
@PostMapping(path = { "{uuid}/add" })
@ApiOperation(nickname = "addObservations", notes = "Add accession observations", value = "Add observations, returns result of update")
@ApiResponses({ @ApiResponse(code = 200, message = "OK", response = AccessionObservations.class, responseContainer = "List") })
public List<?> addObservations(@ApiParam(value = "Table name", required = true, format = "uuid") @PathVariable(name = "uuid") String tableKey, // tableKey
@ApiParam(value = "New accession observations", required = true, type = "org.genesys.amphibian.model.genesys.AccessionObservations") @RequestBody Document observations) {
Table table = get(tableKey);
LOG.warn("Adding {}", observations);
Query query = query(where(AccessionObservations.FIELD_ACCESSION).is(observations.get(AccessionObservations.FIELD_ACCESSION)));
Update update = fromDocument(observations);
UpdateResult updateResult = databaseService.appendData(table, query, update);
LOG.warn("Update result {}", updateResult);
return databaseService.getData(table, query);
}
}
......@@ -23,13 +23,23 @@ import com.fasterxml.jackson.annotation.JsonUnwrapped;
* @author Matija Obreza
*/
public class AccessionObservations {
@JsonProperty("a")
public static final String FIELD_ACCESSION = "accession";
private static final String FIELD_DOI = "doi";
private static final String FIELD_ACCENUMB = "accessionNumber";
private static final String FIELD_GENUS = "genus";
private static final String FIELD_INSTCODE = "instCode";
@JsonProperty(FIELD_ACCESSION)
public UUID accession;
// Raw data from source
@JsonProperty(FIELD_DOI)
public String doi;
@JsonProperty(FIELD_ACCENUMB)
public String acceNumb;
@JsonProperty(FIELD_INSTCODE)
public String instCode;
@JsonProperty(FIELD_GENUS)
public String genus;
/**
......
......@@ -18,9 +18,13 @@ package org.genesys.amphibian.service;
import java.util.List;
import java.util.Map;
import org.bson.Document;
import org.genesys.amphibian.model.Table;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import com.mongodb.client.ListIndexesIterable;
import com.mongodb.client.result.UpdateResult;
public interface DatabaseService {
......@@ -42,5 +46,8 @@ public interface DatabaseService {
<T extends Table> List<?> getData(T table, long startRow, int count);
<T extends Table> UpdateResult appendData(T table, Query query, Update update);
<T extends Table> List<Document> getData(Table table, Query query);
}
......@@ -32,7 +32,7 @@ public interface PreviewService {
Preview getPreview(UUID referenceUuid);
List<Document> getData(UUID referenceUuid, long sheet, long startRow, int count);
List<Document> getData(UUID referenceUuid, long sheet, long startRow, int count, String... selectedColumns);
Preview updateState(String id, State reading, String error);
......
......@@ -30,12 +30,14 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.mongodb.client.ListIndexesIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.IndexOptions;
import com.mongodb.client.result.UpdateResult;
@Service
@Transactional(readOnly = true)
......@@ -43,7 +45,7 @@ public class DatabaseServiceImpl implements DatabaseService {
private static final Logger LOG = LoggerFactory.getLogger(DatabaseServiceImpl.class);
private static final String MONGO_COLPREFIX = "T";
private static final String MONGO_ID = "_id";
private static final String AMPHIBIAN_TABLE_PREFIX = "amphibian.";
@Autowired
......@@ -169,4 +171,16 @@ public class DatabaseServiceImpl implements DatabaseService {
return mongoTemplate.find(q, Document.class, table.getCollection());
}
@Override
@Transactional
public <T extends Table> UpdateResult appendData(T table, Query query, Update update) {
return mongoTemplate.upsert(query, update, table.getCollection());
}
@Override
public <T extends Table> List<Document> getData(Table table, Query query) {
query.fields().exclude(MONGO_ID);
return mongoTemplate.find(query, Document.class, table.getCollection());
}
}
......@@ -146,10 +146,18 @@ public class PreviewServiceImpl implements PreviewService, InitializingBean {
}
@Override
public List<Document> getData(UUID referenceUuid, long sheet, long startRow, int count) {
public List<Document> getData(UUID referenceUuid, long sheet, long startRow, int count, String... selectedFields) {
Query q = query(where(RAWDATA_DATASET).is(referenceUuid).and(RAWDATA_SHEET).is(sheet).and(RAWDATA_ROW).gte(startRow)).with(Sort.by(RAWDATA_ROW));
q.limit(count); // limit rows
q.fields().exclude(RAWDATA_DATASET).exclude(RAWDATA_SHEET).exclude(MONGO_ID); // don't send back reference UUID or sheet (they are passed in)
if (selectedFields == null || selectedFields.length == 0) {
// don't send back reference UUID or sheet (they are passed in)
q.fields().exclude(RAWDATA_DATASET).exclude(RAWDATA_SHEET).exclude(MONGO_ID);
} else {
for (String selectedColumn : selectedFields) {
q.fields().include(selectedColumn).exclude(MONGO_ID);
}
}
return mongoTemplate.find(q, Document.class, MONGO_RAWDATA);
}
......
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