View Javadoc

1   package com.melloware.jukes.gui.view.tasks;
2   
3   import org.apache.commons.logging.Log;
4   import org.apache.commons.logging.LogFactory;
5   
6   import com.jgoodies.uif.util.Worker;
7   import com.melloware.jukes.db.orm.AbstractJukesObject;
8   import com.melloware.jukes.db.orm.Artist;
9   import com.melloware.jukes.db.orm.Disc;
10  import com.melloware.jukes.db.orm.Track;
11  import com.melloware.jukes.exception.MusicTagException;
12  import com.melloware.jukes.file.tag.MusicTag;
13  import com.melloware.jukes.file.tag.TagFactory;
14  
15  /**
16   * In a thread attempts to update all tags for all tracks for a domain object
17   * (Artist, Disc, Track). It will loop through all Artist's Disc's Tracks. It
18   * will loop through all Disc's Tracks. Or it will update a single Track.
19   * <p>
20   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
21   * @author Emil A. Lefkof III <info@melloware.com>
22   * @version 4.0
23   */
24  @SuppressWarnings("unchecked")
25  public final class UpdateTagsTask extends LongTask {
26  
27     private static final Log LOG = LogFactory.getLog(UpdateTagsTask.class);
28     public final AbstractJukesObject domain;
29  
30     /**
31      * Constructor that needs a config object and a filename to work on.
32      * <p>
33      * @param aJukesObject the domain object to work with
34      */
35     public UpdateTagsTask(AbstractJukesObject aJukesObject) {
36        super();
37        this.domain = aJukesObject;
38  
39        current = 0;
40        // calulate the total length of this task
41        if (domain instanceof Artist) {
42           final Artist artist = (Artist) domain;
43           final Object[] discArray = artist.getDiscs().toArray();
44  
45           // calculate the total length first by adding up all tracks
46           for (int i = 0; i < discArray.length; i++) {
47              final Disc disc = (Disc) discArray[i];
48              lengthOfTask += disc.getTracks().size();
49           }
50        } else if (domain instanceof Disc) {
51           final Disc disc = (Disc) domain;
52           final Object[] array = disc.getTracks().toArray();
53           lengthOfTask = array.length;
54        } else if (domain instanceof Track) {
55           lengthOfTask = 1;
56        } else {
57           throw new IllegalArgumentException("Unknown type.");
58        }
59        LOG.debug("Length of Task = " + lengthOfTask);
60     }
61  
62     /**
63      * Gets the domain.
64      * <p>
65      * @return Returns the domain.
66      */
67     public AbstractJukesObject getDomain() {
68        return this.domain;
69     }
70  
71     /**
72      * Called to start the task.
73      */
74     public void go() {
75        final Worker worker = new Worker() {
76           public Object construct() {
77              current = 0;
78              done = false;
79              canceled = false;
80              statMessage = null;
81              return new ActualTask();
82           }
83        };
84        worker.start();
85     }
86  
87     /**
88      * The actual long running task. This runs in a SwingWorker thread.
89      */
90     class ActualTask {
91        @SuppressWarnings("unused")
92        ActualTask() {
93           while (!canceled && !done) {
94              try {
95                 if (domain instanceof Artist) {
96                    final Artist artist = (Artist) domain;
97                    final Object[] discArray = artist.getDiscs().toArray();
98  
99                    // now update all tracks on all discs of artist
100                   DISC_LOOP: for (int i = 0; i < discArray.length; i++) {
101                      final Disc disc = (Disc) discArray[i];
102                      final Object[] trackArray = disc.getTracks().toArray();
103                      TRACK_LOOP: for (int j = 0; j < trackArray.length; j++) {
104                         if ((canceled || done)) {
105                            break DISC_LOOP;
106                         }
107                         current++;
108                         final Track track = (Track) trackArray[j];
109                         statMessage = "Updating: " + track.getName();
110                         updateTag(track);
111                      }
112                   }
113                } else if (domain instanceof Disc) {
114                   final Disc disc = (Disc) domain;
115                   final Object[] array = disc.getTracks().toArray();
116                   for (int i = 0; i < array.length; i++) {
117                      if ((canceled || done)) {
118                         break;
119                      }
120                      current = i;
121                      final Track track = (Track) array[i];
122                      statMessage = "Updating: " + track.getName();
123                      updateTag(track);
124                   }
125                } else if (domain instanceof Track) {
126                   final Track track = (Track) domain;
127                   current = 1;
128                   statMessage = "Updating: " + track.getName();
129                   updateTag(track);
130 
131                } else {
132                   throw new IllegalArgumentException("Unknown type.");
133                }
134 
135                done = true;
136             } catch (MusicTagException ex) {
137                stop();
138                LOG.info("Exception caught updating tags:", ex);
139                statMessage = "Exception caught updating tags: " + ex.getMessage();
140             } catch (RuntimeException ex) {
141                stop();
142                LOG.info("Exception caught updating tags:", ex);
143                statMessage = "Exception caught updating tags: " + ex.getMessage();
144             }
145          }
146       }
147 
148       /**
149        * Updates a music tag filling out all it's info from the track, disc, and
150        * artist based on the track provided.
151        * <p>
152        * @param aTrack the track to update the tag for
153        * @throws MusicTagException if any error occurs updating
154        */
155       private void updateTag(final Track aTrack) throws MusicTagException {
156          if (LOG.isDebugEnabled()) {
157             LOG.debug("Update Tag: " + aTrack.getTrackUrl());
158          }
159 
160          if (aTrack.isNotValid()) {
161             return;
162          }
163          final MusicTag musicTag = TagFactory.getTag(aTrack.getTrackUrl());
164          musicTag.setArtist(aTrack.getDisc().getArtist().getName());
165          musicTag.setDisc(aTrack.getDisc().getName());
166          musicTag.setGenre(aTrack.getDisc().getGenre());
167          musicTag.setYear(aTrack.getDisc().getYear());
168          musicTag.setTitle(aTrack.getName());
169          musicTag.setComment(aTrack.getComment());
170          musicTag.setTrack(aTrack.getTrackNumber());
171          musicTag.save();
172       }
173    }
174 
175 }