1 | package fr.mael.jiwigo.dao; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.util.ArrayList; |
---|
5 | import java.util.List; |
---|
6 | |
---|
7 | import org.jdom.Document; |
---|
8 | import org.jdom.Element; |
---|
9 | |
---|
10 | import fr.mael.jiwigo.om.Category; |
---|
11 | import fr.mael.jiwigo.transverse.enumeration.CategoryEnum; |
---|
12 | import fr.mael.jiwigo.transverse.enumeration.MethodsEnum; |
---|
13 | import fr.mael.jiwigo.transverse.session.SessionManager; |
---|
14 | import fr.mael.jiwigo.transverse.util.Tools; |
---|
15 | |
---|
16 | /* |
---|
17 | Copyright (c) 2010, Mael |
---|
18 | All rights reserved. |
---|
19 | |
---|
20 | Redistribution and use in source and binary forms, with or without |
---|
21 | modification, are permitted provided that the following conditions are met: |
---|
22 | * Redistributions of source code must retain the above copyright |
---|
23 | notice, this list of conditions and the following disclaimer. |
---|
24 | * Redistributions in binary form must reproduce the above copyright |
---|
25 | notice, this list of conditions and the following disclaimer in the |
---|
26 | documentation and/or other materials provided with the distribution. |
---|
27 | * Neither the name of jiwigo nor the |
---|
28 | names of its contributors may be used to endorse or promote products |
---|
29 | derived from this software without specific prior written permission. |
---|
30 | |
---|
31 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
---|
32 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
33 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
34 | DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY |
---|
35 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
36 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
37 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
---|
38 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
39 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
40 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
41 | */ |
---|
42 | /** |
---|
43 | |
---|
44 | * Dao for the categories |
---|
45 | * @author mael |
---|
46 | * |
---|
47 | */ |
---|
48 | public class CategoryDao { |
---|
49 | /** |
---|
50 | * Logger |
---|
51 | */ |
---|
52 | public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory |
---|
53 | .getLog(CategoryDao.class); |
---|
54 | /** |
---|
55 | * Instance to use a singleton |
---|
56 | */ |
---|
57 | private static CategoryDao instance; |
---|
58 | |
---|
59 | private SessionManager sessionManager; |
---|
60 | |
---|
61 | /** |
---|
62 | * Private constructor to use a singleton |
---|
63 | */ |
---|
64 | private CategoryDao(SessionManager sessionManager) { |
---|
65 | this.sessionManager = sessionManager; |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * @return the instance |
---|
70 | */ |
---|
71 | public static CategoryDao getInstance(SessionManager sessionManager) { |
---|
72 | if (instance == null) { |
---|
73 | instance = new CategoryDao(sessionManager); |
---|
74 | } |
---|
75 | return instance; |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * Lists the categories |
---|
80 | * @param recursive true : recursive listing of the categories |
---|
81 | * @return the list of categories |
---|
82 | * @throws IOException |
---|
83 | */ |
---|
84 | public List<Category> list(boolean recursive) throws IOException { |
---|
85 | Document doc = sessionManager.executeReturnDocument(MethodsEnum.LISTER_CATEGORIES.getLabel(), "recursive", |
---|
86 | String.valueOf(recursive)); |
---|
87 | Element element = doc.getRootElement().getChild("categories"); |
---|
88 | List<Element> listElement = (List<Element>) element.getChildren("category"); |
---|
89 | ArrayList<Category> categories = new ArrayList<Category>(); |
---|
90 | for (Element cat : listElement) { |
---|
91 | Category myCat = new Category(); |
---|
92 | myCat.setIdentifier(Integer.valueOf(cat.getAttributeValue(CategoryEnum.ID.getLabel()))); |
---|
93 | myCat.setUrlCategory(cat.getAttributeValue(CategoryEnum.URL.getLabel())); |
---|
94 | myCat.setNbImages(Integer.valueOf(cat.getAttributeValue(CategoryEnum.NB_IMAGES.getLabel()))); |
---|
95 | myCat.setNbTotalImages(Integer.valueOf(cat.getAttributeValue(CategoryEnum.NB_TOTAL_IMAGES.getLabel()))); |
---|
96 | myCat.setName(cat.getChildText(CategoryEnum.NAME.getLabel())); |
---|
97 | String catMeres = cat.getChildText(CategoryEnum.CAT_MERES.getLabel()); |
---|
98 | ArrayList<Integer> idCategoriesMeres = new ArrayList<Integer>(); |
---|
99 | for (String catA : catMeres.split(",")) { |
---|
100 | if (!catA.equals("")) { |
---|
101 | idCategoriesMeres.add(Integer.valueOf(catA)); |
---|
102 | } |
---|
103 | } |
---|
104 | myCat.setIdParentCategories(idCategoriesMeres); |
---|
105 | categories.add(myCat); |
---|
106 | } |
---|
107 | return categories; |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * Creation of a category |
---|
112 | * @param category the category to create |
---|
113 | * @return true if the category is successfully created |
---|
114 | */ |
---|
115 | public boolean create(Category category) { |
---|
116 | try { |
---|
117 | if (category.getDirectParent() != null) { |
---|
118 | return Tools.checkOk(sessionManager.executeReturnDocument(MethodsEnum.AJOUTER_CATEGORIE.getLabel(), |
---|
119 | "name", category.getName(), "parent", String.valueOf(category.getDirectParent()))); |
---|
120 | } else { |
---|
121 | return Tools.checkOk(sessionManager.executeReturnDocument(MethodsEnum.AJOUTER_CATEGORIE.getLabel(), |
---|
122 | "name", category.getName())); |
---|
123 | } |
---|
124 | } catch (IOException e) { |
---|
125 | LOG.error(Tools.getStackTrace(e)); |
---|
126 | } |
---|
127 | return false; |
---|
128 | } |
---|
129 | |
---|
130 | } |
---|