/*
 * Copyright (C) 2003-2004 Red Hat Inc. All Rights Reserved.
 *
 * The contents of this file are subject to the CCM Public
 * License (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.redhat.com/licenses/ccmpl.html.
 *
 * Software distributed under the License is distributed on an
 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
 * or implied. See the License for the specific language
 * governing rights and limitations under the License.
 *
 */
package com.arsdigita.core;

import com.arsdigita.db.DbHelper;
import com.arsdigita.domain.DomainObject;
import com.arsdigita.domain.DomainObjectInstantiator;
import com.arsdigita.kernel.ACSObjectInstantiator;
import com.arsdigita.loader.CoreLoader;
import com.arsdigita.persistence.DataObject;
import com.arsdigita.persistence.Session;
import com.arsdigita.persistence.SessionManager;
import com.arsdigita.persistence.TransactionContext;
import com.arsdigita.persistence.pdl.ManifestSource;
import com.arsdigita.persistence.pdl.NameFilter;
import com.arsdigita.runtime.CompoundInitializer;
import com.arsdigita.runtime.DomainInitEvent;
import com.arsdigita.runtime.LegacyInitEvent;
import com.arsdigita.runtime.LegacyInitializer;
import com.arsdigita.runtime.PDLInitializer;
import com.arsdigita.runtime.RuntimeConfig;
import com.arsdigita.ui.admin.Admin;
import com.arsdigita.ui.sitemap.SiteMap;
import com.arsdigita.util.xml.XML;
import com.arsdigita.web.Host;
import com.arsdigita.web.WebApp;
import com.arsdigita.web.ApplicationType;
import com.arsdigita.workflow.simple.TaskComment;
import com.arsdigita.search.converter.Converter;
import com.arsdigita.search.converter.ConverterRegistry;
import com.arsdigita.search.converter.PDFConverter;
import com.arsdigita.search.converter.ExcelConverter;
import com.arsdigita.search.converter.OOConverter;
import com.arsdigita.search.converter.WordConverter;
import com.arsdigita.search.converter.TextConverter;


/**
 * CoreInitializer
 *
 * @author Rafael H. Schloming &lt;rhs@mit.edu&gt;
 * @version $Revision: #14 $ $Date: 2004/06/11 $
 **/

public class Initializer extends CompoundInitializer {

    public final static String versionId = "$Id: //core-platform/dev/src/com/arsdigita/core/Initializer.java#14 $ by $Author: randyg $, $DateTime: 2004/06/11 11:26:47 $";

    public Initializer() {
        final String url = RuntimeConfig.getConfig().getJDBCURL();
        final int database = DbHelper.getDatabaseFromURL(url);

        add(new com.arsdigita.persistence.Initializer());

        add(new PDLInitializer
            (new ManifestSource
             ("ccm-core.pdl.mf",
              new NameFilter(DbHelper.getDatabaseSuffix(database), "pdl"))));

        add(new LegacyInitializer("com/arsdigita/core/enterprise.init"));
    }

    public final void init(final DomainInitEvent e) {
        super.init(e);

        e.getFactory().registerInstantiator
            (Host.BASE_DATA_OBJECT_TYPE,
             new DomainObjectInstantiator() {
                 public DomainObject doNewInstance(DataObject dobj) {
                     return new Host(dobj);
                 }
             });

        e.getFactory().registerInstantiator
            (ApplicationType.BASE_DATA_OBJECT_TYPE,
             new DomainObjectInstantiator() {
                 public DomainObject doNewInstance(DataObject dobj) {
                     return new ApplicationType(dobj);
                 }
             });

        e.getFactory().registerInstantiator
            (WebApp.BASE_DATA_OBJECT_TYPE,
             new DomainObjectInstantiator() {
                 public DomainObject doNewInstance(final DataObject data) {
                     return new WebApp(data);
                  }
             });

        e.getFactory().registerInstantiator
            (TaskComment.BASE_DATA_OBJECT_TYPE,
             new DomainObjectInstantiator() {
                 public DomainObject doNewInstance(final DataObject data) {
                     return new TaskComment(data);
                 }
             });

        e.getFactory().registerInstantiator
            (Admin.BASE_DATA_OBJECT_TYPE,
             new ACSObjectInstantiator() {
                 public DomainObject doNewInstance(final DataObject data) {
                     return new Admin(data);
                 }
             });

        e.getFactory().registerInstantiator
            (SiteMap.BASE_DATA_OBJECT_TYPE,
             new ACSObjectInstantiator() {
                 public DomainObject doNewInstance(final DataObject data) {
                     return new SiteMap(data);
                 }
             });

        // register the document converters
        Converter converter = new PDFConverter();
        ConverterRegistry.registerConverter(converter, 
                                            converter.getMimeTypes());

        converter = new ExcelConverter();
        ConverterRegistry.registerConverter(converter, 
                                            converter.getMimeTypes());

        converter = new WordConverter();
        ConverterRegistry.registerConverter(converter, 
                                            converter.getMimeTypes());

        converter = new OOConverter();
        ConverterRegistry.registerConverter(converter, 
                                            converter.getMimeTypes());

        converter = new TextConverter();
        ConverterRegistry.registerConverter(converter, 
                                            converter.getMimeTypes());
    }

    public final void init(final LegacyInitEvent e) {
        super.init(e);

        Session session = SessionManager.getSession();
        TransactionContext txn = session.getTransactionContext();
        txn.beginTxn();
        CoreLoader.loadHost();
        txn.commitTxn();

        XML.setupFactories();
    }
}
