source: extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/dao/impl/TagDaoImpl.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: 3.9 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        return getTagsFromDocument((Element) doc.getDocumentElement().getElementsByTagName("tags").item(0));
58
59    }
60
61    /**
62     * COnstructs a list of tags from a document
63     * @param doc the document
64     * @return the list of tags
65     */
66    private List<Tag> getTagsFromDocument(Element element) {
67        NodeList listTags = element.getElementsByTagName("tag");
68
69        //      List<Element> listElement = (List<Element>) element.getChildren("tag");
70        ArrayList<Tag> tags = new ArrayList<Tag>();
71        for (int i = 0; i < listTags.getLength(); i++) {
72            Node nodeTag = listTags.item(0);
73            if (nodeTag.getNodeType() == Node.ELEMENT_NODE) {
74                Element tagElement = (Element) nodeTag;
75                Tag tag = new Tag();
76                tag.setIdentifier(Integer.valueOf(tagElement.getAttribute(TagEnum.ID.getLabel())));
77                tag.setName(tagElement.getAttribute(TagEnum.NAME.getLabel()));
78                tags.add(tag);
79            }
80        }
81        return tags;
82
83    }
84
85    /**
86     * Creation of a tag
87     * @param tag the tag to create
88     * @return true if the tag as been successfully created
89     * @throws ProxyAuthenticationException
90     */
91    public boolean create(Tag tag) throws JiwigoException {
92        try {
93            return Tools.checkOk(sessionManager.executeReturnDocument(MethodsEnum.ADD_TAG.getLabel(), "name",
94                    tag.getName()));
95        } catch (FileAlreadyExistsException e) {
96            LOG.error(Tools.getStackTrace(e));
97            throw new JiwigoException(e);
98        }
99    }
100
101    /**
102     * Function that returns the tags for an image
103     * @param image the image
104     * @return the tags list
105     * @throws IOException
106     * @throws JiwigoException
107     */
108    public List<Tag> tagsForImage(Image image) throws JiwigoException {
109        Document doc = sessionManager.executeReturnDocument(MethodsEnum.GET_INFO.getLabel(), "image_id",
110                String.valueOf(image.getIdentifier()));
111        Element elementImage = (Element) doc.getDocumentElement().getElementsByTagName("image").item(0);
112        Element elementTag = (Element) elementImage.getElementsByTagName("tags").item(0);
113        return getTagsFromDocument(elementTag);
114    }
115
116    public SessionManager getSessionManager() {
117        return sessionManager;
118    }
119
120    public void setSessionManager(SessionManager sessionManager) {
121        this.sessionManager = sessionManager;
122    }
123
124}
Note: See TracBrowser for help on using the repository browser.