1 package org.musicontroller.repair;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.List;
6
7 import org.apache.log4j.Logger;
8 import org.farng.mp3.MP3File;
9 import org.farng.mp3.TagException;
10 import org.musicontroller.core.Link;
11 import org.musicontroller.core.Song;
12 import org.musicontroller.dao.Dao;
13
14
15
16
17
18
19
20
21
22 public class SongLengthRepair {
23 private static Logger _log = Logger.getLogger(SongLengthRepair.class);
24
25
26
27
28
29
30 @SuppressWarnings("unchecked")
31 public static void execute(Dao dao) {
32 List<Song> patients = dao.search("from Song s where s.length=0", null, 0);
33 for(Song patient : patients) {
34 Link link = patient.getLink();
35 if(link==null) {
36 _log.error("Song "+patient.getId()+" has a NULL link.");
37 continue;
38 }
39 String path = link.getUrl();
40 if(path==null) {
41 _log.error("Song "+patient.getId()+" has NULL-URL in its Link.");
42 continue;
43 }
44 File songfile = new File(path);
45 if(!songfile.canRead()) {
46 _log.error("Song "+patient.getId()+" has a non-existent or unreadable file.");
47 continue;
48 }
49 try {
50 MP3File mp3file = new MP3File(songfile,false);
51 int length = mp3file.getLength();
52 patient.setLength(length);
53 dao.save(patient);
54 } catch (IOException e) {
55 _log.error("Song "+patient.getId()+" file unreadble: "+e.getLocalizedMessage());
56 continue;
57 } catch (TagException e) {
58 _log.error("Song "+patient.getId()+" has invalid MP3 file: "+e.getLocalizedMessage());
59 continue;
60 }
61 }
62 }
63 }