/*
 * Copyright 2015-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package org.docksidestage.mylasta.direction.sponsor;

import java.util.TimeZone;

import org.dbflute.optional.OptionalThing;
import org.dbflute.system.DBFluteSystem;
import org.dbflute.system.provider.DfFinalTimeZoneProvider;
import org.dbflute.util.DfTypeUtil;
import org.docksidestage.mylasta.action.FortressUserBean;
import org.lastaflute.core.direction.CurtainBeforeHook;
import org.lastaflute.core.direction.FwAssistantDirector;
import org.lastaflute.core.util.ContainerUtil;
import org.lastaflute.web.login.LoginManager;
import org.lastaflute.web.servlet.request.RequestManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author jflute
 */
public class FortressCurtainBeforeHook implements CurtainBeforeHook {

    private static final Logger logger = LoggerFactory.getLogger(FortressCurtainBeforeHook.class);

    // ===================================================================================
    //                                                                               Hook
    //                                                                              ======
    public void hook(FwAssistantDirector assistantDirector) {
        processDBFluteSystem();
        whiteboxtest_findLoginManager();
    }

    // ===================================================================================
    //                                                                      DBFlute System
    //                                                                      ==============
    protected void processDBFluteSystem() {
        DBFluteSystem.unlock();
        DBFluteSystem.setFinalTimeZoneProvider(createFinalTimeZoneProvider());
        DBFluteSystem.lock();
    }

    protected DfFinalTimeZoneProvider createFinalTimeZoneProvider() {
        return new DfFinalTimeZoneProvider() {
            protected final TimeZone provided = FortressUserTimeZoneProcessProvider.centralTimeZone;

            public TimeZone provide() {
                return provided;
            }

            @Override
            public String toString() {
                return DfTypeUtil.toClassTitle(this) + ":{" + provided.getID() + "}";
            }
        };
    }

    // ===================================================================================
    //                                                                       Whitebox Test
    //                                                                       =============
    protected void whiteboxtest_findLoginManager() {
        OptionalThing<LoginManager> thing = ContainerUtil.getComponent(RequestManager.class).findLoginManager(FortressUserBean.class);
        logger.debug("findLoginManager() when booting: {}", thing);
    }
}

@@ -683,23 +683,91 @@
 ava.
-util.TimeZone;%0A
+math.BigDecimal;%0Aimport java.util.TimeZone;%0A%0Aimport org.dbflute.hook.AccessContext;
 %0Aimp
@@ -939,24 +939,142 @@
 DfTypeUtil;%0A
+import org.docksidestage.dbflute.exbhv.PurchasePaymentBhv;%0Aimport org.docksidestage.dbflute.exentity.PurchasePayment;%0A
 import org.d
@@ -1264,26 +1264,247 @@
 ore.
-util.ContainerUtil
+magic.async.AsyncManager;%0Aimport org.lastaflute.core.time.TimeManager;%0Aimport org.lastaflute.core.util.ContainerUtil;%0Aimport org.lastaflute.db.dbflute.accesscontext.PreparedAccessContext;%0Aimport org.lastaflute.db.jta.stage.TransactionStage
 ;%0Aim
@@ -2263,24 +2263,78 @@
 nManager();%0A
+        whiteboxtest_prepareAccessContextForInsert();%0A
     %7D%0A%0A    /
@@ -3813,11 +3813,1317 @@
 );%0A    %7D
+%0A%0A    protected void whiteboxtest_prepareAccessContextForInsert() %7B%0A        AsyncManager asyncManager = ContainerUtil.getComponent(AsyncManager.class);%0A        TransactionStage stage = ContainerUtil.getComponent(TransactionStage.class);%0A        PurchasePaymentBhv purchasePaymentBhv = ContainerUtil.getComponent(PurchasePaymentBhv.class);%0A        TimeManager timeManager = ContainerUtil.getComponent(TimeManager.class);%0A        AccessContext accessContext = new AccessContext();%0A        accessContext.setAccessLocalDateTimeProvider(() -%3E timeManager.currentDateTime());%0A        accessContext.setAccessUser(%22whiteboxtest%22);%0A        PreparedAccessContext.setAccessContextOnThread(accessContext);%0A        try %7B%0A            asyncManager.async(() -%3E %7B%0A                stage.requiresNew(tx -%3E %7B%0A                    PurchasePayment payment = new PurchasePayment();%0A                    payment.setPurchaseId(1L);%0A                    payment.setPaymentMethodCode_BankTransfer();%0A                    payment.setPaymentDatetime(timeManager.currentDateTime());%0A                    payment.setPaymentAmount(new BigDecimal(88));%0A                    purchasePaymentBhv.insert(payment);%0A                %7D);%0A            %7D);%0A        %7D finally %7B%0A            PreparedAccessContext.clearAccessContextOnThread();%0A        %7D%0A    %7D
 %0A%7D%0A

make test for whiteboxtest_prepareAccessContextForInsert()