Ignore:
Timestamp:
Mar 29, 2011, 8:21:25 PM (13 years ago)
Author:
mlg
Message:

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:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/dao/CommentDao.java

    r9902 r9919  
    22
    33import java.io.IOException;
    4 import java.util.ArrayList;
    54import java.util.List;
    65
    7 import org.w3c.dom.Document;
    8 import org.w3c.dom.Element;
    9 import org.w3c.dom.Node;
    10 import org.w3c.dom.NodeList;
     6import fr.mael.jiwigo.om.Comment;
     7import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
    118
    12 import fr.mael.jiwigo.om.Comment;
    13 import fr.mael.jiwigo.transverse.enumeration.MethodsEnum;
    14 import fr.mael.jiwigo.transverse.exception.FileAlreadyExistsException;
    15 import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException;
    16 import fr.mael.jiwigo.transverse.session.SessionManager;
    17 import fr.mael.jiwigo.transverse.util.Tools;
    18 
    19 /*
    20  *  jiwigo-ws-api Piwigo webservice access Api
    21  *  Copyright (c) 2010-2011 Mael mael@le-guevel.com
    22  *                All Rights Reserved
    23  *
    24  *  This library is free software. It comes without any warranty, to
    25  *  the extent permitted by applicable law. You can redistribute it
    26  *  and/or modify it under the terms of the Do What The Fuck You Want
    27  *  To Public License, Version 2, as published by Sam Hocevar. See
    28  *  http://sam.zoy.org/wtfpl/COPYING for more details.
    29  */
    30 /**
    31 
    32  * Dao for the comments
    33  * @author mael
    34  *
    35  */
    36 public class CommentDao {
    37     /**
    38      * Logger
    39      */
    40     public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
    41             .getLog(CommentDao.class);
    42     /**
    43      *  Instance that allows to use a singleton
    44      */
    45     private static CommentDao instance;
    46 
    47     private SessionManager sessionManager;
    48 
    49     /**
    50      * private constructor, to use a singleton
    51      */
    52     private CommentDao(SessionManager sessionManager) {
    53         this.sessionManager = sessionManager;
    54     }
    55 
    56     /**
    57      * @return the singleton
    58      */
    59     public static CommentDao getInstance(SessionManager sessionManager) {
    60         if (instance == null) {
    61             instance = new CommentDao(sessionManager);
    62         }
    63         return instance;
    64     }
    65 
     9public interface CommentDao {
    6610    /**
    6711     * Listing of the comments for the given image
     
    7115     * @throws ProxyAuthenticationException
    7216     */
    73     public List<Comment> list(Integer idImage) throws IOException, ProxyAuthenticationException {
    74         Document doc = (sessionManager.executeReturnDocument(MethodsEnum.GET_INFO.getLabel(), "image_id", String
    75                 .valueOf(idImage), "comments_per_page", "100"));
    76         Element elementImage = (Element) doc.getDocumentElement().getElementsByTagName("image").item(0);
    77         Element elementComments = (Element) elementImage.getElementsByTagName("comments").item(0);
    78         NodeList listComments = elementComments.getElementsByTagName("comment");
    79         ArrayList<Comment> comments = new ArrayList<Comment>();
    80         for (int i = 0; i < listComments.getLength(); i++) {
    81             Node nodeCom = listComments.item(i);
    82             if (nodeCom.getNodeType() == Node.ELEMENT_NODE) {
    83                 Element com = (Element) nodeCom;
    84                 Comment myCom = new Comment();
    85                 myCom.setIdentifier(Integer.valueOf(com.getAttribute("id")));
    86                 myCom.setDate(com.getAttribute("date"));
    87                 myCom.setAuthor(Tools.getStringValueDom(com, "author"));
    88                 myCom.setContent(Tools.getStringValueDom(com, "content"));
    89                 comments.add(myCom);
    90             }
    91         }
    92         return comments;
    93     }
     17    public List<Comment> list(Integer idImage) throws IOException, ProxyAuthenticationException;
    9418
    9519    /**
     
    10024     * @throws ProxyAuthenticationException
    10125     */
    102     public String getKey(Integer idImage) throws IOException, ProxyAuthenticationException {
    103         Document doc = (sessionManager.executeReturnDocument(MethodsEnum.GET_INFO.getLabel(), "image_id", String
    104                 .valueOf(idImage)));
    105         //      String key = doc.getRootElement().getChild("image").getChild("comment_post").getAttributeValue("key");
    106         Element elementImage = (Element) doc.getDocumentElement().getElementsByTagName("image").item(0);
    107         Element elementCommentPost = (Element) elementImage.getElementsByTagName("comment_post").item(0);
    108         return elementCommentPost.getAttribute("key");
    109     }
     26    public String getKey(Integer idImage) throws IOException, ProxyAuthenticationException;
    11027
    11128    /**
     
    11633     * @throws ProxyAuthenticationException
    11734     */
    118     public boolean create(Comment commentaire) throws IOException, ProxyAuthenticationException {
    119         String key = getKey(commentaire.getImageId());
    120         try {
    121             Thread.sleep(2200);
    122         } catch (InterruptedException e) {
    123             e.printStackTrace();
    124         }
    125         try {
    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         } catch (FileAlreadyExistsException e) {
    130             LOG.error(Tools.getStackTrace(e));
    131             return false;
    132         }
    133 
    134     }
     35    public boolean create(Comment commentaire) throws IOException, ProxyAuthenticationException;
    13536}
Note: See TracChangeset for help on using the changeset viewer.