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

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

Adds support for method pwg.categories.delete
(allows to delete a category).

File size: 5.4 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        //this document will allow to get pwg_token. it is necessary to delete a category
118        Document docStatus = sessionManager.executeReturnDocument(MethodsEnum.SESSION_STATUS.getLabel());
119        String pwgToken = Tools.getStringValueDom(docStatus.getDocumentElement(), "pwg_token");
120        if (pwgToken.equals("")) {
121            throw new JiwigoException("Error getting pwg_token. Returned document was : "
122                    + Tools.documentToString(docStatus));
123        }
124        if (LOG.isDebugEnabled()) {
125            LOG.debug("Deletes category " + category.getIdentifier() + " with pwg_token = " + pwgToken);
126        }
127
128        Document doc = sessionManager.executeReturnDocument(MethodsEnum.DELETE_CATEGORY.getLabel(), "category_id",
129                String.valueOf(category.getIdentifier()), "pwg_token", pwgToken);
130
131        if (LOG.isDebugEnabled()) {
132            LOG.debug(Tools.documentToString(doc));
133        }
134        try {
135            return Tools.checkOk(doc);
136        } catch (FileAlreadyExistsException e) {
137            e.printStackTrace();
138        }
139        return false;
140    }
141
142    public SessionManager getSessionManager() {
143        return sessionManager;
144    }
145
146    public void setSessionManager(SessionManager sessionManager) {
147        this.sessionManager = sessionManager;
148    }
149
150}
Note: See TracBrowser for help on using the repository browser.