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