yakworks.rally.activity.repo.ActivityRepo.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rally-domain Show documentation
Show all versions of rally-domain Show documentation
gorm domain models for core app and testing
/*
* 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.activity.repo
import java.time.LocalDateTime
import groovy.transform.CompileStatic
import org.apache.commons.lang3.StringUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.validation.Errors
import gorm.tools.mango.MangoDetachedCriteria
import gorm.tools.mango.api.QueryArgs
import gorm.tools.model.Persistable
import gorm.tools.problem.ProblemHandler
import gorm.tools.repository.GormRepository
import gorm.tools.repository.PersistArgs
import gorm.tools.repository.events.AfterBindEvent
import gorm.tools.repository.events.BeforeBindEvent
import gorm.tools.repository.events.BeforePersistEvent
import gorm.tools.repository.events.BeforeRemoveEvent
import gorm.tools.repository.events.RepoListener
import gorm.tools.repository.model.LongIdGormRepo
import gorm.tools.validation.Rejector
import grails.gorm.DetachedCriteria
import grails.gorm.transactions.ReadOnly
import yakworks.rally.activity.model.Activity
import yakworks.rally.activity.model.ActivityContact
import yakworks.rally.activity.model.ActivityLink
import yakworks.rally.activity.model.ActivityNote
import yakworks.rally.activity.model.TaskStatus
import yakworks.rally.activity.model.TaskType
import yakworks.rally.attachment.model.Attachment
import yakworks.rally.attachment.model.AttachmentLink
import yakworks.rally.attachment.repo.AttachmentRepo
import yakworks.rally.orgs.model.Org
import yakworks.rally.tag.model.TagLink
import yakworks.security.user.CurrentUser
import static yakworks.rally.activity.model.Activity.Kind as ActKind
@GormRepository
@CompileStatic
class ActivityRepo extends LongIdGormRepo {
@Autowired ActivityLinkRepo activityLinkRepo
@Autowired AttachmentRepo attachmentRepo
@Autowired CurrentUser currentUser
@Autowired ProblemHandler problemHandler
@RepoListener
void beforeValidate(Activity activity, Errors errors) {
updateNameSummary(activity)
validateActDate(activity, errors)
}
void validateActDate(Activity activity, Errors errors) {
if(activity.isNew()) {
if(!activity.actDate) {
activity.actDate = LocalDateTime.now()
}
} else if(activity.hasChanged('actDate')) {
//actDate can not be updated.
Rejector.of(activity, errors).withError('actDate', activity.actDate, 'error.notupdateable', [name:"actDate"])
}
}
@RepoListener
void beforeBind(Activity activity, Map data, BeforeBindEvent e) {
fixUpTaskParams(data)
if (e.isBindUpdate()) {
if (activity.note && data.name) {
activity.note.body = data.name
activity.note.persist()
}
}
}
@RepoListener
void afterBind(Activity activity, Map data, AfterBindEvent e) {
assignOrg(activity, data)
}
@RepoListener
void beforeRemove(Activity activity, BeforeRemoveEvent e) {
if (activity.note) {
ActivityNote note = activity.note
activity.note = null
activity.persist(flush: true)
note.delete()
}
TagLink.remove(activity)
AttachmentLink.repo.remove(activity)
//FIXME missing removal for attachments if its not linked to anything else
// meaning attachment should also be deleted if it only exists for this activity
ActivityLink.repo.remove(activity)
ActivityContact.repo.remove(activity)
}
@RepoListener
void beforePersist(Activity activity, BeforePersistEvent e) {
if(e.data) {
Map data = e.data
addRelatedDomainsToActivity(activity, data)
}
if (activity.task) {
//setup defaults for status and kind
if (!activity.task.status) activity.task.status = TaskStatus.OPEN
if (!activity.task.taskType) activity.task.taskType = TaskType.TODO
}
}
/**
* 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(Activity activity, PersistArgs args) {
Map data = args.data
if(data.attachments) doAttachments(activity, data.attachments)
if(data.contacts != null) ActivityContact.addOrRemove(activity, data.contacts)
if(data.tags != null) TagLink.addOrRemoveTags(activity, data.tags)
if(args.bindAction?.isCreate()){
if(data.linkedId && data.linkedEntity) {
activityLinkRepo.create(data.linkedId as Long, data.linkedEntity as String, activity)
} else if(data.links) {
assert data.links instanceof List