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

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

Changes function name (to english)
version changed to 0.13.1

File size: 4.7 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.Tools;
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 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}
Note: See TracBrowser for help on using the repository browser.