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

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

Complete rewriting of the api
The api now uses interfaces everywhere (for the dao, services and session manager). This allows the developer to use his own implementations if he wants.
Deletion of the singletons. There are now getters and setters. It allows to make dependency injection.
Use of sl4j in the api, instead of using log4j.

File size: 4.3 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.ProxyAuthenticationException;
20import fr.mael.jiwigo.transverse.session.SessionManager;
21import fr.mael.jiwigo.transverse.util.Tools;
22
23/*
24 *  jiwigo-ws-api Piwigo webservice access Api
25 *  Copyright (c) 2010-2011 Mael mael@le-guevel.com
26 *                All Rights Reserved
27 *
28 *  This library is free software. It comes without any warranty, to
29 *  the extent permitted by applicable law. You can redistribute it
30 *  and/or modify it under the terms of the Do What The Fuck You Want
31 *  To Public License, Version 2, as published by Sam Hocevar. See
32 *  http://sam.zoy.org/wtfpl/COPYING for more details.
33 */
34/**
35 * Dao for the categories
36 * @author mael
37 *
38 */
39public class CategoryDaoImpl implements CategoryDao {
40    /**
41     * Logger
42     */
43    private final Logger LOG = LoggerFactory.getLogger(CategoryDaoImpl.class);
44    /**
45     * Instance to use a singleton
46     */
47    private static CategoryDaoImpl instance;
48
49    private SessionManager sessionManager;
50
51    /**
52     * Lists the categories
53     * @param recursive true : recursive listing of the categories
54     * @return the list of categories
55     * @throws IOException
56     * @throws ProxyAuthenticationException
57     */
58    public List<Category> list(boolean recursive) throws IOException, ProxyAuthenticationException {
59        Document doc = sessionManager.executeReturnDocument(MethodsEnum.LISTER_CATEGORIES.getLabel(), "recursive",
60                String.valueOf(recursive));
61        Element element = (Element) doc.getDocumentElement().getElementsByTagName("categories").item(0);
62        NodeList nodeList = element.getElementsByTagName("category");
63        ArrayList<Category> categories = new ArrayList<Category>();
64        for (int i = 0; i < nodeList.getLength(); i++) {
65            Node catNode = nodeList.item(i);
66            if (catNode.getNodeType() == Node.ELEMENT_NODE) {
67                Element cat = (Element) catNode;
68                Category myCat = new Category();
69                myCat.setIdentifier(Integer.valueOf(cat.getAttribute(CategoryEnum.ID.getLabel())));
70                myCat.setUrlCategory(cat.getAttribute(CategoryEnum.URL.getLabel()));
71                myCat.setNbImages(Integer.valueOf(cat.getAttribute(CategoryEnum.NB_IMAGES.getLabel())));
72                myCat.setNbTotalImages(Integer.valueOf(cat.getAttribute(CategoryEnum.NB_TOTAL_IMAGES.getLabel())));
73                myCat.setName(Tools.getStringValueDom(cat, CategoryEnum.NAME.getLabel()));
74                String catMeres = Tools.getStringValueDom(cat, CategoryEnum.CAT_MERES.getLabel());
75                ArrayList<Integer> idCategoriesMeres = new ArrayList<Integer>();
76                myCat.setIdParentCategoriesString(catMeres);
77                for (String catA : catMeres.split(",")) {
78                    if (!catA.equals("")) {
79                        idCategoriesMeres.add(Integer.valueOf(catA));
80                    }
81                }
82                myCat.setIdParentCategories(idCategoriesMeres);
83                categories.add(myCat);
84            }
85        }
86        return categories;
87    }
88
89    /**
90     * Creation of a category
91     * @param category the category to create
92     * @return true if the category is successfully created
93     * @throws ProxyAuthenticationException
94     */
95    public boolean create(Category category) throws ProxyAuthenticationException {
96        try {
97            if (category.getDirectParent() != null) {
98                return Tools.checkOk(sessionManager.executeReturnDocument(MethodsEnum.AJOUTER_CATEGORIE.getLabel(),
99                        "name", category.getName(), "parent", String.valueOf(category.getDirectParent())));
100            } else {
101                return Tools.checkOk(sessionManager.executeReturnDocument(MethodsEnum.AJOUTER_CATEGORIE.getLabel(),
102                        "name", category.getName()));
103            }
104        } catch (IOException e) {
105            LOG.error(Tools.getStackTrace(e));
106        } catch (FileAlreadyExistsException e) {
107            LOG.error(Tools.getStackTrace(e));
108        }
109        return false;
110    }
111
112    public SessionManager getSessionManager() {
113        return sessionManager;
114    }
115
116    public void setSessionManager(SessionManager sessionManager) {
117        this.sessionManager = sessionManager;
118    }
119
120}
Note: See TracBrowser for help on using the repository browser.