Skip to content

Commit

Permalink
Changed OrganisationService to use data binding #994
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisala committed Aug 27, 2024
1 parent d56fb9b commit 71707f6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
3 changes: 0 additions & 3 deletions grails-app/domain/au/org/ala/ecodata/AssociatedOrg.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ class AssociatedOrg {
/** Reference to the Organisation entity if ecodata has a record of the Organisation */
String organisationId

/** The name of the organisation as referenced via the organisationId */
String organisationName
/**
* The name of the organisation in the context of the relationship. e.g. it could be a name used
* in a contract with a project that is different from the current business name of the organisation
Expand Down Expand Up @@ -50,7 +48,6 @@ class AssociatedOrg {
description nullable: true
fromDate nullable: true
toDate nullable: true
organisationName nullable: true
}

}
16 changes: 14 additions & 2 deletions grails-app/domain/au/org/ala/ecodata/Organisation.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,34 @@ class Organisation {
String description
String announcements
String abn
String url
String abnStatus // N/A, Active, Cancelled
String entityName
String entityType // From ABN register
String sourceSystem // MERIT or Collectory
String entityType // Type code from the ABN register
String orgType // Type name as selected in BioCollect/ Name from the ABN register
List<String> businessNames
String state
Integer postcode
List<ExternalId> externalIds // For financial system vendor codes/reference
List<String> indigenousOrganisationRegistration
List<AssociatedOrg> associatedOrgs // e.g. parent organisation such as for NSW LLS group
List<String> contractNames // When contracts are written for projects with this organisation with a name that doesn't match the organisation name
String status = 'active'
String status = Status.ACTIVE

/** Stores configuration information for how reports should be generated for this organisation (if applicable) */
Map config

String collectoryInstitutionId // Reference to the Collectory

Date dateCreated
Date lastUpdated

static embedded = ['externalIds', 'associatedOrgs']

static mapping = {
organisationId index: true
name index:true
version false
}

Expand All @@ -54,13 +62,17 @@ class Organisation {
abnStatus nullable: true
entityName nullable: true
entityType nullable: true
orgType nullable: true
businessNames nullable: true
contractNames nullable: true
state nullable: true
postcode nullable: true
indigenousOrganisationRegistration nullable: true
associatedOrgs nullable: true
abn nullable: true
url nullable: true
config nullable: true
sourceSystem nullable: true
hubId nullable: true, validator: { String hubId, Organisation organisation, Errors errors ->
GormMongoUtil.validateWriteOnceProperty(organisation, 'organisationId', 'hubId', errors)
}
Expand Down
31 changes: 19 additions & 12 deletions grails-app/services/au/org/ala/ecodata/OrganisationService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ package au.org.ala.ecodata
import com.mongodb.client.MongoCollection
import com.mongodb.client.model.Filters
import grails.validation.ValidationException
import grails.web.databinding.DataBinder
import org.bson.conversions.Bson

import static au.org.ala.ecodata.Status.DELETED

/**
* Works with Organisations, mostly CRUD operations at this point.
*/
class OrganisationService {
class OrganisationService implements DataBinder {

/** Use to include related projects in the toMap method */
public static final String PROJECTS = 'projects'

private static final List EXCLUDE_FROM_BINDING = ['organisationId', 'collectoryInstitutionId', 'status', 'id']

static transactional = 'mongo'
static final FLAT = 'flat'

Expand All @@ -40,7 +43,7 @@ class OrganisationService {
}

def list(levelOfDetail = []) {
return Organisation.findAllByStatusNotEqual('deleted').collect{toMap(it, levelOfDetail)}
return Organisation.findAllByStatusNotEqual(DELETED).collect{toMap(it, levelOfDetail)}
}

def create(Map props, boolean createInCollectory = true) {
Expand All @@ -51,12 +54,8 @@ class OrganisationService {
organisation.collectoryInstitutionId = createCollectoryInstitution(props)
}
try {
// name is a mandatory property and hence needs to be set before dynamic properties are used (as they trigger validations)
bindData(organisation, props, [exclude:EXCLUDE_FROM_BINDING])
organisation.save(failOnError: true, flush:true)
props.remove('id')
props.remove('organisationId')
props.remove('collectoryInstitutionId')
commonService.updateProperties(organisation, props)

// Assign the creating user as an admin.
permissionService.addUserAsRoleToOrganisation(userService.getCurrentUserDetails()?.userId, AccessLevel.admin, organisation.organisationId)
Expand Down Expand Up @@ -97,17 +96,25 @@ class OrganisationService {
if (organisation) {

try {
String oldName = organisation.name
commonService.updateProperties(organisation, props)
// if no collectory institution exists for this organisation, create one
// We shouldn't be doing this unless the org is attached to a project that exports data
// to the ALA.
if (!organisation.collectoryInstitutionId || organisation.collectoryInstitutionId == 'null' || organisation.collectoryInstitutionId == '') {
props.collectoryInstitutionId = createCollectoryInstitution(props)
organisation.collectoryInstitutionId = createCollectoryInstitution(props)
}
œ
String oldName = organisation.name
bindData(organisation, props, [exclude:EXCLUDE_FROM_BINDING])

getCommonService().updateProperties(organisation, props)
if (props.name && (oldName != props.name)) {
projectService.updateOrganisationName(organisation.organisationId, props.name)
}
props.contractNames?.each {
if (!it in organisation.contractNames) {

}
}
organisation.save(failOnError:true)
return [status:'ok']
} catch (Exception e) {
Organisation.withSession { session -> session.clear() }
Expand Down Expand Up @@ -136,7 +143,7 @@ class OrganisationService {
if (destroy) {
organisation.delete()
} else {
organisation.status = 'deleted'
organisation.status = DELETED
organisation.save(flush: true, failOnError: true)
}
return [status: 'ok']
Expand Down

0 comments on commit 71707f6

Please sign in to comment.