package act.db.ebean2;

import act.Act;
import act.app.App;
import act.app.event.AppEventId;
import act.db.DbService;
import act.db.di.DaoInjectionListenerBase;
import act.event.AppEventListenerBase;
import org.osgl.$;
import org.osgl.inject.BeanSpec;
import org.osgl.util.Generics;
import org.osgl.util.S;

import java.lang.reflect.Type;
import java.util.EventObject;
import java.util.List;

public class EbeanDaoInjectionListener extends DaoInjectionListenerBase {

    @Override
    public Class[] listenTo() {
        return new Class[]{EbeanDao.class};
    }

    @Override
    public void onInjection(Object injectee, BeanSpec spec) {
        List<Type> typeParameters = spec.typeParams();
        if (typeParameters.isEmpty()) {
            typeParameters = Generics.typeParamImplementations(spec.rawType(), EbeanDao.class);
        }
        if (null == typeParameters) {
            logger.warn("No type parameter information provided");
            return;
        }
        final $.T2<Class, String> resolved = resolve(typeParameters);
        DbService dbService = App.instance().dbServiceManager().dbService(resolved._2);
        if (dbService instanceof EbeanService) {
            final EbeanService service = $.cast(dbService);
            final EbeanDao dao = $.cast(injectee);
            Act.eventBus().bind(AppEventId.DB_SVC_LOADED, new AppEventListenerBase(S.concat(resolved._2, "-ebean-on-dao-injection")) {
                @Override
                public void on(EventObject eventObject) throws Exception {
                    dao.ebean(service.ebean());
                    dao.modelType(resolved._1);
                }
            });
        }
    }
}

@@ -158,47 +158,8 @@
 se;%0A
-import act.event.AppEventListenerBase;%0A
 impo
@@ -297,38 +297,8 @@
 pe;%0A
-import java.util.EventObject;%0A
 impo
@@ -1239,23 +1239,23 @@
 Act.
-eventBus().bind
+jobManager().on
 (App
@@ -1281,33 +1281,8 @@
 ED, 
-new AppEventListenerBase(
 S.co
@@ -1325,16 +1325,31 @@
 ection%22)
+, new Runnable(
 ) %7B%0A    
@@ -1402,52 +1402,13 @@
 oid 
-on(EventObject eventObject) throws Exception
+run()
  %7B%0A 
@@ -1533,16 +1533,22 @@
        %7D
+, true
 );%0A     

fix issue with injecting EbeanServer into EbeanDao