1 | package fr.mael.jiwigo.dao.impl; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.util.ArrayList; |
---|
5 | import java.util.List; |
---|
6 | |
---|
7 | import org.slf4j.Logger; |
---|
8 | import org.slf4j.LoggerFactory; |
---|
9 | import org.w3c.dom.Document; |
---|
10 | import org.w3c.dom.Element; |
---|
11 | import org.w3c.dom.Node; |
---|
12 | import org.w3c.dom.NodeList; |
---|
13 | |
---|
14 | import fr.mael.jiwigo.dao.TagDao; |
---|
15 | import fr.mael.jiwigo.om.Image; |
---|
16 | import fr.mael.jiwigo.om.Tag; |
---|
17 | import fr.mael.jiwigo.transverse.enumeration.MethodsEnum; |
---|
18 | import fr.mael.jiwigo.transverse.enumeration.TagEnum; |
---|
19 | import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException; |
---|
20 | import fr.mael.jiwigo.transverse.exception.JiwigoException; |
---|
21 | import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; |
---|
22 | import fr.mael.jiwigo.transverse.session.SessionManager; |
---|
23 | import 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 | */ |
---|
41 | public 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 | } |
---|