Skip to content

Commit 937b04a

Browse files
committed
DATAJDBC-99 - Eventsupport.
The repository publishes events before and after inserting, updating and deleting entities, as well as after instantiation of entities. JdbcEvent ist the common super class of all events and makes the id and the entity available (if possible). Added issue id comments to tests from previous issues.
1 parent aef0b7e commit 937b04a

18 files changed

+742
-62
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jdbc.mapping.event;
17+
18+
import java.util.function.Function;
19+
20+
/**
21+
* gets published after instantiation and setting of all the properties of an entity. This allows to do some
22+
* postprocessing of entities.
23+
*
24+
* @author Jens Schauder
25+
*/
26+
public class AfterCreationEvent extends JdbcEvent{
27+
28+
/**
29+
* @param instance the newly instantiated entity.
30+
* @param idProvider a function providing the id, for the instance.
31+
* @param <T> type of the entity and the argument of the {@code idProvider}
32+
*/
33+
public <T> AfterCreationEvent(T instance, Function<T, Object> idProvider) {
34+
super(instance, idProvider);
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jdbc.mapping.event;
17+
18+
import java.util.function.Function;
19+
20+
/**
21+
* get published after deletion of an entity. The source might contain the Id or the actual entity, depending on the
22+
* {@code delete(...)} method used.
23+
*
24+
* @author Jens Schauder
25+
*/
26+
public class AfterDeleteEvent extends JdbcEvent{
27+
28+
/**
29+
* @param instance the deleted entity.
30+
* @param idProvider a function providing the id, for the instance.
31+
* @param <T> type of the entity and the argument of the {@code idProvider}
32+
*/
33+
public <T> AfterDeleteEvent(T instance, Function<T, Object> idProvider) {
34+
super(instance, idProvider);
35+
}
36+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jdbc.mapping.event;
17+
18+
import java.util.function.Function;
19+
20+
/**
21+
* gets published after an entity got inserted into the database.
22+
*
23+
* @author Jens Schauder
24+
*/
25+
public class AfterInsertEvent extends AfterSaveEvent {
26+
27+
/**
28+
* @param instance the newly inserted entity.
29+
* @param idProvider a function providing the id, for the instance.
30+
* @param <T> type of the entity and the argument of the {@code idProvider}
31+
*/
32+
public <T> AfterInsertEvent(T instance, Function<T, Object> idProvider) {
33+
super(instance, idProvider);
34+
}
35+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jdbc.mapping.event;
17+
18+
import java.util.function.Function;
19+
20+
/**
21+
* subclasses of this get published after a new instance or a changed instance was saved in the database
22+
* @author Jens Schauder
23+
*/
24+
public class AfterSaveEvent extends JdbcEvent{
25+
26+
/**
27+
* @param instance the newly saved entity.
28+
* @param idProvider a function providing the id, for the instance.
29+
* @param <T> type of the entity and the argument of the {@code idProvider}
30+
*/
31+
<T> AfterSaveEvent(T instance, Function<T, Object> idProvider) {
32+
super(instance, idProvider);
33+
}
34+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jdbc.mapping.event;
17+
18+
import java.util.function.Function;
19+
20+
/**
21+
* gets published after an entity was updated in the database.
22+
*
23+
* @author Jens Schauder
24+
*/
25+
public class AfterUpdateEvent extends AfterSaveEvent {
26+
27+
/**
28+
* @param instance the updated entity.
29+
* @param idProvider a function providing the id, for the instance.
30+
* @param <T> type of the entity and the argument of the {@code idProvider}
31+
*/
32+
public <T> AfterUpdateEvent(T instance, Function<T, Object> idProvider) {
33+
super(instance, idProvider);
34+
}
35+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jdbc.mapping.event;
17+
18+
import java.util.function.Function;
19+
20+
import org.springframework.context.ApplicationEvent;
21+
22+
/**
23+
* gets published when an entity is about to get deleted. {@link ApplicationEvent#getSource()} might contain either the
24+
* entity or the id of the entity, depending on which delete method was used.
25+
*
26+
* @author Jens Schauder
27+
*/
28+
public class BeforeDeleteEvent extends JdbcEvent {
29+
30+
/**
31+
* @param instance the entity about to get deleted. Might be {@literal NULL}
32+
* @param idProvider a function providing the id, for the instance. Must provide a not {@literal NULL} id, when called with {@link #instance}
33+
* @param <T> type of the entity and the argument of the {@code idProvider}
34+
*/
35+
public <T> BeforeDeleteEvent(T instance, Function<T, Object> idProvider) {
36+
super(instance, idProvider);
37+
}
38+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jdbc.mapping.event;
17+
18+
import java.util.function.Function;
19+
20+
/**
21+
* gets published before an entity gets inserted into the database.
22+
*
23+
* When the id-property of the entity must get set manually, an event listener for this event may do so.
24+
*
25+
* @author Jens Schauder
26+
*/
27+
public class BeforeInsertEvent extends BeforeSaveEvent {
28+
29+
/**
30+
* @param instance the entity about to get inserted.
31+
* @param idProvider a function providing the id, for the instance.
32+
* @param <T> type of the entity and the argument of the {@code idProvider}
33+
*/
34+
public <T> BeforeInsertEvent(T instance, Function<T, Object> idProvider) {
35+
super(instance, idProvider);
36+
}
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jdbc.mapping.event;
17+
18+
import java.util.function.Function;
19+
20+
import org.springframework.context.ApplicationEvent;
21+
22+
/**
23+
* subclasses of this get published before an entity gets saved to the database.
24+
*
25+
* @author Jens Schauder
26+
*/
27+
public class BeforeSaveEvent extends JdbcEvent {
28+
29+
/**
30+
* @param instance the entity about to get saved.
31+
* @param idProvider a function providing the id, for the instance.
32+
* @param <T> type of the entity and the argument of the {@code idProvider}
33+
*/
34+
<T> BeforeSaveEvent(T instance, Function<T, Object> idProvider) {
35+
super(instance, idProvider);
36+
}
37+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jdbc.mapping.event;
17+
18+
import java.util.function.Function;
19+
20+
/**
21+
* gets published before an entity gets updated in the database.
22+
*
23+
* @author Jens Schauder
24+
*/
25+
public class BeforeUpdateEvent extends BeforeSaveEvent {
26+
27+
/**
28+
* @param instance the entity about to get saved.
29+
* @param idProvider a function providing the id, for the instance.
30+
* @param <T> type of the entity and the argument of the {@code idProvider}
31+
*/
32+
public <T> BeforeUpdateEvent(T instance, Function<T, Object> idProvider) {
33+
super(instance, idProvider);
34+
}
35+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jdbc.mapping.event;
17+
18+
import java.util.function.Function;
19+
20+
import org.springframework.context.ApplicationEvent;
21+
22+
/**
23+
* is the common superclass for all events published by JDBC repositories.
24+
*
25+
* It is recommendet not to use the {@link #getSource()} since it may contain the entity if it was available, when the
26+
* event was published, or in case of delete events only the Id.
27+
*
28+
* Use the dedicated methods {@link #getId()} or {@link #getInstance()} instead. Note that the later might be
29+
* {@literal NULL} in the cases mentioned above.
30+
*
31+
* @author Jens Schauder
32+
*/
33+
public class JdbcEvent extends ApplicationEvent {
34+
35+
private final Object id;
36+
private final Object instance;
37+
38+
<T> JdbcEvent(T instance, Function<T, Object> idProvider) {
39+
40+
super(instance == null ? idProvider.apply(instance) : instance);
41+
this.instance = instance;
42+
this.id = idProvider.apply(instance);
43+
}
44+
45+
/**
46+
* the entity for which this event was publish. Might be {@literal NULL} in cases of delete events where only the id
47+
* was provided to the delete method.
48+
*
49+
* @return instance of the entity triggering this event.
50+
*/
51+
public Object getInstance() {
52+
return instance;
53+
}
54+
55+
/**
56+
* the id of the entity, triggering this event. Guaranteed not to be {@literal NULL}.
57+
* @return
58+
*/
59+
public Object getId() {
60+
return id;
61+
}
62+
}

0 commit comments

Comments
 (0)