-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContactEventListener.groovy
89 lines (76 loc) · 3.63 KB
/
ContactEventListener.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package nl.gridshore.sample.addressbook.listener
import nl.gridshore.sample.addressbook.AddressEntry
import nl.gridshore.sample.addressbook.ContactEntry
import nl.gridshore.sample.addressbook.domain.Address
import nl.gridshore.sample.addressbook.event.AddressAddedEvent
import nl.gridshore.sample.addressbook.event.ContactCreatedEvent
import nl.gridshore.sample.addressbook.event.ContactDeletedEvent
import nl.gridshore.sample.addressbook.event.ContactNameChangedEvent
import org.axonframework.domain.Event
import nl.gridshore.sample.addressbook.event.AddressRemovedEvent
import nl.gridshore.sample.addressbook.event.AddressChangedEvent
/**
* @author Jettro Coenradie
*/
class ContactEventListener implements org.axonframework.eventhandling.EventListener {
def ContactEventListener(eventBus) {
eventBus.subscribe this
}
void handle(Event event) {
println "Contact event listener recevied an event : ${event.class.simpleName}"
if (event.class in [ContactCreatedEvent, ContactNameChangedEvent, ContactDeletedEvent,
AddressAddedEvent, AddressRemovedEvent, AddressChangedEvent]) {
doHandle(event)
} else {
println "ContactEventListener : Event not supported"
}
}
private void doHandle(ContactCreatedEvent event) {
def contact = new ContactEntry(name: event.name, identifier: event.aggregateIdentifier.toString())
contact.save()
}
private void doHandle(ContactNameChangedEvent event) {
ContactEntry foundContact = ContactEntry.findByIdentifier(event.aggregateIdentifier.toString())
foundContact.name = event.newName
foundContact.save()
}
private void doHandle(ContactDeletedEvent event) {
ContactEntry foundContact = ContactEntry.findByIdentifier(event.aggregateIdentifier.toString())
List<AddressEntry> foundAddresses = AddressEntry.findAllByContactName(foundContact.name)
foundContact.delete()
foundAddresses.each {address ->
address.delete()
}
}
private void doHandle(AddressAddedEvent event) {
ContactEntry foundContact = ContactEntry.findByIdentifier(event.aggregateIdentifier.toString())
Address address = event.addedAddress
AddressEntry addressEntry = new AddressEntry(
contactName: foundContact.name,
contactIdentifier: foundContact.identifier,
streetAndNumber: address.streetAndNumber,
zipCode: address.zipCode,
city: address.city,
addressType: event.addressType)
addressEntry.save()
}
private void doHandle(AddressRemovedEvent event) {
ContactEntry foundContact = ContactEntry.findByIdentifier(event.aggregateIdentifier.toString())
AddressEntry foundAddress = AddressEntry.findByAddressTypeAndContactIdentifier(event.addressType,foundContact.identifier)
if (foundAddress) {
foundAddress.delete()
}
}
private void doHandle(AddressChangedEvent event) {
ContactEntry foundContact = ContactEntry.findByIdentifier(event.aggregateIdentifier.toString())
AddressEntry foundAddress = AddressEntry.findByAddressTypeAndContactIdentifier(event.addressType,foundContact.identifier)
Address address = event.addedAddress
foundAddress.contactName = foundContact.name
foundAddress.contactIdentifier = foundContact.identifier
foundAddress.streetAndNumber = address.streetAndNumber
foundAddress.zipCode = address.zipCode
foundAddress.city = address.city
foundAddress.addressType = event.addressType
foundAddress.save()
}
}