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
23
24
25
26
27
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
49
50 public AmazonItem(Item aAmazonItem) {
51 super();
52 initialize(aAmazonItem);
53 }
54
55
56
57
58
59
60 public String getDisc() {
61 return this.disc;
62 }
63
64
65
66
67
68
69 public String getArtist() {
70 return this.artist;
71 }
72
73
74
75
76
77
78 public String getItemId() {
79 return this.itemId;
80 }
81
82
83
84
85
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
101
102
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
118
119
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
135
136
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
147
148
149
150 public String getLargeImageUrl() {
151 return this.largeImageUrl;
152 }
153
154
155
156
157
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
168
169
170
171 public String getMediumImageUrl() {
172 return this.mediumImageUrl;
173 }
174
175
176
177
178
179
180 public String getReleaseDate() {
181 return this.releaseDate;
182 }
183
184
185
186
187
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
198
199
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
210
211
212
213 public String getSmallImageUrl() {
214 return this.smallImageUrl;
215 }
216
217
218
219
220
221
222 public Collection getTracks() {
223 return this.tracks;
224 }
225
226
227
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
247
248 public int hashCode() {
249 return new HashCodeBuilder(17, 37).append(artist).append(disc).append(releaseDate).toHashCode();
250 }
251
252
253
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
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
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
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
331
332
333
334 public int getBestImageHeight() {
335 return this.bestImageHeight;
336 }
337
338
339
340
341
342
343 public int getBestImageWidth() {
344 return this.bestImageWidth;
345 }
346
347 }