Skip to content

Commit f84d13d

Browse files
committed
1 parent de0993e commit f84d13d

2 files changed

Lines changed: 34 additions & 17 deletions

File tree

src/main/java/act/db/DbBind.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,18 @@
5454
* Default value is an empty string meaning use the {@link #value() binding name}
5555
* as the property name.
5656
*
57-
* Note this setting has no effect unless {@link #byId()} value is `false`
58-
*
5957
* @return the property name used to query the database
6058
*/
6159
String field() default "";
6260

6361
/**
6462
* Indicate if it shall use the resolved value to search for ID or normal field
63+
*
64+
* **Note** this setting has no effect when any one of the following follow case is `true`
65+
*
66+
* 1. the parameter type or field type is collection or iterable
67+
* 2. {@link #field()} setting is not blank
68+
*
6569
* @return `true` if it shall bind by ID field, `false` otherwise
6670
*/
6771
boolean byId() default true;

src/main/java/act/db/di/FindBy.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.osgl.util.*;
3636

3737
import javax.validation.constraints.NotNull;
38+
import java.util.ArrayList;
3839
import java.util.Collection;
3940

4041
public class FindBy extends ValueLoader.Base {
@@ -54,11 +55,11 @@ protected void initialized() {
5455

5556
rawType = spec.rawType();
5657
notNull = spec.hasAnnotation(NotNull.class);
57-
findOne = !(Collection.class.isAssignableFrom(rawType));
58+
findOne = !(Iterable.class.isAssignableFrom(rawType));
5859
dao = app.dbServiceManager().dao(findOne ? rawType : (Class) spec.typeParams().get(0));
5960

6061
queryFieldName = S.string(options.get("field"));
61-
byId = S.blank(queryFieldName) && (Boolean) options.get("byId");
62+
byId = !findOne && S.blank(queryFieldName) && (Boolean) options.get("byId");
6263
resolver = app.resolverManager().resolver(byId ? dao.idType() : (Class) options.get("fieldType"));
6364
if (null == resolver) {
6465
throw new IllegalArgumentException("Cannot find String value resolver for type: " + dao.idType());
@@ -79,29 +80,41 @@ public Object get() {
7980
ActContext ctx = ActContext.Base.currentContext();
8081
E.illegalStateIf(null == ctx);
8182
String value = resolve(requestParamName, ctx);
82-
if (S.blank(value)) {
83-
return ensureNotNull(null, "null");
83+
if (S.empty(value)) {
84+
if (findOne) {
85+
return ensureNotNull(null, "null");
86+
}
8487
}
85-
Object by = resolver.resolve(value);
86-
ensureNotNull(by, value);
88+
Object by = null == value ? null : resolver.resolve(value);
89+
if (findOne) ensureNotNull(by, value);
8790
if (null == by) {
8891
return null;
8992
}
90-
Collection col = findOne ? null : (Collection) App.instance().getInstance(rawType);
91-
if (byId) {
92-
Object bean = dao.findById(by);
93-
if (findOne) {
94-
return ensureNotNull(bean, value);
93+
Collection col = null;
94+
if (!findOne) {
95+
if (rawType.equals(Iterable.class)) {
96+
if (S.empty(value)) {
97+
return dao.findAll();
98+
} else {
99+
col = new ArrayList();
100+
}
95101
} else {
96-
col.add(bean);
97-
return col;
102+
col = (Collection) App.instance().getInstance(rawType);
98103
}
104+
}
105+
if (byId) {
106+
Object bean = dao.findById(by);
107+
return ensureNotNull(bean, value);
99108
} else {
100109
if (findOne) {
101110
Object found = dao.findOneBy(Keyword.of(queryFieldName).javaVariable(), by);
102111
return ensureNotNull(found, value);
103112
} else {
104-
col.addAll(C.list(dao.findBy(Keyword.of(queryFieldName).javaVariable(), by)));
113+
if (S.empty(value)) {
114+
col.addAll(dao.findAllAsList());
115+
} else {
116+
col.addAll(C.list(dao.findBy(Keyword.of(queryFieldName).javaVariable(), by)));
117+
}
105118
return col;
106119
}
107120
}
@@ -133,7 +146,7 @@ private Object ensureNotNull(Object obj, String value) {
133146

134147
private static String resolve(String bindName, ActContext ctx) {
135148
String value = ctx.paramVal(bindName);
136-
if (S.notBlank(value)) {
149+
if (S.notEmpty(value)) {
137150
return value;
138151
}
139152

0 commit comments

Comments
 (0)