source: extensions/PiwigoLib/PiwigoLib/Service/PwgSessionService.cs @ 11911

Last change on this file since 11911 was 11911, checked in by bayral, 13 years ago

WPF inprovement

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