From 62a1cd9657f48ec2ef8106e3596b2c150d180a4d Mon Sep 17 00:00:00 2001 From: Ingo Weinzierl Date: Sat, 3 Oct 2015 17:42:12 +0200 Subject: [PATCH] #393 Added missing notifications for pre/post save and pre/post remove. --- library/src/main/java/com/orm/SugarRecord.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index beed28a4..8e96a10a 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -24,7 +24,9 @@ import java.util.NoSuchElementException; import javax.persistence.PostPersist; +import javax.persistence.PostRemove; import javax.persistence.PrePersist; +import javax.persistence.PreRemove; import static com.orm.SugarContext.getSugarContext; @@ -323,8 +325,12 @@ public boolean delete() { Long id = getId(); Class type = getClass(); if (id != null && id > 0L) { + getSugarContext().getEntitylistenerManager().notify(this, PreRemove.class); + SQLiteDatabase db = getSugarContext().getSugarDb().getDB(); Log.i("Sugar", type.getSimpleName() + " deleted : " + id); + + getSugarContext().getEntitylistenerManager().notify(this, PostRemove.class); return db.delete(NamingHelper.toSQLName(type), "Id=?", new String[]{id.toString()}) == 1; } else { Log.i("Sugar", "Cannot delete object: " + type.getSimpleName() + " - object has not been saved"); @@ -340,8 +346,12 @@ public static boolean delete(Object object) { field.setAccessible(true); Long id = (Long) field.get(object); if (id != null && id > 0L) { + getSugarContext().getEntitylistenerManager().notify(object, PreRemove.class); SQLiteDatabase db = getSugarContext().getSugarDb().getDB(); + boolean deleted = db.delete(NamingHelper.toSQLName(type), "Id=?", new String[]{id.toString()}) == 1; + + getSugarContext().getEntitylistenerManager().notify(object, PostRemove.class); Log.i("Sugar", type.getSimpleName() + " deleted : " + id); return deleted; } else {