#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 ICSharpCode.Core;
using OSGeo.MapGuide.MaestroAPI;
using System.Drawing;
using OSGeo.MapGuide.MaestroAPI.Services;
namespace Maestro.Base.Services
{
using Res = Properties.Resources;
using Maestro.Base.Templates;
using System.IO;
using Maestro.Shared.UI;
///
/// An application-level service for creating new resources from a set of pre-defined templates
///
internal class NewItemTemplateService : ServiceBase
{
private Dictionary> _templates;
public override void Initialize()
{
base.Initialize();
_templates = new Dictionary>();
var tpls = AddInTree.BuildItems("/Maestro/NewItemTemplates", this); //NOXLATE
foreach (var tp in tpls)
{
if (!_templates.ContainsKey(tp.Category))
_templates[tp.Category] = new List();
_templates[tp.Category].Add(tp);
LoggingService.Info("Registered default template: " + tp.GetType()); //NOXLATE
}
LoggingService.Info("Initialized: New Item Template Service"); //NOXLATE
}
internal void InitUserTemplates()
{
if (!_templates.ContainsKey(Strings.TPL_CATEGORY_USERDEF))
_templates[Strings.TPL_CATEGORY_USERDEF] = new List();
UserItemTemplate[] utpls = ScanUserTemplates();
foreach (var ut in utpls)
{
_templates[Strings.TPL_CATEGORY_USERDEF].Add(ut);
LoggingService.Info("Adding user template: " + ut.TemplatePath);
}
}
private UserItemTemplate[] ScanUserTemplates()
{
List tpls = new List();
//TODO: Store path in preferences
string userTplPath = Path.Combine(FileUtility.ApplicationRootPath, "UserTemplates"); //NOXLATE
if (Directory.Exists(userTplPath))
{
foreach (string file in Directory.GetFiles(userTplPath))
{
try
{
var tpl = new UserItemTemplate(file);
tpls.Add(tpl);
}
catch (Exception)
{
LoggingService.Info("Could not load user template: " + file); //NOXLATE
}
}
}
return tpls.ToArray();
}
///
/// Gets an array of all template categories
///
///
public string[] GetCategories()
{
List values = new List(_templates.Keys);
return values.ToArray();
}
///
/// Represents a set of templates
///
public class TemplateSet
{
private Dictionary> _templates;
internal TemplateSet(IEnumerable templates)
{
_templates = new Dictionary>();
foreach (var tpl in templates)
{
if (!_templates.ContainsKey(tpl.Category))
_templates[tpl.Category] = new List();
_templates[tpl.Category].Add(tpl);
}
foreach (var key in _templates.Keys)
{
_templates[key].Sort();
}
}
///
/// Gets the categories for the template set
///
///
public IEnumerable GetCategories() { return _templates.Keys; }
///
/// Gets the templates for the specified category
///
///
///
public IEnumerable GetTemplatesForCategory(string category)
{
return _templates[category];
}
}
///
/// Gets the set of templates that matches the given categories and site version
///
/// An array of template categories
///
///
public TemplateSet GetItemTemplates(string[] categories, Version siteVersion)
{
List templates = new List();
foreach (var cat in categories)
{
if (_templates.ContainsKey(cat))
{
var matches = new List();
foreach (var tpl in _templates[cat])
{
if (siteVersion >= tpl.MinimumSiteVersion)
matches.Add(tpl);
}
templates.AddRange(matches);
}
}
return new TemplateSet(templates.ToArray());
}
}
}