Skip to content

Commit c17ad01

Browse files
committed
#328 Refactored CallbackSupport
1 parent 79abbc6 commit c17ad01

File tree

2 files changed

+91
-77
lines changed

2 files changed

+91
-77
lines changed
Lines changed: 28 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,51 @@
11
/*
2-
Copyright 2009-2014 Igor Polevoy
2+
Copyright 2009-2015 Igor Polevoy
33
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
77
8-
http://www.apache.org/licenses/LICENSE-2.0
8+
http://www.apache.org/licenses/LICENSE-2.0
99
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
1515
*/
16-
17-
1816
package org.javalite.activejdbc;
1917

2018
/**
2119
* @author Igor Polevoy
20+
* @author Eric Nielsen
2221
*/
23-
class CallbackSupport {
22+
abstract class CallbackSupport {
2423

25-
void fireBeforeSave(Model m){
26-
beforeSave();
27-
for (CallbackListener callback : m.modelRegistryLocal().callbacks()) {
28-
callback.beforeSave(m);
29-
}
24+
protected void beforeSave() {
25+
// overridable
3026
}
31-
32-
void fireAfterSave(Model m){
33-
afterSave();
34-
for (CallbackListener callback : m.modelRegistryLocal().callbacks()) {
35-
callback.afterSave(m);
36-
}
27+
protected void afterSave() {
28+
// overridable
3729
}
3830

39-
void fireBeforeCreate(Model m){
40-
beforeCreate();
41-
for (CallbackListener callback : m.modelRegistryLocal().callbacks()) {
42-
callback.beforeCreate(m);
43-
}
31+
protected void beforeCreate() {
32+
// overridable
4433
}
45-
46-
void fireAfterCreate(Model m){
47-
afterCreate();
48-
for (CallbackListener callback : m.modelRegistryLocal().callbacks()) {
49-
callback.afterCreate(m);
50-
}
34+
protected void afterCreate() {
35+
// overridable
5136
}
5237

53-
void fireBeforeDelete(Model m){
54-
beforeDelete();
55-
for (CallbackListener callback : m.modelRegistryLocal().callbacks()) {
56-
callback.beforeDelete(m);
57-
}
38+
protected void beforeDelete() {
39+
// overridable
5840
}
59-
60-
void fireAfterDelete(Model m){
61-
afterDelete();
62-
for (CallbackListener callback : m.modelRegistryLocal().callbacks()) {
63-
callback.afterDelete(m);
64-
}
41+
protected void afterDelete() {
42+
// overridable
6543
}
6644

67-
void fireBeforeValidation(Model m){
68-
beforeValidation();
69-
for(CallbackListener callback: m.modelRegistryLocal().callbacks())
70-
callback.beforeValidation(m);
45+
protected void beforeValidation() {
46+
// overridable
7147
}
72-
73-
void fireAfterValidation(Model m){
74-
afterValidation();
75-
for (CallbackListener callback : m.modelRegistryLocal().callbacks()) {
76-
callback.afterValidation(m);
77-
}
48+
protected void afterValidation() {
49+
// overridable
7850
}
79-
80-
//overridable instance methods
81-
protected void beforeSave(){}
82-
protected void afterSave(){}
83-
84-
protected void beforeCreate(){}
85-
protected void afterCreate(){}
86-
87-
protected void beforeDelete(){}
88-
protected void afterDelete(){}
89-
90-
protected void beforeValidation(){}
91-
protected void afterValidation(){}
9251
}

activejdbc/src/main/java/org/javalite/activejdbc/Model.java

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,61 @@ public abstract class Model extends CallbackSupport implements Externalizable {
7070
protected Model() {
7171
}
7272

73+
private void fireBeforeSave() {
74+
beforeSave();
75+
for (CallbackListener callback : modelRegistryLocal().callbacks()) {
76+
callback.beforeSave(this);
77+
}
78+
}
79+
80+
private void fireAfterSave() {
81+
afterSave();
82+
for (CallbackListener callback : modelRegistryLocal().callbacks()) {
83+
callback.afterSave(this);
84+
}
85+
}
86+
87+
private void fireBeforeCreate() {
88+
beforeCreate();
89+
for (CallbackListener callback : modelRegistryLocal().callbacks()) {
90+
callback.beforeCreate(this);
91+
}
92+
}
93+
94+
private void fireAfterCreate() {
95+
afterCreate();
96+
for (CallbackListener callback : modelRegistryLocal().callbacks()) {
97+
callback.afterCreate(this);
98+
}
99+
}
100+
101+
private void fireBeforeDelete() {
102+
beforeDelete();
103+
for (CallbackListener callback : modelRegistryLocal().callbacks()) {
104+
callback.beforeDelete(this);
105+
}
106+
}
107+
108+
private void fireAfterDelete() {
109+
afterDelete();
110+
for (CallbackListener callback : modelRegistryLocal().callbacks()) {
111+
callback.afterDelete(this);
112+
}
113+
}
114+
115+
private void fireBeforeValidation() {
116+
beforeValidation();
117+
for(CallbackListener callback: modelRegistryLocal().callbacks())
118+
callback.beforeValidation(this);
119+
}
120+
121+
private void fireAfterValidation() {
122+
afterValidation();
123+
for (CallbackListener callback : modelRegistryLocal().callbacks()) {
124+
callback.afterValidation(this);
125+
}
126+
}
127+
73128
public static MetaModel getMetaModel() {
74129
return ModelDelegate.metaModelOf(modelClass());
75130
}
@@ -289,7 +344,7 @@ public boolean frozen(){
289344
* @return true if a record was deleted, false if not.
290345
*/
291346
public boolean delete() {
292-
fireBeforeDelete(this);
347+
fireBeforeDelete();
293348
boolean result;
294349
if( 1 == new DB(getMetaModelLocal().getDbName()).exec("DELETE FROM " + getMetaModelLocal().getTableName()
295350
+ " WHERE " + getIdName() + "= ?", getId())) {
@@ -304,7 +359,7 @@ public boolean delete() {
304359
else{
305360
result = false;
306361
}
307-
fireAfterDelete(this);
362+
fireAfterDelete();
308363
return result;
309364
}
310365

@@ -1937,15 +1992,15 @@ public boolean isValid(){
19371992
* Executes all validators attached to this model.
19381993
*/
19391994
public void validate() {
1940-
fireBeforeValidation(this);
1995+
fireBeforeValidation();
19411996
errors = new Errors();
19421997
List<Validator> validators = modelRegistryLocal().validators();
19431998
if (validators != null) {
19441999
for (Validator validator : validators) {
19452000
validator.validate(this);
19462001
}
19472002
}
1948-
fireAfterValidation(this);
2003+
fireAfterValidation();
19492004
}
19502005

19512006
public boolean hasErrors() {
@@ -2385,7 +2440,7 @@ public void defrost(){
23852440
public boolean save() {
23862441
if(frozen) throw new FrozenException(this);
23872442

2388-
fireBeforeSave(this);
2443+
fireBeforeSave();
23892444

23902445
validate();
23912446
if (hasErrors()) {
@@ -2398,7 +2453,7 @@ public boolean save() {
23982453
} else {
23992454
result = update();
24002455
}
2401-
fireAfterSave(this);
2456+
fireAfterSave();
24022457
return result;
24032458
}
24042459

@@ -2431,7 +2486,7 @@ public static Long count(String query, Object... params) {
24312486
*/
24322487
public boolean insert() {
24332488

2434-
fireBeforeCreate(this);
2489+
fireBeforeCreate();
24352490
//TODO: fix this as created_at and updated_at attributes will be set even if insertion failed
24362491
doCreatedAt();
24372492
doUpdatedAt();
@@ -2476,7 +2531,7 @@ public boolean insert() {
24762531
attributes.put(metaModel.getVersionColumn(), 1);
24772532
}
24782533

2479-
fireAfterCreate(this);
2534+
fireAfterCreate();
24802535

24812536
return done;
24822537
} catch (DBException e) {

0 commit comments

Comments
 (0)