/*
 * Copyright (C) 2003-2004 Red Hat Inc. All Rights Reserved.
 *
 * This library 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 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library 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 this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 */
package com.arsdigita.packaging;

import com.arsdigita.runtime.CCM;
import com.arsdigita.util.Classes;
import com.arsdigita.util.JavaPropertyWriter;
import com.arsdigita.util.parameter.ErrorList;
import com.arsdigita.util.parameter.ParameterContext;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
 * Config
 *
 * This class is <strong>not</strong> supported API.
 *
 * @author Rafael H. Schloming &lt;rhs@mit.edu&gt;
 * @version $Revision: #7 $ $Date: 2004/08/16 $
 **/

public final class Config extends ParameterMap {

    public final static String versionId = "$Id: Config.java 736 2005-09-01 10:46:05Z sskracic $ by $Author: sskracic $, $DateTime: 2004/08/16 18:10:38 $";

    private ConfigRegistry m_reg;

    public Config(ConfigRegistry reg) {
        m_reg = reg;

        for (Iterator it = m_reg.getContexts().iterator(); it.hasNext(); ) {
            Class klass = (Class) it.next();
            ParameterContext context =
                (ParameterContext) Classes.newInstance(klass);
            addContext(context);
        }
    }

    void load(ErrorList errs) {
        for (Iterator it = getContexts().iterator(); it.hasNext(); ) {
            ParameterContext ctx = (ParameterContext) it.next();
            m_reg.load(ctx, errs);
        }
    }

    public final boolean load(PrintStream out) {
        ErrorList errs = new ErrorList();
        load(errs);
        if (!errs.isEmpty()) {
            out.println(" *** Warning ***");
            errs.report(new OutputStreamWriter(out));
            return false;
        }

        return true;
    }

    public final void save() throws IOException {
        save(CCM.getConfigDirectory());
    }

    void save(File conf) throws IOException {
        if (!conf.exists()) {
            throw new IOException("no such directory: " + conf);
        }

        if (conf.isFile()) {
            throw new IOException("expecting directory: " + conf);
        }

        if (!conf.canWrite()) {
            throw new IOException("cannot write to directory: " + conf);
        }

        HashMap stores = new HashMap();
        for (Iterator it = getContexts().iterator(); it.hasNext(); ) {
            ParameterContext ctx = (ParameterContext) it.next();
            String storage = m_reg.getStorage(ctx.getClass());
            Properties props = (Properties) stores.get(storage);
            if (props == null) {
                props = new Properties();
                stores.put(storage, props);
            }

            ctx.save(new JavaPropertyWriter(props));
        }

        for (Iterator it = stores.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry me = (Map.Entry) it.next();
            String storage = (String) me.getKey();
            Properties props = (Properties) me.getValue();
            if (props.size() == 0) { continue; }
            File file = new File(conf, storage);
            File dir = file.getParentFile();
            dir.mkdirs();
            if (!(dir.exists() && dir.isDirectory())) {
                throw new IOException("unable to create directory: " + dir);
            }
            try {
                FileOutputStream out = new FileOutputStream(file);
                props.store
                    (out, " Generated by " + getClass().getName());
                out.close();
            } catch (IOException e) {
                throw new IOException
                    ("unable to save file: " + e.getMessage());
            }
        }
    }

}
