yavsc/SalesCatalog/XmlImplementation/XmlCatalogProvider.cs

94 lines
2.7 KiB
C#

10 years ago
using System;
using System.Xml.Serialization;
using System.Configuration;
using System.IO;
using System.Xml;
Refactoring: moving the Catalog manager and model into the Yavsc.Model.FrontOffice namespace * Web.config: * Catalog.xml: * MyClass.cs: * Note.cs: * Euro.cs: * Unit.cs: * Text.cs: * Link.cs: * Price.cs: * Label.cs: * Brand.cs: * Scalar.cs: * Option.cs: * Period.cs: * YavscModel.csproj: * Catalog.cs: * Service.cs: * Product.cs: * YavscClient.csproj: * CatalogManager.cs: * Currency.cs: * CheckBox.cs: * SaleForm.cs: * FormInput.cs: * CatalogProvider.cs: * TextInput.cs: * SelectItem.cs: * SalesCatalog.csproj: * FilesInput.cs: * FormElement.cs: * SelectInput.cs: * IValueProvider.cs: * StockStatus.cs: * RadioButton.cs: * Commande.cs: * ProductImage.cs: * WebCatalogExtensions.cs: * TemplateException.cs: * ProductCategory.cs: * PhysicalProduct.cs: * Note.cs: * Link.cs: * Text.cs: * Euro.cs: * Unit.cs: * WorkFlowManager.cs: * Brand.cs: * Label.cs: * Price.cs: * Scalar.cs: * FrontOfficeController.cs: * Period.cs: * Option.cs: * Product.cs: * Service.cs: * Catalog.cs: * SaleForm.cs: * Currency.cs: * CheckBox.cs: * TextInput.cs: * FrontOfficeApiController.cs: * FormInput.cs: * SelectItem.cs: * FilesInput.cs: * XmlCatalog.cs: * FormElement.cs: * SelectInput.cs: * RadioButton.cs: * StockStatus.cs: * ProductImage.cs: * CatalogHelper.cs: * CatalogManager.cs: * CatalogProvider.cs: * ProductCategory.cs: * PhysicalProduct.cs: * XmlCatalogProvider.cs: * CatalogProviderConfigurationElement.cs: * CatalogProvidersConfigurationSection.cs: * CatalogProvidersConfigurationCollection.cs: * CatalogProviderConfigurationElement.cs: * CatalogProvidersConfigurationSection.cs: * CatalogProvidersConfigurationCollection.cs: * CatalogHelper.cs:
10 years ago
using Yavsc.Model.FrontOffice;
10 years ago
namespace SalesCatalog.XmlImplementation
{
/// <summary>
/// Xml catalog provider.
/// In charge of getting the catalog data,
/// returning a Catalog object from GetCatalog
/// </summary>
10 years ago
public class XmlCatalogProvider: CatalogProvider
{
#region implemented abstract members of SalesCatalog.CatalogProvider
/// <summary>
/// Gets the catalog, loading it from
/// the file system at a first call,
/// and when its last write time has changed.
/// </summary>
/// <returns>The catalog.</returns>
10 years ago
public override Catalog GetCatalog ()
{
// Assert fileName != null
FileInfo fi = new FileInfo (fileName);
10 years ago
if (!fi.Exists)
throw new ConfigurationErrorsException(
string.Format("No catalog found ({0})",fileName));
10 years ago
if (fi.LastWriteTime > lastModification)
LoadCatalog ();
return catInstance;
}
/// <summary>
/// The catalog instance.
/// </summary>
10 years ago
protected XmlCatalog catInstance = null;
/// <summary>
/// The last modification date of the file loaded.
/// </summary>
10 years ago
protected DateTime lastModification = new DateTime(0);
/// <summary>
/// The name of the file loaded.
/// </summary>
10 years ago
protected string fileName = null;
#endregion
/// <summary>
/// Initialize the catalog
/// using the specified name and config.
/// </summary>
/// <param name="name">Name.</param>
/// <param name="config">Config.</param>
10 years ago
public override void Initialize (string name, System.Collections.Specialized.NameValueCollection config)
{
if (config ["connection"] == null)
throw new Exception ("the 'connection' parameter is null " +
"(it should be the absolute path to the xml catalog)");
string basedir = AppDomain.CurrentDomain.BaseDirectory;
// config ["connection"] starts with "~/"
fileName = Path.Combine(basedir, config ["connection"].Substring (2));
10 years ago
LoadCatalog ();
}
10 years ago
private void LoadCatalog ()
{
10 years ago
try {
10 years ago
FileInfo fi = new FileInfo (fileName);
10 years ago
if (!fi.Exists)
10 years ago
throw new Exception (
string.Format ("Le fichier Xml decrivant le catalogue n'existe pas ({0})", fi.FullName));
XmlSerializer xsr = new XmlSerializer (typeof(XmlCatalog),new Type[]{
typeof(Service),
typeof(PhysicalProduct),
typeof(Euro),
typeof(Text),
typeof(TextInput),
typeof(SelectInput)});
using (FileStream fs = fi.OpenRead()) {
catInstance = (XmlCatalog) xsr.Deserialize (fs);
}
fileName = fi.FullName;
lastModification = fi.LastWriteTime;
10 years ago
}
catch (Exception e) {
lastModification = new DateTime (0);
throw e;
}
10 years ago
}
}
}