/Flickr4Java/src/main/java/com/flickr4java/flickr/groups/discuss/GroupDiscussInterface.java

https://gitlab.com/sugesk/flickr4java · Java · 238 lines · 153 code · 36 blank · 49 comment · 12 complexity · b688d742eae38417998aba05711e590c MD5 · raw file

  1. package com.flickr4java.flickr.groups.discuss;
  2. import com.flickr4java.flickr.FlickrException;
  3. import com.flickr4java.flickr.Response;
  4. import com.flickr4java.flickr.Transport;
  5. import com.flickr4java.flickr.util.XMLUtilities;
  6. import org.w3c.dom.Element;
  7. import org.w3c.dom.NodeList;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. public class GroupDiscussInterface {
  11. /**
  12. * Group.Discuss Interface.
  13. *
  14. * @author Jonathan Willis
  15. */
  16. public static final String METHOD_TOPICS_GET_LIST = "flickr.groups.discuss.topics.getList";
  17. public static final String METHOD_TOPICS_GET_INFO = "flickr.groups.discuss.topics.getInfo";
  18. public static final String METHOD_REPLIES_GET_LIST = "flickr.groups.discuss.replies.getList";
  19. public static final String METHOD_REPLIES_GET_INFO = "flickr.groups.discuss.replies.getInfo";
  20. private final String apiKey;
  21. private final String sharedSecret;
  22. private final Transport transportAPI;
  23. public GroupDiscussInterface(String apiKey, String sharedSecret, Transport transportAPI) {
  24. this.apiKey = apiKey;
  25. this.sharedSecret = sharedSecret;
  26. this.transportAPI = transportAPI;
  27. }
  28. /**
  29. * Get a list of topics from a group.
  30. *
  31. * @param groupId
  32. * Unique identifier of a group returns a list of topics for a given group {@link Group}.
  33. * @param perPage
  34. * Number of records per page.
  35. * @param page
  36. * Result-section.
  37. * @return A group topic list
  38. * @throws FlickrException
  39. * @see <a href="http://www.flickr.com/services/api/flickr.groups.discuss.topics.getList.html">API Documentation</a>
  40. */
  41. public TopicList<Topic> getTopicsList(String groupId, int perPage, int page) throws FlickrException {
  42. TopicList<Topic> topicList = new TopicList<Topic>();
  43. Map<String, Object> parameters = new HashMap<String, Object>();
  44. parameters.put("method", METHOD_TOPICS_GET_LIST);
  45. parameters.put("group_id", groupId);
  46. if (perPage > 0) {
  47. parameters.put("per_page", "" + perPage);
  48. }
  49. if (page > 0) {
  50. parameters.put("page", "" + page);
  51. }
  52. Response response = transportAPI.get(transportAPI.getPath(), parameters, apiKey, sharedSecret);
  53. if (response.isError()) {
  54. throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
  55. }
  56. Element topicElements = response.getPayload();
  57. topicList.setPage(topicElements.getAttribute("page"));
  58. topicList.setPages(topicElements.getAttribute("pages"));
  59. topicList.setPerPage(topicElements.getAttribute("perpage"));
  60. topicList.setTotal(topicElements.getAttribute("total"));
  61. topicList.setGroupId(topicElements.getAttribute("group_id"));
  62. topicList.setIconServer(Integer.parseInt(topicElements.getAttribute("iconserver")));
  63. topicList.setIconFarm(Integer.parseInt(topicElements.getAttribute("iconfarm")));
  64. topicList.setName(topicElements.getAttribute("name"));
  65. topicList.setMembers(Integer.parseInt(topicElements.getAttribute("members")));
  66. topicList.setPrivacy(Integer.parseInt(topicElements.getAttribute("privacy")));
  67. topicList.setLanguage(topicElements.getAttribute("lang"));
  68. topicList.setIsPoolModerated("1".equals(topicElements.getAttribute("ispoolmoderated")));
  69. NodeList topicNodes = topicElements.getElementsByTagName("topic");
  70. for (int i = 0; i < topicNodes.getLength(); i++) {
  71. Element element = (Element) topicNodes.item(i);
  72. topicList.add(parseTopic(element));
  73. }
  74. return topicList;
  75. }
  76. /**
  77. * Get info for a given topic
  78. *
  79. * @param topicId
  80. * Unique identifier of a topic for a given group {@link Topic}.
  81. * @return A group topic
  82. * @throws FlickrException
  83. * @see <a href="http://www.flickr.com/services/api/flickr.groups.discuss.topics.getInfo.html">API Documentation</a>
  84. */
  85. public Topic getTopicInfo(String topicId) throws FlickrException {
  86. Map<String, Object> parameters = new HashMap<String, Object>();
  87. parameters.put("method", METHOD_TOPICS_GET_INFO);
  88. parameters.put("topic_id", topicId);
  89. Response response = transportAPI.get(transportAPI.getPath(), parameters, apiKey, sharedSecret);
  90. if (response.isError()) {
  91. throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
  92. }
  93. Element topicElement = response.getPayload();
  94. return parseTopic(topicElement);
  95. }
  96. /**
  97. * Get list of replies
  98. *
  99. * @param topicId
  100. * Unique identifier of a topic for a given group {@link Topic}.
  101. * @return A reply object
  102. * @throws FlickrException
  103. * @see <a href="http://www.flickr.com/services/api/flickr.groups.discuss.replies.getList.html">API Documentation</a>
  104. */
  105. public ReplyObject getReplyList(String topicId, int perPage, int page) throws FlickrException {
  106. ReplyList<Reply> reply = new ReplyList<Reply>();
  107. TopicList<Topic> topic = new TopicList<Topic>();
  108. Map<String, Object> parameters = new HashMap<String, Object>();
  109. parameters.put("method", METHOD_REPLIES_GET_LIST);
  110. parameters.put("topic_id", topicId);
  111. if (perPage > 0) {
  112. parameters.put("per_page", "" + perPage);
  113. }
  114. if (page > 0) {
  115. parameters.put("page", "" + page);
  116. }
  117. Response response = transportAPI.get(transportAPI.getPath(), parameters, apiKey, sharedSecret);
  118. if (response.isError()) {
  119. throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
  120. }
  121. Element replyElements = response.getPayload();
  122. ReplyObject ro = new ReplyObject();
  123. NodeList replyNodes = replyElements.getElementsByTagName("reply");
  124. for (int i = 0; i < replyNodes.getLength(); i++) {
  125. Element replyNodeElement = (Element) replyNodes.item(i);
  126. // Element replyElement = XMLUtilities.getChild(replyNodeElement, "reply");
  127. reply.add(parseReply(replyNodeElement));
  128. ro.setReplyList(reply);
  129. }
  130. NodeList topicNodes = replyElements.getElementsByTagName("topic");
  131. for (int i = 0; i < topicNodes.getLength(); i++) {
  132. Element replyNodeElement = (Element) replyNodes.item(i);
  133. // Element topicElement = XMLUtilities.getChild(replyNodeElement, "topic");
  134. topic.add(parseTopic(replyNodeElement));
  135. ro.setTopicList(topic);
  136. }
  137. return ro;
  138. }
  139. /**
  140. * Get info for a given topic reply
  141. *
  142. * @param topicId
  143. * Unique identifier of a topic for a given group {@link Topic}.
  144. * @param replyId
  145. * Unique identifier of a reply for a given topic {@link Reply}.
  146. * @return A group topic
  147. * @throws FlickrException
  148. * @see <a href="http://www.flickr.com/services/api/flickr.groups.discuss.replies.getInfo.html">API Documentation</a>
  149. */
  150. public Reply getReplyInfo(String topicId, String replyId) throws FlickrException {
  151. Map<String, Object> parameters = new HashMap<String, Object>();
  152. parameters.put("method", METHOD_REPLIES_GET_INFO);
  153. parameters.put("topic_id", topicId);
  154. parameters.put("reply_id", replyId);
  155. Response response = transportAPI.get(transportAPI.getPath(), parameters, apiKey, sharedSecret);
  156. if (response.isError()) {
  157. throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
  158. }
  159. Element replyElement = response.getPayload();
  160. return parseReply(replyElement);
  161. }
  162. private Topic parseTopic(Element tElement) {
  163. Topic topic = new Topic();
  164. topic.setAuthorId(tElement.getAttribute("author"));
  165. topic.setAuthorname(tElement.getAttribute("authorname"));
  166. topic.setIsCanDelete("1".equals(tElement.getAttribute("can_delete")));
  167. topic.setIsCanEdit("1".equals(tElement.getAttribute("can_edit")));
  168. topic.setIsCanReply("1".equals(tElement.getAttribute("can_reply")));
  169. if (!tElement.getAttribute("count_replies").equals("")) {
  170. topic.setCountReplies(Integer.parseInt(tElement.getAttribute("count_replies")));
  171. }
  172. topic.setDatecreate(tElement.getAttribute("datecreate"));
  173. topic.setDatelastpost(tElement.getAttribute("datelastpost"));
  174. topic.setIconfarm(Integer.parseInt(tElement.getAttribute("iconfarm")));
  175. topic.setIconserver(Integer.parseInt(tElement.getAttribute("iconserver")));
  176. topic.setIsLocked("1".equals(tElement.getAttribute("is_locked")));
  177. topic.setMessage(XMLUtilities.getChildValue(tElement, "message"));
  178. topic.setRole(tElement.getAttribute("role"));
  179. topic.setIsSticky("1".equals(tElement.getAttribute("is_sticky")));
  180. topic.setSubject(tElement.getAttribute("subject"));
  181. topic.setTopicId(tElement.getAttribute("id"));
  182. topic.setIsPro("1".equals(tElement.getAttribute("is_pro")));
  183. topic.setLastReply(tElement.getAttribute("last_reply"));
  184. return topic;
  185. }
  186. private Reply parseReply(Element rElement) {
  187. Reply reply = new Reply();
  188. reply.setAuthorId(rElement.getAttribute("author"));
  189. reply.setAuthorname(rElement.getAttribute("authorname"));
  190. reply.setIsCanDelete("1".equals(rElement.getAttribute("can_delete")));
  191. reply.setIsCanEdit("1".equals(rElement.getAttribute("can_edit")));
  192. reply.setDatecreate(rElement.getAttribute("datecreate"));
  193. reply.setLastEdit(rElement.getAttribute("lastedit"));
  194. reply.setIconfarm(Integer.parseInt(rElement.getAttribute("iconfarm")));
  195. reply.setIconserver(Integer.parseInt(rElement.getAttribute("iconserver")));
  196. reply.setMessage(XMLUtilities.getChildValue(rElement, "message"));
  197. reply.setRole(rElement.getAttribute("role"));
  198. reply.setReplyId(rElement.getAttribute("id"));
  199. reply.setIsPro("1".equals(rElement.getAttribute("is_pro")));
  200. return reply;
  201. }
  202. }