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