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

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

Async image thumbail retriving

File size: 5.3 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                    }
47                } catch (PwgProxyException ex)
48                {
49                    throw new PwgServiceException("GetPwgSession : a error is raised by proxy.", ex);
50                }
51
52            }
53
54            return PwgSession;
55        }
56        /// <summary>
57        /// Log to the gallery using a account
58        /// </summary>
59        /// <param name="UserName"></param>
60        /// <param name="Password"></param>
61        /// <returns></returns>
62        public PwgSession Login(String UserName, String Password)
63        {
64            PwgSession = null;
65            Boolean isConnected = false;
66            try
67            {
68                PwgSessionProxyResponse response = PwgSessionProxy.pwg_session_login(UserName, Password);
69
70                if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok)
71                {
72
73                    if (response.Erreur != null)
74                    {
75                        throw new PwgServiceException("Login, the server has return the error.",
76                            response.Erreur.Code,
77                            response.Erreur.Message);
78                    }
79                    else
80                    {
81                        throw new PwgServiceException("Login : a error occurs during server process.");
82                    }
83                }
84                else
85                {
86                    isConnected = true;
87                }
88            }
89            catch (PwgProxyException ex)
90            {
91                throw new PwgServiceException("Login : a error is raised by proxy.", ex);
92            }
93
94            PwgSession sess= GetPwgSession();
95            sess.IsConnected = isConnected;
96            return sess;
97        }
98
99        public PwgSession Logout()
100        {
101            PwgSession = null;
102            Boolean isConnected = false;
103            try
104            {
105                PwgSessionProxyResponse response = PwgSessionProxy.pwg_session_logout();
106
107                if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok )
108                {
109                    if (response.Erreur != null)
110                    {
111                        throw new PwgServiceException("Logout, the server has return the error.",
112                            response.Erreur.Code, 
113                            response.Erreur.Message);
114                    }
115                    else
116                    {
117                        throw new PwgServiceException("Logout : a error occurs during server process.");
118                    }
119                }
120            }
121            catch (PwgProxyException ex)
122            {
123                throw new PwgServiceException("Logout : a error is raised by proxy.", ex);
124            }
125
126            PwgSession sess = GetPwgSession();
127            sess.IsConnected = isConnected;
128            return sess;
129        }
130        /// <summary>
131        /// private: convert response to dto object
132        /// </summary>
133        /// <param name="response"></param>
134        /// <param name="session"></param>
135        internal static void ConvertProxyResponseToDTO(PwgSessionProxyResponse response, ref PwgSession session)
136        {
137            if (session == null)
138            {
139                session = new PwgSession();
140            }
141
142            if (response != null)
143            {
144                session.CharSet = response.CharSet;
145                session.Language = response.Language;
146                session.Status = PwgEnumHelper<PwgSessionStatusEnum>.enumValueOf(response.Status);
147                session.Template = response.Template;
148                session.Theme = response.Theme;
149                session.UserName = response.UserName;
150                session.SecurityToken = response.PwgToken;
151            }
152
153        }
154    }
155}
Note: See TracBrowser for help on using the repository browser.