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 | * jiwigo-ws-api Piwigo webservice access Api |
---|
18 | * Copyright (c) 2010-2011 Mael mael@le-guevel.com |
---|
19 | * All Rights Reserved |
---|
20 | * |
---|
21 | * This library is free software. It comes without any warranty, to |
---|
22 | * the extent permitted by applicable law. You can redistribute it |
---|
23 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
24 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
25 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
26 | */ |
---|
27 | /** |
---|
28 | |
---|
29 | * Dao for the comments |
---|
30 | * @author mael |
---|
31 | * |
---|
32 | */ |
---|
33 | public class CommentDao { |
---|
34 | /** |
---|
35 | * Logger |
---|
36 | */ |
---|
37 | public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory |
---|
38 | .getLog(CommentDao.class); |
---|
39 | /** |
---|
40 | * Instance that allows to use a singleton |
---|
41 | */ |
---|
42 | private static CommentDao instance; |
---|
43 | |
---|
44 | private SessionManager sessionManager; |
---|
45 | |
---|
46 | /** |
---|
47 | * private constructor, to use a singleton |
---|
48 | */ |
---|
49 | private CommentDao(SessionManager sessionManager) { |
---|
50 | this.sessionManager = sessionManager; |
---|
51 | } |
---|
52 | |
---|
53 | /** |
---|
54 | * @return the singleton |
---|
55 | */ |
---|
56 | public static CommentDao getInstance(SessionManager sessionManager) { |
---|
57 | if (instance == null) { |
---|
58 | instance = new CommentDao(sessionManager); |
---|
59 | } |
---|
60 | return instance; |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * Listing of the comments for the given image |
---|
65 | * @param idImage id of the image |
---|
66 | * @return list of comments |
---|
67 | * @throws IOException |
---|
68 | * @throws ProxyAuthenticationException |
---|
69 | */ |
---|
70 | public List<Comment> list(Integer idImage) throws IOException, ProxyAuthenticationException { |
---|
71 | Document doc = (sessionManager.executeReturnDocument(MethodsEnum.GET_INFO.getLabel(), "image_id", String |
---|
72 | .valueOf(idImage), "comments_per_page", "100")); |
---|
73 | Element element = doc.getRootElement().getChild("image").getChild("comments"); |
---|
74 | List<Element> listElement = (List<Element>) element.getChildren("comment"); |
---|
75 | ArrayList<Comment> comments = new ArrayList<Comment>(); |
---|
76 | for (Element com : listElement) { |
---|
77 | Comment myCom = new Comment(); |
---|
78 | myCom.setIdentifier(Integer.valueOf(com.getAttributeValue("id"))); |
---|
79 | myCom.setDate(com.getAttributeValue("date")); |
---|
80 | myCom.setAuthor(com.getChildText("author")); |
---|
81 | myCom.setContent(com.getChildText("content")); |
---|
82 | comments.add(myCom); |
---|
83 | } |
---|
84 | return comments; |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * Gets the key essential to add a comment |
---|
89 | * @param idImage the id of the image |
---|
90 | * @return the key |
---|
91 | * @throws IOException |
---|
92 | * @throws ProxyAuthenticationException |
---|
93 | */ |
---|
94 | public String getKey(Integer idImage) throws IOException, ProxyAuthenticationException { |
---|
95 | Document doc = (sessionManager.executeReturnDocument(MethodsEnum.GET_INFO.getLabel(), "image_id", String |
---|
96 | .valueOf(idImage))); |
---|
97 | String key = doc.getRootElement().getChild("image").getChild("comment_post").getAttributeValue("key"); |
---|
98 | return key; |
---|
99 | } |
---|
100 | |
---|
101 | /** |
---|
102 | * Creates a comment |
---|
103 | * @param commentaire the comment to add |
---|
104 | * @return true if the comment is successfully added |
---|
105 | * @throws IOException |
---|
106 | * @throws ProxyAuthenticationException |
---|
107 | */ |
---|
108 | public boolean create(Comment commentaire) throws IOException, ProxyAuthenticationException { |
---|
109 | String key = getKey(commentaire.getImageId()); |
---|
110 | try { |
---|
111 | Thread.sleep(2200); |
---|
112 | } catch (InterruptedException e) { |
---|
113 | e.printStackTrace(); |
---|
114 | } |
---|
115 | return Tools.checkOk(sessionManager.executeReturnDocument(MethodsEnum.AJOUTER_COMMENTAIRE.getLabel(), |
---|
116 | "image_id", String.valueOf(commentaire.getImageId()), "author", commentaire.getAuthor(), "content", |
---|
117 | commentaire.getContent(), "key", key)); |
---|
118 | |
---|
119 | } |
---|
120 | } |
---|