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

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

Complete rewriting of the api
The api now uses interfaces everywhere (for the dao, services and session manager). This allows the developer to use his own implementations if he wants.
Deletion of the singletons. There are now getters and setters. It allows to make dependency injection.
Use of sl4j in the api, instead of using log4j.

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