Skip to content

Commit

Permalink
#328 Refactored CallbackSupport
Browse files Browse the repository at this point in the history
  • Loading branch information
ericbn committed Feb 2, 2015
1 parent 79abbc6 commit c17ad01
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 77 deletions.
@@ -1,92 +1,51 @@
/*
Copyright 2009-2014 Igor Polevoy
Copyright 2009-2015 Igor Polevoy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/


package org.javalite.activejdbc;

/**
* @author Igor Polevoy
* @author Eric Nielsen
*/
class CallbackSupport {
abstract class CallbackSupport {

void fireBeforeSave(Model m){
beforeSave();
for (CallbackListener callback : m.modelRegistryLocal().callbacks()) {
callback.beforeSave(m);
}
protected void beforeSave() {
// overridable
}

void fireAfterSave(Model m){
afterSave();
for (CallbackListener callback : m.modelRegistryLocal().callbacks()) {
callback.afterSave(m);
}
protected void afterSave() {
// overridable
}

void fireBeforeCreate(Model m){
beforeCreate();
for (CallbackListener callback : m.modelRegistryLocal().callbacks()) {
callback.beforeCreate(m);
}
protected void beforeCreate() {
// overridable
}

void fireAfterCreate(Model m){
afterCreate();
for (CallbackListener callback : m.modelRegistryLocal().callbacks()) {
callback.afterCreate(m);
}
protected void afterCreate() {
// overridable
}

void fireBeforeDelete(Model m){
beforeDelete();
for (CallbackListener callback : m.modelRegistryLocal().callbacks()) {
callback.beforeDelete(m);
}
protected void beforeDelete() {
// overridable
}

void fireAfterDelete(Model m){
afterDelete();
for (CallbackListener callback : m.modelRegistryLocal().callbacks()) {
callback.afterDelete(m);
}
protected void afterDelete() {
// overridable
}

void fireBeforeValidation(Model m){
beforeValidation();
for(CallbackListener callback: m.modelRegistryLocal().callbacks())
callback.beforeValidation(m);
protected void beforeValidation() {
// overridable
}

void fireAfterValidation(Model m){
afterValidation();
for (CallbackListener callback : m.modelRegistryLocal().callbacks()) {
callback.afterValidation(m);
}
protected void afterValidation() {
// overridable
}

//overridable instance methods
protected void beforeSave(){}
protected void afterSave(){}

protected void beforeCreate(){}
protected void afterCreate(){}

protected void beforeDelete(){}
protected void afterDelete(){}

protected void beforeValidation(){}
protected void afterValidation(){}
}
71 changes: 63 additions & 8 deletions activejdbc/src/main/java/org/javalite/activejdbc/Model.java
Expand Up @@ -70,6 +70,61 @@ public abstract class Model extends CallbackSupport implements Externalizable {
protected Model() {
}

private void fireBeforeSave() {
beforeSave();
for (CallbackListener callback : modelRegistryLocal().callbacks()) {
callback.beforeSave(this);
}
}

private void fireAfterSave() {
afterSave();
for (CallbackListener callback : modelRegistryLocal().callbacks()) {
callback.afterSave(this);
}
}

private void fireBeforeCreate() {
beforeCreate();
for (CallbackListener callback : modelRegistryLocal().callbacks()) {
callback.beforeCreate(this);
}
}

private void fireAfterCreate() {
afterCreate();
for (CallbackListener callback : modelRegistryLocal().callbacks()) {
callback.afterCreate(this);
}
}

private void fireBeforeDelete() {
beforeDelete();
for (CallbackListener callback : modelRegistryLocal().callbacks()) {
callback.beforeDelete(this);
}
}

private void fireAfterDelete() {
afterDelete();
for (CallbackListener callback : modelRegistryLocal().callbacks()) {
callback.afterDelete(this);
}
}

private void fireBeforeValidation() {
beforeValidation();
for(CallbackListener callback: modelRegistryLocal().callbacks())
callback.beforeValidation(this);
}

private void fireAfterValidation() {
afterValidation();
for (CallbackListener callback : modelRegistryLocal().callbacks()) {
callback.afterValidation(this);
}
}

public static MetaModel getMetaModel() {
return ModelDelegate.metaModelOf(modelClass());
}
Expand Down Expand Up @@ -289,7 +344,7 @@ public boolean frozen(){
* @return true if a record was deleted, false if not.
*/
public boolean delete() {
fireBeforeDelete(this);
fireBeforeDelete();
boolean result;
if( 1 == new DB(getMetaModelLocal().getDbName()).exec("DELETE FROM " + getMetaModelLocal().getTableName()
+ " WHERE " + getIdName() + "= ?", getId())) {
Expand All @@ -304,7 +359,7 @@ public boolean delete() {
else{
result = false;
}
fireAfterDelete(this);
fireAfterDelete();
return result;
}

Expand Down Expand Up @@ -1937,15 +1992,15 @@ public boolean isValid(){
* Executes all validators attached to this model.
*/
public void validate() {
fireBeforeValidation(this);
fireBeforeValidation();
errors = new Errors();
List<Validator> validators = modelRegistryLocal().validators();
if (validators != null) {
for (Validator validator : validators) {
validator.validate(this);
}
}
fireAfterValidation(this);
fireAfterValidation();
}

public boolean hasErrors() {
Expand Down Expand Up @@ -2385,7 +2440,7 @@ public void defrost(){
public boolean save() {
if(frozen) throw new FrozenException(this);

fireBeforeSave(this);
fireBeforeSave();

validate();
if (hasErrors()) {
Expand All @@ -2398,7 +2453,7 @@ public boolean save() {
} else {
result = update();
}
fireAfterSave(this);
fireAfterSave();
return result;
}

Expand Down Expand Up @@ -2431,7 +2486,7 @@ public static Long count(String query, Object... params) {
*/
public boolean insert() {

fireBeforeCreate(this);
fireBeforeCreate();
//TODO: fix this as created_at and updated_at attributes will be set even if insertion failed
doCreatedAt();
doUpdatedAt();
Expand Down Expand Up @@ -2476,7 +2531,7 @@ public boolean insert() {
attributes.put(metaModel.getVersionColumn(), 1);
}

fireAfterCreate(this);
fireAfterCreate();

return done;
} catch (DBException e) {
Expand Down

0 comments on commit c17ad01

Please sign in to comment.