Common CustomDataSource
Common_CustomDataSource_CSharp\CustomDataSource_CSharp\TiledMapDataSource_CSharp\TileFunctionality.cs
// Copyright 2007 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 ESRI.ArcGIS.ADF.Web.DataSources;
using ESRI.ArcGIS.ADF.Web;

namespace TiledMapDataSource_CSharp
{
    public class TileFunctionality : ITileFunctionality
    {
        private string serviceUrl;

        public WebImageFormat ImageFormat
        {
            get { return WebImageFormat.JPG; }
        }
        
        TileCacheInfo _tileCacheInfo =  null;
        public TileFunctionality(string name, MapResource resource, TileCacheInfo tileCacheInfo)
        {
            this.name = name;
            this.resource = resource;
            _tileCacheInfo = tileCacheInfo;
        }

        #region ITileFunctionality Members

        public ESRI.ArcGIS.ADF.Web.MapImage Draw
            (string mapFunctionalityName, long column, long row, int level)
        {
            int realLevel = _tileCacheInfo.Levels[level];
            string baseUrl = _tileCacheInfo.TileUrls[realLevel];
            if (string.IsNullOrEmpty(_tileCacheInfo.TileUrlGeneratorAssembly)
                || string.IsNullOrEmpty(_tileCacheInfo.TileUrlGeneratorClass))
                return null;
            ITileUrlGenerator urlGen = getInstanceOfType
                (_tileCacheInfo.TileUrlGeneratorAssembly,
                _tileCacheInfo.TileUrlGeneratorClass) as ITileUrlGenerator;
            if (urlGen == null)
                return null;
           
            MapFunctionality mapFunc = GetMapFunctionality(mapFunctionalityName);
            if (mapFunc == null)
                throw new ArgumentException("mapFunctionalityName");

            serviceUrl = urlGen.GetTileUrl
                (column, row, realLevel, baseUrl, mapFunc.LayerVisibility);

            return new ESRI.ArcGIS.ADF.Web.MapImage(serviceUrl, null);
        }

        public string ServiceUrl
        {
            get { return serviceUrl; }
        }

        public string UrlGeneratorJSFunction
        {
            get { return null; }
        }

        #endregion

        #region IGISFunctionality Members
        private string name = string.Empty;
        IGISResource resource = null;
        System.Web.UI.WebControls.WebControl _webControl;

        public System.Web.UI.WebControls.WebControl WebControl
        {
            get { return _webControl; }
            set { _webControl = value; }
        }

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

        public IGISResource Resource
        {
            get
            {
                return this.resource;
            }
            set
            {
                this.resource = value;
            }
        }

        bool _initialized = false;

        public bool Initialized
        {
            get { return _initialized; }
        }
        
        public void LoadState()
        {
            
        }

        public void Initialize()
        {
            _initialized = true;
        }

        public void SaveState()
        {
            
        }

        public void Dispose()
        {
            _initialized = false;
        }

        public bool Supports(string operation)
        {
            return true;
        }

        #endregion

        #region private methods

        private MapFunctionality GetMapFunctionality(string name)
        {
            MapFunctionality mapFunctionality = resource.Functionalities.Find(name) as MapFunctionality;
            return mapFunctionality;
        }

        private object getInstanceOfType(string assemblyName, string type)
        {
            object o = null;
            try
            {
                System.Reflection.Assembly assembly = System.Reflection.Assembly.Load(assemblyName);
                o = assembly.CreateInstance(type);
            }
            catch (Exception e)
            {
                string mess = e.Message;
            }
            return o;
        }
        #endregion


    }
}