client/src/Iri.Modernisation.Data/Models/Loader.cs
author totetm <>
Tue, 19 Jan 2010 09:49:56 +0100
changeset 34 4d9ebc6fbbe8
parent 30 644e3cd48034
child 35 43bb1b8ed555
permissions -rw-r--r--
Web Migration Update
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
     1
using System;
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
     2
using System.Net;
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
     3
using System.Windows;
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
     4
using System.Windows.Controls;
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
     5
using System.Windows.Documents;
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
     6
using System.Windows.Ink;
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
     7
using System.Windows.Input;
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
     8
using System.Windows.Media;
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
     9
using System.Windows.Media.Animation;
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    10
using System.Windows.Shapes;
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    11
using System.Xml.Linq;
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    12
using System.IO;
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    13
using System.Collections.Generic;
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    14
using System.Threading;
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    15
namespace Iri.Modernisation.Data.Models
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    16
{
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    17
    /// <summary>
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    18
    /// Classe permettant de charger un XML et de convertir son contenu en un type ReturnType désiré
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    19
    /// </summary>
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    20
    /// <typeparam name="ReturnType">Type Désiré</typeparam>
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    21
    public class Loader<ResultType>
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    22
    {
34
4d9ebc6fbbe8 Web Migration Update
totetm <>
parents: 30
diff changeset
    23
4d9ebc6fbbe8 Web Migration Update
totetm <>
parents: 30
diff changeset
    24
        public String Path { get; private set; }
30
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    25
        /// <summary>
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    26
        /// WebClient qui permet de charger le XML distant
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    27
        /// </summary>
34
4d9ebc6fbbe8 Web Migration Update
totetm <>
parents: 30
diff changeset
    28
        public  WebClient xmlClient {get; private set;}
4d9ebc6fbbe8 Web Migration Update
totetm <>
parents: 30
diff changeset
    29
        /// <summary>
4d9ebc6fbbe8 Web Migration Update
totetm <>
parents: 30
diff changeset
    30
        /// Fonction permettant de transcrire le document XML en Type désiré
4d9ebc6fbbe8 Web Migration Update
totetm <>
parents: 30
diff changeset
    31
        /// </summary>
30
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    32
        private Func<XDocument, ResultType> ProcessFunction { get; set; }
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    33
      
34
4d9ebc6fbbe8 Web Migration Update
totetm <>
parents: 30
diff changeset
    34
        /// <summary>
4d9ebc6fbbe8 Web Migration Update
totetm <>
parents: 30
diff changeset
    35
        /// Constructeur
4d9ebc6fbbe8 Web Migration Update
totetm <>
parents: 30
diff changeset
    36
        /// </summary>
4d9ebc6fbbe8 Web Migration Update
totetm <>
parents: 30
diff changeset
    37
        /// <param name="processFunction">Fonction de transtypage</param>
30
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    38
       public Loader(Func<XDocument,ResultType> processFunction)
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    39
        {
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    40
            ProcessFunction = processFunction;
34
4d9ebc6fbbe8 Web Migration Update
totetm <>
parents: 30
diff changeset
    41
            xmlClient = new WebClient();
30
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    42
        }
34
4d9ebc6fbbe8 Web Migration Update
totetm <>
parents: 30
diff changeset
    43
4d9ebc6fbbe8 Web Migration Update
totetm <>
parents: 30
diff changeset
    44
        /// <summary>
4d9ebc6fbbe8 Web Migration Update
totetm <>
parents: 30
diff changeset
    45
        /// Téléchargement des données 
4d9ebc6fbbe8 Web Migration Update
totetm <>
parents: 30
diff changeset
    46
        /// </summary>
4d9ebc6fbbe8 Web Migration Update
totetm <>
parents: 30
diff changeset
    47
        /// <param name="path">Chemin du fichier</param>
4d9ebc6fbbe8 Web Migration Update
totetm <>
parents: 30
diff changeset
    48
        public  void Load(String path)
30
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    49
        {
34
4d9ebc6fbbe8 Web Migration Update
totetm <>
parents: 30
diff changeset
    50
            Path = path;
30
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    51
            xmlClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(xmlClient_DownloadStringCompleted);
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    52
            xmlClient.DownloadStringAsync(new Uri(path,UriKind.RelativeOrAbsolute));
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    53
       
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    54
        }
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    55
       
34
4d9ebc6fbbe8 Web Migration Update
totetm <>
parents: 30
diff changeset
    56
        /// <summary>
4d9ebc6fbbe8 Web Migration Update
totetm <>
parents: 30
diff changeset
    57
        /// Une fois le fichier télécharger, on commence le transtypage
4d9ebc6fbbe8 Web Migration Update
totetm <>
parents: 30
diff changeset
    58
        /// </summary>
4d9ebc6fbbe8 Web Migration Update
totetm <>
parents: 30
diff changeset
    59
        /// <param name="sender"></param>
4d9ebc6fbbe8 Web Migration Update
totetm <>
parents: 30
diff changeset
    60
        /// <param name="e"></param>
30
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    61
        void xmlClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    62
        {
34
4d9ebc6fbbe8 Web Migration Update
totetm <>
parents: 30
diff changeset
    63
            //On utilise la fonction donnée lors de la construction du Loader pour créer l'objet grâce au fichier XML
30
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    64
            ResultType result = ProcessFunction.Invoke(XDocument.Parse(e.Result));
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    65
            if (LoaderFinished != null)
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    66
            {
34
4d9ebc6fbbe8 Web Migration Update
totetm <>
parents: 30
diff changeset
    67
                //Une fois le Transtypage Fini, on lance l'évenement indiquant que le chargement est fini et donnant
4d9ebc6fbbe8 Web Migration Update
totetm <>
parents: 30
diff changeset
    68
                // en argument l'objet créé.
30
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    69
                LoaderFinished(this, new LoaderEventArgs<ResultType>(result));
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    70
            }
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    71
        }
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    72
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    73
        public event EventHandler<LoaderEventArgs<ResultType>> LoaderFinished;
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    74
      
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    75
       
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    76
    }
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    77
    public class LoaderEventArgs<ResultType> : EventArgs
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    78
    {
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    79
        public ResultType CreatedObject { get; private set; }
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    80
        public LoaderEventArgs(ResultType createdObject)
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    81
        {
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    82
            CreatedObject = createdObject;
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    83
        }
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    84
    }
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    85
    
644e3cd48034 Introduce in Web context
totetm <>
parents:
diff changeset
    86
}