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

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

Modifications to handle proxy error.
Two types of error are handled : proxy address error and proxy authentication error.
Connecting to jiwigo via a proxy seems to work (tried with my own server... gonna have to try with an external server).
version is 0.13.1.1

File size: 4.9 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.ProxyAuthenticationException;
14import fr.mael.jiwigo.transverse.session.SessionManager;
15import fr.mael.jiwigo.transverse.util.Tools;
16
17/*
18Copyright (c) 2010, Mael
19All rights reserved.
20
21Redistribution and use in source and binary forms, with or without
22modification, are permitted provided that the following conditions are met:
23 * Redistributions of source code must retain the above copyright
24   notice, this list of conditions and the following disclaimer.
25 * Redistributions in binary form must reproduce the above copyright
26   notice, this list of conditions and the following disclaimer in the
27   documentation and/or other materials provided with the distribution.
28 * Neither the name of jiwigo nor the
29   names of its contributors may be used to endorse or promote products
30   derived from this software without specific prior written permission.
31
32THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
33ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
34WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY
36DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
39ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
41SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42*/
43/**
44
45 * Dao for the categories
46 * @author mael
47 *
48 */
49public class CategoryDao {
50    /**
51     * Logger
52     */
53    public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
54            .getLog(CategoryDao.class);
55    /**
56     * Instance to use a singleton
57     */
58    private static CategoryDao instance;
59
60    private SessionManager sessionManager;
61
62    /**
63     * Private constructor to use a singleton
64     */
65    private CategoryDao(SessionManager sessionManager) {
66        this.sessionManager = sessionManager;
67    }
68
69    /**
70     * @return the instance
71     */
72    public static CategoryDao getInstance(SessionManager sessionManager) {
73        if (instance == null) {
74            instance = new CategoryDao(sessionManager);
75        }
76        return instance;
77    }
78
79    /**
80     * Lists the categories
81     * @param recursive true : recursive listing of the categories
82     * @return the list of categories
83     * @throws IOException
84     * @throws ProxyAuthenticationException
85     */
86    public List<Category> list(boolean recursive) throws IOException, ProxyAuthenticationException {
87        Document doc = sessionManager.executeReturnDocument(MethodsEnum.LISTER_CATEGORIES.getLabel(), "recursive",
88                String.valueOf(recursive));
89        Element element = doc.getRootElement().getChild("categories");
90        List<Element> listElement = (List<Element>) element.getChildren("category");
91        ArrayList<Category> categories = new ArrayList<Category>();
92        for (Element cat : listElement) {
93            Category myCat = new Category();
94            myCat.setIdentifier(Integer.valueOf(cat.getAttributeValue(CategoryEnum.ID.getLabel())));
95            myCat.setUrlCategory(cat.getAttributeValue(CategoryEnum.URL.getLabel()));
96            myCat.setNbImages(Integer.valueOf(cat.getAttributeValue(CategoryEnum.NB_IMAGES.getLabel())));
97            myCat.setNbTotalImages(Integer.valueOf(cat.getAttributeValue(CategoryEnum.NB_TOTAL_IMAGES.getLabel())));
98            myCat.setName(cat.getChildText(CategoryEnum.NAME.getLabel()));
99            String catMeres = cat.getChildText(CategoryEnum.CAT_MERES.getLabel());
100            ArrayList<Integer> idCategoriesMeres = new ArrayList<Integer>();
101            for (String catA : catMeres.split(",")) {
102                if (!catA.equals("")) {
103                    idCategoriesMeres.add(Integer.valueOf(catA));
104                }
105            }
106            myCat.setIdParentCategories(idCategoriesMeres);
107            categories.add(myCat);
108        }
109        return categories;
110    }
111
112    /**
113     * Creation of a category
114     * @param category the category to create
115     * @return true if the category is successfully created
116     * @throws ProxyAuthenticationException
117     */
118    public boolean create(Category category) throws ProxyAuthenticationException {
119        try {
120            if (category.getDirectParent() != null) {
121                return Tools.checkOk(sessionManager.executeReturnDocument(MethodsEnum.AJOUTER_CATEGORIE.getLabel(),
122                        "name", category.getName(), "parent", String.valueOf(category.getDirectParent())));
123            } else {
124                return Tools.checkOk(sessionManager.executeReturnDocument(MethodsEnum.AJOUTER_CATEGORIE.getLabel(),
125                        "name", category.getName()));
126            }
127        } catch (IOException e) {
128            LOG.error(Tools.getStackTrace(e));
129        }
130        return false;
131    }
132
133}
Note: See TracBrowser for help on using the repository browser.