Skip to content

Commit

Permalink
chennaione#393 Added missing notifications for pre/post save and pre/…
Browse files Browse the repository at this point in the history
…post remove.
  • Loading branch information
iweinzierl committed Oct 3, 2015
1 parent 8498dbe commit 62a1cd9
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions library/src/main/java/com/orm/SugarRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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");
Expand All @@ -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 {
Expand Down

0 comments on commit 62a1cd9

Please sign in to comment.