-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1591 from AtlasOfLivingAustralia/feature/hcat
Feature/hcat
- Loading branch information
Showing
10 changed files
with
197 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
178 changes: 178 additions & 0 deletions
178
grails-app/controllers/au/org/ala/biocollect/ReferenceAssessmentController.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
package au.org.ala.biocollect | ||
|
||
import au.org.ala.biocollect.merit.* | ||
import grails.converters.JSON | ||
import grails.web.servlet.mvc.GrailsParameterMap | ||
|
||
import java.time.Instant | ||
|
||
class ReferenceAssessmentController { | ||
UserService userService | ||
ProjectActivityService projectActivityService | ||
ActivityService activityService | ||
|
||
|
||
private def createAssessmentRecordFromReference(Object referenceActivity, Object assessProjectActivity, boolean deIdentify) { | ||
def refDoc = referenceActivity.documents[0] | ||
|
||
def baseUrl = '' | ||
if (!refDoc["url"].startsWith('http')) { | ||
baseUrl = grailsApplication.config.getProperty("grails.serverURL", String) | ||
} | ||
|
||
def assessPhoto = [ | ||
licence: refDoc["licence"], | ||
notes: refDoc["notes"], | ||
filesize: refDoc["filesize"], | ||
staged: true, | ||
url: baseUrl + refDoc["url"], | ||
filename: refDoc["filename"], | ||
attribution: referenceActivity.outputs[0].data["imageAttribution"], | ||
name: refDoc["name"], | ||
documentId: '', | ||
contentType: refDoc["contentType"], | ||
dateTaken: refDoc["dateTaken"], | ||
formattedSize: refDoc["formattedSize"], | ||
thumbnailUrl: baseUrl + refDoc["thumbnailUrl"], | ||
status: "active" | ||
] | ||
|
||
def assessActivity = [ | ||
outputs: [ | ||
[ | ||
outputId: "", | ||
outputNotCompleted: false, | ||
data: [ | ||
recordedBy: userService.getCurrentUserDisplayName(), | ||
upperConditionBound: "0", | ||
lowerConditionBound: "0", | ||
overallConditionBestEstimate: "0", | ||
mvgGroup: referenceActivity.outputs[0].data.vegetationStructureGroup, | ||
huchinsonGroup: referenceActivity.outputs[0].data.huchinsonGroup, | ||
sitePhoto: [assessPhoto], | ||
deIdentify: deIdentify ? "Yes" : "No" | ||
], | ||
name: assessProjectActivity["pActivityFormName"] | ||
] | ||
], | ||
projectActivityId: assessProjectActivity["projectActivityId"], | ||
userId: userService.getCurrentUserId(), | ||
projectStage: "", | ||
embargoed: false, | ||
type: assessProjectActivity["pActivityFormName"], | ||
projectId: assessProjectActivity["projectId"], | ||
mainTheme: "" | ||
] | ||
|
||
// Create the new assessment activity record | ||
activityService.update("", assessActivity) | ||
|
||
// Update the numTimesReferenced field on the reference record | ||
referenceActivity.outputs[0].data.numTimesReferenced = | ||
referenceActivity.outputs[0].data.numTimesReferenced as Integer + 1 | ||
activityService.update(referenceActivity.activityId, referenceActivity) | ||
|
||
// Return the assessment activity | ||
assessActivity | ||
} | ||
|
||
def requestRecords() { | ||
def config = grailsApplication.config.getProperty("refAssess", Map) | ||
def body = request.JSON | ||
def result | ||
|
||
// Ensure BioCollect is configured for reference assessment projects | ||
if (!config) { | ||
response.status = 500 | ||
result = [message: 'The application is not configured for reference assessment projects'] | ||
render result as JSON | ||
return | ||
} | ||
|
||
// Ensure the body of the request contains the required fields | ||
if (!body['vegetationStructureGroups'] || !body['climateGroups'] || !body.keySet().contains('deIdentify')) { | ||
response.status = 400 | ||
result = [message: 'Please ensure the assessment record request contains all relevant fields'] | ||
render result as JSON | ||
return | ||
} | ||
|
||
// Ensure the user is authenticated | ||
if (!userService.getCurrentUserId()) { | ||
response.status = 403 | ||
result = [message: 'User is not authenticated'] | ||
render result as JSON | ||
return | ||
} | ||
|
||
// Get the activity records for the reference survey | ||
def refActivitiesSearch = activityService.search([ | ||
projectActivityId: config.reference.projectActivityId | ||
]) | ||
def refActivities = refActivitiesSearch.resp.activities | ||
def maxRecordsToCreate = config.assessment.maxRecordsToCreate as Integer | ||
|
||
// Ensure the reference records exist | ||
def numRefActivities = refActivities?.size() | ||
if (numRefActivities == 0) { | ||
response.status = 404 | ||
result = [message: 'No reference records found in reference survey'] | ||
render result as JSON | ||
return | ||
} | ||
|
||
// Filter out any records without data or documents | ||
refActivities = refActivities.findAll { | ||
it.outputs[0].keySet().contains('data') && | ||
it.documents.size() > 0 | ||
} | ||
|
||
// Filter out reference activities by the supplied vegetation structure groups & climate groups | ||
refActivities = refActivities.findAll { | ||
body["vegetationStructureGroups"].contains(it.outputs[0].data["vegetationStructureGroup"]) && | ||
body["climateGroups"].contains(it.outputs[0].data["huchinsonGroup"]) | ||
} | ||
|
||
// Split & sort the reference activities into: | ||
// Priority records (assessed <= 3 times), prioritising records assessed the MOST | ||
// Other records (assessed > 3 times), prioritising records assessed the LEAST | ||
|
||
def priorityRecords = refActivities | ||
.findAll { it.outputs[0].data.numTimesReferenced as Integer <= 3 } | ||
.sort{ -(it.outputs[0].data.numTimesReferenced as Integer) } | ||
def otherRecords = refActivities | ||
.findAll { it.outputs[0].data.numTimesReferenced as Integer > 3 } | ||
.sort{ it.outputs[0].data.numTimesReferenced as Integer } | ||
|
||
// Combine the two lists | ||
refActivities = priorityRecords + otherRecords | ||
|
||
// Ensure there are reference records after filtering | ||
if (refActivities.size() == 0) { | ||
response.status = 400 | ||
result = [message: "No reference images matching your criteria could be found."] | ||
render result as JSON | ||
return | ||
} | ||
|
||
def assessProjectActivity = projectActivityService.get(config.assessment.projectActivityId) | ||
def assessActivities = [] | ||
for ( | ||
int projectIndex = 0; | ||
projectIndex < Math.min(maxRecordsToCreate, refActivities.size()); | ||
projectIndex++ | ||
) { | ||
assessActivities.push( | ||
createAssessmentRecordFromReference( | ||
refActivities[projectIndex], | ||
assessProjectActivity, | ||
body['deIdentify'] | ||
) | ||
) | ||
} | ||
|
||
response.status = 200 | ||
result = [message: "Found ${assessActivities.size()} images for assessment, please standby..."] | ||
render result as JSON | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,7 +86,6 @@ | |
</div> | ||
</g:if> | ||
</g:if> | ||
|
||
</div> | ||
</div> | ||
</div> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters