|
| 1 | +/** |
| 2 | + * @author acaplan |
| 3 | + */ |
| 4 | +package com.flickr4java.flickr.galleries; |
| 5 | + |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +import org.w3c.dom.Element; |
| 11 | +import org.w3c.dom.NodeList; |
| 12 | + |
| 13 | +import com.flickr4java.flickr.Flickr; |
| 14 | +import com.flickr4java.flickr.FlickrException; |
| 15 | +import com.flickr4java.flickr.Response; |
| 16 | +import com.flickr4java.flickr.Transport; |
| 17 | +import com.flickr4java.flickr.people.User; |
| 18 | +import com.flickr4java.flickr.util.XMLUtilities; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author acaplan |
| 22 | + * |
| 23 | + */ |
| 24 | +public class GalleriesInterface { |
| 25 | + |
| 26 | + public static final String METHOD_ADD_PHOTO = "flickr.galleries.addPhoto"; |
| 27 | + public static final String METHOD_CREATE = "flickr.galleries.create"; |
| 28 | + public static final String METHOD_EDIT_META = "flickr.galleries.editMeta"; |
| 29 | + public static final String METHOD_EDIT_PHOTO = "flickr.galleries.editPhoto"; |
| 30 | + public static final String METHOD_EDIT_PHOTOS = "flickr.galleries.editPhotos"; |
| 31 | + public static final String METHOD_GET_INFO = "flickr.galleries.getInfo"; |
| 32 | + public static final String METHOD_GET_LIST = "flickr.galleries.getList"; |
| 33 | + public static final String METHOD_GET_LIST_FOR_PHOTO = "flickr.galleries.getListForPhoto"; |
| 34 | + public static final String METHOD_GET_PHOTOS = "flickr.galleries.getPhotos"; |
| 35 | + |
| 36 | + private String apiKey; |
| 37 | + private String sharedSecret; |
| 38 | + private Transport transport; |
| 39 | + |
| 40 | + /** |
| 41 | + * Construct a GalleriesInterface. |
| 42 | + * |
| 43 | + * @param apiKey |
| 44 | + * The API key |
| 45 | + * @param transportAPI |
| 46 | + * The Transport interface |
| 47 | + */ |
| 48 | + public GalleriesInterface(String apiKey, String sharedSecret, Transport transportAPI) { |
| 49 | + this.apiKey = apiKey; |
| 50 | + this.sharedSecret = sharedSecret; |
| 51 | + this.transport = transportAPI; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Return the list of galleries created by a user. Sorted from newest to oldest. |
| 56 | + * |
| 57 | + * @param userId |
| 58 | + * The user you want to check for |
| 59 | + * @param perPage |
| 60 | + * Number of galleries per page |
| 61 | + * @param page |
| 62 | + * The page number |
| 63 | + * @return gallery |
| 64 | + * @throws FlickrException |
| 65 | + * |
| 66 | + * @see http://www.flickr.com/services/api/flickr.galleries.getList.html |
| 67 | + */ |
| 68 | + public List<Gallery> getList(String userId, int perPage, int page) throws FlickrException { |
| 69 | + Map<String, Object> parameters = new HashMap<String, Object>(); |
| 70 | + parameters.put("method", METHOD_GET_LIST); |
| 71 | + parameters.put(Flickr.API_KEY, apiKey); |
| 72 | + parameters.put("user_id", userId); |
| 73 | + if (perPage > 0) { |
| 74 | + parameters.put("per_page", String.valueOf(perPage)); |
| 75 | + } |
| 76 | + if (page > 0) { |
| 77 | + parameters.put("page", String.valueOf(page)); |
| 78 | + } |
| 79 | + |
| 80 | + Response response = transport.post(transport.getPath(), parameters, sharedSecret); |
| 81 | + if (response.isError()) { |
| 82 | + throw new FlickrException(response.getErrorCode(), response.getErrorMessage()); |
| 83 | + } |
| 84 | + |
| 85 | + Element element = response.getPayload(); |
| 86 | + GalleryList<Gallery> galleries = new GalleryList<Gallery>(); |
| 87 | + galleries.setPage(element.getAttribute("page")); |
| 88 | + galleries.setPages(element.getAttribute("pages")); |
| 89 | + galleries.setPerPage(element.getAttribute("per_page")); |
| 90 | + galleries.setTotal(element.getAttribute("total")); |
| 91 | + |
| 92 | + NodeList galleryNodes = element.getElementsByTagName("gallery"); |
| 93 | + for (int i = 0; i < galleryNodes.getLength(); i++) { |
| 94 | + Element galleryElement = (Element) galleryNodes.item(i); |
| 95 | + Gallery gallery = new Gallery(); |
| 96 | + gallery.setId(galleryElement.getAttribute("id")); |
| 97 | + gallery.setUrl(galleryElement.getAttribute("url")); |
| 98 | + |
| 99 | + User owner = new User(); |
| 100 | + owner.setId(galleryElement.getAttribute("owner")); |
| 101 | + gallery.setOwner(owner); |
| 102 | + gallery.setCreateDate(galleryElement.getAttribute("date_create")); |
| 103 | + gallery.setUpdateDate(galleryElement.getAttribute("date_update")); |
| 104 | + gallery.setPrimaryPhotoId(galleryElement.getAttribute("primary_photo_id")); |
| 105 | + gallery.setPrimaryPhotoServer(galleryElement.getAttribute("primary_photo_server")); |
| 106 | + gallery.setPrimaryPhotoFarm(galleryElement.getAttribute("primary_photo_farm")); |
| 107 | + gallery.setPrimaryPhotoSecret(galleryElement.getAttribute("primary_photo_secret")); |
| 108 | + gallery.setPhotoCount(galleryElement.getAttribute("count_photos")); |
| 109 | + gallery.setVideoCount(galleryElement.getAttribute("count_videos")); |
| 110 | + |
| 111 | + galleries.add(gallery); |
| 112 | + } |
| 113 | + return galleries; |
| 114 | + } |
| 115 | + |
| 116 | + public void addPhoto(String strGalleryId, String photoId, String strComment) throws FlickrException { |
| 117 | + Map<String, Object> parameters = new HashMap<String, Object>(); |
| 118 | + parameters.put("method", METHOD_ADD_PHOTO); |
| 119 | + parameters.put(Flickr.API_KEY, apiKey); |
| 120 | + parameters.put("gallery_id", strGalleryId); |
| 121 | + parameters.put("photo_id", photoId); |
| 122 | + parameters.put("comment", strComment); |
| 123 | + |
| 124 | + Response response = transport.post(transport.getPath(), parameters, sharedSecret); |
| 125 | + if (response.isError()) { |
| 126 | + throw new FlickrException(response.getErrorCode(), response.getErrorMessage()); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + public void editMeta(String strGalleryId, String strTitle, String strDescription) throws FlickrException { |
| 131 | + Map<String, Object> parameters = new HashMap<String, Object>(); |
| 132 | + parameters.put("method", METHOD_EDIT_META); |
| 133 | + parameters.put(Flickr.API_KEY, apiKey); |
| 134 | + parameters.put("title", strTitle); |
| 135 | + parameters.put("description", strDescription); |
| 136 | + |
| 137 | + Response response = transport.post(transport.getPath(), parameters, sharedSecret); |
| 138 | + if (response.isError()) { |
| 139 | + throw new FlickrException(response.getErrorCode(), response.getErrorMessage()); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + public void editPhoto(String strGalleryId, String strPhotoId, String strComment) throws FlickrException { |
| 144 | + Map<String, Object> parameters = new HashMap<String, Object>(); |
| 145 | + parameters.put("method", METHOD_EDIT_PHOTO); |
| 146 | + parameters.put(Flickr.API_KEY, apiKey); |
| 147 | + parameters.put("gallery_id", strGalleryId); |
| 148 | + parameters.put("photo_id", strPhotoId); |
| 149 | + parameters.put("comment", strComment); |
| 150 | + |
| 151 | + Response response = transport.post(transport.getPath(), parameters, sharedSecret); |
| 152 | + if (response.isError()) { |
| 153 | + throw new FlickrException(response.getErrorCode(), response.getErrorMessage()); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + public void editPhotos(String strGalleryId, String strPrimaryPhotoId, String strPhotoIds) throws FlickrException { |
| 158 | + Map<String, Object> parameters = new HashMap<String, Object>(); |
| 159 | + parameters.put("method", METHOD_EDIT_PHOTOS); |
| 160 | + parameters.put(Flickr.API_KEY, apiKey); |
| 161 | + parameters.put("gallery_id", strGalleryId); |
| 162 | + parameters.put("primary_photo_id", strPrimaryPhotoId); |
| 163 | + parameters.put("photo_ids", strPhotoIds); |
| 164 | + |
| 165 | + Response response = transport.post(transport.getPath(), parameters, sharedSecret); |
| 166 | + if (response.isError()) { |
| 167 | + throw new FlickrException(response.getErrorCode(), response.getErrorMessage()); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + public Gallery getInfo(String strGalleryId) throws FlickrException { |
| 172 | + Map<String, Object> parameters = new HashMap<String, Object>(); |
| 173 | + parameters.put("method", METHOD_GET_INFO); |
| 174 | + parameters.put(Flickr.API_KEY, apiKey); |
| 175 | + parameters.put("gallery_id", strGalleryId); |
| 176 | + |
| 177 | + Response response = transport.post(transport.getPath(), parameters, sharedSecret); |
| 178 | + if (response.isError()) { |
| 179 | + throw new FlickrException(response.getErrorCode(), response.getErrorMessage()); |
| 180 | + } |
| 181 | + |
| 182 | + Element galleryElement = response.getPayload(); |
| 183 | + Gallery gallery = new Gallery(); |
| 184 | + gallery.setId(galleryElement.getAttribute("id")); |
| 185 | + gallery.setUrl(galleryElement.getAttribute("url")); |
| 186 | + |
| 187 | + User owner = new User(); |
| 188 | + owner.setId(galleryElement.getAttribute("owner")); |
| 189 | + gallery.setOwner(owner); |
| 190 | + gallery.setCreateDate(galleryElement.getAttribute("date_create")); |
| 191 | + gallery.setUpdateDate(galleryElement.getAttribute("date_update")); |
| 192 | + gallery.setPrimaryPhotoId(galleryElement.getAttribute("primary_photo_id")); |
| 193 | + gallery.setPrimaryPhotoServer(galleryElement.getAttribute("primary_photo_server")); |
| 194 | + gallery.setPrimaryPhotoFarm(galleryElement.getAttribute("primary_photo_farm")); |
| 195 | + gallery.setPrimaryPhotoSecret(galleryElement.getAttribute("primary_photo_secret")); |
| 196 | + gallery.setPhotoCount(galleryElement.getAttribute("count_photos")); |
| 197 | + gallery.setVideoCount(galleryElement.getAttribute("count_videos")); |
| 198 | + |
| 199 | + gallery.setTitle(XMLUtilities.getChildValue(galleryElement, "title")); |
| 200 | + gallery.setDesc(XMLUtilities.getChildValue(galleryElement, "description")); |
| 201 | + return gallery; |
| 202 | + } |
| 203 | + |
| 204 | + /* |
| 205 | + public Gallery create(String strTitle, String strDescription, String primaryPhotoId) throws FlickrException { |
| 206 | + Map<String, Object> parameters = new HashMap<String, Object>(); |
| 207 | + parameters.put("method", METHOD_CREATE); |
| 208 | + parameters.put(Flickr.API_KEY, apiKey); |
| 209 | + parameters.put("title", strTitle); |
| 210 | + parameters.put("description", strDescription); |
| 211 | + if (primaryPhotoId != null) { |
| 212 | + parameters.put("primary_photo_id ", primaryPhotoId); |
| 213 | + } |
| 214 | +
|
| 215 | + Response response = transport.post(transport.getPath(), parameters, sharedSecret); |
| 216 | + if (response.isError()) { |
| 217 | + throw new FlickrException(response.getErrorCode(), response.getErrorMessage()); |
| 218 | + } |
| 219 | +
|
| 220 | + Element element = response.getPayload(); |
| 221 | + NodeList galleryNodes = element.getElementsByTagName("gallery"); |
| 222 | + Element galleryElement = (Element) galleryNodes.item(0); |
| 223 | + Gallery gallery = new Gallery(); |
| 224 | + gallery.setId(galleryElement.getAttribute("id")); |
| 225 | + gallery.setUrl(galleryElement.getAttribute("url")); |
| 226 | + gallery.setTitle(strTitle); |
| 227 | + gallery.setDesc(strDescription); |
| 228 | + return gallery; |
| 229 | + } |
| 230 | +*/ |
| 231 | +} |
0 commit comments