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

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

Removes jdom lib and changes apache HTTPClient to the newest one
For more compatibility. I _think_ android is now compatible.

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