#region Disclaimer / License
// Copyright (C) 2010, Jackie Ng
// http://trac.osgeo.org/mapguide/wiki/maestro, jumpinjackie@gmail.com
//
// 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using OSGeo.MapGuide.MaestroAPI.Services;
using OSGeo.MapGuide.ObjectModels.Common;
using OSGeo.MapGuide.MaestroAPI.Schema;
using OSGeo.MapGuide.ObjectModels.Capabilities;
using OSGeo.MapGuide.ObjectModels.FeatureSource;
namespace OSGeo.MapGuide.MaestroAPI.Resource.Validation
{
///
/// Provides the means for the resource validation system to avoid validating
/// already validated resources
///
public class ResourceValidationContext
{
private Dictionary _validated;
private Dictionary _resources;
private Dictionary _schemas;
private Dictionary _spatialContexts;
private Dictionary _caps;
private IResourceService _resSvc;
private IFeatureService _featSvc;
///
/// Initializes a new instance of the class.
///
/// The res SVC.
/// The feat SVC.
public ResourceValidationContext(IResourceService resSvc, IFeatureService featSvc)
{
_resSvc = resSvc;
_featSvc = featSvc;
_validated = new Dictionary();
_resources = new Dictionary();
_schemas = new Dictionary();
_spatialContexts = new Dictionary();
_caps = new Dictionary();
}
///
/// Clears all cached items and validated resources
///
public void Reset()
{
_validated.Clear();
_resources.Clear();
_schemas.Clear();
_spatialContexts.Clear();
}
///
/// Gets the spatial contexts.
///
/// The resource id.
///
public FdoSpatialContextList GetSpatialContexts(string resourceId)
{
if (_spatialContexts.ContainsKey(resourceId))
{
Trace.TraceInformation("Fetching cached spatial contexts of: " + resourceId); //NOXLATE
return _spatialContexts[resourceId];
}
var scList = _featSvc.GetSpatialContextInfo(resourceId, false);
_spatialContexts[resourceId] = scList;
return scList;
}
///
/// Describes the feature source
///
/// The resource id.
///
public FeatureSourceDescription DescribeFeatureSource(string resourceId)
{
if (_schemas.ContainsKey(resourceId))
{
Trace.TraceInformation("Fetching cached schema of: " + resourceId); //NOXLATE
return _schemas[resourceId];
}
var desc = _featSvc.DescribeFeatureSource(resourceId);
_schemas[resourceId] = desc;
return desc;
}
///
/// Gets the resource.
///
/// The resource id.
///
public IResource GetResource(string resourceId)
{
if (_resources.ContainsKey(resourceId))
{
Trace.TraceInformation("Fetching cached copy of: " + resourceId); //NOXLATE
return _resources[resourceId];
}
var res = _resSvc.GetResource(resourceId);
_resources[resourceId] = res;
return res;
}
///
/// Determines whether the specified resource has already been validated
///
/// The resource id.
///
/// true if [the specified resource has already been validated]; otherwise, false.
///
public bool IsAlreadyValidated(string resourceId)
{
var res = _validated.ContainsKey(resourceId);
if (res)
Trace.TraceInformation("Skipping validation: " + resourceId); //NOXLATE
return res;
}
///
/// Marks the specified resource id as being validated.
///
/// The resource id.
public void MarkValidated(string resourceId)
{
_validated[resourceId] = resourceId;
Trace.TraceInformation("Validated: " + resourceId); //NOXLATE
}
///
/// Gets whether the specified resource exists
///
/// The resource id.
///
public bool ResourceExists(string resourceId)
{
return _resSvc.ResourceExists(resourceId);
}
}
}