Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2021 Yak.Works - Licensed under the Apache License, Version 2.0 (the "License")
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*/
package yakworks.rally.orgs.repo
import javax.inject.Inject
import groovy.transform.CompileStatic
import org.springframework.dao.DataRetrievalFailureException
import org.springframework.validation.Errors
import gorm.tools.databinding.BindAction
import gorm.tools.model.Persistable
import gorm.tools.model.SourceType
import gorm.tools.problem.ValidationProblem
import gorm.tools.repository.GormRepo
import gorm.tools.repository.PersistArgs
import gorm.tools.repository.events.AfterRemoveEvent
import gorm.tools.repository.events.BeforeBindEvent
import gorm.tools.repository.events.BeforeRemoveEvent
import gorm.tools.repository.events.RepoListener
import gorm.tools.repository.model.LongIdGormRepo
import gorm.tools.utils.GormMetaUtils
import gorm.tools.validation.Rejector
import yakworks.rally.orgs.OrgService
import yakworks.rally.orgs.model.Company
import yakworks.rally.orgs.model.Contact
import yakworks.rally.orgs.model.Location
import yakworks.rally.orgs.model.Org
import yakworks.rally.orgs.model.OrgSource
import yakworks.rally.orgs.model.OrgType
/**
* base or OrgRepo. common functionality refactored out so can be overriden in application.
*/
@CompileStatic
abstract class AbstractOrgRepo extends LongIdGormRepo {
@Inject LocationRepo locationRepo
@Inject ContactRepo contactRepo
@Inject OrgTagRepo orgTagRepo
@Inject OrgSourceRepo orgSourceRepo
@Inject OrgService orgService
@RepoListener
void beforeValidate(Org org, Errors errors) {
if(org.isNew()) {
Rejector.validateNotNull(org, errors, 'type')
}
}
/**
* makes sure org has a company on it, and sets it self if its a company
*/
void ensureCompany(Org org){
if (org.companyId == null) {
if (org.type == OrgType.Company){
org.companyId = org.id
} else {
org.companyId = Company.DEFAULT_COMPANY_ID
}
}
}
@RepoListener
void beforeBind(Org org, Map data, BeforeBindEvent be) {
if (be.isBindCreate()) {
ensureCompany(org)
org.type = getOrgTypeFromData(data)
//bind id early or generate one as we use it in afterBind
if(data.id) {
//dont depend on the args.bindId setting and always do it
org.id = data.id as Long
} else {
generateId(org)
}
}
}
@Override
void doBeforePersistWithData(Org org, PersistArgs args) {
Map data = args.data
if (args.bindAction == BindAction.Create) {
verifyNumAndOrgSource(org, data)
//if no orgType then let it fall through and fail validation, orgService is nullable so its easier to mock for testing
if(org.type && orgService?.isOrgMemberEnabled())
orgService.setupMember(org, data.remove('member') as Map)
}
// we do primary location and contact here before persist so we persist org only once with contactId it is created
if(data.location) createOrUpdatePrimaryLocation(org, data.location as Map)
// do contact, support keyContact for legacy and Customers
def contactData = data.contact ?: data.keyContact
if(contactData) createOrUpdatePrimaryContact(org, contactData as Map)
}
/**
* Called after persist if its had a bind action (create or update) and it has data
* creates or updates One-to-Many associations for this entity.
*/
@Override
void doAfterPersistWithData(Org org, PersistArgs args) {
Map data = args.data
if(data.locations) persistToManyWithOrgId(org, Location.repo, data.locations as List