forked from michaellavelle/spring-data-dynamodb
-
Notifications
You must be signed in to change notification settings - Fork 141
/
AbstractDynamoDBEventListener.java
149 lines (127 loc) · 4.49 KB
/
AbstractDynamoDBEventListener.java
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* Copyright © 2018 spring-data-dynamodb (https://github.com/derjust/spring-data-dynamodb)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.socialsignin.spring.data.dynamodb.mapping.event;
/*
* Copyright 2014 by the original author(s).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import com.amazonaws.services.dynamodbv2.datamodeling.PaginatedQueryList;
import com.amazonaws.services.dynamodbv2.datamodeling.PaginatedScanList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.core.GenericTypeResolver;
import java.util.List;
import java.util.function.Consumer;
/**
* Base class to implement domain class specific {@link ApplicationListener}s.
*
* @author Michael Lavelle
* @author Sebastian Just
*/
public abstract class AbstractDynamoDBEventListener<E> implements ApplicationListener<DynamoDBMappingEvent<?>> {
private static final Logger LOG = LoggerFactory.getLogger(AbstractDynamoDBEventListener.class);
private final Class<?> domainClass;
/**
* Creates a new {@link AbstractDynamoDBEventListener}.
*/
public AbstractDynamoDBEventListener() {
Class<?> typeArgument = GenericTypeResolver.resolveTypeArgument(this.getClass(),
AbstractDynamoDBEventListener.class);
this.domainClass = typeArgument == null ? Object.class : typeArgument;
}
protected Class<?> getDomainClass() {
return this.domainClass;
}
/*
* (non-Javadoc)
*
* @see org.springframework.context.ApplicationListener#onApplicationEvent(org
* .springframework.context.ApplicationEvent)
*/
@Override
public void onApplicationEvent(DynamoDBMappingEvent<?> event) {
@SuppressWarnings("unchecked")
E source = (E) event.getSource();
// source can not be null as java.util.EventObject can not be constructed with
// null
assert source != null;
if (event instanceof AfterScanEvent) {
publishEachElement((PaginatedScanList<?>) source, this::onAfterScan);
return;
} else if (event instanceof AfterQueryEvent) {
publishEachElement((PaginatedQueryList<?>) source, this::onAfterQuery);
return;
}
// Check for matching domain type and invoke callbacks
else if (domainClass.isAssignableFrom(source.getClass())) {
if (event instanceof BeforeSaveEvent) {
onBeforeSave(source);
return;
} else if (event instanceof AfterSaveEvent) {
onAfterSave(source);
return;
} else if (event instanceof BeforeDeleteEvent) {
onBeforeDelete(source);
return;
} else if (event instanceof AfterDeleteEvent) {
onAfterDelete(source);
return;
} else if (event instanceof AfterLoadEvent) {
onAfterLoad(source);
return;
}
}
// we should never end up here
assert false;
}
@SuppressWarnings("unchecked")
private void publishEachElement(List<?> list, Consumer<E> publishMethod) {
list.stream().filter(o -> domainClass.isAssignableFrom(o.getClass())).map(o -> (E) o).forEach(publishMethod);
}
public void onBeforeSave(E source) {
LOG.debug("onBeforeSave({}, {})", source);
}
public void onAfterSave(E source) {
LOG.debug("onAfterSave({}, {})", source);
}
public void onAfterLoad(E source) {
LOG.debug("onAfterLoad({})", source);
}
public void onAfterDelete(E source) {
LOG.debug("onAfterDelete({})", source);
}
public void onBeforeDelete(E source) {
LOG.debug("onBeforeDelete({})", source);
}
public void onAfterScan(E source) {
LOG.debug("onAfterScan({})", source);
}
public void onAfterQuery(E source) {
LOG.debug("onAfterQuery({})", source);
}
}