|
30
|
1 |
using System; |
|
|
2 |
using System.Net; |
|
|
3 |
using System.Windows; |
|
|
4 |
using System.Windows.Controls; |
|
|
5 |
using System.Windows.Documents; |
|
|
6 |
using System.Windows.Ink; |
|
|
7 |
using System.Windows.Input; |
|
|
8 |
using System.Windows.Media; |
|
|
9 |
using System.Windows.Media.Animation; |
|
|
10 |
using System.Windows.Shapes; |
|
|
11 |
using System.Xml.Linq; |
|
|
12 |
using System.IO; |
|
|
13 |
using System.Collections.Generic; |
|
|
14 |
using System.Threading; |
|
|
15 |
namespace Iri.Modernisation.Data.Models |
|
|
16 |
{ |
|
36
|
17 |
/// <summary> |
|
|
18 |
/// Interface permettant une utilisation dans la classe Sync |
|
|
19 |
/// </summary> |
|
35
|
20 |
public interface ILoader |
|
|
21 |
{ |
|
36
|
22 |
|
|
|
23 |
event EventHandler<EventArgs> LoaderFinished; |
|
35
|
24 |
} |
|
|
25 |
|
|
30
|
26 |
/// <summary> |
|
|
27 |
/// Classe permettant de charger un XML et de convertir son contenu en un type ReturnType désiré |
|
|
28 |
/// </summary> |
|
|
29 |
/// <typeparam name="ReturnType">Type Désiré</typeparam> |
|
36
|
30 |
public class Loader<ResultType> : ILoader |
|
30
|
31 |
{ |
|
34
|
32 |
|
|
35
|
33 |
private AutoResetEvent Are { get; set; } |
|
|
34 |
|
|
|
35 |
public String Path { get; set; } |
|
30
|
36 |
/// <summary> |
|
|
37 |
/// WebClient qui permet de charger le XML distant |
|
|
38 |
/// </summary> |
|
34
|
39 |
public WebClient xmlClient {get; private set;} |
|
|
40 |
/// <summary> |
|
|
41 |
/// Fonction permettant de transcrire le document XML en Type désiré |
|
|
42 |
/// </summary> |
|
30
|
43 |
private Func<XDocument, ResultType> ProcessFunction { get; set; } |
|
|
44 |
|
|
34
|
45 |
/// <summary> |
|
|
46 |
/// Constructeur |
|
|
47 |
/// </summary> |
|
|
48 |
/// <param name="processFunction">Fonction de transtypage</param> |
|
30
|
49 |
public Loader(Func<XDocument,ResultType> processFunction) |
|
|
50 |
{ |
|
|
51 |
ProcessFunction = processFunction; |
|
34
|
52 |
xmlClient = new WebClient(); |
|
30
|
53 |
} |
|
34
|
54 |
|
|
|
55 |
/// <summary> |
|
|
56 |
/// Téléchargement des données |
|
|
57 |
/// </summary> |
|
|
58 |
/// <param name="path">Chemin du fichier</param> |
|
35
|
59 |
public void Load(String path) |
|
30
|
60 |
{ |
|
35
|
61 |
//Here it's working |
|
|
62 |
// Are.Set(); |
|
|
63 |
this.Path = path; |
|
30
|
64 |
xmlClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(xmlClient_DownloadStringCompleted); |
|
35
|
65 |
|
|
30
|
66 |
xmlClient.DownloadStringAsync(new Uri(path,UriKind.RelativeOrAbsolute)); |
|
35
|
67 |
|
|
|
68 |
|
|
|
69 |
//Here not |
|
|
70 |
//Are.Set(); |
|
|
71 |
|
|
|
72 |
|
|
30
|
73 |
} |
|
|
74 |
|
|
34
|
75 |
/// <summary> |
|
|
76 |
/// Une fois le fichier télécharger, on commence le transtypage |
|
|
77 |
/// </summary> |
|
|
78 |
/// <param name="sender"></param> |
|
|
79 |
/// <param name="e"></param> |
|
35
|
80 |
private void xmlClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) |
|
30
|
81 |
{ |
|
35
|
82 |
|
|
34
|
83 |
//On utilise la fonction donnée lors de la construction du Loader pour créer l'objet grâce au fichier XML |
|
30
|
84 |
ResultType result = ProcessFunction.Invoke(XDocument.Parse(e.Result)); |
|
|
85 |
if (LoaderFinished != null) |
|
|
86 |
{ |
|
34
|
87 |
//Une fois le Transtypage Fini, on lance l'évenement indiquant que le chargement est fini et donnant |
|
|
88 |
// en argument l'objet créé. |
|
35
|
89 |
|
|
30
|
90 |
LoaderFinished(this, new LoaderEventArgs<ResultType>(result)); |
|
35
|
91 |
|
|
|
92 |
|
|
30
|
93 |
} |
|
|
94 |
} |
|
35
|
95 |
public void Execute(Object sender) |
|
|
96 |
{ |
|
|
97 |
Are = (AutoResetEvent)sender; |
|
|
98 |
Load(this.Path); |
|
|
99 |
|
|
|
100 |
} |
|
36
|
101 |
public event EventHandler<EventArgs> LoaderFinished; |
|
30
|
102 |
|
|
|
103 |
|
|
|
104 |
} |
|
35
|
105 |
public class LoaderEventArgs<ResultType> : EventArgs |
|
30
|
106 |
{ |
|
|
107 |
public ResultType CreatedObject { get; private set; } |
|
35
|
108 |
public AutoResetEvent Are { get; private set; } |
|
30
|
109 |
public LoaderEventArgs(ResultType createdObject) |
|
|
110 |
{ |
|
|
111 |
CreatedObject = createdObject; |
|
35
|
112 |
Are = null; |
|
|
113 |
} |
|
|
114 |
public LoaderEventArgs(ResultType createdObject,AutoResetEvent are) |
|
|
115 |
{ |
|
|
116 |
CreatedObject = createdObject; |
|
|
117 |
Are = are; |
|
|
118 |
|
|
30
|
119 |
} |
|
|
120 |
} |
|
35
|
121 |
|
|
|
122 |
|
|
30
|
123 |
|
|
|
124 |
} |