View Javadoc

1   package com.melloware.jukes.db.orm;
2   
3   import java.io.File;
4   import java.util.Date;
5   import java.util.HashSet;
6   import java.util.Set;
7   
8   import org.apache.commons.lang.StringUtils;
9   import org.apache.commons.lang.builder.CompareToBuilder;
10  import org.apache.commons.lang.builder.EqualsBuilder;
11  import org.apache.commons.lang.builder.HashCodeBuilder;
12  import org.apache.commons.lang.builder.ToStringBuilder;
13  import org.apache.commons.lang.builder.ToStringStyle;
14  
15  import com.melloware.jukes.db.audit.Auditable;
16  
17  /**
18   * Business POJO representing an ALBUM.
19   * <p>
20   * Implements Auditable so that the user and date information is updated when
21   * this object is updated using a Hibernate Interceptor.
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  public final class Disc
28      extends AbstractJukesObject
29      implements Auditable,
30                 Comparable {
31  
32      public static final String PROPERTYNAME_CREATED_DATE = "createdDate";
33      public static final String PROPERTYNAME_MODIFIED_DATE = "modifiedDate";
34      public static final String PROPERTYNAME_ID = "id";
35      public static final String PROPERTYNAME_LOCATION = "location";
36      public static final String PROPERTYNAME_BITRATE = "bitrate";
37      public static final String PROPERTYNAME_COVER_URL = "coverUrl";
38      public static final String PROPERTYNAME_COVER_SIZE = "coverSize";
39      public static final String PROPERTYNAME_CREATED_USER = "createdUser";
40      public static final String PROPERTYNAME_GENRE = "genre";
41      public static final String PROPERTYNAME_MODIFIED_USER = "modifiedUser";
42      public static final String PROPERTYNAME_NAME = "name";
43      public static final String PROPERTYNAME_YEAR = "year";
44      public static final String PROPERTYNAME_DURATION = "duration";
45      public static final String PROPERTYNAME_DURATION_TIME = "durationTime";
46      public static final String PROPERTYNAME_ARTIST = "artist";
47      public static final String PROPERTYNAME_NOTES = "notes";
48  
49      private Artist artist;
50      private Date createdDate;
51      private Date modifiedDate;
52      private File file = null;
53      private Long bitrate;
54      private long coverSize;
55      private long duration;
56      private Long id;
57      private Set tracks;
58      private String coverUrl;
59      private String createdUser;
60      private String durationTime;
61      private String genre;
62      private String location;
63      private String modifiedUser;
64      private String name;
65      private String notes;
66      private String year;
67  
68      /** default constructor */
69      public Disc() {
70          super();
71      }
72  
73      /** constructor with id */
74      public Disc(Long id) {
75          this.id = id;
76      }
77  
78      public Artist getArtist() {
79          return this.artist;
80      }
81  
82      /**
83       * This date determines how the NEW flag is checked in the isNew() function.
84       * <p>
85       * @return the created date is used to check in the isNew() function
86       */
87      public Date getAuditDate() {
88          return this.createdDate;
89      }
90  
91      /**
92       * @return Returns the bitrate.
93       */
94      public Long getBitrate() {
95          return this.bitrate;
96      }
97  
98      /**
99       * This is used to fool the tree for lazy loading of tree nodes.
100      * <p>
101      * @return the number of children this object has
102      */
103     public int getChildCount() {
104         if (childCount == -1) {
105             // childCount = HibernateDao.count("Track as track where track.disc = " + this.id);
106             childCount = 10;
107         }
108         return childCount;
109     }
110 
111     /**
112      * Gets the coverSize.
113      * <p>
114      * @return Returns the coverSize.
115      */
116     public long getCoverSize() {
117         return this.coverSize;
118     }
119 
120     public String getCoverUrl() {
121         return this.coverUrl;
122     }
123 
124     public Date getCreatedDate() {
125         return this.createdDate;
126     }
127 
128     public String getCreatedUser() {
129         return this.createdUser;
130     }
131 
132     /**
133      * Gets the display format based on a format from prefs.  The format is in
134      * aFormat and can have values %b forbitrate, %y for year,
135      * %r for running time, and %d for disc.
136      *
137      * Examples:
138      * %y -%d = 2005 - Guero
139      * <p>
140      * @param aFormat the string format like %y -%d to rename 2005 - Guero
141      * @return the value of the display text
142      */
143     public String getDisplayText(final String aFormat) {
144         String display = aFormat;
145         if (StringUtils.isBlank(display)) {
146             display = "%y %d";
147         }
148         String result = "";
149         display = StringUtils.replace(display, "%b", getBitrate().toString());
150         display = StringUtils.replace(display, "%y", getYear());
151         display = StringUtils.replace(display, "%r", getDurationTime());
152         display = StringUtils.replace(display, "%d", getName());
153         display = StringUtils.replace(display, "%D", getName().toUpperCase());
154         result = display;
155 
156         return result;
157     }
158 
159     /**
160      * Gets the duration.
161      * <p>
162      * @return Returns the duration.
163      */
164     public long getDuration() {
165         return this.duration;
166     }
167 
168     /**
169      * Gets the durationTime.
170      * <p>
171      * @return Returns the durationTime.
172      */
173     public String getDurationTime() {
174         return this.durationTime;
175     }
176 
177     public String getGenre() {
178         return this.genre;
179     }
180 
181     public Long getId() {
182         return this.id;
183     }
184 
185     /**
186      * Gets the location.
187      * <p>
188      * @return Returns the location.
189      */
190     public String getLocation() {
191         return this.location;
192     }
193 
194     public Date getModifiedDate() {
195         return this.modifiedDate;
196     }
197 
198     public String getModifiedUser() {
199         return this.modifiedUser;
200     }
201 
202     public String getName() {
203         return this.name;
204     }
205 
206     /**
207      * Gets the notes.
208      * <p>
209      * @return Returns the notes.
210      */
211     public String getNotes() {
212         return this.notes;
213     }
214 
215     public Set getTracks() {
216         return this.tracks;
217     }
218 
219     /**
220      * Returns the release year.
221      * <p>
222      * @return the release year
223      */
224     public String getYear() {
225         return this.year;
226     }
227 
228     public void setArtist(final Artist artist) {
229         this.artist = artist;
230     }
231 
232     /**
233      * @param aBitrate The bitrate to set.
234      */
235     public void setBitrate(final Long aBitrate) {
236         final Long old = getBitrate();
237         this.bitrate = aBitrate;
238         firePropertyChange(PROPERTYNAME_BITRATE, old, aBitrate);
239 
240     }
241 
242     /**
243      * Sets the coverSize.
244      * <p>
245      * @param aCoverSize The coverSize to set.
246      */
247     public void setCoverSize(final long aCoverSize) {
248         final long old = getCoverSize();
249         this.coverSize = aCoverSize;
250         firePropertyChange(PROPERTYNAME_COVER_SIZE, old, aCoverSize);
251     }
252 
253     public void setCoverUrl(final String coverUrl) {
254         final String old = getCoverUrl();
255         this.coverUrl = coverUrl;
256         firePropertyChange(PROPERTYNAME_COVER_URL, old, coverUrl);
257     }
258 
259     public void setCreatedDate(final Date createdDate) {
260         final Date old = getCreatedDate();
261         this.createdDate = createdDate;
262         firePropertyChange(PROPERTYNAME_CREATED_DATE, old, createdDate);
263     }
264 
265     public void setCreatedUser(final String createdUser) {
266         final String old = getCreatedUser();
267         this.createdUser = createdUser;
268         firePropertyChange(PROPERTYNAME_CREATED_USER, old, createdUser);
269     }
270 
271     /**
272      * Sets the duration.
273      * <p>
274      * @param aDuration The duration to set.
275      */
276     public void setDuration(final long aDuration) {
277         final long old = getDuration();
278         this.duration = aDuration;
279         firePropertyChange(PROPERTYNAME_DURATION, old, aDuration);
280     }
281 
282     /**
283      * Sets the durationTime.
284      * <p>
285      * @param aDurationTime The durationTime to set.
286      */
287     public void setDurationTime(final String aDurationTime) {
288         final String old = getDurationTime();
289         this.durationTime = aDurationTime;
290         firePropertyChange(PROPERTYNAME_DURATION_TIME, old, aDurationTime);
291     }
292 
293     public void setGenre(final String genre) {
294         final String old = getGenre();
295         this.genre = genre;
296         firePropertyChange(PROPERTYNAME_GENRE, old, genre);
297     }
298 
299     public void setId(final Long id) {
300         final Long old = getId();
301         this.id = id;
302         firePropertyChange(PROPERTYNAME_ID, old, id);
303     }
304 
305     /**
306      * Sets the location.
307      * <p>
308      * @param aLocation The location to set.
309      */
310     public void setLocation(final String aLocation) {
311         final String old = getLocation();
312         this.location = aLocation;
313         this.file = null;
314         firePropertyChange(PROPERTYNAME_LOCATION, old, aLocation);
315     }
316 
317     public void setModifiedDate(final Date modifiedDate) {
318         final Date old = getModifiedDate();
319         this.modifiedDate = modifiedDate;
320         firePropertyChange(PROPERTYNAME_MODIFIED_DATE, old, modifiedDate);
321     }
322 
323     public void setModifiedUser(final String modifiedUser) {
324         final String old = getModifiedUser();
325         this.modifiedUser = modifiedUser;
326         firePropertyChange(PROPERTYNAME_MODIFIED_USER, old, modifiedUser);
327     }
328 
329     public void setName(final String name) {
330         final String old = getName();
331         this.name = name;
332         firePropertyChange(PROPERTYNAME_NAME, old, name);
333     }
334 
335     /**
336      * Sets the notes.
337      * <p>
338      * @param aNotes The notes to set.
339      */
340     public void setNotes(final String aNotes) {
341         final String old = getNotes();
342         this.notes = aNotes;
343         firePropertyChange(PROPERTYNAME_NOTES, old, aNotes);
344     }
345 
346     public void setTracks(final Set tracks) {
347         this.tracks = tracks;
348     }
349 
350     /**
351      * Sets the release year.
352      * <p>
353      * @param year the year of this release
354      */
355     public void setYear(final String year) {
356         final String old = getYear();
357         this.year = year;
358         firePropertyChange(PROPERTYNAME_YEAR, old, year);
359     }
360 
361     /* (non-Javadoc)
362      * @see com.melloware.jukes.db.orm.AbstractJukesObject#isValid()
363      */
364     public boolean isValid() {
365         boolean result = false;
366         if (StringUtils.isNotBlank(getLocation())) {
367             if (this.file == null) {
368                 this.file = new File(getLocation());
369             }
370             result = this.file.exists();
371         }
372         return result;
373     }
374 
375     @SuppressWarnings("unchecked")
376     public void addTrack(final Track track) {
377         if (this.tracks == null) {
378             this.tracks = new HashSet();
379         }
380         this.tracks.add(track);
381         track.setDisc(this);
382     }
383 
384     /* (non-Javadoc)
385      * @see java.lang.Comparable#compareTo(java.lang.Object)
386      */
387     public int compareTo(Object object) {
388         final Disc disc = (Disc)object;
389         final CompareToBuilder builder = new CompareToBuilder();
390         builder.append(this.getName().toUpperCase(), disc.getName().toUpperCase());
391         return builder.toComparison();
392     }
393 
394     /* (non-Javadoc)
395      * @see java.lang.Object#equals(java.lang.Object)
396      */
397     public boolean equals(Object obj) {
398         if (!(obj instanceof Disc)) {
399             return false;
400         }
401         if (this == obj) {
402             return true;
403         }
404         final Disc rhs = (Disc)obj;
405         final EqualsBuilder builder = new EqualsBuilder();
406         builder.append(artist, rhs.artist);
407         builder.append(name, rhs.name);
408         return builder.isEquals();
409     }
410 
411     /* (non-Javadoc)
412      * @see java.lang.Object#hashCode()
413      */
414     public int hashCode() {
415         return new HashCodeBuilder(17, 37).append(artist).append(name).toHashCode();
416     }
417 
418     /* (non-Javadoc)
419      * @see java.lang.Object#toString()
420      */
421     public String toString() {
422         final ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE);
423         builder.append("id", id);
424         builder.append("name", name);
425         builder.append("year", year);
426         builder.append("genre", genre);
427         builder.append("bitrate", bitrate);
428         builder.append("coverUrl", coverUrl);
429         builder.append("location", location);
430         return builder.toString();
431     }
432 
433 }