diff --git a/grails-app/domain/au/org/ala/ecodata/ExternalId.groovy b/grails-app/domain/au/org/ala/ecodata/ExternalId.groovy index c8c4bb09f..8ab58aed6 100644 --- a/grails-app/domain/au/org/ala/ecodata/ExternalId.groovy +++ b/grails-app/domain/au/org/ala/ecodata/ExternalId.groovy @@ -17,7 +17,6 @@ class ExternalId implements Comparable { static constraints = { } - static List idTypesByMonitor IdType idType String externalId @@ -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 - } } diff --git a/grails-app/services/au/org/ala/ecodata/SiteService.groovy b/grails-app/services/au/org/ala/ecodata/SiteService.groovy index 2e48008f5..b3455ab96 100644 --- a/grails-app/services/au/org/ala/ecodata/SiteService.groovy +++ b/grails-app/services/au/org/ala/ecodata/SiteService.groovy @@ -29,6 +29,7 @@ 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 @@ -36,6 +37,24 @@ class SiteService { 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. @@ -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 diff --git a/src/test/groovy/au/org/ala/ecodata/ProjectServiceSpec.groovy b/src/test/groovy/au/org/ala/ecodata/ProjectServiceSpec.groovy index e63b11105..7924276c7 100644 --- a/src/test/groovy/au/org/ala/ecodata/ProjectServiceSpec.groovy +++ b/src/test/groovy/au/org/ala/ecodata/ProjectServiceSpec.groovy @@ -930,6 +930,10 @@ class ProjectServiceSpec extends MongoSpec implements ServiceUnitTest> [ contextual: [ elect : 'cl11163' @@ -937,16 +941,21 @@ class ProjectServiceSpec extends MongoSpec implements ServiceUnitTest> [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: @@ -964,20 +973,37 @@ class ProjectServiceSpec extends MongoSpec implements ServiceUnitTest - 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: @@ -987,15 +1013,17 @@ class ProjectServiceSpec extends MongoSpec implements ServiceUnitTest - 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: @@ -1005,15 +1033,15 @@ class ProjectServiceSpec extends MongoSpec implements ServiceUnitTest - 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: @@ -1021,13 +1049,15 @@ class ProjectServiceSpec extends MongoSpec implements ServiceUnitTest + 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: