source: extensions/PiwigoLib/PiwigoLib/Service/PwgCategoriesService.cs @ 11935

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

root category handled

File size: 16.9 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 PwgCategoriesService : IPwgCategoriesService
15    {
16        /// <summary>
17        /// Get the list of category the user is connected can access
18        /// </summary>
19        /// <param name="sortedByCounter"></param>
20        /// <returns></returns>
21        public List<PwgCategory> GetListOfCategory(Int32? CatgeroryId, Boolean? Recursive, Boolean? PublicOnly)
22        {
23            List<PwgCategory> returnValue = new List<PwgCategory>();
24
25            try
26            {
27                PwgCategoriesProxyResponse response = PwgCategoriesProxy.pwg_categories_getList(CatgeroryId, Recursive, PublicOnly);
28
29                if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok)
30                {
31                    if (response.Erreur != null)
32                    {
33                        throw new PwgServiceException("GetListOfCategory, the server has return the error.",
34                            response.Erreur.Code,
35                            response.Erreur.Message);
36                    }
37                    else
38                    {
39                        throw new PwgServiceException("GetListOfCategory : a error occurs during server process.");
40                    }
41                }
42                else
43                {
44                    returnValue = ConvertProxyResponseToDTO(response, CatgeroryId);
45                }
46            }
47            catch (PwgProxyException ex)
48            {
49                throw new PwgServiceException("GetListOfCategory : a error is raised by proxy.", ex);
50            }
51            return returnValue;
52        }
53
54        /// <summary>
55        /// Get List of all Category, must be logged as admin
56        /// </summary>
57        /// <returns></returns>
58        public List<PwgCategory> GetAdminListOfCategory()
59        {
60            List<PwgCategory> returnValue = new List<PwgCategory>();
61
62            try
63            {
64                PwgCategoriesProxyResponse response = PwgCategoriesProxy.pwg_categories_getAdminList();
65
66                if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok)
67                {
68                    if (response.Erreur != null)
69                    {
70                        throw new PwgServiceException("GetAdminListOfCategory, the server has return the error.",
71                            response.Erreur.Code,
72                            response.Erreur.Message);
73                    }
74                    else
75                    {
76                        throw new PwgServiceException("GetAdminListOfCategory : a error occurs during server process.");
77                    }
78                }
79                else
80                {
81                    returnValue = ConvertProxyResponseToDTO(response, null);
82                }
83            }
84            catch (PwgProxyException ex)
85            {
86                throw new PwgServiceException("GetAdminListOfCategory : a error is raised by proxy.", ex);
87            }
88            return returnValue;
89        }
90
91        /// <summary>
92        /// Add a category to the gallery
93        /// </summary>
94        /// <param name="categoryName"></param>
95        /// <param name="upperCatId"></param>
96        /// <param name="newId"> Id used by the new category</param>
97        /// <param name="messageInfo">Message returned by the server</param>
98        /// <returns>false, if a error occurs</returns>
99        public Boolean AddCategory(String categoryName, Int32? upperCatId, ref Int32 newId, ref String messageInfo)
100        {
101            Boolean returnValue = false;
102
103            try
104            {
105                PwgAddRequestProxyResponse response = PwgCategoriesProxy.pwg_categories_add(categoryName, upperCatId);
106
107                if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok)
108                {
109                    if (response.Erreur != null)
110                    {
111                        throw new PwgServiceException("AddCategory, the server has return the error.",
112                            response.Erreur.Code,
113                            response.Erreur.Message);
114                    }
115                    else
116                    {
117                        throw new PwgServiceException("AddCategory : a error occurs during server process.");
118                    }
119                }
120                else
121                {
122                    returnValue = true;
123                    newId = response.NewId;
124                    messageInfo = response.Message;
125                }
126            }
127            catch (PwgProxyException ex)
128            {
129                throw new PwgServiceException("AddCategory : a error is raised by proxy.", ex);
130            }
131            return returnValue;
132        }
133
134
135        /// <summary>
136        /// Update category data
137        /// </summary>
138        /// <param name="IdCat"></param>
139        /// <param name="categoryName"></param>
140        /// <param name="categoryComment"></param>
141        /// <returns></returns>
142        public Boolean UpdateCategoryInfo(Int32 IdCat, String categoryName, String categoryComment)
143        {
144            Boolean returnValue = false;
145
146            try
147            {
148                PwgBaseProxyReponse response = PwgCategoriesProxy.pwg_categories_setInfo(IdCat, categoryName, categoryComment);
149
150                if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok)
151                {
152                    if (response.Erreur != null)
153                    {
154                        throw new PwgServiceException("UpdateCategoryInfo, the server has return the error.",
155                            response.Erreur.Code,
156                            response.Erreur.Message);
157                    }
158                    else
159                    {
160                        throw new PwgServiceException("UpdateCategoryInfo : a error occurs during server process.");
161                    }
162                }
163                else
164                {
165                    returnValue = true;
166                }
167            }
168            catch (PwgProxyException ex)
169            {
170                throw new PwgServiceException("UpdateCategoryInfo : a error is raised by proxy.", ex);
171            }
172            return returnValue;
173        }
174
175        /// <summary>
176        /// return the list of PwgImage in the category
177        /// </summary>
178        /// <param name="categoryId"></param>
179        /// <param name="recursive"></param>
180        /// <param name="perPage"></param>
181        /// <param name="Page"></param>
182        /// <param name="Order"></param>
183        /// <param name="minRate"></param>
184        /// <param name="maxRate"></param>
185        /// <param name="minHit"></param>
186        /// <param name="maxHit"></param>
187        /// <param name="minDateAvailable"></param>
188        /// <param name="maxDateAvailable"></param>
189        /// <param name="minDateCreated"></param>
190        /// <param name="maxDateCreated"></param>
191        /// <param name="minRatio"></param>
192        /// <param name="maxRatio"></param>
193        /// <param name="withThumbnail"></param>
194        /// <param name="PageReturned"></param>
195        /// <param name="PerPageeReturned"></param>
196        /// <param name="CountReturned"></param>
197        /// <returns></returns>
198        public List<PwgImage> GetListOfImagesFormCategory(Int32 categoryId,
199                                                    Boolean? recursive,
200                                                    Int32? perPage,
201                                                    Int32? Page,
202                                                    Int32? Order,
203                                                    Int32? minRate,
204                                                    Int32? maxRate,
205                                                    Int32? minHit,
206                                                    Int32? maxHit,
207                                                    DateTime? minDateAvailable,
208                                                    DateTime? maxDateAvailable,
209                                                    DateTime? minDateCreated,
210                                                    DateTime? maxDateCreated,
211                                                    Int32? minRatio,
212                                                    Int32? maxRatio,
213                                                    Boolean? withThumbnail,
214                                                    ref Int32 PageReturned,
215                                                    ref Int32 PerPageeReturned,
216                                                    ref Int32 CountReturned)
217        {
218            List<PwgImage> returnValue = new List<PwgImage>();
219
220            try
221            {
222                PwgImagesProxyResponse response = PwgCategoriesProxy.pwg_categories_getImages(categoryId,
223                                                            recursive,
224                                                            perPage,
225                                                            Page,
226                                                            Order,
227                                                            minRate,
228                                                            maxRate,
229                                                            minHit,
230                                                            maxHit,
231                                                            minDateAvailable,
232                                                            maxDateAvailable,
233                                                            minDateCreated,
234                                                            maxDateCreated,
235                                                            minRatio,
236                                                            maxRatio,
237                                                            withThumbnail);
238
239                if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok)
240                {
241                    if (response.Erreur != null)
242                    {
243                        throw new PwgServiceException("GetListOfImagesFormCategory, the server has return the error.",
244                            response.Erreur.Code,
245                            response.Erreur.Message);
246                    }
247                    else
248                    {
249                        throw new PwgServiceException("GetListOfImagesFormCategory : a error occurs during server process.");
250                    }
251                }
252                else
253                {
254                    PwgImagesService.ConvertProxyResponseToDTO(response, returnValue,
255                                                            ref PageReturned,
256                                                            ref PerPageeReturned,
257                                                            ref CountReturned);
258                }
259            }
260            catch (PwgProxyException ex)
261            {
262                throw new PwgServiceException("GetListOfImagesFormCategory : a error is raised by proxy.", ex);
263            }
264            return returnValue;
265        }
266        /// <summary>
267        /// Delete a category
268        /// </summary>
269        /// <param name="categoryId"></param>
270        /// <param name="SecurityToken"></param>
271        /// <param name="PhotoDeletionMode"></param>
272        /// <returns></returns>
273        public Boolean DeleteCategory(Int32 categoryId, String SecurityToken, PwgCategoryPhotoDeletionModeEnum PhotoDeletionMode)
274        {
275            Boolean returnValue = false;
276
277            try
278            {
279                if (PhotoDeletionMode == PwgCategoryPhotoDeletionModeEnum.Unknow)
280                {
281                    PhotoDeletionMode = PwgCategoryPhotoDeletionModeEnum.OrphansOnly;
282                }
283
284                PwgBaseProxyReponse response = PwgCategoriesProxy.pwg_categories_delete(categoryId, SecurityToken, PhotoDeletionMode);
285
286                if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok)
287                {
288                   if (response.Erreur != null)
289                    {
290                        throw new PwgServiceException("DeleteCategory, the server has return the error.",
291                            response.Erreur.Code,
292                            response.Erreur.Message);
293                    }
294                    else
295                    {
296                        throw new PwgServiceException("DeleteCategory : a error occurs during server process.");
297                    }
298                }
299                else
300                {
301                    returnValue = true;
302                }
303            }
304            catch (PwgProxyException ex)
305            {
306                throw new PwgServiceException("DeleteCategory : a error is raised by proxy.", ex);
307            }
308            return returnValue;
309        }
310
311        public Boolean MoveCategory(List<Int32> categoriesId, Int32 ParentCategoryId, String SecurityToken)
312        {
313            Boolean returnValue = false;
314
315            try
316            {
317                PwgBaseProxyReponse response = PwgCategoriesProxy.pwg_categories_move(categoriesId, ParentCategoryId, SecurityToken);
318
319                if (response.Retour != PwgBaseProxyReponseRetourEnum.Ok)
320                {
321                    if (response.Erreur != null)
322                    {
323                        throw new PwgServiceException("DeleteCategory, the server has return the error.",
324                            response.Erreur.Code,
325                            response.Erreur.Message);
326                    }
327                    else
328                    {
329                        throw new PwgServiceException("DeleteCategory : a error occurs during server process.");
330                    }
331                }
332                else
333                {
334                    returnValue = true;
335                }
336            }
337            catch (PwgProxyException ex)
338            {
339                throw new PwgServiceException("DeleteCategory : a error is raised by proxy.", ex);
340            }
341            return returnValue;
342        }
343        /// <summary>
344        /// convert response to dto object
345        /// </summary>
346        /// <param name="response"></param>
347        /// <param name="session"></param>
348        internal static List<PwgCategory> ConvertProxyResponseToDTO(PwgCategoriesProxyResponse response, Int32? CatgeroryId)
349        {
350            List<PwgCategory> returnValue = new List<PwgCategory>();
351
352            foreach (PwgCategoryProxyResponse respCat in response.Categories)
353            {
354                if ((!CatgeroryId.HasValue) || (CatgeroryId.Value != respCat.Id))
355                {
356                    returnValue.Add(ConvertProxyResponseToDTO(respCat));
357                }
358            }
359
360            return returnValue;
361        }
362
363
364        /// <summary>
365        /// convert response to dto object
366        /// </summary>
367        /// <param name="response"></param>
368        /// <param name="session"></param>
369        internal static PwgCategory ConvertProxyResponseToDTO(PwgCategoryProxyResponse response)
370        {
371            PwgCategory returnValue = new PwgCategory();
372
373            try
374            {
375                returnValue.Id = response.Id;
376                returnValue.Name = response.Name;
377
378                returnValue.DeepImagesCount = response.DeepImagesCount;
379                returnValue.ImagesCount = response.ImagesCount;
380                returnValue.SubCategoriesCount = response.SubCategoriesCount;
381
382                returnValue.UpperCategoriesId = new List<Int32>();
383                if (response.UpperCategoryId != null)
384                {
385                    foreach (String StringId in response.UpperCategoryId.Split(','))
386                    {
387                        returnValue.UpperCategoriesId.Add(Int32.Parse(StringId));
388                    }
389                }
390
391                if (response.LastDate != null)
392                {
393                    DateTime tryDate;
394                    if (DateTime.TryParse(response.LastDate, out tryDate))
395                    {
396                        returnValue.LastDate = tryDate;
397                    }
398                }
399
400                if (String.IsNullOrEmpty(response.Url))
401                {
402                    returnValue.Url = null;
403                }
404                else
405                {
406                    returnValue.Url = (new UriBuilder(response.Url)).Uri;
407                }
408            }
409            catch (Exception ex)
410            {
411                throw new PwgServiceException("ConvertProxyResponseToDTO : a error is raised when converting PwgTagProxyResponse.", ex);
412            }
413
414            return returnValue;
415        }
416    }
417}
Note: See TracBrowser for help on using the repository browser.