source: extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/dao/CategoryDao.java @ 6962

Last change on this file since 6962 was 6962, checked in by mlg, 14 years ago

"Feature" :
Until now, it was not possible to create a "root" category. It is now possible.

File size: 3.9 KB
Line 
1package fr.mael.jiwigo.dao;
2
3import java.io.IOException;
4import java.util.ArrayList;
5import java.util.List;
6
7import org.jdom.Document;
8import org.jdom.Element;
9
10import fr.mael.jiwigo.Main;
11import fr.mael.jiwigo.om.Category;
12import fr.mael.jiwigo.transverse.enumeration.MethodsEnum;
13import fr.mael.jiwigo.transverse.util.Outil;
14
15/**
16   Copyright (c) 2010, Mael
17   All rights reserved.
18
19   Redistribution and use in source and binary forms, with or without
20   modification, are permitted provided that the following conditions are met:
21    * Redistributions of source code must retain the above copyright
22      notice, this list of conditions and the following disclaimer.
23    * Redistributions in binary form must reproduce the above copyright
24      notice, this list of conditions and the following disclaimer in the
25      documentation and/or other materials provided with the distribution.
26    * Neither the name of jiwigo nor the
27      names of its contributors may be used to endorse or promote products
28      derived from this software without specific prior written permission.
29
30   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
31   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33   DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY
34   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
37   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40   
41 * Dao des catégories
42 * @author mael
43 *
44 */
45public class CategoryDao {
46    /**
47     * Logger
48     */
49    public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory.getLog(Main.class);
50    /**
51     * Instance, afin d'utiliser un singleton
52     */
53    private static CategoryDao instance;
54
55    /**
56     * Constructeur privé, afin que seule l'instance soit accessible
57     */
58    private CategoryDao() {
59
60    }
61
62    /**
63     * @return l'instance
64     */
65    public static CategoryDao getInstance() {
66        if (instance == null) {
67            instance = new CategoryDao();
68        }
69        return instance;
70    }
71
72    /**
73     * Lister les catégorie
74     * @param rafraichir permet de dire si on utilise les catégories présentes dans le cache ou pas
75     * @param recursive listing récursif des catégories
76     * @return la liste des catégories
77     * @throws IOException
78     */
79    public List<Category> lister(boolean recursive) throws IOException {
80        Document doc = Main.sessionManager.executerReturnDocument(MethodsEnum.LISTER_CATEGORIES.getLabel(),
81                "recursive", String.valueOf(recursive));
82        Element element = doc.getRootElement().getChild("categories");
83        List<Element> listElement = (List<Element>) element.getChildren("category");
84        ArrayList<Category> categories = new ArrayList<Category>();
85        for (Element cat : listElement) {
86            Category myCat = new Category(cat);
87            categories.add(myCat);
88        }
89        return categories;
90    }
91
92    /**
93     * Création d'une catégorie
94     * @param category la catégorie à créer
95     * @return true si la catégorie a bien été créée
96     */
97    public boolean creer(Category category) {
98        try {
99            if (category.getParentDirect() != null) {
100                return Outil.checkOk(Main.sessionManager.executerReturnDocument(MethodsEnum.AJOUTER_CATEGORIE
101                        .getLabel(), "name", category.getNom(), "parent", String.valueOf(category.getParentDirect())));
102            } else {
103                return Outil.checkOk(Main.sessionManager.executerReturnDocument(MethodsEnum.AJOUTER_CATEGORIE
104                        .getLabel(), "name", category.getNom()));
105            }
106        } catch (IOException e) {
107            LOG.error(Outil.getStackTrace(e));
108        }
109        return false;
110    }
111
112}
Note: See TracBrowser for help on using the repository browser.