1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Text; |
---|
4 | |
---|
5 | using Com.Piwigo.Lib.DTO; |
---|
6 | using Com.Piwigo.Lib.Proxy; |
---|
7 | using Com.Piwigo.Lib.Proxy.Response; |
---|
8 | using Com.Piwigo.Lib.DTO.Helper; |
---|
9 | |
---|
10 | namespace Com.Piwigo.Lib.Service |
---|
11 | { |
---|
12 | |
---|
13 | static public class PwgSessionService |
---|
14 | { |
---|
15 | static private PwgSession PwgSession = null; |
---|
16 | |
---|
17 | /// <summary> |
---|
18 | /// Return informations about user logged |
---|
19 | /// </summary> |
---|
20 | /// <returns></returns> |
---|
21 | static public PwgSession GetPwgSession() |
---|
22 | { |
---|
23 | if (PwgSession == null) |
---|
24 | { |
---|
25 | try |
---|
26 | { |
---|
27 | PwgSessionProxyResponse response = PwgSessionProxy.pwg_session_getStatus(); |
---|
28 | |
---|
29 | if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok) |
---|
30 | { |
---|
31 | if (response.Erreur != null) |
---|
32 | { |
---|
33 | throw new PwgServiceException("GetPwgSession, the server has return the error.", |
---|
34 | response.Erreur.Code, |
---|
35 | response.Erreur.Message); |
---|
36 | } |
---|
37 | else |
---|
38 | { |
---|
39 | throw new PwgServiceException("GetPwgSession : a error occurs during server process."); |
---|
40 | } |
---|
41 | } |
---|
42 | else |
---|
43 | { |
---|
44 | ConvertProxyResponseToDTO(response, ref PwgSession); |
---|
45 | } |
---|
46 | } catch (PwgProxyException ex) |
---|
47 | { |
---|
48 | throw new PwgServiceException("GetPwgSession : a error is raised by proxy.", ex); |
---|
49 | } |
---|
50 | |
---|
51 | } |
---|
52 | |
---|
53 | return PwgSession; |
---|
54 | } |
---|
55 | /// <summary> |
---|
56 | /// Log to the gallery using a account |
---|
57 | /// </summary> |
---|
58 | /// <param name="UserName"></param> |
---|
59 | /// <param name="Password"></param> |
---|
60 | /// <returns></returns> |
---|
61 | static public PwgSession Login(String UserName, String Password) |
---|
62 | { |
---|
63 | PwgSession = null; |
---|
64 | try |
---|
65 | { |
---|
66 | PwgSessionProxyResponse response = PwgSessionProxy.pwg_session_login(UserName, Password); |
---|
67 | |
---|
68 | if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok ) |
---|
69 | { |
---|
70 | if (response.Erreur != null) |
---|
71 | { |
---|
72 | throw new PwgServiceException("Login, the server has return the error.", |
---|
73 | response.Erreur.Code, |
---|
74 | response.Erreur.Message); |
---|
75 | } |
---|
76 | else |
---|
77 | { |
---|
78 | throw new PwgServiceException("Login : a error occurs during server process."); |
---|
79 | } |
---|
80 | } |
---|
81 | } |
---|
82 | catch (PwgProxyException ex) |
---|
83 | { |
---|
84 | throw new PwgServiceException("Login : a error is raised by proxy.", ex); |
---|
85 | } |
---|
86 | |
---|
87 | return GetPwgSession(); |
---|
88 | } |
---|
89 | |
---|
90 | static public PwgSession Logout() |
---|
91 | { |
---|
92 | PwgSession = null; |
---|
93 | try |
---|
94 | { |
---|
95 | PwgSessionProxyResponse response = PwgSessionProxy.pwg_session_logout(); |
---|
96 | |
---|
97 | if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok ) |
---|
98 | { |
---|
99 | if (response.Erreur != null) |
---|
100 | { |
---|
101 | throw new PwgServiceException("Logout, the server has return the error.", |
---|
102 | response.Erreur.Code, |
---|
103 | response.Erreur.Message); |
---|
104 | } |
---|
105 | else |
---|
106 | { |
---|
107 | throw new PwgServiceException("Logout : a error occurs during server process."); |
---|
108 | } |
---|
109 | } |
---|
110 | } |
---|
111 | catch (PwgProxyException ex) |
---|
112 | { |
---|
113 | throw new PwgServiceException("Logout : a error is raised by proxy.", ex); |
---|
114 | } |
---|
115 | |
---|
116 | return GetPwgSession(); |
---|
117 | } |
---|
118 | /// <summary> |
---|
119 | /// private: convert response to dto object |
---|
120 | /// </summary> |
---|
121 | /// <param name="response"></param> |
---|
122 | /// <param name="session"></param> |
---|
123 | static private void ConvertProxyResponseToDTO(PwgSessionProxyResponse response, ref PwgSession session) |
---|
124 | { |
---|
125 | if (session == null) |
---|
126 | { |
---|
127 | session = new PwgSession(); |
---|
128 | } |
---|
129 | |
---|
130 | if (response != null) |
---|
131 | { |
---|
132 | session.CharSet = response.CharSet; |
---|
133 | session.Language = response.Language; |
---|
134 | session.Status = PwgEnumHelper<PwgSessionStatusEnum>.enumValueOf(response.Status); |
---|
135 | session.Template = response.Template; |
---|
136 | session.Theme = response.Theme; |
---|
137 | session.UserName = response.UserName; |
---|
138 | } |
---|
139 | |
---|
140 | } |
---|
141 | } |
---|
142 | } |
---|