/*
 Copyright (c) 2007 - 2008 Eric J. Feminella <eric@ericfeminella.com>
 All rights reserved.

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is furnished
 to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 @internal
 */

package com.ericfeminella.air.cairngorm.business
{
    import com.adobe.cairngorm.CairngormError;
    import com.adobe.cairngorm.CairngormMessageCodes;
    import com.adobe.cairngorm.business.ServiceLocator;
    import mx.core.Application;
    import mx.events.FlexEvent;

    /**
     *
     * <code>AIRServiceLocator</code> provides a mechanism from which AIR APIs,
     * specifically <code>SQLConnection</code>, <code>SQLStatement</code>) can
     * be located (referenced) from the Cairngorm <code>ServiceLocator</code>
     * as a Service via <code>SQLService</code>.
     *
     * <p>
     * <code>AIRServiceLocator</code> allows for Adobe AIR specific APIs to be
     * retrieved from a Cairngorm <code>ServiceLocator</code> implementation in the
     * same way one would access RPC Services such as <code>HTTPService</code>,
     * <code>WebService</code> etc. from the <code>ServiceLocator</code>
     * </p>
     *
     * @example The following example demonstrates a typical implementation of
     * <code>AIRServiceLocator</code>
     *
     * <listing version="3.0">
     *
     * &lt;?xml version="1.0" encoding="utf-8"?&gt;
     * &lt;services:AIRServiceLocator xmlns:mx = "http://www.adobe.com/2006/mxml"
     *                                xmlns:services = "com.ericfeminella.air.cairngorm.business.*\" &gt;
     *     &lt;mx:Script&gt;
     *         &lt;![CDATA[
     *             import mx.events.FlexEvent;
     *
     *             public static const RDBMS:String = "rdbms";
     *
     *             protected override function preinitialize(event:FlexEvent) : void
     *             {
     *              rdbms.open();
     *             }
     *         ]]&gt;
     *     &lt;/mx:Script&gt;
     *
     *  &lt;services:SQLService id = "rdbms"
     *                  localDatabaseFilePath = "{ File.desktopDirectory.nativePath }"
     *                  localDatabaseFileName = "air.cairngorm.example.db"
     *                  /&gt;
     *
     * &lt;/services:AIRServiceLocator&gt;
     *
     * </listing>
     *
     * @see com.adobe.cairngorm.business.ServiceLocator
     *
     */
    public class AIRServiceLocator extends ServiceLocator
    {
        /**
         *
         * Defines the Singleton instance of <code>AIRServiceLocator</code>
         *
         */
        protected static var instance:AIRServiceLocator;

        /**
         *
         * Defines the <code>SQLServices</code> instance which contains a reference
         * to all <code>SQLService</code> defined on a <code>AIRServiceLocator</code>
         * instance
         *
         */
        protected var services:SQLServices;

        /**
         *
         * <code>AIRServiceLocator</code> constructor Instantiates the Singleton
         * instance of <code>AIRServiceLocator</code> and adds an listener for the
         * <code>FlexEvent.PREINITIALIZE</code> event
         *
         * @throws <code>CairngormMessageCodes.SINGLETON_EXCEPTION</code>
         *
         */
        public function AIRServiceLocator()
        {
            if ( instance != null )
            {
                throw new CairngormError( CairngormMessageCodes.SINGLETON_EXCEPTION, "AIRServiceLocator" );
            }
            instance = this;
            Application( Application.application ).addEventListener( FlexEvent.PREINITIALIZE, preinitialize );

        }

        /**
         *
         * Retrieves the Singleton instance of the <code>AIRServiceLocator</code>
         *
         * @return singleton instance of <code>AIRServiceLocator</code>
         *
         */
        public static function getInstance() : AIRServiceLocator
        {
            if ( instance == null )
            {
                instance = new AIRServiceLocator();
            }
            return instance;
        }

        /**
         *
         * Retrieves a specific <code>SQLService</code> which has been defined
         * by the <code>AIRServiceLocator</code> instance
         *
         * @example The following example demonstrates how a <code>SQLService</code>
         * instance can be referenced via <code>AIRServiceLocator.getSQLService</code>
         *
         * <listing version="3.0">
         *
         * var service:SQLService;
         * service = AIRServiceLocator.getInstance().getSQLService( Services.SERVICE );
         *
         * </listing>
         *
         * @param  unique identifier of the <code>SQLService</code> instance
         * @return <code>SQLService</code> instance with the specified name
         *
         */
        public function getSQLService(name:String) : SQLService
        {
           if ( services == null )
           {
               services = new SQLServices();
               services.register( this );
           }
           return SQLService( services.getService( name ) );
        }

        /**
         *
         * Determines if the specified <code>SQLService</code> has been defined
         * on a <code>AIRServiceLocator</code> instance
         *
         * @param  unique identifier of the <code>SQLService</code> instance
         * @return true if the service exists, otherwise false
         *
         */
        public function hasSQLService(name:String) : Boolean
        {
            return getSQLService( name ) != null;
        }

        /**
         *
         * Abstract method which is invoked when <code>Application.preinitialize</code>
         * event is dispatched.
         *
         * <p>
         * This method is provided in order to allow clients to override in order to
         * initialize services at startup of the application.
         * </p>
         *
         * @param <code>FlexEvent.PREINITIALIZE</code>
         *
         */
        protected function preinitialize(event:FlexEvent) : void
        {
        }
    }
}