source: extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/dao/CommentDao.java @ 9430

Last change on this file since 9430 was 9430, checked in by mlg, 13 years ago

Modifications to handle proxy error.
Two types of error are handled : proxy address error and proxy authentication error.
Connecting to jiwigo via a proxy seems to work (tried with my own server... gonna have to try with an external server).
version is 0.13.1.1

File size: 4.8 KB
Line 
1package fr.mael.jiwigo.dao;
2
3import java.io.IOException;
4import java.util.ArrayList;
5import java.util.List;
6
7import org.jdom.Document;
8import org.jdom.Element;
9
10import fr.mael.jiwigo.om.Comment;
11import fr.mael.jiwigo.transverse.enumeration.MethodsEnum;
12import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
13import fr.mael.jiwigo.transverse.session.SessionManager;
14import fr.mael.jiwigo.transverse.util.Tools;
15
16/*
17Copyright (c) 2010, Mael
18All rights reserved.
19
20Redistribution and use in source and binary forms, with or without
21modification, 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
31THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
32ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
33WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY
35DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
38ON 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
40SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41*/
42/**
43
44 * Dao for the comments
45 * @author mael
46 *
47 */
48public 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}
Note: See TracBrowser for help on using the repository browser.