- Timestamp:
- Mar 29, 2011, 8:21:25 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
extensions/jiwigo-ws-api/src/main/java/fr/mael/jiwigo/dao/CommentDao.java
r9902 r9919 2 2 3 3 import java.io.IOException; 4 import java.util.ArrayList;5 4 import java.util.List; 6 5 7 import org.w3c.dom.Document; 8 import org.w3c.dom.Element; 9 import org.w3c.dom.Node; 10 import org.w3c.dom.NodeList; 6 import fr.mael.jiwigo.om.Comment; 7 import fr.mael.jiwigo.transverse.exception.ProxyAuthenticationException; 11 8 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 9 public interface CommentDao { 66 10 /** 67 11 * Listing of the comments for the given image … … 71 15 * @throws ProxyAuthenticationException 72 16 */ 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; 94 18 95 19 /** … … 100 24 * @throws ProxyAuthenticationException 101 25 */ 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; 110 27 111 28 /** … … 116 33 * @throws ProxyAuthenticationException 117 34 */ 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; 135 36 }
Note: See TracChangeset
for help on using the changeset viewer.