View Javadoc

1   package com.melloware.jukes.ws;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   import java.util.List;
6   
7   import org.apache.commons.logging.Log;
8   import org.apache.commons.logging.LogFactory;
9   
10  import com.amazonaws.ecs.AmazonECS;
11  import com.amazonaws.ecs.AmazonECSException;
12  import com.amazonaws.ecs.model.Item;
13  import com.amazonaws.ecs.model.ItemSearchRequest;
14  import com.amazonaws.ecs.model.ItemSearchResponse;
15  import com.amazonaws.ecs.model.Items;
16  import com.amazonaws.ecs.query.AmazonECSQuery;
17  import com.melloware.jukes.exception.WebServiceException;
18  
19  /**
20   * Static methods for performing web services calls to the Amazon E-Commerce Web
21   * Services application.
22   * <p>
23   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
24   * @author Emil A. Lefkof III <info@melloware.com>
25   * @version 4.0
26   */
27  @SuppressWarnings("unchecked")
28  public final class AmazonSearch {
29     private static final Log LOG = LogFactory.getLog(AmazonSearch.class);
30  
31     // the sort order (orig-rel-date, artistrank, titlerank)
32     public static final String SORT_TITLE = "titlerank";
33     public static final String SORT_ARTIST = "artistrank";
34     public static final String SORT_DATE = "orig-rel-date";
35  
36     // key used for all Amazon queries registered to Emil Lefkof
37     private static final String AWSACCESSKEY = "1RMS75PEHXF93EPJVXR2";
38  
39     // the area to search amazon
40     private static final String SEARCH_INDEX = "Music";
41  
42     // the data to bring back
43     private static final String RG_IMAGES = "Images";
44     private static final String RG_ITEMS = "ItemAttributes";
45     private static final String RG_TRACKS = "Tracks";
46  
47     private static final AmazonECS SERVICE = new AmazonECSQuery(AWSACCESSKEY, "Jukes");
48  
49     /**
50      * Default constructor. Private so no instantiation.
51      */
52     private AmazonSearch() {
53        super();
54     }
55  
56     /**
57      * Finds a collection of Amazon products by Disc Name.
58      * <p>
59      * @param aDiscTitle the name of the disc to search for
60      * @return a collection of AmazonItems matching the search criteria
61      * @throws WebServiceException if any error occurs querying the SOAP service
62      */
63     public static Collection findItemsByDisc(String aDiscTitle) throws WebServiceException {
64        return findItemsByArtistDiscSort("", aDiscTitle, SORT_TITLE);
65     }
66  
67     /**
68      * Finds a collection of Amazon products by Artist Name.
69      * <p>
70      * @param aArtistName the name of the artist to search for
71      * @return a collection of AmazonItems matching the search criteria
72      * @throws WebServiceException if any error occurs querying the SOAP service
73      */
74     public static Collection findItemsByArtist(String aArtistName) throws WebServiceException {
75        return findItemsByArtistDiscSort(aArtistName, "", SORT_TITLE);
76     }
77  
78     /**
79      * Finds a collection of Amazon products by artist and disc name.
80      * <p>
81      * @param aArtistName the name of the artist to search for
82      * @param aDiscTitle the name of the disc to search for
83      * @return a collection of AmazonItems matching the search criteria
84      * @throws WebServiceException if any error occurs querying the SOAP service
85      */
86     public static Collection findItemsByArtistDisc(String aArtistName, String aDiscTitle) throws WebServiceException {
87        return findItemsByArtistDiscSort(aArtistName, aDiscTitle, SORT_TITLE);
88     }
89  
90     /**
91      * Finds a collection of Amazon products by artist and disc name.
92      * <p>
93      * @param aArtistName the name of the artist to search for
94      * @param aDiscTitle the name of the disc to search for
95      * @param aSort what field to sort by
96      * @return a collection of AmazonItems matching the search criteria
97      * @throws WebServiceException if any error occurs querying the SOAP service
98      */
99     public static Collection findItemsByArtistDiscSort(String aArtistName, String aDiscTitle, String aSort)
100             throws WebServiceException {
101       if (LOG.isDebugEnabled()) {
102          LOG.debug("Amazon findItemsByArtistDiscSort: " + aArtistName + " - " + aDiscTitle + " - " + aSort);
103       }
104 
105       Collection collection = null;
106       try {
107          final ItemSearchRequest request = new ItemSearchRequest();
108          request.setArtist(aArtistName);
109          request.setTitle(aDiscTitle);
110          request.setSort(aSort);
111          request.setSearchIndex(SEARCH_INDEX);
112          final ArrayList responseGroup = new ArrayList();
113          responseGroup.add(RG_IMAGES);
114          responseGroup.add(RG_ITEMS);
115          responseGroup.add(RG_TRACKS);
116          request.setResponseGroup(responseGroup);
117 
118          final ItemSearchResponse response = SERVICE.itemSearch(request);
119          collection = loadItems(response.getItems());
120       } catch (AmazonECSException ex) {
121          LOG.error(ex.getMessage(), ex);
122          throw new WebServiceException(ex);
123       }
124 
125       return collection;
126    }
127 
128    /**
129     * Loads a collection with AmazonItems from a SOAP response.
130     * <p>
131     * @param aItems the items to load
132     * @return a collection of AmazonItem objects or an empty collection
133     */
134    private static Collection loadItems(List<Items> aItems) {
135       ArrayList collection = new ArrayList();
136       if (aItems != null) {
137 
138          for (Items items : aItems) {
139             if (items.getItem() != null) {
140                // loop through and add them to a collection
141                List<Item> itemsList = items.getItem();
142                for (Item item : itemsList) {
143                   collection.add(new AmazonItem(item));
144                }
145             }
146          }
147 
148       }
149       return collection;
150    }
151 
152 }