View Javadoc

1   package com.melloware.jukes.db.orm;
2   
3   import java.io.File;
4   import java.util.Date;
5   
6   import org.apache.commons.lang.StringUtils;
7   import org.apache.commons.lang.builder.CompareToBuilder;
8   import org.apache.commons.lang.builder.EqualsBuilder;
9   import org.apache.commons.lang.builder.HashCodeBuilder;
10  import org.apache.commons.lang.builder.ToStringBuilder;
11  import org.apache.commons.lang.builder.ToStringStyle;
12  
13  import com.melloware.jukes.db.audit.Auditable;
14  
15  /**
16   * Business POJO representing an TRACK.
17   * <p>
18   * Implements Auditable so that the user and date information is updated when
19   * this object is updated using a Hibernate Interceptor.
20   * <p>
21   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
22   * @author Emil A. Lefkof III <info@melloware.com>
23   * @version 4.0
24   */
25  public final class Track
26      extends AbstractJukesObject
27      implements Auditable,
28                 Comparable {
29  
30      public static final String PROPERTYNAME_CREATED_DATE = "createdDate";
31      public static final String PROPERTYNAME_MODIFIED_DATE = "modifiedDate";
32      public static final String PROPERTYNAME_DURATION = "duration";
33      public static final String PROPERTYNAME_ID = "id";
34      public static final String PROPERTYNAME_BITRATE = "bitrate";
35      public static final String PROPERTYNAME_COMMENT = "comment";
36      public static final String PROPERTYNAME_CREATED_USER = "createdUser";
37      public static final String PROPERTYNAME_DURATION_TIME = "durationTime";
38      public static final String PROPERTYNAME_MODIFIED_USER = "modifiedUser";
39      public static final String PROPERTYNAME_NAME = "name";
40      public static final String PROPERTYNAME_TRACK_NUMBER = "trackNumber";
41      public static final String PROPERTYNAME_TRACK_URL = "trackUrl";
42      public static final String PROPERTYNAME_TRACK_SIZE = "trackSize";
43  
44      private Date createdDate;
45      private Date modifiedDate;
46      private Disc disc;
47      private File file = null;
48      private Long bitrate;
49      private long duration;
50      private Long id;
51      private long trackSize;
52      private String comment;
53      private String createdUser;
54      private String durationTime;
55      private String modifiedUser;
56      private String name;
57      private String trackNumber;
58      private String trackUrl;
59  
60      /** default constructor */
61      public Track() {
62          super();
63      }
64  
65      /** constructor with id */
66      public Track(Long id) {
67          this.id = id;
68      }
69  
70      /**
71       * This date determines how the NEW flag is checked in the isNew() function.
72       * <p>
73       * @return the created date is used to check in the isNew() function
74       */
75      public Date getAuditDate() {
76          return this.createdDate;
77      }
78  
79      public Long getBitrate() {
80          return this.bitrate;
81      }
82  
83      /**
84       * This is used to fool the tree for lazy loading of tree nodes.
85       * <p>
86       * @return the number of children this object has
87       */
88      public int getChildCount() {
89          // tracks have no children so return 0
90          return NO_CHILDREN;
91      }
92  
93      /**
94       * Gets the comment.
95       * <p>
96       * @return Returns the comment.
97       */
98      public String getComment() {
99          return this.comment;
100     }
101 
102     public Date getCreatedDate() {
103         return this.createdDate;
104     }
105 
106     public String getCreatedUser() {
107         return this.createdUser;
108     }
109 
110     public Disc getDisc() {
111         return this.disc;
112     }
113 
114     /**
115      * Gets the display format based on a format from prefs.  The format is in
116      * aFormat and can have values %b for bitrate, %n for track number,
117      * %r for duration, and %t for title.
118      *
119      * Examples:
120      * %n -%t = 01 - Track.mp3
121      * <p>
122      * @param aFormat the string format like %n -%t to display 01 - Track.mp3
123      * @return the value of the display text
124      */
125     public String getDisplayText(final String aFormat) {
126         String display = aFormat;
127         if (StringUtils.isBlank(display)) {
128             display = "%n. %t";
129         }
130         String result = "";
131         display = StringUtils.replace(display, "%b", getBitrate().toString());
132         display = StringUtils.replace(display, "%n", getTrackNumber());
133         display = StringUtils.replace(display, "%r", getDurationTime());
134         display = StringUtils.replace(display, "%t", getName());
135         display = StringUtils.replace(display, "%T", getName().toUpperCase());
136         result = display;
137 
138         return result;
139     }
140 
141     public long getDuration() {
142         return this.duration;
143     }
144 
145     public String getDurationTime() {
146         return this.durationTime;
147     }
148 
149     public Long getId() {
150         return this.id;
151     }
152 
153     public Date getModifiedDate() {
154         return this.modifiedDate;
155     }
156 
157     public String getModifiedUser() {
158         return this.modifiedUser;
159     }
160 
161     public String getName() {
162         return this.name;
163     }
164 
165     public String getTrackNumber() {
166         return this.trackNumber;
167     }
168 
169     /**
170      * Gets the trackSize.
171      * <p>
172      * @return Returns the trackSize.
173      */
174     public long getTrackSize() {
175         return this.trackSize;
176     }
177 
178     public String getTrackUrl() {
179         return this.trackUrl;
180     }
181 
182     public void setBitrate(final Long bitrate) {
183         final Long old = getBitrate();
184         this.bitrate = bitrate;
185         firePropertyChange(PROPERTYNAME_BITRATE, old, bitrate);
186     }
187 
188     /**
189      * Sets the comment.
190      * <p>
191      * @param aComment The comment to set.
192      */
193     public void setComment(final String aComment) {
194         final String old = getComment();
195         this.comment = aComment;
196         firePropertyChange(PROPERTYNAME_COMMENT, old, aComment);
197     }
198 
199     public void setCreatedDate(final Date createdDate) {
200         final Date old = getCreatedDate();
201         this.createdDate = createdDate;
202         firePropertyChange(PROPERTYNAME_CREATED_DATE, old, createdDate);
203     }
204 
205     public void setCreatedUser(final String createdUser) {
206         final String old = getCreatedUser();
207         this.createdUser = createdUser;
208         firePropertyChange(PROPERTYNAME_CREATED_USER, old, createdUser);
209     }
210 
211     public void setDisc(final Disc disc) {
212         this.disc = disc;
213     }
214 
215     public void setDuration(final long duration) {
216         final long old = getDuration();
217         this.duration = duration;
218         firePropertyChange(PROPERTYNAME_DURATION, old, duration);
219     }
220 
221     public void setDurationTime(final String durationTime) {
222         final String old = getDurationTime();
223         this.durationTime = durationTime;
224         firePropertyChange(PROPERTYNAME_DURATION_TIME, old, durationTime);
225     }
226 
227     public void setId(final Long id) {
228         final Long old = getId();
229         this.id = id;
230         firePropertyChange(PROPERTYNAME_ID, old, id);
231     }
232 
233     public void setModifiedDate(final Date modifiedDate) {
234         final Date old = getModifiedDate();
235         this.modifiedDate = modifiedDate;
236         firePropertyChange(PROPERTYNAME_MODIFIED_DATE, old, modifiedDate);
237     }
238 
239     public void setModifiedUser(final String modifiedUser) {
240         final String old = getModifiedUser();
241         this.modifiedUser = modifiedUser;
242         firePropertyChange(PROPERTYNAME_MODIFIED_USER, old, modifiedUser);
243     }
244 
245     public void setName(final String name) {
246         final String old = getName();
247         this.name = name;
248         firePropertyChange(PROPERTYNAME_NAME, old, name);
249     }
250 
251     public void setTrackNumber(final String trackNumber) {
252         final String old = getTrackNumber();
253         this.trackNumber = trackNumber;
254         firePropertyChange(PROPERTYNAME_TRACK_NUMBER, old, trackNumber);
255     }
256 
257     /**
258      * Sets the trackSize.
259      * <p>
260      * @param aTrackSize The trackSize to set.
261      */
262     public void setTrackSize(final long aTrackSize) {
263         final long old = getTrackSize();
264         this.trackSize = aTrackSize;
265         firePropertyChange(PROPERTYNAME_TRACK_SIZE, old, aTrackSize);
266     }
267 
268     public void setTrackUrl(final String trackUrl) {
269         final String old = getTrackUrl();
270         this.trackUrl = trackUrl;
271         this.file = null;
272         firePropertyChange(PROPERTYNAME_TRACK_URL, old, trackUrl);
273     }
274 
275     /* (non-Javadoc)
276      * @see com.melloware.jukes.db.orm.AbstractJukesObject#isValid()
277      */
278     public boolean isValid() {
279         boolean result = false;
280         if (StringUtils.isNotBlank(getTrackUrl())) {
281             if (this.file == null) {
282                 this.file = new File(getTrackUrl());
283             }
284             result = this.file.exists();
285         }
286         return result;
287     }
288 
289     /* (non-Javadoc)
290      * @see java.lang.Comparable#compareTo(java.lang.Object)
291      */
292     public int compareTo(Object object) {
293         final Track track = (Track)object;
294         final CompareToBuilder builder = new CompareToBuilder();
295         builder.append(this.getTrackNumber(), track.getTrackNumber());
296         return builder.toComparison();
297     }
298 
299     /* (non-Javadoc)
300      * @see java.lang.Object#equals(java.lang.Object)
301      */
302     public boolean equals(Object obj) {
303         if (!(obj instanceof Track)) {
304             return false;
305         }
306         if (this == obj) {
307             return true;
308         }
309         final Track rhs = (Track)obj;
310         final EqualsBuilder builder = new EqualsBuilder();
311         builder.append(disc, rhs.disc);
312         builder.append(trackNumber, rhs.trackNumber);
313         builder.append(name, rhs.name);
314         return builder.isEquals();
315     }
316 
317     /* (non-Javadoc)
318      * @see java.lang.Object#hashCode()
319      */
320     public int hashCode() {
321         return new HashCodeBuilder(17, 37).append(disc).append(trackNumber).append(name).toHashCode();
322     }
323 
324     /* (non-Javadoc)
325      * @see java.lang.Object#toString()
326      */
327     public String toString() {
328         final ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE);
329         builder.append("id", id);
330         builder.append("name", name);
331         builder.append("trackNumber", trackNumber);
332         builder.append("bitrate", bitrate);
333         builder.append("duration", duration);
334         builder.append("durationTime", durationTime);
335         builder.append("trackUrl", trackUrl);
336         return builder.toString();
337     }
338 
339 }