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

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

Changes function name (to english)
version changed to 0.13.1

File size: 4.5 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.session.SessionManager;
13import fr.mael.jiwigo.transverse.util.Tools;
14
15/*
16Copyright (c) 2010, Mael
17All rights reserved.
18
19Redistribution and use in source and binary forms, with or without
20modification, 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
30THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
31ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY
34DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
37ON 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
39SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40*/
41/**
42
43 * Dao for the comments
44 * @author mael
45 *
46 */
47public 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}
Note: See TracBrowser for help on using the repository browser.