View Javadoc

1   package com.melloware.jukes.ws;
2   
3   import java.awt.Image;
4   import java.util.ArrayList;
5   import java.util.Collection;
6   import java.util.List;
7   
8   import org.apache.commons.lang.StringUtils;
9   import org.apache.commons.lang.builder.EqualsBuilder;
10  import org.apache.commons.lang.builder.HashCodeBuilder;
11  import org.apache.commons.lang.builder.ToStringBuilder;
12  import org.apache.commons.lang.builder.ToStringStyle;
13  
14  import com.amazonaws.ecs.model.Disc;
15  import com.amazonaws.ecs.model.Item;
16  import com.amazonaws.ecs.model.Track;
17  import com.amazonaws.ecs.model.Tracks;
18  import com.melloware.jukes.file.FileUtil;
19  import com.melloware.jukes.file.image.ImageFactory;
20  
21  /**
22   * Wrapper class for an Amazon Product to mask the complexity from the user and
23   * provide easy accessor methods to the data contained within.
24   * <p>
25   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
26   * @author Emil A. Lefkof III <info@melloware.com>
27   * @version 4.0
28   */
29  @SuppressWarnings("unchecked")
30  public final class AmazonItem {
31  
32     private final Collection tracks = new ArrayList();
33     private Image largeImage = null;
34     private Image mediumImage = null;
35     private Image smallImage = null;
36     private String disc = null;
37     private String artist = null;
38     private String itemId = null;
39     private String largeImageUrl = null;
40     private String mediumImageUrl = null;
41     private String releaseDate = null;
42     private String releaseYear = null;
43     private String smallImageUrl = null;
44     private int bestImageWidth = 0;
45     private int bestImageHeight = 0;
46  
47     /**
48      * Default constructor. Private because should never be used.
49      */
50     public AmazonItem(Item aAmazonItem) {
51        super();
52        initialize(aAmazonItem);
53     }
54  
55     /**
56      * Gets the disc.
57      * <p>
58      * @return Returns the disc.
59      */
60     public String getDisc() {
61        return this.disc;
62     }
63  
64     /**
65      * Gets the artist.
66      * <p>
67      * @return Returns the artist.
68      */
69     public String getArtist() {
70        return this.artist;
71     }
72  
73     /**
74      * Gets the itemId.
75      * <p>
76      * @return Returns the itemId.
77      */
78     public String getItemId() {
79        return this.itemId;
80     }
81  
82     /**
83      * Tries first large, then medium, then small
84      * <p>
85      * @return Returns the best fit url
86      */
87     public String getBestImageUrl() {
88        if (StringUtils.isNotBlank(this.largeImageUrl)) {
89           return this.largeImageUrl;
90        } else if (StringUtils.isNotBlank(this.mediumImageUrl)) {
91           return this.mediumImageUrl;
92        } else if (StringUtils.isNotBlank(this.smallImageUrl)) {
93           return this.smallImageUrl;
94        } else {
95           return "";
96        }
97     }
98  
99     /**
100     * Tries first large, then medium, then small
101     * <p>
102     * @return Returns the best fit image
103     */
104    public Image getBestImage() {
105       if (StringUtils.isNotBlank(this.largeImageUrl)) {
106          return getLargeImage();
107       } else if (StringUtils.isNotBlank(this.mediumImageUrl)) {
108          return getMediumImage();
109       } else if (StringUtils.isNotBlank(this.smallImageUrl)) {
110          return getSmallImage();
111       } else {
112          return null;
113       }
114    }
115 
116    /**
117     * Tries first small, then medium, then large
118     * <p>
119     * @return Returns the best fit image
120     */
121    public Image getSmallestImage() {
122       if (StringUtils.isNotBlank(this.smallImageUrl)) {
123          return getSmallImage();
124       } else if (StringUtils.isNotBlank(this.mediumImageUrl)) {
125          return getMediumImage();
126       } else if (StringUtils.isNotBlank(this.largeImageUrl)) {
127          return getLargeImage();
128       } else {
129          return null;
130       }
131    }
132 
133    /**
134     * Gets the largeImage.
135     * <p>
136     * @return Returns the largeImage.
137     */
138    public Image getLargeImage() {
139       if ((this.largeImage == null) && (this.largeImageUrl != null)) {
140          this.largeImage = ImageFactory.getImageFromUrl(this.largeImageUrl);
141       }
142       return this.largeImage;
143    }
144 
145    /**
146     * Gets the largeImageUrl.
147     * <p>
148     * @return Returns the largeImageUrl.
149     */
150    public String getLargeImageUrl() {
151       return this.largeImageUrl;
152    }
153 
154    /**
155     * Gets the mediumImage.
156     * <p>
157     * @return Returns the mediumImage.
158     */
159    public Image getMediumImage() {
160       if ((this.mediumImage == null) && (this.mediumImageUrl != null)) {
161          this.mediumImage = ImageFactory.getImageFromUrl(this.mediumImageUrl);
162       }
163       return this.mediumImage;
164    }
165 
166    /**
167     * Gets the mediumImageUrl.
168     * <p>
169     * @return Returns the mediumImageUrl.
170     */
171    public String getMediumImageUrl() {
172       return this.mediumImageUrl;
173    }
174 
175    /**
176     * Gets the releaseDate.
177     * <p>
178     * @return Returns the releaseDate.
179     */
180    public String getReleaseDate() {
181       return this.releaseDate;
182    }
183 
184    /**
185     * Gets the releaseYear.
186     * <p>
187     * @return Returns the releaseYear.
188     */
189    public String getReleaseYear() {
190       if ((this.releaseYear == null) && (this.releaseDate != null) && (this.releaseDate.length() > 4)) {
191          this.releaseYear = this.releaseDate.substring(0, 4);
192       }
193       return this.releaseYear;
194    }
195 
196    /**
197     * Gets the smallImage.
198     * <p>
199     * @return Returns the smallImage.
200     */
201    public Image getSmallImage() {
202       if ((this.smallImage == null) && (this.smallImageUrl != null)) {
203          this.smallImage = ImageFactory.getImageFromUrl(this.smallImageUrl);
204       }
205       return this.smallImage;
206    }
207 
208    /**
209     * Gets the smallImageUrl.
210     * <p>
211     * @return Returns the smallImageUrl.
212     */
213    public String getSmallImageUrl() {
214       return this.smallImageUrl;
215    }
216 
217    /**
218     * Gets the tracks.
219     * <p>
220     * @return Returns the tracks.
221     */
222    public Collection getTracks() {
223       return this.tracks;
224    }
225 
226    /**
227     * Default Equals method.
228     */
229    public boolean equals(Object obj) {
230       if (!(obj instanceof AmazonItem)) {
231          return false;
232       }
233       if (this == obj) {
234          return true;
235       }
236       AmazonItem rhs = (AmazonItem) obj;
237       EqualsBuilder builder = new EqualsBuilder();
238       builder.append(artist, rhs.artist);
239       builder.append(disc, rhs.disc);
240       builder.append(itemId, rhs.itemId);
241 
242       return builder.isEquals();
243    }
244 
245    /**
246     * Default hashcode method.
247     */
248    public int hashCode() {
249       return new HashCodeBuilder(17, 37).append(artist).append(disc).append(releaseDate).toHashCode();
250    }
251 
252    /**
253     * Default toString() method.
254     */
255    public String toString() {
256       ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE);
257       builder.append("artist", artist);
258       builder.append("disc", disc);
259       builder.append("itemId", itemId);
260       builder.append("smallImageUrl", smallImageUrl);
261       builder.append("mediumImageUrl", mediumImageUrl);
262       builder.append("largeImageUrl", largeImageUrl);
263       builder.append("releaseDate", releaseDate);
264 
265       return builder.toString();
266    }
267 
268    /**
269     * Initialize all the fields of this object from the Amazon Item.
270     */
271    private void initialize(Item aAmazonItem) {
272       final Item amazonItem = aAmazonItem;
273       if (amazonItem == null) {
274          throw new IllegalArgumentException("Amazon Item is null.");
275       }
276 
277       this.itemId = amazonItem.getASIN();
278 
279       if (amazonItem.isSetSmallImage()) {
280          this.smallImageUrl = amazonItem.getSmallImage().getURL();
281          this.bestImageHeight = amazonItem.getSmallImage().getHeight().getValue().intValue();
282          this.bestImageWidth = amazonItem.getSmallImage().getWidth().getValue().intValue();
283       }
284 
285       if (amazonItem.isSetMediumImage()) {
286          this.mediumImageUrl = amazonItem.getMediumImage().getURL();
287          this.bestImageHeight = amazonItem.getMediumImage().getHeight().getValue().intValue();
288          this.bestImageWidth = amazonItem.getMediumImage().getWidth().getValue().intValue();
289       }
290 
291       if (amazonItem.isSetLargeImage()) {
292          this.largeImageUrl = amazonItem.getLargeImage().getURL();
293          this.bestImageHeight = amazonItem.getLargeImage().getHeight().getValue().intValue();
294          this.bestImageWidth = amazonItem.getLargeImage().getWidth().getValue().intValue();
295       }
296 
297       // now get all fields from the Item Attributes
298       if (amazonItem.getItemAttributes() != null) {
299 
300          if (amazonItem.getItemAttributes().getArtist() != null) {
301             if (amazonItem.getItemAttributes().getArtist().size() > 0) {
302                this.artist = FileUtil.capitalize(amazonItem.getItemAttributes().getArtist().get(0));
303             }
304          }
305 
306          if (amazonItem.getItemAttributes().getTitle() != null) {
307             this.disc = FileUtil.capitalize(amazonItem.getItemAttributes().getTitle());
308          }
309 
310          if (amazonItem.getItemAttributes().getReleaseDate() != null) {
311             this.releaseDate = amazonItem.getItemAttributes().getReleaseDate();
312          }
313       }
314 
315       // now get the Tracks if there are any and put them in a collection
316       if (amazonItem.getTracks() != null) {
317          Tracks tracks = amazonItem.getTracks();
318          List<Disc> discList = tracks.getDisc();
319          for (Disc disc : discList) {
320             List<Track> trackList = disc.getTrack();
321             for (Track track : trackList) {
322                String trackName = FileUtil.capitalize(track.getValue());
323                this.tracks.add(trackName);
324             }
325          }
326       }
327    }
328 
329    /**
330     * Gets the bestImageHeight.
331     * <p>
332     * @return Returns the bestImageHeight.
333     */
334    public int getBestImageHeight() {
335       return this.bestImageHeight;
336    }
337 
338    /**
339     * Gets the bestImageWidth.
340     * <p>
341     * @return Returns the bestImageWidth.
342     */
343    public int getBestImageWidth() {
344       return this.bestImageWidth;
345    }
346 
347 }