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

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

MAj des truc existant et debut du upload de fichier

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                    }
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            try
66            {
67                PwgSessionProxyResponse response = PwgSessionProxy.pwg_session_login(UserName, Password);
68
69                if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok )
70                {
71                    if (response.Erreur != null)
72                    {
73                        throw new PwgServiceException("Login, the server has return the error.", 
74                            response.Erreur.Code, 
75                            response.Erreur.Message);
76                    }
77                    else
78                    {
79                        throw new PwgServiceException("Login : a error occurs during server process.");
80                    }
81                }
82            }
83            catch (PwgProxyException ex)
84            {
85                throw new PwgServiceException("Login : a error is raised by proxy.", ex);
86            }
87
88            return GetPwgSession();
89        }
90
91        public PwgSession Logout()
92        {
93            PwgSession = null;
94            try
95            {
96                PwgSessionProxyResponse response = PwgSessionProxy.pwg_session_logout();
97
98                if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok )
99                {
100                    if (response.Erreur != null)
101                    {
102                        throw new PwgServiceException("Logout, the server has return the error.",
103                            response.Erreur.Code, 
104                            response.Erreur.Message);
105                    }
106                    else
107                    {
108                        throw new PwgServiceException("Logout : a error occurs during server process.");
109                    }
110                }
111            }
112            catch (PwgProxyException ex)
113            {
114                throw new PwgServiceException("Logout : a error is raised by proxy.", ex);
115            }
116
117            return GetPwgSession();
118        }
119        /// <summary>
120        /// private: convert response to dto object
121        /// </summary>
122        /// <param name="response"></param>
123        /// <param name="session"></param>
124        internal static void ConvertProxyResponseToDTO(PwgSessionProxyResponse response, ref PwgSession session)
125        {
126            if (session == null)
127            {
128                session = new PwgSession();
129            }
130
131            if (response != null)
132            {
133                session.CharSet = response.CharSet;
134                session.Language = response.Language;
135                session.Status = PwgEnumHelper<PwgSessionStatusEnum>.enumValueOf(response.Status);
136                session.Template = response.Template;
137                session.Theme = response.Theme;
138                session.UserName = response.UserName;
139                session.SecurityToken = response.PwgToken;
140            }
141
142        }
143    }
144}
Note: See TracBrowser for help on using the repository browser.