/*
 * $Id$
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.struts2.tiles;

import org.apache.tiles.TilesApplicationContext;
import org.apache.tiles.TilesException;
import org.apache.tiles.context.TilesContextFactory;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.definition.DefinitionsFactory;
import org.apache.tiles.factory.TilesContainerFactory;
import org.apache.tiles.impl.BasicTilesContainer;
import org.apache.tiles.preparer.PreparerFactory;

import javax.servlet.jsp.PageContext;
import java.util.Map;


public class StrutsTilesContainerFactory extends TilesContainerFactory {

    /**
     * Initialize the container in a struts specific manner.
     * 
     * @param context
     * @param container
     * @throws TilesException
     */
    public void initializeContainer(Object context,
                                    BasicTilesContainer container)
            throws TilesException {

        Map<String, String> initParmMap =
               TilesContainerFactory.getInitParameterMap(context);

        TilesContextFactory contextFactory = (TilesContextFactory)
                TilesContainerFactory.createFactory(
                        initParmMap, TilesContainerFactory.CONTEXT_FACTORY_INIT_PARAM);

        contextFactory = new StrutsTilesContextFactory(contextFactory);

        DefinitionsFactory defsFactory = (DefinitionsFactory)
                TilesContainerFactory.createFactory(
                         initParmMap,
                         TilesContainerFactory.DEFINITIONS_FACTORY_INIT_PARAM);

        PreparerFactory prepFactory =
                (PreparerFactory) TilesContainerFactory.createFactory(
                        initParmMap,
                        TilesContainerFactory.PREPARER_FACTORY_INIT_PARAM);

        TilesApplicationContext tilesContext =
                contextFactory.createApplicationContext(context);

        container.setDefinitionsFactory(defsFactory);
        container.setContextFactory(contextFactory);
        container.setPreparerFactory(prepFactory);
        container.setApplicationContext(tilesContext);

        container.init(getInitParameterMap(context));

    }

    /**
     * Wrapper factory, used to decorate the TilesRequestContext with a
     * FreemarkerResult aware version.
     * 
     */
    class StrutsTilesContextFactory implements TilesContextFactory {

        private TilesContextFactory factory;

        public StrutsTilesContextFactory(TilesContextFactory factory) {
            this.factory = factory;
        }

        public void init(Map map) {
            factory.init(map);
        }

        public TilesApplicationContext createApplicationContext(Object context) {
            return factory.createApplicationContext(context);
        }

        public TilesRequestContext createRequestContext(TilesApplicationContext tilesApplicationContext, Object request, Object response) {
            TilesRequestContext context = factory.createRequestContext(tilesApplicationContext, request, response);
            return new StrutsTilesRequestContext(context);
        }

        public TilesRequestContext createRequestContext(TilesApplicationContext tilesApplicationContext, PageContext pageContext) {
            TilesRequestContext context = factory.createRequestContext(tilesApplicationContext, pageContext);
            return new StrutsTilesRequestContext(context);
        }
    }
}
