Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- refactored code
- fixed broken test?
  • Loading branch information
temi committed Aug 20, 2024
1 parent d62b4c3 commit 3e2a472
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 48 deletions.
16 changes: 0 additions & 16 deletions grails-app/domain/au/org/ala/ecodata/ExternalId.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class ExternalId implements Comparable {

static constraints = {
}
static List idTypesByMonitor

IdType idType
String externalId
Expand All @@ -27,19 +26,4 @@ class ExternalId implements Comparable {
ExternalId other = (ExternalId)otherId
return (idType.ordinal()+externalId).compareTo(other?.idType?.ordinal()+other?.externalId)
}

static List getMonitorIdTypes() {
if (!idTypesByMonitor){
List monitorIdTypes = []
IdType.values().each {
if (it.name().startsWith('MONITOR_')) {
monitorIdTypes << it
}
}

idTypesByMonitor = monitorIdTypes
}

idTypesByMonitor
}
}
21 changes: 20 additions & 1 deletion grails-app/services/au/org/ala/ecodata/SiteService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,32 @@ class SiteService {
static final EMSA_SITE = 'EMSA site'
static final REPORTING_SITE = 'Reporting site'
static final INTERSECTION_CURRENT = 'CURRENT'
static List idTypesByMonitor

def grailsApplication, activityService, projectService, commonService, webService, documentService, metadataService, cacheService
PermissionService permissionService
ProjectActivityService projectActivityService
SpatialService spatialService
ElasticSearchService elasticSearchService

/**
* Get list of monitor external ids.
* @return
*/
static List getMonitorIdTypes() {
if (!idTypesByMonitor) {
List monitorIdTypes = []
ExternalId.IdType.values().toList().each {
if (it.name().startsWith('MONITOR_')) {
monitorIdTypes << it
}
}

idTypesByMonitor = monitorIdTypes
}

idTypesByMonitor
}

/**
* Returns all sites in the system in a list.
Expand Down Expand Up @@ -1015,7 +1034,7 @@ class SiteService {

if (site.externalIds) {
List idTypes = site.externalIds.idType
if (!ExternalId.getMonitorIdTypes()*.name().intersect(idTypes).isEmpty())
if (!getMonitorIdTypes().intersect(idTypes).isEmpty())
code = Site.EMSA_SITE_CODE
} else if (site.type == Site.TYPE_COMPOUND) {
code = Site.REPORTING_SITE_CODE
Expand Down
92 changes: 61 additions & 31 deletions src/test/groovy/au/org/ala/ecodata/ProjectServiceSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -930,23 +930,32 @@ class ProjectServiceSpec extends MongoSpec implements ServiceUnitTest<ProjectSer
Map projectMap
Map result
Project project1
Site site1, site2
Site.metaClass.getDbo = {
delegate.properties
}
metadataService.getGeographicConfig(_) >> [
contextual: [
elect : 'cl11163'
],
"checkForBoundaryIntersectionInLayers" : [ "cl11163" ]
]
metadataService.getGeographicFacetConfig("cl11163", "12345") >> [name: "elect", grouped: true]
Project.withSession { session ->
project1 = new Project(projectId: '111', name: "Project 111", hubId:"12345", isMERIT: true).save(flush: true, failOnError: true)
Site site1 = new Site(siteId: 's1', name: "Site 1", type: "compound", projects: ['111'], extent: [ source: "point", geometry: [intersectionAreaByFacets: ["elect": ["CURRENT": ["bean": 0.1, "canberra": 0.2, "fenner": 0.25]]]]]).save(flush: true, failOnError: true)
Site site2 = new Site(siteId: 's2', name: "Site 2", type: "compound", projects: ['111'], extent: [ source: "point", geometry: [intersectionAreaByFacets: ["elect": ["CURRENT": ["bean": 0.7, "canberra": 0.4, "fenner": 0.5]]]]]).save(flush: true, failOnError: true)
project1.metaClass.getDbo = { new BasicDBObject(project1.properties) }
session.flush()
project1 = new Project(projectId: '111', name: "Project 111", hubId:"12345", isMERIT: true)
site1 = new Site(siteId: 's1', name: "Site 1", type: "compound", status: 'active', projects: ['111'], extent: [ source: "point", geometry: [intersectionAreaByFacets: ["elect": ["CURRENT": ["bean": 0.1, "canberra": 0.2, "fenner": 0.25]]]]])
site2 = new Site(siteId: 's2', name: "Site 2", type: "compound", status: 'active', projects: ['111'], extent: [ source: "point", geometry: [intersectionAreaByFacets: ["elect": ["CURRENT": ["bean": 0.7, "canberra": 0.4, "fenner": 0.5]]]]])
Project.withTransaction {
project1.save(flush: true, failOnError: true)
site1.save(flush: true, failOnError: true)
site2.save(flush: true, failOnError: true)
}
project1.metaClass.getDbo = { new BasicDBObject(project1.properties) }
when:
projectMap = service.toMap(project1, ProjectService.ALL)
Project.withTransaction {
projectMap = service.toMap(project1, ProjectService.ALL)
}
result = service.orderLayerIntersectionsByAreaOfProjectSites(projectMap)
then:
Expand All @@ -964,20 +973,37 @@ class ProjectServiceSpec extends MongoSpec implements ServiceUnitTest<ProjectSer
Project project1
ManagementUnit mu
List result
Project.withSession { session ->
project1 = new Project(projectId: '111', name: "Project 111", hubId:"12345", isMERIT: true, managementUnitId: 'mu1').save(flush: true, failOnError: true)
mu = new ManagementUnit(managementUnitId: 'mu1', name: "Management Unit 1", managementUnitSiteId: 's4').save(flush: true, failOnError: true)
site1 = new Site(siteId: 's1', name: "Site 1", type: "compound", projects: ['111'], extent: [ source: "point", geometry: [intersectionAreaByFacets: ["elect": ["CURRENT": ["bean": 0.1, "canberra": 0.2, "fenner": 0.25]]]]]).save(flush: true, failOnError: true)
site2 = new Site(siteId: 's2', name: "Site 2", type: "compound", projects: ['111'], extent: [ source: "point", geometry: [intersectionAreaByFacets: ["elect": ["CURRENT": ["bean": 0.7, "canberra": 0.4, "fenner": 0.5]]]]]).save(flush: true, failOnError: true)
site3 = new Site(siteId: 's3', name: "Site 3", externalIds: [[idType: ExternalId.IdType.MONITOR_PROTOCOL_INTERNAL_ID, externalId: '1']], projects: ['111'], extent: [ source: "point", geometry: [intersectionAreaByFacets: ["elect": ["CURRENT": ["bean": 0.0, "canberra": 0.1, "fenner": 0.6]]]]]).save(flush: true, failOnError: true)
site4 = new Site(siteId: 's4', name: "Site 4", type: "worksArea", extent: [ source: "point", geometry: [intersectionAreaByFacets: ["elect": ["CURRENT": ["bean": 0.7, "canberra": 0.4, "fenner": 0.5]]]]]).save(flush: true, failOnError: true)
site5 = new Site(siteId: 's5', name: "Site 5", type: "worksArea", projects: ['111'], extent: [ source: "point", geometry: [intersectionAreaByFacets: ["elect": ["CURRENT": ["bean": 0.7, "canberra": 0.4, "fenner": 0.5]]]]]).save(flush: true, failOnError: true)
project1.metaClass.getDbo = { new BasicDBObject(project1.properties) }
session.flush()
project1 = new Project(projectId: '111', name: "Project 111", hubId:"12345", isMERIT: true, managementUnitId: 'mu1')
mu = new ManagementUnit(managementUnitId: 'mu1', name: "Management Unit 1", managementUnitSiteId: 's4')
site1 = new Site(siteId: 's1', name: "Site 1", type: "compound", status: 'active', projects: ['111'], extent: [ source: "point", geometry: [intersectionAreaByFacets: ["elect": ["CURRENT": ["bean": 0.1, "canberra": 0.2, "fenner": 0.25]]]]])
site2 = new Site(siteId: 's2', name: "Site 2", type: "compound", status: 'active', projects: ['111'], extent: [ source: "point", geometry: [intersectionAreaByFacets: ["elect": ["CURRENT": ["bean": 0.7, "canberra": 0.4, "fenner": 0.5]]]]])
site3 = new Site(siteId: 's3', name: "Site 3", externalIds: [[idType: ExternalId.IdType.MONITOR_PROTOCOL_INTERNAL_ID, externalId: '1']], status: 'active', projects: ['111'], extent: [ source: "point", geometry: [intersectionAreaByFacets: ["elect": ["CURRENT": ["bean": 0.0, "canberra": 0.1, "fenner": 0.6]]]]])
site4 = new Site(siteId: 's4', name: "Site 4", type: "worksArea", status: 'active', extent: [ source: "point", geometry: [intersectionAreaByFacets: ["elect": ["CURRENT": ["bean": 0.7, "canberra": 0.4, "fenner": 0.5]]]]])
site5 = new Site(siteId: 's5', name: "Site 5", type: "worksArea", status: 'active', projects: ['111'], extent: [ source: "point", geometry: [intersectionAreaByFacets: ["elect": ["CURRENT": ["bean": 0.7, "canberra": 0.4, "fenner": 0.5]]]]])
Project.withTransaction {
project1.save(flush: true, failOnError: true)
mu.save(flush: true, failOnError: true)
site1.save(flush: true, failOnError: true)
site2.save(flush: true, failOnError: true)
site3.save(flush: true, failOnError: true)
site4.save(flush: true, failOnError: true)
site5.save(flush: true, failOnError: true)
}
ManagementUnit.metaClass.getDbo = {
delegate.properties
}
Project.metaClass.getDbo = {
delegate.properties
}
Site.metaClass.getDbo = {
delegate.properties
}
when: // returns reporting and EMSA sites Only
projectMap = service.toMap(project1, ProjectService.ALL)
Project.withTransaction {
projectMap = service.toMap(project1, ProjectService.ALL)
}
result = service.getRepresentativeSitesOfProject(projectMap)
then:
Expand All @@ -987,15 +1013,17 @@ class ProjectServiceSpec extends MongoSpec implements ServiceUnitTest<ProjectSer
result.siteId[2] == 's3'
when: // returns planning/project extent sites
Project.withSession { session ->
site1.type = Site.TYPE_PROJECT_AREA
site1.type = Site.TYPE_PROJECT_AREA
site2.type = Site.TYPE_WORKS_AREA
Project.withTransaction {
site1.save(flush: true)
site2.type = Site.TYPE_WORKS_AREA
site2.save(flush: true)
site3.delete(flush: true)
session.flush()
}
Project.withTransaction {
projectMap = service.toMap(project1, ProjectService.ALL)
}
projectMap = service.toMap(project1, ProjectService.ALL)
result = service.getRepresentativeSitesOfProject(projectMap)
then:
Expand All @@ -1005,29 +1033,31 @@ class ProjectServiceSpec extends MongoSpec implements ServiceUnitTest<ProjectSer
result.siteId[2] == 's5'
when: // returns Management Unit boundaries
Project.withSession { session ->
site1.projects = site2.projects = site5.projects = []
site1.projects = site2.projects = site5.projects = []
Project.withTransaction {
site1.save(flush: true)
site2.save(flush: true)
site5.save(flush: true)
session.flush()
}
projectMap = service.toMap(project1, ProjectService.ALL)
Project.withTransaction {
projectMap = service.toMap(project1, ProjectService.ALL)
}
result = service.getRepresentativeSitesOfProject(projectMap)
then:
result.size() == 1
result.siteId[0] == 's4'
when:// returns empty
Project.withSession { session ->
Project.withTransaction {
project1.managementUnitId = null
project1.save(flush: true)
session.flush()
}
projectMap = service.toMap(project1, ProjectService.ALL)
Project.withTransaction {
projectMap = service.toMap(project1, ProjectService.ALL)
}
result = service.getRepresentativeSitesOfProject(projectMap)
then:
Expand Down

0 comments on commit 3e2a472

Please sign in to comment.