PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Flickr4Java/src/main/java/com/flickr4java/flickr/groups/GroupsInterface.java

https://gitlab.com/sugesk/flickr4java
Java | 326 lines | 205 code | 44 blank | 77 comment | 33 complexity | 234fd29e652b30d556dd7dd9501bd1cf MD5 | raw file
  1. /*
  2. * Copyright (c) 2005 Aetrion LLC.
  3. */
  4. package com.flickr4java.flickr.groups;
  5. import com.flickr4java.flickr.FlickrException;
  6. import com.flickr4java.flickr.Response;
  7. import com.flickr4java.flickr.Transport;
  8. import com.flickr4java.flickr.util.XMLUtilities;
  9. import org.apache.log4j.Logger;
  10. import org.w3c.dom.Element;
  11. import org.w3c.dom.NodeList;
  12. import java.util.ArrayList;
  13. import java.util.Collection;
  14. import java.util.HashMap;
  15. import java.util.List;
  16. import java.util.Map;
  17. /**
  18. * Interface for working with Flickr Groups.
  19. *
  20. * @author Anthony Eden
  21. * @version $Id: GroupsInterface.java,v 1.19 2009/07/11 20:30:27 x-mago Exp $
  22. */
  23. public class GroupsInterface {
  24. private static Logger _log = Logger.getLogger(GroupsInterface.class);
  25. public static final String METHOD_BROWSE = "flickr.groups.browse";
  26. public static final String METHOD_GET_ACTIVE_LIST = "flickr.groups.getActiveList";
  27. public static final String METHOD_GET_INFO = "flickr.groups.getInfo";
  28. public static final String METHOD_SEARCH = "flickr.groups.search";
  29. public static final String METHOD_JOIN = "flickr.groups.join";
  30. public static final String METHOD_JOIN_REQUEST = "flickr.groups.joinRequest";
  31. public static final String METHOD_LEAVE = "flickr.groups.leave";
  32. private final String apiKey;
  33. private final String sharedSecret;
  34. private final Transport transportAPI;
  35. public GroupsInterface(String apiKey, String sharedSecret, Transport transportAPI) {
  36. this.apiKey = apiKey;
  37. this.sharedSecret = sharedSecret;
  38. this.transportAPI = transportAPI;
  39. }
  40. /**
  41. * Browse groups for the given category ID. If a null value is passed for the category then the root category is used.
  42. *
  43. * @param catId
  44. * The optional category id. Null value will be ignored.
  45. * @return The Collection of Photo objects
  46. * @throws FlickrException
  47. * @deprecated Flickr returns just empty results
  48. */
  49. @Deprecated
  50. public Category browse(String catId) throws FlickrException {
  51. List<Subcategory> subcategories = new ArrayList<Subcategory>();
  52. List<Group> groups = new ArrayList<Group>();
  53. Map<String, Object> parameters = new HashMap<String, Object>();
  54. parameters.put("method", METHOD_BROWSE);
  55. if (catId != null) {
  56. parameters.put("cat_id", catId);
  57. }
  58. Response response = transportAPI.get(transportAPI.getPath(), parameters, apiKey, sharedSecret);
  59. if (response.isError()) {
  60. throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
  61. }
  62. Element categoryElement = response.getPayload();
  63. Category category = new Category();
  64. category.setName(categoryElement.getAttribute("name"));
  65. category.setPath(categoryElement.getAttribute("path"));
  66. category.setPathIds(categoryElement.getAttribute("pathids"));
  67. NodeList subcatNodes = categoryElement.getElementsByTagName("subcat");
  68. for (int i = 0; i < subcatNodes.getLength(); i++) {
  69. Element node = (Element) subcatNodes.item(i);
  70. Subcategory subcategory = new Subcategory();
  71. subcategory.setId(Integer.parseInt(node.getAttribute("id")));
  72. subcategory.setName(node.getAttribute("name"));
  73. subcategory.setCount(Integer.parseInt(node.getAttribute("count")));
  74. subcategories.add(subcategory);
  75. }
  76. NodeList groupNodes = categoryElement.getElementsByTagName("group");
  77. for (int i = 0; i < groupNodes.getLength(); i++) {
  78. Element node = (Element) groupNodes.item(i);
  79. Group group = new Group();
  80. group.setId(node.getAttribute("nsid"));
  81. group.setName(node.getAttribute("name"));
  82. group.setMembers(node.getAttribute("members"));
  83. groups.add(group);
  84. }
  85. category.setGroups(groups);
  86. category.setSubcategories(subcategories);
  87. return category;
  88. }
  89. /**
  90. * Get the info for a specified group.
  91. *
  92. * This method does not require authentication.
  93. *
  94. * @param groupId
  95. * The group id
  96. * @return The Group object
  97. */
  98. public Group getInfo(String groupId) throws FlickrException {
  99. Map<String, Object> parameters = new HashMap<String, Object>();
  100. parameters.put("method", METHOD_GET_INFO);
  101. parameters.put("group_id", groupId);
  102. Response response = transportAPI.get(transportAPI.getPath(), parameters, apiKey, sharedSecret);
  103. if (response.isError()) {
  104. throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
  105. }
  106. Element groupElement = response.getPayload();
  107. Group group = new Group();
  108. group.setId(groupElement.getAttribute("id"));
  109. group.setIconFarm(groupElement.getAttribute("iconfarm"));
  110. group.setIconServer(groupElement.getAttribute("iconserver"));
  111. group.setLang(groupElement.getAttribute("lang"));
  112. group.setPoolModerated(groupElement.getAttribute("ispoolmoderated").equals("0") ? false : true);
  113. group.setName(XMLUtilities.getChildValue(groupElement, "name"));
  114. group.setDescription(XMLUtilities.getChildValue(groupElement, "description"));
  115. group.setMembers(XMLUtilities.getChildValue(groupElement, "members"));
  116. group.setPrivacy(XMLUtilities.getChildValue(groupElement, "privacy"));
  117. group.setPoolCount(XMLUtilities.getChildValue(groupElement, "pool_count"));
  118. group.setTopicCount(XMLUtilities.getChildValue(groupElement, "topic_count"));
  119. NodeList throttleNodes = groupElement.getElementsByTagName("throttle");
  120. int n = throttleNodes.getLength();
  121. if (n == 1) {
  122. Element throttleElement = (Element) throttleNodes.item(0);
  123. Throttle throttle = new Throttle();
  124. group.setThrottle(throttle);
  125. throttle.setMode(throttleElement.getAttribute("mode"));
  126. String countStr = throttleElement.getAttribute("count");
  127. String remainingStr = throttleElement.getAttribute("remaining");
  128. if (countStr != null && countStr.length() > 0) {
  129. throttle.setCount(Integer.parseInt(countStr));
  130. }
  131. if (remainingStr != null && remainingStr.length() > 0) {
  132. throttle.setRemaining(Integer.parseInt(remainingStr));
  133. }
  134. } else if (n > 1) {
  135. _log.warn("WARNING: more than one throttle element in group");
  136. }
  137. NodeList restrictionNodes = groupElement.getElementsByTagName("restrictions");
  138. n = restrictionNodes.getLength();
  139. if (n == 1) {
  140. Element restrictionElement = (Element) restrictionNodes.item(0);
  141. Restriction restriction = new Restriction();
  142. group.setRestriction(restriction);
  143. restriction.setIsPhotosOk("1".equals(restrictionElement.getAttribute("photos_ok")));
  144. restriction.setIsVideosOk("1".equals(restrictionElement.getAttribute("videos_ok")));
  145. restriction.setIsImagesOk("1".equals(restrictionElement.getAttribute("images_ok")));
  146. restriction.setIsScreensOk("1".equals(restrictionElement.getAttribute("screens_ok")));
  147. restriction.setIsArtOk("1".equals(restrictionElement.getAttribute("art_ok")));
  148. restriction.setIsSafeOk("1".equals(restrictionElement.getAttribute("safe_ok")));
  149. restriction.setIsModerateOk("1".equals(restrictionElement.getAttribute("moderate_ok")));
  150. restriction.setIsRestrictedOk("1".equals(restrictionElement.getAttribute("restricted_ok")));
  151. restriction.setIsHasGeo("1".equals(restrictionElement.getAttribute("has_geo")));
  152. } else if (n > 1) {
  153. _log.warn("WARNING: more than one throttle element in group");
  154. }
  155. NodeList blastNodes = groupElement.getElementsByTagName("blast");
  156. n = blastNodes.getLength();
  157. if (n == 1) {
  158. Element blastElement = (Element) blastNodes.item(0);
  159. Blast blast = new Blast();
  160. group.setBlast(blast);
  161. blast.setUserId(blastElement.getAttribute("user_id"));
  162. blast.setDateBlastAdded(blastElement.getAttribute("date_blast_added"));
  163. blast.setBlast(XMLUtilities.getChildValue(groupElement, "blast"));
  164. } else if (n > 1) {
  165. _log.warn("WARNING: more than one throttle element in group");
  166. }
  167. return group;
  168. }
  169. /**
  170. * Search for groups. 18+ groups will only be returned for authenticated calls where the authenticated user is over 18. This method does not require
  171. * authentication.
  172. *
  173. * @param text
  174. * The text to search for.
  175. * @param perPage
  176. * Number of groups to return per page. If this argument is 0, it defaults to 100. The maximum allowed value is 500.
  177. * @param page
  178. * The page of results to return. If this argument is 0, it defaults to 1.
  179. * @return A GroupList Object. Only the fields <em>id</em>, <em>name</em> and <em>eighteenplus</em> in the Groups will be set.
  180. * @throws FlickrException
  181. */
  182. public Collection<Group> search(String text, int perPage, int page) throws FlickrException {
  183. GroupList<Group> groupList = new GroupList<Group>();
  184. Map<String, Object> parameters = new HashMap<String, Object>();
  185. parameters.put("method", METHOD_SEARCH);
  186. parameters.put("text", text);
  187. if (perPage > 0) {
  188. parameters.put("per_page", String.valueOf(perPage));
  189. }
  190. if (page > 0) {
  191. parameters.put("page", String.valueOf(page));
  192. }
  193. Response response = transportAPI.get(transportAPI.getPath(), parameters, apiKey, sharedSecret);
  194. if (response.isError()) {
  195. throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
  196. }
  197. Element groupsElement = response.getPayload();
  198. NodeList groupNodes = groupsElement.getElementsByTagName("group");
  199. groupList.setPage(XMLUtilities.getIntAttribute(groupsElement, "page"));
  200. groupList.setPages(XMLUtilities.getIntAttribute(groupsElement, "pages"));
  201. groupList.setPerPage(XMLUtilities.getIntAttribute(groupsElement, "perpage"));
  202. groupList.setTotal(XMLUtilities.getIntAttribute(groupsElement, "total"));
  203. for (int i = 0; i < groupNodes.getLength(); i++) {
  204. Element groupElement = (Element) groupNodes.item(i);
  205. Group group = new Group();
  206. group.setId(groupElement.getAttribute("nsid"));
  207. group.setName(groupElement.getAttribute("name"));
  208. groupList.add(group);
  209. }
  210. return groupList;
  211. }
  212. /**
  213. * Join a group as a public member.
  214. *
  215. * Note: if a group has rules - the client must display the rules to the user and the user must accept them prior to joining the group. The acceptRules
  216. * parameter indicates that the user has accepted those rules.
  217. *
  218. * @param groupId
  219. * - the id of the group to join
  220. * @param acceptRules
  221. * - if a group has rules, true indicates the user has accepted the rules
  222. *
  223. * @see <a href="http://www.flickr.com/services/api/flickr.groups.join.html">flickr.groups.join</a>
  224. */
  225. public void join(String groupId, Boolean acceptRules) throws FlickrException {
  226. Map<String, Object> parameters = new HashMap<String, Object>();
  227. parameters.put("method", METHOD_JOIN);
  228. parameters.put("group_id", groupId);
  229. if (acceptRules != null) {
  230. parameters.put("accept_rules", acceptRules);
  231. }
  232. Response response = transportAPI.post(transportAPI.getPath(), parameters, apiKey, sharedSecret);
  233. if (response.isError()) {
  234. throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
  235. }
  236. }
  237. /**
  238. * Request to join a group.
  239. *
  240. * Note: if a group has rules, the client must display the rules to the user and the user must accept them (which is indicated by passing a true value to
  241. * acceptRules) prior to making the join request.
  242. *
  243. * @param groupId
  244. * - groupId parameter
  245. * @param message
  246. * - (required) message to group administrator
  247. * @param acceptRules
  248. * - (required) parameter indicating user has accepted groups rules
  249. */
  250. public void joinRequest(String groupId, String message, boolean acceptRules) throws FlickrException {
  251. Map<String, Object> parameters = new HashMap<String, Object>();
  252. parameters.put("method", METHOD_JOIN_REQUEST);
  253. parameters.put("group_id", groupId);
  254. parameters.put("message", message);
  255. parameters.put("accept_rules", acceptRules);
  256. Response response = transportAPI.post(transportAPI.getPath(), parameters, apiKey, sharedSecret);
  257. if (response.isError()) {
  258. throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
  259. }
  260. }
  261. /**
  262. * Leave a group.
  263. *
  264. * @see <a href="http://www.flickr.com/services/api/flickr.groups.leave.html">lickr.groups.leave</a> for a description of the various behaviors possible
  265. * when a user leaves a group.
  266. *
  267. * @param groupId
  268. * - the id of the group to leave
  269. * @param deletePhotos
  270. * - delete photos by this user from group
  271. */
  272. public void leave(String groupId, Boolean deletePhotos) throws FlickrException {
  273. Map<String, Object> parameters = new HashMap<String, Object>();
  274. parameters.put("method", METHOD_LEAVE);
  275. parameters.put("group_id", groupId);
  276. parameters.put("delete_photos", deletePhotos);
  277. Response response = transportAPI.post(transportAPI.getPath(), parameters, apiKey, sharedSecret);
  278. if (response.isError()) {
  279. throw new FlickrException(response.getErrorCode(), response.getErrorMessage());
  280. }
  281. }
  282. }