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.Comment; |
---|
11 | import fr.mael.jiwigo.transverse.enumeration.MethodsEnum; |
---|
12 | import fr.mael.jiwigo.transverse.session.SessionManager; |
---|
13 | import fr.mael.jiwigo.transverse.util.Tools; |
---|
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 | /** |
---|
42 | |
---|
43 | * Dao for the comments |
---|
44 | * @author mael |
---|
45 | * |
---|
46 | */ |
---|
47 | public class CommentDao { |
---|
48 | /** |
---|
49 | * Logger |
---|
50 | */ |
---|
51 | public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory |
---|
52 | .getLog(CommentDao.class); |
---|
53 | /** |
---|
54 | * Instance that allows to use a singleton |
---|
55 | */ |
---|
56 | private static CommentDao instance; |
---|
57 | |
---|
58 | private SessionManager sessionManager; |
---|
59 | |
---|
60 | /** |
---|
61 | * private constructor, to use a singleton |
---|
62 | */ |
---|
63 | private CommentDao(SessionManager sessionManager) { |
---|
64 | this.sessionManager = sessionManager; |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * @return the singleton |
---|
69 | */ |
---|
70 | public static CommentDao getInstance(SessionManager sessionManager) { |
---|
71 | if (instance == null) { |
---|
72 | instance = new CommentDao(sessionManager); |
---|
73 | } |
---|
74 | return instance; |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | * Listing of the comments for the given image |
---|
79 | * @param idImage id of the image |
---|
80 | * @return list of comments |
---|
81 | * @throws IOException |
---|
82 | */ |
---|
83 | public List<Comment> list(Integer idImage) throws IOException { |
---|
84 | Document doc = (sessionManager.executeReturnDocument(MethodsEnum.GET_INFO.getLabel(), "image_id", String |
---|
85 | .valueOf(idImage))); |
---|
86 | Element element = doc.getRootElement().getChild("image").getChild("comments"); |
---|
87 | List<Element> listElement = (List<Element>) element.getChildren("comment"); |
---|
88 | ArrayList<Comment> comments = new ArrayList<Comment>(); |
---|
89 | for (Element com : listElement) { |
---|
90 | Comment myCom = new Comment(); |
---|
91 | myCom.setIdentifier(Integer.valueOf(com.getAttributeValue("id"))); |
---|
92 | myCom.setDate(com.getAttributeValue("date")); |
---|
93 | myCom.setAuthor(com.getChildText("author")); |
---|
94 | myCom.setContent(com.getChildText("content")); |
---|
95 | comments.add(myCom); |
---|
96 | } |
---|
97 | return comments; |
---|
98 | } |
---|
99 | |
---|
100 | /** |
---|
101 | * Gets the key essential to add a comment |
---|
102 | * @param idImage the id of the image |
---|
103 | * @return the key |
---|
104 | * @throws IOException |
---|
105 | */ |
---|
106 | public String getKey(Integer idImage) throws IOException { |
---|
107 | Document doc = (sessionManager.executeReturnDocument(MethodsEnum.GET_INFO.getLabel(), "image_id", String |
---|
108 | .valueOf(idImage))); |
---|
109 | String key = doc.getRootElement().getChild("image").getChild("comment_post").getAttributeValue("key"); |
---|
110 | return key; |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * Creates a comment |
---|
115 | * @param commentaire the comment to add |
---|
116 | * @return true if the comment is successfully added |
---|
117 | * @throws IOException |
---|
118 | */ |
---|
119 | public boolean create(Comment commentaire) throws IOException { |
---|
120 | String key = getKey(commentaire.getImageId()); |
---|
121 | try { |
---|
122 | Thread.sleep(2200); |
---|
123 | } catch (InterruptedException e) { |
---|
124 | e.printStackTrace(); |
---|
125 | } |
---|
126 | return Tools.checkOk(sessionManager.executeReturnDocument(MethodsEnum.AJOUTER_COMMENTAIRE.getLabel(), |
---|
127 | "image_id", String.valueOf(commentaire.getImageId()), "author", commentaire.getAuthor(), "content", |
---|
128 | commentaire.getContent(), "key", key)); |
---|
129 | |
---|
130 | } |
---|
131 | } |
---|