Common Custom data source
Common_CustomDataSource_CSharp\TiledMapDataSource_CSharp\MapResource.cs
// Copyright 2010 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See the use restrictions.
// 


using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Collections;
using ESRI.ArcGIS.ADF.Web.DataSources;
using ESRI.ArcGIS.ADF.Web.Display.Graphics;

namespace TiledMapDataSource_CSharp
{
    public class MapResource : IMapResource
    {
        public MapResource() { }
        public MapResource(string name, GISDataSource dataSource)
        {
            this.name = name;
            this.dataSource = dataSource;
        }

        #region IMapResource implementation

        private IMapInformation mapInformation = null;
        private ESRI.ArcGIS.ADF.Web.DisplaySettings displaySettings = null;

        public IMapInformation MapInformation
        {
            get { return mapInformation; }
            set { mapInformation = value; }
        }

        public ESRI.ArcGIS.ADF.Web.DisplaySettings DisplaySettings
        {
            get { return displaySettings; }
            set { displaySettings = value; }
        }

        #endregion

        #region IGISResource implementation

        bool initialized = false;
        private string name = string.Empty;
        private string resourceDefinition = string.Empty;
        private IGISDataSource dataSource = null;
        private GISFunctionalityCollection functionalities = new GISFunctionalityCollection();
        internal Dictionary<string, bool> layerVisibility = null;
        private int validationtimeout = 0;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public int ValidationTimeout
        {
            get { return validationtimeout; }
            set { validationtimeout = value; }
        }

        public string ResourceDefinition
        {
            get { return resourceDefinition; }
            set { resourceDefinition = value; }
        }

        public IGISDataSource DataSource
        {
            get { return dataSource; }
            set { dataSource = value; }
        }

        public GISFunctionalityCollection Functionalities
        {
            get { return functionalities; }
            set { functionalities = value; }
        }

        public bool SupportsFunctionality(System.Type functionalityType)
        {
            if (functionalityType == typeof(ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality)) return true;
            else if (functionalityType == typeof(ESRI.ArcGIS.ADF.Web.DataSources.ITileFunctionality)) return true;
            else if (functionalityType == typeof(ESRI.ArcGIS.ADF.Web.DataSources.IMapTocFunctionality)) return true;
            else return false;
        }

        public IGISFunctionality CreateFunctionality(System.Type functionalityType, string functionalityName)
        {
            IGISFunctionality func = null;
            if (functionalityType == typeof(ESRI.ArcGIS.ADF.Web.DataSources.IMapFunctionality))
            {
                func = new MapFunctionality(functionalityName, this);
            }
            else if (functionalityType == typeof(ESRI.ArcGIS.ADF.Web.DataSources.ITileFunctionality))
            {
                TileCacheInfo tileCacheInfo = mapInformation.TileCacheInfo as TileCacheInfo;
                func = new TileFunctionality(functionalityName, this, tileCacheInfo);
            }
            else if (functionalityType == typeof(ESRI.ArcGIS.ADF.Web.DataSources.IMapTocFunctionality))
            {
                TileCacheInfo tileCacheInfo = mapInformation.TileCacheInfo as TileCacheInfo;
                func = new MapTocFunctionality(functionalityName, this, tileCacheInfo);
            }
            else
            {
                throw new ArgumentException("functionalityType");
            }
            return func;
        }

        public bool Initialized
        {
            get { return initialized; }
        }

        public void LoadState()
        {
            if (dataSource == null) return;
            if (dataSource.State == null) return;

            object o = dataSource.State[key];
            if (o != null)
            {
                MapResource mr = o as MapResource;
                mapInformation = mr.mapInformation;
                layerVisibility = mr.layerVisibility;
            }
        }

        public void Initialize()
        {
            if (mapInformation == null)
            {
                mapInformation = new MapInformation(DataSource.DataSourceDefinition,
                    ResourceDefinition);            
            }

            if (layerVisibility == null)
            {
                TileCacheInfo tileCacheInfo = (TileCacheInfo) mapInformation.TileCacheInfo;
                layerVisibility = new Dictionary<string, bool>();
                if (tileCacheInfo.Layers != null)
                {
                    foreach (KeyValuePair<string, string> kvp in tileCacheInfo.Layers)
                        layerVisibility.Add(kvp.Key, true);
                }
            }
            initialized = true;
        }

        public void SaveState()
        {
            if (dataSource == null) return;
            if (dataSource.State == null) return;
            dataSource.State[key] = this;
        }

        public void Dispose()
        {
            initialized = false;
        }

        public void ClearState()
        {
            if (DataSource == null || DataSource.State == null) return;
            DataSource.State[key] = null;
        }

        #endregion

        #region private Key Properties
        internal string key
        {
            //Key should be unique for resource definition in case the resource definition changes at run time.
            get { return string.Format("{0}:{1}:{2}", this.GetType(), Name, ResourceDefinition); }
        }
        #endregion

    }
}