3535import org .osgl .util .*;
3636
3737import javax .validation .constraints .NotNull ;
38+ import java .util .ArrayList ;
3839import java .util .Collection ;
3940
4041public 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