Skip to content

Commit d6f553a

Browse files
author
Igor Polevoy
committed
#137 Add afterLoaded() callback
1 parent 9351a6a commit d6f553a

File tree

6 files changed

+88
-4
lines changed

6 files changed

+88
-4
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public class CallbackAdapter<T extends Model> implements CallbackListener<T>{
2424

2525
@Override public void beforeSave(T m) {}
2626

27+
@Override public void afterLoad(T m) {}
28+
2729
@Override public void afterSave(T m) {}
2830

2931
@Override public void beforeCreate(T m) {}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
* @author Igor Polevoy
2222
*/
2323
public interface CallbackListener<T extends Model> {
24+
25+
void afterLoad(T m);
26+
2427
void beforeSave(T m);
2528
void afterSave(T m);
2629

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ abstract class CallbackSupport {
2424
protected void beforeSave() {
2525
// overridable
2626
}
27+
28+
protected void afterLoad() {
29+
// overridable
30+
}
31+
2732
protected void afterSave() {
2833
// overridable
2934
}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ public abstract class Model extends CallbackSupport implements Externalizable {
7171
protected Model() {
7272
}
7373

74+
private void fireAfterLoad() {
75+
afterLoad();
76+
for (CallbackListener callback : modelRegistryLocal().callbacks()) {
77+
callback.afterLoad(this);
78+
}
79+
}
80+
7481
private void fireBeforeSave() {
7582
beforeSave();
7683
for (CallbackListener callback : modelRegistryLocal().callbacks()) {
@@ -161,7 +168,7 @@ protected Set<String> dirtyAttributeNames() {
161168
* are new values for it.
162169
*/
163170
public <T extends Model> T fromMap(Map input) {
164-
hydrate(input);
171+
hydrate(input, false);
165172
dirtyAttributeNames.addAll(input.keySet());
166173
return (T) this;
167174
}
@@ -174,7 +181,7 @@ public <T extends Model> T fromMap(Map input) {
174181
*
175182
* @param attributesMap map containing values for this instance.
176183
*/
177-
protected void hydrate(Map<String, Object> attributesMap) {
184+
protected void hydrate(Map<String, Object> attributesMap, boolean fireAfterLoad) {
178185
Set<String> attributeNames = getMetaModelLocal().getAttributeNames();
179186
for (Map.Entry<String, Object> entry : attributesMap.entrySet()) {
180187
if (attributeNames.contains(entry.getKey())) {
@@ -186,6 +193,11 @@ protected void hydrate(Map<String, Object> attributesMap) {
186193
}
187194
}
188195
}
196+
197+
if(fireAfterLoad){
198+
fireAfterLoad();
199+
}
200+
189201
}
190202

191203

@@ -1079,7 +1091,7 @@ public <P extends Model> P parent(Class<P> parentClass, boolean cache) {
10791091
} else {
10801092
try {
10811093
P parent = parentClass.newInstance();
1082-
parent.hydrate(results.get(0));
1094+
parent.hydrate(results.get(0), true);
10831095
if (parentMM.cached()) {
10841096
QueryCache.instance().addItem(parentTable, query, new Object[]{fkValue}, parent);
10851097
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ static <T extends Model> T instance(Map<String, Object> map, MetaModel metaModel
228228
try {
229229
T instance = clazz.newInstance();
230230
instance.setMetamodelLocal(metaModel);
231-
instance.hydrate(map);
231+
instance.hydrate(map, true);
232232
return instance;
233233
} catch(InstantiationException e) {
234234
throw new InitException("Failed to create a new instance of: " + metaModel.getModelClass() + ", are you sure this class has a default constructor?");
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.javalite.activejdbc;
2+
3+
import org.javalite.activejdbc.test.ActiveJDBCTest;
4+
import org.javalite.activejdbc.test_models.Apple;
5+
import org.javalite.activejdbc.test_models.Person;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
9+
import java.util.Map;
10+
11+
/**
12+
* @author Igor Polevoy on 4/7/15.
13+
*/
14+
public class Issue137Spec extends ActiveJDBCTest {
15+
16+
17+
// this is needed to not break the build, since this callback will modify
18+
// value on the Person model in other tests.
19+
static class Switch {
20+
boolean enabled = true;
21+
}
22+
23+
static Switch sw = new Switch();
24+
25+
static CallbackAdapter adapter = new CallbackAdapter() {
26+
@Override
27+
public void afterLoad(Model m) {
28+
if (sw.enabled) {
29+
m.set("name", m.get("name") + " :suffix added after load");
30+
}
31+
}
32+
};
33+
34+
static {
35+
Person.callbackWith(adapter);
36+
}
37+
38+
@Before
39+
public void before() throws Exception {
40+
super.before();
41+
deleteAndPopulateTable("people");
42+
}
43+
44+
@Test
45+
public void shouldFireAfterLoadFromDB() {
46+
sw.enabled = true;
47+
a(Person.findAll().orderBy("name").get(0).get("name")).shouldBeEqual("Joe :suffix added after load");
48+
sw.enabled = false;
49+
}
50+
51+
@Test
52+
public void shouldNotFireAfterLoadFromMap() {
53+
sw.enabled = true;
54+
Person p = new Person();
55+
Person p1 = (Person) Person.findAll().orderBy("name").get(0);
56+
Map m = p1.toMap();
57+
m.put("name", "Jim");
58+
p.fromMap(m);
59+
a(p.get("name")).shouldBeEqual("Jim");
60+
sw.enabled = false;
61+
}
62+
}

0 commit comments

Comments
 (0)