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   import org.apache.commons.lang.StringUtils;
6   
7   import com.jgoodies.uif.util.ResourceUtils;
8   import com.jgoodies.uif.util.Worker;
9   import com.melloware.jukes.db.orm.AbstractJukesObject;
10  import com.melloware.jukes.db.orm.Artist;
11  import com.melloware.jukes.db.orm.Disc;
12  import com.melloware.jukes.db.orm.Track;
13  import com.melloware.jukes.exception.MusicTagException;
14  import com.melloware.jukes.file.tag.MusicTag;
15  import com.melloware.jukes.file.tag.TagFactory;
16  import com.melloware.jukes.util.MessageUtil;
17  
18  /**
19   * In a thread attempts to update all tags for all tracks for a domain object
20   * (Artist, Disc, Track). It will loop through all Artist's Disc's Tracks. It
21   * will loop through all Disc's Tracks. Or it will update a single Track.
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  @SuppressWarnings("unchecked")
28  public final class UpdateTagsTask extends LongTask {
29     private static final Log LOG = LogFactory.getLog(UpdateTagsTask.class);
30     public final AbstractJukesObject domain;
31  
32     /**
33      * Constructor that needs a config object and a filename to work on.
34      * <p>
35      * @param aJukesObject the domain object to work with
36      */
37     public UpdateTagsTask(AbstractJukesObject aJukesObject) {
38        super();
39        this.domain = aJukesObject;
40  
41        current = 0;
42        // calulate the total length of this task
43        if (domain instanceof Artist) {
44           final Artist artist = (Artist) domain;
45           final Object[] discArray = artist.getDiscs().toArray();
46  
47           // calculate the total length first by adding up all tracks
48           for (int i = 0; i < discArray.length; i++) {
49              final Disc disc = (Disc) discArray[i];
50              lengthOfTask += disc.getTracks().size();
51           }
52        } else if (domain instanceof Disc) {
53           final Disc disc = (Disc) domain;
54           final Object[] array = disc.getTracks().toArray();
55           lengthOfTask = array.length;
56        } else if (domain instanceof Track) {
57           lengthOfTask = 1;
58        } else {
59           throw new IllegalArgumentException("Unknown type.");
60        }
61        LOG.debug("Length of Task = " + lengthOfTask);
62     }
63  
64     /**
65      * Gets the domain.
66      * <p>
67      * @return Returns the domain.
68      */
69     public AbstractJukesObject getDomain() {
70        return this.domain;
71     }
72  
73     /**
74      * Called to start the task.
75      */
76     public void go() {
77        final Worker worker = new Worker() {
78           public Object construct() {
79              current = 0;
80              done = false;
81              canceled = false;
82              statMessage = null;
83              return new ActualTask();
84           }
85        };
86        worker.start();
87     }
88  
89     /**
90      * The actual long running task. This runs in a SwingWorker thread.
91      */
92     class ActualTask {
93        @SuppressWarnings("unused")
94        ActualTask() {
95      	 warning = false;//AZ
96      	 boolean setComment = true;//AZ
97           while (!canceled && !done) {
98              try {
99                 if (domain instanceof Artist) {
100                   final Artist artist = (Artist) domain;
101                   final Object[] discArray = artist.getDiscs().toArray();
102 
103                   // now update all tracks on all discs of artist
104                   DISC_LOOP: for (int i = 0; i < discArray.length; i++) {
105                      final Disc disc = (Disc) discArray[i];
106                      final Object[] trackArray = disc.getTracks().toArray();
107                      TRACK_LOOP: for (int j = 0; j < trackArray.length; j++) {
108                         if ((canceled || done)) {
109                            break DISC_LOOP;
110                         }
111                         current++;
112                         final Track track = (Track) trackArray[j];
113                         statMessage = ResourceUtils.getString("label.Updating") + track.getName();
114                         updateTag(track);
115                      }
116                   }
117                } else if (domain instanceof Disc) {
118                   final Disc disc = (Disc) domain;
119                   String discNotes = disc.getNotes();//AZ
120                   if (discNotes != null) {
121                   if (discNotes.length() > 255) {
122                 	  discNotes.substring(0, 254);
123                   }
124                   }
125                   final Object[] array = disc.getTracks().toArray();
126                   //Look for track comments
127                   for (int i = 0; i < array.length; i++) {
128                 	  final Track track = (Track) array[i];
129                 	  if (StringUtils.isNotEmpty(track.getComment())) {
130                 		  setComment = false;
131                 	  }
132                   }
133                   
134                   for (int i = 0; i < array.length; i++) {
135                      if ((canceled || done)) {
136                         break;
137                      }
138                      current = i;
139                      final Track track = (Track) array[i];
140                      //if Track Comments are empty then set them to be equal to Disc Notes
141                      if ((setComment) && (StringUtils.isNotEmpty(discNotes))) {
142                     	 track.setComment(discNotes);
143                      }
144                      statMessage = ResourceUtils.getString("label.Updating") + track.getName();
145                      updateTag(track);
146                      if (setComment) {
147                     	 track.setComment(""); 
148                      }
149                   }
150                } else if (domain instanceof Track) {
151                   final Track track = (Track) domain;
152                   current = 1;
153                   statMessage = ResourceUtils.getString("label.Updating") + track.getName();
154                   updateTag(track);
155 
156                } else {
157                   throw new IllegalArgumentException("Unknown type.");
158                }
159                done = true;
160             } catch (MusicTagException ex) {
161                stop();
162                LOG.warn("Exception caught updating tags: " + ex.getMessage());
163                statMessage = "Exception caught updating tags: " + ex.getMessage();
164                MessageUtil.showError(null,ResourceUtils.getString("label.Errorwritingfile"));//AZ
165             } catch (RuntimeException ex) {
166                stop();
167                LOG.warn("Exception caught updating tags: "+ ex.getMessage());
168                statMessage = "Exception caught updating tags: " + ex.getMessage();
169                MessageUtil.showError(null, ResourceUtils.getString("label.Errorwritingfile"));//AZ
170             }
171          }
172       }
173 
174       /**
175        * Updates a music tag filling out all it's info from the track, disc, and
176        * artist based on the track provided.
177        * <p>
178        * @param aTrack the track to update the tag for
179        * @throws MusicTagException if any error occurs updating
180        */
181       private void updateTag(final Track aTrack) throws MusicTagException {
182          if (LOG.isDebugEnabled()) {
183             LOG.debug("Update Tag: " + aTrack.getTrackUrl());
184          }
185 
186          if (aTrack.isNotValid()) {
187         	 warning = true;//AZ
188         	 LOG.warn("Error Updating Tag: " + aTrack.getTrackUrl());
189         	 return;
190          }
191          try {
192          final MusicTag musicTag = TagFactory.getTag(aTrack.getTrackUrl());
193          musicTag.setArtist(aTrack.getDisc().getArtist().getName());
194          musicTag.setDisc(aTrack.getDisc().getName());
195          musicTag.setGenre(aTrack.getDisc().getGenre());
196          musicTag.setYear(aTrack.getDisc().getYear());
197          musicTag.setTitle(aTrack.getName());
198          musicTag.setComment(aTrack.getComment());
199          musicTag.setTrack(aTrack.getTrackNumber());
200          musicTag.save();
201          }
202          catch (Exception ex) {
203         	 warning = true;//AZ
204         	 LOG.warn("Exception caught updating tags: "+ ex.getMessage());
205          }
206       }
207    }
208 
209 }