Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#932 #933

Merged
merged 4 commits into from
May 1, 2024
Merged

#932 #933

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ plugins {
id "com.gorylenko.gradle-git-properties" version "2.4.1"
}

version "4.5"
version "4.5.1-SNAPSHOT"
group "au.org.ala"
description "Ecodata"

Expand Down
39 changes: 28 additions & 11 deletions grails-app/services/au/org/ala/ecodata/ParatooService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import static grails.async.Promises.task
*/
@Slf4j
class ParatooService {
static final Object LOCK = new Object()
static final String DATASET_DATABASE_TABLE = 'Database Table'
static final int PARATOO_MAX_RETRIES = 3
static final String PARATOO_PROTOCOL_PATH = '/protocols'
Expand Down Expand Up @@ -182,18 +183,21 @@ class ParatooService {

dataSet.surveyId = paratooCollectionId.toMap() // No codec to save this to mongo

if (!project.custom) {
project.custom = [:]
}
if (!project.custom.dataSets) {
project.custom.dataSets = []
}

dataSet.orgMintedIdentifier = paratooCollectionId.encodeAsOrgMintedIdentifier()

log.info "Minting identifier for Monitor collection: ${paratooCollectionId}: ${dataSet.orgMintedIdentifier}"
project.custom.dataSets << dataSet
Map result = projectService.update([custom: project.custom], projectId, false)
Map result
synchronized (LOCK) {
Map latestProject = projectService.get(projectId)
if (!latestProject.custom) {
latestProject.custom = [:]
}
if (!latestProject.custom.dataSets) {
latestProject.custom.dataSets = []
}
latestProject.custom.dataSets << dataSet
result = projectService.update([custom: latestProject.custom], projectId, false)
}

if (!result.error) {
result.orgMintedIdentifier = dataSet.orgMintedIdentifier
Expand Down Expand Up @@ -233,7 +237,15 @@ class ParatooService {
promise.onError { Throwable e ->
log.error("An error occurred feching ${collection.orgMintedUUID}: ${e.message}", e)
}
def result = projectService.update([custom: project.project.custom], project.id, false)

def result
synchronized (LOCK) {
Map latestProject = projectService.get(project.id)
Map latestDataSet = latestProject.custom?.dataSets?.find { it.dataSetId == collection.orgMintedUUID }
latestDataSet.putAll(dataSet)
result = projectService.update([custom: latestProject.custom], project.id, false)
}

[updateResult: result, promise: promise]
}

Expand Down Expand Up @@ -302,7 +314,12 @@ class ParatooService {
dataSet.format = DATASET_DATABASE_TABLE
dataSet.sizeUnknown = true

projectService.update([custom: project.project.custom], project.id, false)
synchronized (LOCK) {
Map latestProject = projectService.get(project.project.projectId)
Map latestDataSet = latestProject.custom?.dataSets?.find { it.dataSetId == collection.orgMintedUUID }
latestDataSet.putAll(dataSet)
projectService.update([custom: latestProject.custom], project.id, false)
}
}
}
}
Expand Down
19 changes: 14 additions & 5 deletions src/test/groovy/au/org/ala/ecodata/ParatooServiceSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,13 @@ class ParatooServiceSpec extends MongoSpec implements ServiceUnitTest<ParatooSer
setup:
ParatooCollectionId collectionId = buildCollectionId()
String projectId = 'p1'
Map project = GormMongoUtil.extractDboProperties(getProject())

when:
Map result = service.mintCollectionId('u1', collectionId)

then:
1 * projectService.get(projectId) >> project
1 * projectService.update(_, projectId, false) >> { data, pId, updateCollectory ->
Map dataSet = data.custom.dataSets[1] // The stubbed project already has a dataSet, so the new one will be index=1
assert dataSet.surveyId != null
Expand Down Expand Up @@ -192,6 +194,7 @@ class ParatooServiceSpec extends MongoSpec implements ServiceUnitTest<ParatooSer
then:
1 * webService.doPost(*_) >> [resp: [collections: ["coarse-woody-debris-survey": [uuid: "1", createdAt: "2023-09-01T00:00:00.123Z", start_date_time: "2023-09-01T00:00:00.123Z", end_date_time: "2023-09-01T00:00:00.123Z"]]]]
1 * tokenService.getAuthToken(true) >> Mock(AccessToken)
2 * projectService.get(projectId) >> [projectId: projectId, custom: [dataSets: [dataSet]]]
1 * projectService.update([custom: [dataSets: [expectedDataSetAsync]]], 'p1', false) >> [status: 'ok']
1 * projectService.update([custom: [dataSets: [expectedDataSetSync]]], 'p1', false) >> [status: 'ok']
1 * activityService.create(_) >> [activityId: '123']
Expand Down Expand Up @@ -302,6 +305,7 @@ class ParatooServiceSpec extends MongoSpec implements ServiceUnitTest<ParatooSer
1 * webService.doPost(*_) >> [resp: surveyData]
1 * tokenService.getAuthToken(true) >> Mock(AccessToken)
2 * projectService.update(_, projectId, false) >> [status: 'ok']
2 * projectService.get(projectId) >> [projectId: projectId, custom: [dataSets: [dataSet]]]
1 * siteService.create(_) >> { site = it[0]; [siteId: 's1'] }
1 * activityService.create(_) >> [activityId: '123']
1 * recordService.getAllByActivity('123') >> []
Expand Down Expand Up @@ -332,10 +336,8 @@ class ParatooServiceSpec extends MongoSpec implements ServiceUnitTest<ParatooSer

}

private void setupData() {
Hub hub = new Hub(hubId: "merit", urlPath: "merit")
hub.save(failOnError: true, flush: true)
Project project = new Project(
private Map getProject(){
[
projectId:"p1",
name:"Project 1",
grantId:"g1",
Expand All @@ -348,7 +350,14 @@ class ParatooServiceSpec extends MongoSpec implements ServiceUnitTest<ParatooSer
monitoring:[rows:[[protocols:['protocol category 2', 'protocol category 3']]]]
], dataSets: [[
dataSetId:'c1'
]]])
]]]
]
}

private void setupData() {
Hub hub = new Hub(hubId: "merit", urlPath: "merit")
hub.save(failOnError: true, flush: true)
Project project = new Project(getProject())
project.save(failOnError: true, flush: true)
UserPermission userPermission = new UserPermission(accessLevel: AccessLevel.admin, userId: userId, entityId: 'p1', entityType: Project.name)
userPermission.save(failOnError: true, flush: true)
Expand Down
Loading