source: extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/dao/impl/CategoryDaoImpl.java @ 10719

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

deletes useless comments

File size: 5.1 KB
Line 
1package fr.mael.jiwigo.dao.impl;
2
3import java.io.IOException;
4import java.util.ArrayList;
5import java.util.List;
6
7import org.slf4j.Logger;
8import org.slf4j.LoggerFactory;
9import org.w3c.dom.Document;
10import org.w3c.dom.Element;
11import org.w3c.dom.Node;
12import org.w3c.dom.NodeList;
13
14import fr.mael.jiwigo.dao.CategoryDao;
15import fr.mael.jiwigo.om.Category;
16import fr.mael.jiwigo.transverse.enumeration.CategoryEnum;
17import fr.mael.jiwigo.transverse.enumeration.MethodsEnum;
18import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException;
19import fr.mael.jiwigo.transverse.exception.JiwigoException;
20import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
21import fr.mael.jiwigo.transverse.session.SessionManager;
22import fr.mael.jiwigo.transverse.util.Tools;
23
24/*
25 *  jiwigo-ws-api Piwigo webservice access Api
26 *  Copyright (c) 2010-2011 Mael mael@le-guevel.com
27 *                All Rights Reserved
28 *
29 *  This library is free software. It comes without any warranty, to
30 *  the extent permitted by applicable law. You can redistribute it
31 *  and/or modify it under the terms of the Do What The Fuck You Want
32 *  To Public License, Version 2, as published by Sam Hocevar. See
33 *  http://sam.zoy.org/wtfpl/COPYING for more details.
34 */
35/**
36 * Dao for the categories
37 * @author mael
38 *
39 */
40public class CategoryDaoImpl implements CategoryDao {
41    /**
42     * Logger
43     */
44    private final Logger LOG = LoggerFactory.getLogger(CategoryDaoImpl.class);
45    /**
46     * Instance to use a singleton
47     */
48    private static CategoryDaoImpl instance;
49
50    private SessionManager sessionManager;
51
52    /**
53     * Lists the categories
54     * @param recursive true : recursive listing of the categories
55     * @return the list of categories
56     * @throws JiwigoException
57     * @throws IOException
58     * @throws ProxyAuthenticationException
59     */
60    public List<Category> list(boolean recursive) throws JiwigoException {
61        Document doc = sessionManager.executeReturnDocument(MethodsEnum.LISTER_CATEGORIES.getLabel(), "recursive",
62                String.valueOf(recursive));
63        Element element = (Element) doc.getDocumentElement().getElementsByTagName("categories").item(0);
64        NodeList nodeList = element.getElementsByTagName("category");
65        ArrayList<Category> categories = new ArrayList<Category>();
66        for (int i = 0; i < nodeList.getLength(); i++) {
67            Node catNode = nodeList.item(i);
68            if (catNode.getNodeType() == Node.ELEMENT_NODE) {
69                Element cat = (Element) catNode;
70                Category myCat = new Category();
71                myCat.setIdentifier(Integer.valueOf(cat.getAttribute(CategoryEnum.ID.getLabel())));
72                myCat.setUrlCategory(cat.getAttribute(CategoryEnum.URL.getLabel()));
73                myCat.setNbImages(Integer.valueOf(cat.getAttribute(CategoryEnum.NB_IMAGES.getLabel())));
74                myCat.setNbTotalImages(Integer.valueOf(cat.getAttribute(CategoryEnum.NB_TOTAL_IMAGES.getLabel())));
75                myCat.setName(Tools.getStringValueDom(cat, CategoryEnum.NAME.getLabel()));
76                String catMeres = Tools.getStringValueDom(cat, CategoryEnum.CAT_MERES.getLabel());
77                ArrayList<Integer> idCategoriesMeres = new ArrayList<Integer>();
78                myCat.setIdParentCategoriesString(catMeres);
79                for (String catA : catMeres.split(",")) {
80                    if (!catA.equals("")) {
81                        idCategoriesMeres.add(Integer.valueOf(catA));
82                    }
83                }
84                myCat.setIdParentCategories(idCategoriesMeres);
85                categories.add(myCat);
86            }
87        }
88        return categories;
89    }
90
91    /**
92     * Creation of a category
93     * @param category the category to create
94     * @return true if the category is successfully created
95     * @throws ProxyAuthenticationException
96     * @throws JiwigoException
97     */
98    public boolean create(Category category) throws JiwigoException {
99        try {
100            if (category.getDirectParent() != null) {
101                return Tools.checkOk(sessionManager.executeReturnDocument(MethodsEnum.AJOUTER_CATEGORIE.getLabel(),
102                        "name", category.getName(), "parent", String.valueOf(category.getDirectParent())));
103            } else {
104                return Tools.checkOk(sessionManager.executeReturnDocument(MethodsEnum.AJOUTER_CATEGORIE.getLabel(),
105                        "name", category.getName()));
106            }
107        } catch (FileAlreadyExistsException e) {
108            LOG.error(Tools.getStackTrace(e));
109            throw new JiwigoException(e);
110        }
111    }
112
113    /**
114     * @see fr.mael.jiwigo.dao.CategoryDao#delete(fr.mael.jiwigo.om.Category)
115     */
116    public boolean delete(Category category) throws JiwigoException {
117        String pwgToken = sessionManager.getPwgToken();
118        if (pwgToken == null) {
119            throw new JiwigoException("Error : received a null pwg_token");
120        }
121        if (LOG.isDebugEnabled()) {
122            LOG.debug("Deletes category " + category.getIdentifier() + " with pwg_token = " + pwgToken);
123        }
124
125        Document doc = sessionManager.executeReturnDocument(MethodsEnum.DELETE_CATEGORY.getLabel(), "category_id",
126                String.valueOf(category.getIdentifier()), "pwg_token", pwgToken);
127
128        if (LOG.isDebugEnabled()) {
129            LOG.debug(Tools.documentToString(doc));
130        }
131        try {
132            return Tools.checkOk(doc);
133        } catch (FileAlreadyExistsException e) {
134            e.printStackTrace();
135        }
136        return false;
137    }
138
139    public SessionManager getSessionManager() {
140        return sessionManager;
141    }
142
143    public void setSessionManager(SessionManager sessionManager) {
144        this.sessionManager = sessionManager;
145    }
146
147}
Note: See TracBrowser for help on using the repository browser.