source: extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/dao/impl/CommentDaoImpl.java @ 10505

Last change on this file since 10505 was 10505, checked in by anthony43, 13 years ago

huge refactoring to introduce JiwigoException : a generic exception encapsulating low level exceptions (IOException, ProxyAuthentication, etc...)
The aim is to have, on the consumer side, just one exception to catch them all.
this refactoring may need a code review to check the changes.

File size: 4.4 KB
Line 
1package fr.mael.jiwigo.dao.impl;
2
3import java.io.IOException;
4import java.util.ArrayList;
5import java.util.List;
6
7import org.slf4j.Logger;
8import org.slf4j.LoggerFactory;
9import org.w3c.dom.Document;
10import org.w3c.dom.Element;
11import org.w3c.dom.Node;
12import org.w3c.dom.NodeList;
13
14import fr.mael.jiwigo.dao.CommentDao;
15import fr.mael.jiwigo.om.Comment;
16import fr.mael.jiwigo.transverse.enumeration.MethodsEnum;
17import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException;
18import fr.mael.jiwigo.transverse.exception.JiwigoException;
19import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
20import fr.mael.jiwigo.transverse.session.SessionManager;
21import fr.mael.jiwigo.transverse.util.Tools;
22
23/*
24 *  jiwigo-ws-api Piwigo webservice access Api
25 *  Copyright (c) 2010-2011 Mael mael@le-guevel.com
26 *                All Rights Reserved
27 *
28 *  This library is free software. It comes without any warranty, to
29 *  the extent permitted by applicable law. You can redistribute it
30 *  and/or modify it under the terms of the Do What The Fuck You Want
31 *  To Public License, Version 2, as published by Sam Hocevar. See
32 *  http://sam.zoy.org/wtfpl/COPYING for more details.
33 */
34/**
35
36 * Dao for the comments
37 * @author mael
38 *
39 */
40public class CommentDaoImpl implements CommentDao {
41    /**
42     * Logger
43     */
44    private final Logger LOG = LoggerFactory.getLogger(CommentDaoImpl.class);
45
46    private SessionManager sessionManager;
47
48    /**
49     * Listing of the comments for the given image
50     * @param idImage id of the  image
51     * @return list of comments
52     * @throws IOException
53     * @throws ProxyAuthenticationException
54     */
55    public List<Comment> list(Integer idImage) throws JiwigoException {
56        Document doc = (sessionManager.executeReturnDocument(MethodsEnum.GET_INFO.getLabel(), "image_id",
57                String.valueOf(idImage), "comments_per_page", "100"));
58        Element elementImage = (Element) doc.getDocumentElement().getElementsByTagName("image").item(0);
59        Element elementComments = (Element) elementImage.getElementsByTagName("comments").item(0);
60        NodeList listComments = elementComments.getElementsByTagName("comment");
61        ArrayList<Comment> comments = new ArrayList<Comment>();
62        for (int i = 0; i < listComments.getLength(); i++) {
63            Node nodeCom = listComments.item(i);
64            if (nodeCom.getNodeType() == Node.ELEMENT_NODE) {
65                Element com = (Element) nodeCom;
66                Comment myCom = new Comment();
67                myCom.setIdentifier(Integer.valueOf(com.getAttribute("id")));
68                myCom.setDate(com.getAttribute("date"));
69                myCom.setAuthor(Tools.getStringValueDom(com, "author"));
70                myCom.setContent(Tools.getStringValueDom(com, "content"));
71                comments.add(myCom);
72            }
73        }
74        return comments;
75    }
76
77    /**
78     * Gets the key essential to add a comment
79     * @param idImage the id of the image
80     * @return the key
81     * @throws IOException
82     * @throws ProxyAuthenticationException
83     */
84    public String getKey(Integer idImage) throws JiwigoException {
85        Document doc = (sessionManager.executeReturnDocument(MethodsEnum.GET_INFO.getLabel(), "image_id",
86                String.valueOf(idImage)));
87        //      String key = doc.getRootElement().getChild("image").getChild("comment_post").getAttributeValue("key");
88        Element elementImage = (Element) doc.getDocumentElement().getElementsByTagName("image").item(0);
89        Element elementCommentPost = (Element) elementImage.getElementsByTagName("comment_post").item(0);
90        return elementCommentPost.getAttribute("key");
91    }
92
93    /**
94     * Creates a comment
95     * @param commentaire the comment to add
96     * @return true if the comment is successfully added
97     * @throws IOException
98     * @throws ProxyAuthenticationException
99     */
100    public boolean create(Comment commentaire) throws JiwigoException {
101        String key = getKey(commentaire.getImageId());
102        try {
103            Thread.sleep(2200);
104        } catch (InterruptedException e) {
105            e.printStackTrace();
106        }
107        try {
108            return Tools.checkOk(sessionManager.executeReturnDocument(MethodsEnum.AJOUTER_COMMENTAIRE.getLabel(),
109                    "image_id", String.valueOf(commentaire.getImageId()), "author", commentaire.getAuthor(), "content",
110                    commentaire.getContent(), "key", key));
111        } catch (FileAlreadyExistsException e) {
112            LOG.error(Tools.getStackTrace(e));
113            return false;
114        }
115
116    }
117
118    public SessionManager getSessionManager() {
119        return sessionManager;
120    }
121
122    public void setSessionManager(SessionManager sessionManager) {
123        this.sessionManager = sessionManager;
124    }
125
126}
Note: See TracBrowser for help on using the repository browser.