1 | package fr.mael.jiwigo.dao; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.util.ArrayList; |
---|
5 | import java.util.List; |
---|
6 | |
---|
7 | import org.jdom.Document; |
---|
8 | import org.jdom.Element; |
---|
9 | |
---|
10 | import fr.mael.jiwigo.om.Image; |
---|
11 | import fr.mael.jiwigo.om.Tag; |
---|
12 | import fr.mael.jiwigo.transverse.enumeration.MethodsEnum; |
---|
13 | import fr.mael.jiwigo.transverse.enumeration.TagEnum; |
---|
14 | import fr.mael.jiwigo.transverse.session.SessionManager; |
---|
15 | import fr.mael.jiwigo.transverse.util.Outil; |
---|
16 | /* |
---|
17 | Copyright (c) 2010, Mael |
---|
18 | All rights reserved. |
---|
19 | |
---|
20 | Redistribution and use in source and binary forms, with or without |
---|
21 | modification, are permitted provided that the following conditions are met: |
---|
22 | * Redistributions of source code must retain the above copyright |
---|
23 | notice, this list of conditions and the following disclaimer. |
---|
24 | * Redistributions in binary form must reproduce the above copyright |
---|
25 | notice, this list of conditions and the following disclaimer in the |
---|
26 | documentation and/or other materials provided with the distribution. |
---|
27 | * Neither the name of jiwigo nor the |
---|
28 | names of its contributors may be used to endorse or promote products |
---|
29 | derived from this software without specific prior written permission. |
---|
30 | |
---|
31 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
---|
32 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
33 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
34 | DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY |
---|
35 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
36 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
37 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
---|
38 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
39 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
40 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
41 | */ |
---|
42 | |
---|
43 | |
---|
44 | /** |
---|
45 | * Dao of the categories |
---|
46 | * @author mael |
---|
47 | * |
---|
48 | */ |
---|
49 | public class TagDao { |
---|
50 | /** |
---|
51 | * Logger |
---|
52 | */ |
---|
53 | public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory.getLog(TagDao.class); |
---|
54 | /** |
---|
55 | * Instance, to use a singleton |
---|
56 | */ |
---|
57 | private static TagDao instance; |
---|
58 | |
---|
59 | private SessionManager sessionManager; |
---|
60 | |
---|
61 | /** |
---|
62 | * private constructor to use a singleton |
---|
63 | */ |
---|
64 | private TagDao(SessionManager sessionManager) { |
---|
65 | this.sessionManager = sessionManager; |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * @return the instance |
---|
70 | */ |
---|
71 | public static TagDao getInstance(SessionManager sessionManager) { |
---|
72 | if (instance == null) { |
---|
73 | instance = new TagDao(sessionManager); |
---|
74 | } |
---|
75 | return instance; |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * lists the tags |
---|
80 | * @return the list of tags |
---|
81 | * @throws IOException |
---|
82 | */ |
---|
83 | public List<Tag> lister() throws IOException { |
---|
84 | Document doc = sessionManager.executerReturnDocument(MethodsEnum.TAGS_ADMIN_LIST.getLabel()); |
---|
85 | // System.out.println(Outil.documentToString(doc)); |
---|
86 | return getTagsFromDocument(doc.getRootElement().getChild("tags")); |
---|
87 | |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * COnstructs a list of tags from a document |
---|
92 | * @param doc the document |
---|
93 | * @return the list of tags |
---|
94 | */ |
---|
95 | private List<Tag> getTagsFromDocument(Element element) { |
---|
96 | |
---|
97 | List<Element> listElement = (List<Element>) element.getChildren("tag"); |
---|
98 | ArrayList<Tag> tags = new ArrayList<Tag>(); |
---|
99 | for (Element tagElement : listElement) { |
---|
100 | Tag tag = new Tag(); |
---|
101 | tag.setId(Integer.valueOf(tagElement.getAttributeValue(TagEnum.ID.getLabel()))); |
---|
102 | tag.setNom(tagElement.getAttributeValue(TagEnum.NAME.getLabel())); |
---|
103 | tags.add(tag); |
---|
104 | } |
---|
105 | return tags; |
---|
106 | |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * Creation of a tag |
---|
111 | * @param tag the tag to create |
---|
112 | * @return true if the tag as been successfully created |
---|
113 | */ |
---|
114 | public boolean creer(Tag tag) { |
---|
115 | try { |
---|
116 | return Outil.checkOk(sessionManager.executerReturnDocument(MethodsEnum.ADD_TAG.getLabel(), "name", tag |
---|
117 | .getNom())); |
---|
118 | } catch (IOException e) { |
---|
119 | LOG.error(Outil.getStackTrace(e)); |
---|
120 | } |
---|
121 | return false; |
---|
122 | } |
---|
123 | |
---|
124 | /** |
---|
125 | * Function that returns the tags for an image |
---|
126 | * @param image the image |
---|
127 | * @return the tags list |
---|
128 | * @throws IOException |
---|
129 | */ |
---|
130 | public List<Tag> tagsForImage(Image image) throws IOException { |
---|
131 | Document doc = sessionManager.executerReturnDocument(MethodsEnum.GET_INFO.getLabel(), "image_id", String |
---|
132 | .valueOf(image.getIdentifiant())); |
---|
133 | return getTagsFromDocument(doc.getRootElement().getChild("image").getChild("tags")); |
---|
134 | } |
---|
135 | |
---|
136 | } |
---|