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