mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-10-23 10:03:59 +00:00
#904 - ENH: Add @PreSoftDelete @PostSoftDelete ... such that methods on beans can be used for these callbacks
This commit is contained in:
+29
-2
@@ -15,6 +15,8 @@ import javax.persistence.PrePersist;
|
||||
import javax.persistence.PreRemove;
|
||||
import javax.persistence.PreUpdate;
|
||||
|
||||
import com.avaje.ebean.annotation.PostSoftDelete;
|
||||
import com.avaje.ebean.annotation.PreSoftDelete;
|
||||
import com.avaje.ebean.event.BeanPersistAdapter;
|
||||
import com.avaje.ebean.event.BeanPersistRequest;
|
||||
import com.avaje.ebean.event.BeanPostConstructListener;
|
||||
@@ -28,12 +30,12 @@ import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanDescriptor;
|
||||
* lifecycle events.
|
||||
* </p>
|
||||
*/
|
||||
public class BeanLifecycleAdapterFactory {
|
||||
class BeanLifecycleAdapterFactory {
|
||||
|
||||
/**
|
||||
* Register a BeanPersistController for methods annotated with lifecycle events.
|
||||
*/
|
||||
public void addLifecycleMethods(DeployBeanDescriptor<?> deployDesc) {
|
||||
void addLifecycleMethods(DeployBeanDescriptor<?> deployDesc) {
|
||||
|
||||
Method[] methods = deployDesc.getBeanType().getMethods();
|
||||
|
||||
@@ -70,6 +72,8 @@ public class BeanLifecycleAdapterFactory {
|
||||
private final List<Method> postUpdates = new ArrayList<>();
|
||||
private final List<Method> preDeletes = new ArrayList<>();
|
||||
private final List<Method> postDeletes = new ArrayList<>();
|
||||
private final List<Method> preSoftDeletes = new ArrayList<>();
|
||||
private final List<Method> postSoftDeletes = new ArrayList<>();
|
||||
private final List<Method> postLoads = new ArrayList<>();
|
||||
private final List<Method> postConstructs = new ArrayList<>();
|
||||
|
||||
@@ -110,6 +114,14 @@ public class BeanLifecycleAdapterFactory {
|
||||
postDeletes.add(method);
|
||||
hasPersistMethods = true;
|
||||
}
|
||||
if (method.isAnnotationPresent(PreSoftDelete.class)) {
|
||||
preSoftDeletes.add(method);
|
||||
hasPersistMethods = true;
|
||||
}
|
||||
if (method.isAnnotationPresent(PostSoftDelete.class)) {
|
||||
postSoftDeletes.add(method);
|
||||
hasPersistMethods = true;
|
||||
}
|
||||
|
||||
if (method.isAnnotationPresent(PostLoad.class)) {
|
||||
postLoads.add(method);
|
||||
@@ -139,14 +151,18 @@ public class BeanLifecycleAdapterFactory {
|
||||
private final Method[] postUpdates;
|
||||
private final Method[] preDeletes;
|
||||
private final Method[] postDeletes;
|
||||
private final Method[] preSoftDeletes;
|
||||
private final Method[] postSoftDeletes;
|
||||
|
||||
PersistMethodsHolder(MethodsHolder methodsHolder) {
|
||||
this.preInserts = toArray(methodsHolder.preInserts);
|
||||
this.preUpdates = toArray(methodsHolder.preUpdates);
|
||||
this.preDeletes = toArray(methodsHolder.preDeletes);
|
||||
this.preSoftDeletes = toArray(methodsHolder.preSoftDeletes);
|
||||
this.postInserts = toArray(methodsHolder.postInserts);
|
||||
this.postUpdates = toArray(methodsHolder.postUpdates);
|
||||
this.postDeletes = toArray(methodsHolder.postDeletes);
|
||||
this.postSoftDeletes = toArray(methodsHolder.postSoftDeletes);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,6 +203,12 @@ public class BeanLifecycleAdapterFactory {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean preSoftDelete(BeanPersistRequest<?> request) {
|
||||
invoke(methodHolder.preSoftDeletes, request);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean preInsert(BeanPersistRequest<?> request) {
|
||||
invoke(methodHolder.preInserts, request);
|
||||
@@ -204,6 +226,11 @@ public class BeanLifecycleAdapterFactory {
|
||||
invoke(methodHolder.postDeletes, request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postSoftDelete(BeanPersistRequest<?> request) {
|
||||
invoke(methodHolder.postSoftDeletes, request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postInsert(BeanPersistRequest<?> request) {
|
||||
invoke(methodHolder.postInserts, request);
|
||||
|
||||
@@ -87,8 +87,15 @@ public class TestLifecycleAnnotatedBean extends BaseTestCase {
|
||||
Ebean.save(bean);
|
||||
Ebean.delete(bean);
|
||||
|
||||
assertThat(bean.getBuffer()).contains("preSoftDelete");
|
||||
assertThat(bean.getBuffer()).contains("postSoftDelete");
|
||||
|
||||
Ebean.deletePermanent(bean);
|
||||
|
||||
assertThat(bean.getBuffer()).contains("preRemove1");
|
||||
assertThat(bean.getBuffer()).contains("preRemove2");
|
||||
assertThat(bean.getBuffer()).contains("postRemove1");
|
||||
assertThat(bean.getBuffer()).contains("postRemove2");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -98,7 +105,7 @@ public class TestLifecycleAnnotatedBean extends BaseTestCase {
|
||||
bean.setName("Persisted");
|
||||
|
||||
Ebean.save(bean);
|
||||
Ebean.delete(bean);
|
||||
Ebean.deletePermanent(bean);
|
||||
|
||||
assertThat(bean.getBuffer()).contains("postRemove1");
|
||||
assertThat(bean.getBuffer()).contains("postRemove2");
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
package com.avaje.tests.model.basic;
|
||||
|
||||
import com.avaje.ebean.annotation.PostSoftDelete;
|
||||
import com.avaje.ebean.annotation.PreSoftDelete;
|
||||
import com.avaje.ebean.annotation.SoftDelete;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
@@ -22,6 +26,9 @@ public class EBasicWithLifecycle {
|
||||
|
||||
String name;
|
||||
|
||||
@SoftDelete
|
||||
boolean deleted;
|
||||
|
||||
@Version
|
||||
Long version;
|
||||
|
||||
@@ -87,6 +94,16 @@ public class EBasicWithLifecycle {
|
||||
buffer.append("postRemove2");
|
||||
}
|
||||
|
||||
@PostSoftDelete
|
||||
public void postSoftDelete() {
|
||||
buffer.append("postSoftDelete");
|
||||
}
|
||||
|
||||
@PreSoftDelete
|
||||
public void preSoftDelete() {
|
||||
buffer.append("preSoftDelete");
|
||||
}
|
||||
|
||||
@PostLoad
|
||||
public void postLoad1() {
|
||||
buffer.append("postLoad1");
|
||||
@@ -123,6 +140,14 @@ public class EBasicWithLifecycle {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public boolean isDeleted() {
|
||||
return deleted;
|
||||
}
|
||||
|
||||
public void setDeleted(boolean deleted) {
|
||||
this.deleted = deleted;
|
||||
}
|
||||
|
||||
public Long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user