View Javadoc

1   package com.melloware.jukes.gui.view.tasks;
2   
3   import java.io.File;
4   import java.util.HashMap;
5   import java.util.Iterator;
6   import java.util.List;
7   import java.util.Map;
8   
9   import org.apache.commons.io.FileUtils;
10  import org.apache.commons.lang.StringUtils;
11  import org.apache.commons.logging.Log;
12  import org.apache.commons.logging.LogFactory;
13  
14  import com.jgoodies.uif.util.ResourceUtils;
15  import com.jgoodies.uif.util.Worker;
16  import com.melloware.jukes.db.orm.Artist;
17  import com.melloware.jukes.db.orm.Catalog;
18  import com.melloware.jukes.db.orm.Disc;
19  import com.melloware.jukes.file.Disclist;
20  
21  /**
22   * In a thread attempts to load the disclist one disc at a time and notify user
23   * of the progress.
24   * <p>
25   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
26   * @author Emil A. Lefkof III <info@melloware.com>
27   * @version 4.0 AZ Development 2009
28   */
29  @SuppressWarnings("unchecked")
30  public final class LoadDisclistTask extends LongTask {
31  
32     private static final Log LOG = LogFactory.getLog(LoadDisclistTask.class);
33     private static final String BREAK = " - ";
34     private final File file;
35     private final List discLines;
36     private final Disclist disclist;
37     private final Map artists;
38  
39     /**
40      * Constructor that needs a file to load.
41      * <p>
42      * @param aFile the file to load
43      * @throws Exception if any error occurs
44      */
45     public LoadDisclistTask(File aFile, Disclist aDisclist) throws Exception {
46        super();
47        LOG.debug("Loading disclist");
48        this.file = aFile;
49        this.disclist = aDisclist;
50  
51        // put all the artists in a map for faster access
52        List artists = Catalog.findAllArtists();
53        HashMap map = new HashMap(artists.size());
54        Iterator iter = artists.iterator();
55        while (iter.hasNext()) {
56           Artist element = (Artist) iter.next();
57           map.put(element.getName(), element);
58        }
59        this.artists = map;
60  
61        // load disclist
62        discLines = FileUtils.readLines(file, null);
63        lengthOfTask = discLines.size();
64        LOG.debug("Length of Task = " + lengthOfTask);
65     }
66  
67     /**
68      * Called to start the task.
69      */
70     @Override
71     public void go() {
72        final Worker worker = new Worker() {
73           @Override
74           public Object construct() {
75              current = 0;
76              done = false;
77              canceled = false;
78              statMessage = null;
79              return new ActualTask();
80           }
81        };
82        worker.start();
83     }
84  
85     /**
86      * Try to locate this disc in the database and load into disclist.
87      * <p>
88      * @param artistName the artist name
89      * @param discName the disc name
90      * @return the Disc if found else null
91      */
92     private Disc findDisc(String artistName, String discName) {
93        Disc result = null;
94  
95        Artist artist = (Artist) this.artists.get(artistName);
96        if (artist == null) {
97           return result;
98        }
99        Iterator iter = artist.getDiscs().iterator();
100       DISC_LOOP: while (iter.hasNext()) {
101          Disc disc = (Disc) iter.next();
102          if (StringUtils.equalsIgnoreCase(disc.getName(), discName)) {
103             result = disc;
104             break DISC_LOOP;
105          }
106       }
107 
108       return result;
109    }
110 
111    /**
112     * The actual long running task. This runs in a SwingWorker thread.
113     */
114    class ActualTask {
115       ActualTask() {
116          while (!canceled && !done) {
117             try {
118                if (discLines != null) { // NOPMD
119                   int count = 1;
120                   final Iterator iter = discLines.iterator();
121                   while ((iter.hasNext()) && (!canceled && !done)) {
122                      current = count;
123                      count++;
124                      String element = (String) iter.next();
125 
126                      String artist = StringUtils.substringBefore(element, BREAK).trim();
127                      element = StringUtils.substringAfter(element, BREAK).trim();
128                      element = StringUtils.substringBeforeLast(element, BREAK).trim();
129                      String discString = element;
130                      Disc disc = findDisc(artist, discString);
131                      if (disc != null) {
132                         statMessage = ResourceUtils.getString("messages.Loading") + disc.getName();
133                         disclist.add(disc);
134                      }
135 
136                   }
137                }
138 
139                done = true;
140             } catch (Exception ex) {
141                stop();
142                LOG.info(ResourceUtils.getString("messages.ExceptionLoadingDiscs"), ex);
143                statMessage = ResourceUtils.getString("messages.ExceptionLoadingDiscs") + ex.getMessage();
144             }
145          }
146       }
147 
148    }
149 
150 }