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

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

Adds a better Exception management
new Exception : FileAlreadyExistsException.

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