|
| 1 | +/** |
| 2 | + * @author acaplan |
| 3 | + */ |
| 4 | +package com.flickr4java.flickr.cameras; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +import org.apache.log4j.Logger; |
| 12 | +import org.w3c.dom.Element; |
| 13 | +import org.w3c.dom.NodeList; |
| 14 | + |
| 15 | +import com.flickr4java.flickr.Flickr; |
| 16 | +import com.flickr4java.flickr.FlickrException; |
| 17 | +import com.flickr4java.flickr.Response; |
| 18 | +import com.flickr4java.flickr.Transport; |
| 19 | +import com.flickr4java.flickr.util.XMLUtilities; |
| 20 | + |
| 21 | +/** |
| 22 | + * Interface for flickr.collections.* methods. |
| 23 | + * |
| 24 | + * @author callmeal |
| 25 | + * @version $Id$ Copyright (c) 2012 allanc |
| 26 | + */ |
| 27 | +public class CamerasInterface { |
| 28 | + private static final String METHOD_GET_BRANDS = "flickr.cameras.getBrands"; |
| 29 | + |
| 30 | + private static final String METHOD_GET_BRAND_MODELS = "flickr.cameras.getBrandModels"; |
| 31 | + |
| 32 | + /** |
| 33 | + * Logger for log4j. |
| 34 | + */ |
| 35 | + @SuppressWarnings("unused") |
| 36 | + private static Logger _log = Logger.getLogger(CamerasInterface.class); |
| 37 | + |
| 38 | + private final String apiKey; |
| 39 | + |
| 40 | + private final String sharedSecret; |
| 41 | + |
| 42 | + private final Transport transportAPI; |
| 43 | + |
| 44 | + public CamerasInterface(String apiKey, String sharedSecret, Transport transportAPI) { |
| 45 | + this.apiKey = apiKey; |
| 46 | + this.sharedSecret = sharedSecret; |
| 47 | + this.transportAPI = transportAPI; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Returns all the brands of cameras that Flickr knows about. |
| 52 | + * |
| 53 | + * This method does not require authentication. |
| 54 | + * |
| 55 | + * |
| 56 | + * @return List of Brands |
| 57 | + * @throws FlickrException |
| 58 | + */ |
| 59 | + public List<Brand> getBrands() throws FlickrException { |
| 60 | + Map<String, Object> parameters = new HashMap<String, Object>(); |
| 61 | + parameters.put("method", METHOD_GET_BRANDS); |
| 62 | + parameters.put(Flickr.API_KEY, apiKey); |
| 63 | + |
| 64 | + Response response = transportAPI.get(transportAPI.getPath(), parameters, sharedSecret); |
| 65 | + if (response.isError()) { |
| 66 | + throw new FlickrException(response.getErrorCode(), response.getErrorMessage()); |
| 67 | + } |
| 68 | + |
| 69 | + List<Brand> lst = new ArrayList<Brand>(); |
| 70 | + Element mElement = response.getPayload(); |
| 71 | + NodeList brandElements = mElement.getElementsByTagName("brand"); |
| 72 | + for (int i = 0; i < brandElements.getLength(); i++) { |
| 73 | + Element brandElement = (Element) brandElements.item(i); |
| 74 | + Brand brand = new Brand(); |
| 75 | + brand.setId(brandElement.getAttribute("id")); |
| 76 | + brand.setName(brandElement.getAttribute("name")); |
| 77 | + lst.add(brand); |
| 78 | + } |
| 79 | + |
| 80 | + return lst; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Returns all the brands of cameras that Flickr knows about. |
| 85 | + * |
| 86 | + * This method does not require authentication. |
| 87 | + * |
| 88 | + * |
| 89 | + * @return List of Brands |
| 90 | + * @throws FlickrException |
| 91 | + */ |
| 92 | + public List<Camera> getBrandModels(String strBrand) throws FlickrException { |
| 93 | + Map<String, Object> parameters = new HashMap<String, Object>(); |
| 94 | + parameters.put("method", METHOD_GET_BRAND_MODELS); |
| 95 | + parameters.put(Flickr.API_KEY, apiKey); |
| 96 | + parameters.put("brand", strBrand); |
| 97 | + |
| 98 | + Response response = transportAPI.get(transportAPI.getPath(), parameters, sharedSecret); |
| 99 | + if (response.isError()) { |
| 100 | + throw new FlickrException(response.getErrorCode(), response.getErrorMessage()); |
| 101 | + } |
| 102 | + |
| 103 | + List<Camera> lst = new ArrayList<Camera>(); |
| 104 | + Element mElement = response.getPayload(); |
| 105 | + NodeList cameraElements = mElement.getElementsByTagName("camera"); |
| 106 | + for (int i = 0; i < cameraElements.getLength(); i++) { |
| 107 | + Element cameraElement = (Element) cameraElements.item(i); |
| 108 | + Camera cam = new Camera(); |
| 109 | + cam.setId(cameraElement.getAttribute("id")); |
| 110 | + cam.setName(XMLUtilities.getChildValue(cameraElement, "name")); |
| 111 | + |
| 112 | + NodeList detailsNodes = cameraElement.getElementsByTagName("details"); |
| 113 | + int n = detailsNodes.getLength(); |
| 114 | + if (n == 1) { |
| 115 | + Element detailElement = (Element) detailsNodes.item(0); |
| 116 | + Details detail = new Details(); |
| 117 | + cam.setDetails(detail); |
| 118 | + detail.setMegapixels(XMLUtilities.getChildValue(detailElement, "megapixels")); |
| 119 | + detail.setZoom(XMLUtilities.getChildValue(detailElement, "zoom")); |
| 120 | + detail.setLcdSize(XMLUtilities.getChildValue(detailElement, "lcd_screen_size")); |
| 121 | + detail.setStorageType(XMLUtilities.getChildValue(detailElement, "memory_type")); |
| 122 | + } |
| 123 | + |
| 124 | + NodeList imageNodes = cameraElement.getElementsByTagName("images"); |
| 125 | + n = imageNodes.getLength(); |
| 126 | + if (n == 1) { |
| 127 | + Element imageElement = (Element) imageNodes.item(0); |
| 128 | + cam.setSmallImage(XMLUtilities.getChildValue(imageElement, "small")); |
| 129 | + cam.setLargeImage(XMLUtilities.getChildValue(imageElement, "large")); |
| 130 | + } |
| 131 | + |
| 132 | + lst.add(cam); |
| 133 | + } |
| 134 | + |
| 135 | + return lst; |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments