/*
 * ApplicationElementContext.java  $Revision: 1.2 $  $Date: 2007/10/03 04:39:39 $
 *
 * Copyright (C) 2007 Meldcraft Corporation
 *
 * This file is part of Scaffold.
 * 
 * Scaffold is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Scaffold is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with Scaffold.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.meldcraft.application;

import java.util.HashMap;
import java.util.Map;

import com.meldcraft.util.PropertiesHelper;

/**
 * @version $Revision: 1.2 $ $Date: 2007/10/03 04:39:39 $
 * @author Rob Richards
 */
public class ApplicationElementContext
{
    private ApplicationContext  mApplicationContext;
    private String              mElementId;
    private Map                 mDefaultConfiguration;
    private Map                 mOverrideConfiguration;
    private Map                 mConfiguration;

    public ApplicationElementContext(ApplicationContext context, String elementId)
    {
        this(context, elementId, null);
    }

    public ApplicationElementContext(ApplicationContext context, String elementId,
                                     Map defaultConfig)
    {
        this(context, elementId, defaultConfig, null);
    }

    public ApplicationElementContext(ApplicationContext context, String elementId,
                                     Map defaultConfig, Map overrideConfig)
    {
        mApplicationContext = context;
        mElementId = elementId;
        mDefaultConfiguration = (defaultConfig == null) ? null : new HashMap(defaultConfig);
        mOverrideConfiguration = (overrideConfig == null) ? null : new HashMap(overrideConfig);
        mConfiguration = assembleConfig();
    }

    public ApplicationContext getApplicationContext()
    {
        return mApplicationContext;
    }

    public Map getConfiguration()
    {
        return mConfiguration;
    }

    public Map getDefaultConfiguration()
    {
        return mDefaultConfiguration;
    }

    public String getElementId()
    {
        return mElementId;
    }

    public Map getOverrideConfiguration()
    {
        return mOverrideConfiguration;
    }

    protected Map assembleConfig()
    {
        Map config = new HashMap();

        if (mDefaultConfiguration != null)
        {
            config.putAll(mDefaultConfiguration);
        }

        Map aconfig = mApplicationContext.getConfiguration();
        if (aconfig != null)
        {
            if (mElementId == null)
            {
                config.putAll(aconfig);
            }
            else
            {
                config.putAll(PropertiesHelper.getSubset(aconfig, mElementId + "."));
            }
        }

        if (mOverrideConfiguration != null)
        {
            config.putAll(mOverrideConfiguration);
        }

        return config;
    }
}



