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

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

Complete rewriting of the api
The api now uses interfaces everywhere (for the dao, services and session manager). This allows the developer to use his own implementations if he wants.
Deletion of the singletons. There are now getters and setters. It allows to make dependency injection.
Use of sl4j in the api, instead of using log4j.

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