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.Comment; |
---|
12 | import fr.mael.jiwigo.transverse.enumeration.MethodsEnum; |
---|
13 | import fr.mael.jiwigo.transverse.util.Outil; |
---|
14 | |
---|
15 | /** |
---|
16 | Copyright (c) 2010, Mael |
---|
17 | All rights reserved. |
---|
18 | |
---|
19 | Redistribution and use in source and binary forms, with or without |
---|
20 | modification, are permitted provided that the following conditions are met: |
---|
21 | * Redistributions of source code must retain the above copyright |
---|
22 | notice, this list of conditions and the following disclaimer. |
---|
23 | * Redistributions in binary form must reproduce the above copyright |
---|
24 | notice, this list of conditions and the following disclaimer in the |
---|
25 | documentation and/or other materials provided with the distribution. |
---|
26 | * Neither the name of jiwigo nor the |
---|
27 | names of its contributors may be used to endorse or promote products |
---|
28 | derived from this software without specific prior written permission. |
---|
29 | |
---|
30 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
---|
31 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
32 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
33 | DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY |
---|
34 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
35 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
36 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
---|
37 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
38 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
39 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
40 | |
---|
41 | * Dao des commentaires |
---|
42 | * @author mael |
---|
43 | * |
---|
44 | */ |
---|
45 | public class CommentDao { |
---|
46 | /** |
---|
47 | * Logger |
---|
48 | */ |
---|
49 | public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory |
---|
50 | .getLog(CommentDao.class); |
---|
51 | /** |
---|
52 | * Instance qui permet d'utiliser un singleton |
---|
53 | */ |
---|
54 | private static CommentDao instance; |
---|
55 | |
---|
56 | /** |
---|
57 | * Constructeur privé, pour n'avoir accès qu'au singleton |
---|
58 | */ |
---|
59 | private CommentDao() { |
---|
60 | |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * @return le singleton |
---|
65 | */ |
---|
66 | public static CommentDao getInstance() { |
---|
67 | if (instance == null) { |
---|
68 | instance = new CommentDao(); |
---|
69 | } |
---|
70 | return instance; |
---|
71 | } |
---|
72 | |
---|
73 | /** |
---|
74 | * Listing des commentaires pour une image donnée |
---|
75 | * @param idImage identifiant de l'image |
---|
76 | * @return liste des commentaires |
---|
77 | * @throws IOException |
---|
78 | */ |
---|
79 | public List<Comment> lister(Integer idImage) throws IOException { |
---|
80 | Document doc = (Main.sessionManager.executerReturnDocument(MethodsEnum.GET_INFO.getLabel(), "image_id", String |
---|
81 | .valueOf(idImage))); |
---|
82 | Element element = doc.getRootElement().getChild("image").getChild("comments"); |
---|
83 | List<Element> listElement = (List<Element>) element.getChildren("comment"); |
---|
84 | ArrayList<Comment> comments = new ArrayList<Comment>(); |
---|
85 | for (Element com : listElement) { |
---|
86 | Comment myCom = new Comment(); |
---|
87 | myCom.setIdentifiant(Integer.valueOf(com.getAttributeValue("id"))); |
---|
88 | myCom.setDate(com.getAttributeValue("date")); |
---|
89 | myCom.setAuthor(com.getChildText("author")); |
---|
90 | myCom.setContent(com.getChildText("content")); |
---|
91 | comments.add(myCom); |
---|
92 | } |
---|
93 | return comments; |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * Recupération de la clé pour l'ajout d'un com |
---|
98 | * @param idImage |
---|
99 | * @return |
---|
100 | * @throws IOException |
---|
101 | */ |
---|
102 | public String getKey(Integer idImage) throws IOException { |
---|
103 | Document doc = (Main.sessionManager.executerReturnDocument(MethodsEnum.GET_INFO.getLabel(), "image_id", String |
---|
104 | .valueOf(idImage))); |
---|
105 | String key = doc.getRootElement().getChild("image").getChild("comment_post").getAttributeValue("key"); |
---|
106 | return key; |
---|
107 | } |
---|
108 | |
---|
109 | public boolean creer(Comment commentaire) throws IOException { |
---|
110 | String key = getKey(commentaire.getImageId()); |
---|
111 | try { |
---|
112 | Thread.sleep(2200); |
---|
113 | } catch (InterruptedException e) { |
---|
114 | e.printStackTrace(); |
---|
115 | } |
---|
116 | return Outil.checkOk(Main.sessionManager.executerReturnDocument(MethodsEnum.AJOUTER_COMMENTAIRE.getLabel(), |
---|
117 | "image_id", String.valueOf(commentaire.getImageId()), "author", commentaire.getAuthor(), "content", |
---|
118 | commentaire.getContent(), "key", key)); |
---|
119 | |
---|
120 | } |
---|
121 | } |
---|