source: extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/dao/CategoryDao.java @ 9387

Last change on this file since 9387 was 9387, checked in by mlg, 13 years ago

First commit for jiwigo-ws-api

File size: 4.8 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.om.Category;
11import fr.mael.jiwigo.transverse.enumeration.CategoryEnum;
12import fr.mael.jiwigo.transverse.enumeration.MethodsEnum;
13import fr.mael.jiwigo.transverse.session.SessionManager;
14import fr.mael.jiwigo.transverse.util.Outil;
15
16/*
17Copyright (c) 2010, Mael
18All rights reserved.
19
20Redistribution and use in source and binary forms, with or without
21modification, 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
31THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
32ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
33WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY
35DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
38ON 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
40SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41*/
42/**
43
44 * Dao for the categories
45 * @author mael
46 *
47 */
48public 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 rafraichir true : uses the categories of the cache
81     * @param recursive true : recursive listing of the categories
82     * @return the list of categories
83     * @throws IOException
84     */
85    public List<Category> lister(boolean recursive) throws IOException {
86        Document doc = sessionManager.executerReturnDocument(MethodsEnum.LISTER_CATEGORIES.getLabel(), "recursive",
87                String.valueOf(recursive));
88        Element element = doc.getRootElement().getChild("categories");
89        List<Element> listElement = (List<Element>) element.getChildren("category");
90        ArrayList<Category> categories = new ArrayList<Category>();
91        for (Element cat : listElement) {
92            Category myCat = new Category();
93            myCat.setIdentifiant(Integer.valueOf(cat.getAttributeValue(CategoryEnum.ID.getLabel())));
94            myCat.setUrlCategory(cat.getAttributeValue(CategoryEnum.URL.getLabel()));
95            myCat.setNbImages(Integer.valueOf(cat.getAttributeValue(CategoryEnum.NB_IMAGES.getLabel())));
96            myCat.setNbTotalImages(Integer.valueOf(cat.getAttributeValue(CategoryEnum.NB_TOTAL_IMAGES.getLabel())));
97            myCat.setNom(cat.getChildText(CategoryEnum.NAME.getLabel()));
98            String catMeres = cat.getChildText(CategoryEnum.CAT_MERES.getLabel());
99            ArrayList<Integer> idCategoriesMeres = new ArrayList<Integer>();
100            for (String catA : catMeres.split(",")) {
101                if (!catA.equals("")) {
102                    idCategoriesMeres.add(Integer.valueOf(catA));
103                }
104            }
105            myCat.setIdCategoriesMeres(idCategoriesMeres);
106            categories.add(myCat);
107        }
108        return categories;
109    }
110
111    /**
112     * Creation of a category
113     * @param category the category to create
114     * @return true if the category is successfully created
115     */
116    public boolean creer(Category category) {
117        try {
118            if (category.getParentDirect() != null) {
119                return Outil.checkOk(sessionManager.executerReturnDocument(MethodsEnum.AJOUTER_CATEGORIE.getLabel(),
120                        "name", category.getNom(), "parent", String.valueOf(category.getParentDirect())));
121            } else {
122                return Outil.checkOk(sessionManager.executerReturnDocument(MethodsEnum.AJOUTER_CATEGORIE.getLabel(),
123                        "name", category.getNom()));
124            }
125        } catch (IOException e) {
126            LOG.error(Outil.getStackTrace(e));
127        }
128        return false;
129    }
130
131}
Note: See TracBrowser for help on using the repository browser.