1 | package fr.mael.jiwigo.service; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.util.List; |
---|
5 | |
---|
6 | import fr.mael.jiwigo.dao.CategoryDao; |
---|
7 | import fr.mael.jiwigo.om.Category; |
---|
8 | import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; |
---|
9 | import fr.mael.jiwigo.transverse.session.SessionManager; |
---|
10 | |
---|
11 | /* |
---|
12 | * jiwigo-ws-api Piwigo webservice access Api |
---|
13 | * Copyright (c) 2010-2011 Mael mael@le-guevel.com |
---|
14 | * All Rights Reserved |
---|
15 | * |
---|
16 | * This library is free software. It comes without any warranty, to |
---|
17 | * the extent permitted by applicable law. You can redistribute it |
---|
18 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
19 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
20 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
21 | */ |
---|
22 | /** |
---|
23 | |
---|
24 | * @author mael |
---|
25 | * |
---|
26 | */ |
---|
27 | public class CategoryService { |
---|
28 | /** |
---|
29 | * Logger |
---|
30 | */ |
---|
31 | public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory |
---|
32 | .getLog(CategoryService.class); |
---|
33 | |
---|
34 | /** |
---|
35 | * singleton |
---|
36 | */ |
---|
37 | private static CategoryService instance; |
---|
38 | |
---|
39 | private SessionManager sessionManager; |
---|
40 | |
---|
41 | /** |
---|
42 | * @return the singleton |
---|
43 | */ |
---|
44 | public static CategoryService getInstance(SessionManager sessionManager) { |
---|
45 | if (instance == null) { |
---|
46 | instance = new CategoryService(sessionManager); |
---|
47 | } |
---|
48 | return instance; |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * private constructor to use a singleton |
---|
53 | */ |
---|
54 | private CategoryService(SessionManager sessionManager) { |
---|
55 | this.sessionManager = sessionManager; |
---|
56 | |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * Lists all categories |
---|
61 | * @param recursive true : recursive listing |
---|
62 | * @return the list of categories |
---|
63 | * @throws IOException |
---|
64 | * @throws ProxyAuthenticationException |
---|
65 | */ |
---|
66 | public List<Category> list(boolean recursive) throws IOException, ProxyAuthenticationException { |
---|
67 | return CategoryDao.getInstance(sessionManager).list(recursive); |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | * Allows to create the categories tree |
---|
72 | * @return list of categories |
---|
73 | * @throws IOException |
---|
74 | * @throws ProxyAuthenticationException |
---|
75 | */ |
---|
76 | public List<Category> makeTree() throws IOException, ProxyAuthenticationException { |
---|
77 | List<Category> list = CategoryDao.getInstance(sessionManager).list(true); |
---|
78 | for (Category category : list) { |
---|
79 | for (Category category2 : list) { |
---|
80 | if (category2.getIdParentCategories().size() != 1 |
---|
81 | && category.getIdentifier().equals( |
---|
82 | category2.getIdParentCategories().get(category2.getIdParentCategories().size() - 2))) { |
---|
83 | category.getChildCategories().add(category2); |
---|
84 | category2.getParentCategories().add(category); |
---|
85 | } |
---|
86 | |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | return list; |
---|
91 | |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | * creation of a category |
---|
96 | * @param nom name of the category |
---|
97 | * @param parent parent category |
---|
98 | * @return true if successful |
---|
99 | * @throws ProxyAuthenticationException |
---|
100 | */ |
---|
101 | public boolean create(String nom, Integer parent) throws ProxyAuthenticationException { |
---|
102 | Category category = new Category(); |
---|
103 | category.setDirectParent(parent); |
---|
104 | category.setName(nom); |
---|
105 | return CategoryDao.getInstance(sessionManager).create(category); |
---|
106 | } |
---|
107 | |
---|
108 | /** |
---|
109 | * creation of a category without parent |
---|
110 | * @param nom name of the category |
---|
111 | * @return true if successful |
---|
112 | * @throws ProxyAuthenticationException |
---|
113 | */ |
---|
114 | public boolean create(String nom) throws ProxyAuthenticationException { |
---|
115 | Category category = new Category(); |
---|
116 | category.setName(nom); |
---|
117 | return CategoryDao.getInstance(sessionManager).create(category); |
---|
118 | } |
---|
119 | } |
---|