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.Tools; |
---|
16 | |
---|
17 | /** |
---|
18 | * Dao of the categories |
---|
19 | * @author mael |
---|
20 | * |
---|
21 | */ |
---|
22 | public class TagDao { |
---|
23 | /** |
---|
24 | * Logger |
---|
25 | */ |
---|
26 | public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory.getLog(TagDao.class); |
---|
27 | /** |
---|
28 | * Instance, to use a singleton |
---|
29 | */ |
---|
30 | private static TagDao instance; |
---|
31 | |
---|
32 | private SessionManager sessionManager; |
---|
33 | |
---|
34 | /** |
---|
35 | * private constructor to use a singleton |
---|
36 | */ |
---|
37 | private TagDao(SessionManager sessionManager) { |
---|
38 | this.sessionManager = sessionManager; |
---|
39 | } |
---|
40 | |
---|
41 | /** |
---|
42 | * @return the instance |
---|
43 | */ |
---|
44 | public static TagDao getInstance(SessionManager sessionManager) { |
---|
45 | if (instance == null) { |
---|
46 | instance = new TagDao(sessionManager); |
---|
47 | } |
---|
48 | return instance; |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * lists the tags |
---|
53 | * @return the list of tags |
---|
54 | * @throws IOException |
---|
55 | */ |
---|
56 | public List<Tag> list() throws IOException { |
---|
57 | Document doc = sessionManager.executeReturnDocument(MethodsEnum.TAGS_ADMIN_LIST.getLabel()); |
---|
58 | // System.out.println(Outil.documentToString(doc)); |
---|
59 | return getTagsFromDocument(doc.getRootElement().getChild("tags")); |
---|
60 | |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * COnstructs a list of tags from a document |
---|
65 | * @param doc the document |
---|
66 | * @return the list of tags |
---|
67 | */ |
---|
68 | private List<Tag> getTagsFromDocument(Element element) { |
---|
69 | |
---|
70 | List<Element> listElement = (List<Element>) element.getChildren("tag"); |
---|
71 | ArrayList<Tag> tags = new ArrayList<Tag>(); |
---|
72 | for (Element tagElement : listElement) { |
---|
73 | Tag tag = new Tag(); |
---|
74 | tag.setIdentifier(Integer.valueOf(tagElement.getAttributeValue(TagEnum.ID.getLabel()))); |
---|
75 | tag.setName(tagElement.getAttributeValue(TagEnum.NAME.getLabel())); |
---|
76 | tags.add(tag); |
---|
77 | } |
---|
78 | return tags; |
---|
79 | |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * Creation of a tag |
---|
84 | * @param tag the tag to create |
---|
85 | * @return true if the tag as been successfully created |
---|
86 | */ |
---|
87 | public boolean create(Tag tag) { |
---|
88 | try { |
---|
89 | return Tools.checkOk(sessionManager.executeReturnDocument(MethodsEnum.ADD_TAG.getLabel(), "name", tag |
---|
90 | .getName())); |
---|
91 | } catch (IOException e) { |
---|
92 | LOG.error(Tools.getStackTrace(e)); |
---|
93 | } |
---|
94 | return false; |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * Function that returns the tags for an image |
---|
99 | * @param image the image |
---|
100 | * @return the tags list |
---|
101 | * @throws IOException |
---|
102 | */ |
---|
103 | public List<Tag> tagsForImage(Image image) throws IOException { |
---|
104 | Document doc = sessionManager.executeReturnDocument(MethodsEnum.GET_INFO.getLabel(), "image_id", String |
---|
105 | .valueOf(image.getIdentifier())); |
---|
106 | return getTagsFromDocument(doc.getRootElement().getChild("image").getChild("tags")); |
---|
107 | } |
---|
108 | |
---|
109 | } |
---|