Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding methods validate, beforeCreate, afterCreate, beforeSave and after... #321

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/com/activeandroid/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ public final void delete() {
}

public final Long save() {
if(!validate())
return null;

if(mId==null) {
if(!beforeCreate())
return null;
}

if(!beforeSave())
return null;

final SQLiteDatabase db = Cache.openDatabase();
final ContentValues values = new ContentValues();

Expand Down Expand Up @@ -153,16 +164,26 @@ else if (ReflectionUtils.isSubclassOf(fieldType, Enum.class)) {

if (mId == null) {
mId = db.insert(mTableInfo.getTableName(), null, values);
afterCreate();
}
else {
db.update(mTableInfo.getTableName(), values, idName+"=" + mId, null);
}

Cache.getContext().getContentResolver()
.notifyChange(ContentProvider.createUri(mTableInfo.getType(), mId), null);

afterSave();

return mId;
}

public boolean validate() { return true; }
public boolean beforeCreate() { return true; }
public void afterCreate() {}
public boolean beforeSave() { return true; }
public void afterSave() {}

// Convenience methods

public static void delete(Class<? extends Model> type, long id) {
Expand Down