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

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

Bug correction : ClassCastException when trying to add tags.

File size: 4.0 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.TagDao;
15import fr.mael.jiwigo.om.Image;
16import fr.mael.jiwigo.om.Tag;
17import fr.mael.jiwigo.transverse.enumeration.MethodsEnum;
18import fr.mael.jiwigo.transverse.enumeration.TagEnum;
19import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException;
20import fr.mael.jiwigo.transverse.exception.JiwigoException;
21import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
22import fr.mael.jiwigo.transverse.session.SessionManager;
23import fr.mael.jiwigo.transverse.util.Tools;
24
25/*
26 *  jiwigo-ws-api Piwigo webservice access Api
27 *  Copyright (c) 2010-2011 Mael mael@le-guevel.com
28 *                All Rights Reserved
29 *
30 *  This library is free software. It comes without any warranty, to
31 *  the extent permitted by applicable law. You can redistribute it
32 *  and/or modify it under the terms of the Do What The Fuck You Want
33 *  To Public License, Version 2, as published by Sam Hocevar. See
34 *  http://sam.zoy.org/wtfpl/COPYING for more details.
35 */
36/**
37 * Dao of the categories
38 * @author mael
39 *
40 */
41public class TagDaoImpl implements TagDao {
42    /**
43     * Logger
44     */
45    private final Logger LOG = LoggerFactory.getLogger(ImageDaoImpl.class);
46
47    private SessionManager sessionManager;
48
49    /**
50     * lists the tags
51     * @return the list of tags
52     * @throws IOException
53     * @throws ProxyAuthenticationException
54     */
55    public List<Tag> list() throws JiwigoException {
56        Document doc = sessionManager.executeReturnDocument(MethodsEnum.TAGS_ADMIN_LIST.getLabel());
57        //      System.out.println(Outil.documentToString(doc));
58        NodeList nodeList = doc.getDocumentElement().getElementsByTagName("tags");
59        return getTagsFromDocument((Element) doc.getDocumentElement().getElementsByTagName("tags").item(0));
60
61    }
62
63    /**
64     * COnstructs a list of tags from a document
65     * @param doc the document
66     * @return the list of tags
67     */
68    private List<Tag> getTagsFromDocument(Element element) {
69        NodeList listTags = element.getElementsByTagName("tag");
70
71        //      List<Element> listElement = (List<Element>) element.getChildren("tag");
72        ArrayList<Tag> tags = new ArrayList<Tag>();
73        for (int i = 0; i < listTags.getLength(); i++) {
74            Node nodeTag = listTags.item(0);
75            if (nodeTag.getNodeType() == Node.ELEMENT_NODE) {
76                Element tagElement = (Element) nodeTag;
77                Tag tag = new Tag();
78                tag.setIdentifier(Integer.valueOf(tagElement.getAttribute(TagEnum.ID.getLabel())));
79                tag.setName(tagElement.getAttribute(TagEnum.NAME.getLabel()));
80                tags.add(tag);
81            }
82        }
83        return tags;
84
85    }
86
87    /**
88     * Creation of a tag
89     * @param tag the tag to create
90     * @return true if the tag as been successfully created
91     * @throws ProxyAuthenticationException
92     */
93    public boolean create(Tag tag) throws JiwigoException {
94        try {
95            return Tools.checkOk(sessionManager.executeReturnDocument(MethodsEnum.ADD_TAG.getLabel(), "name",
96                    tag.getName()));
97        } catch (FileAlreadyExistsException e) {
98            LOG.error(Tools.getStackTrace(e));
99            throw new JiwigoException(e);
100        }
101    }
102
103    /**
104     * Function that returns the tags for an image
105     * @param image the image
106     * @return the tags list
107     * @throws IOException
108     * @throws JiwigoException
109     */
110    public List<Tag> tagsForImage(Image image) throws JiwigoException {
111        Document doc = sessionManager.executeReturnDocument(MethodsEnum.GET_INFO.getLabel(), "image_id",
112                String.valueOf(image.getIdentifier()));
113        Element elementImage = (Element) doc.getDocumentElement().getElementsByTagName("image").item(0);
114        Element elementTag = (Element) elementImage.getElementsByTagName("tags").item(0);
115        return getTagsFromDocument(elementTag);
116    }
117
118    public SessionManager getSessionManager() {
119        return sessionManager;
120    }
121
122    public void setSessionManager(SessionManager sessionManager) {
123        this.sessionManager = sessionManager;
124    }
125
126}
Note: See TracBrowser for help on using the repository browser.