View Javadoc

1   package com.melloware.jukes.ws;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   
6   import org.apache.commons.logging.Log;
7   import org.apache.commons.logging.LogFactory;
8   
9   import entagged.freedb.Freedb;
10  import entagged.freedb.FreedbException;
11  import entagged.freedb.FreedbQueryResult;
12  import entagged.freedb.FreedbReadResult;
13  import entagged.freedb.FreedbSettings;
14  
15  import com.melloware.jukes.util.MessageUtil;
16  import com.melloware.jukes.exception.WebServiceException;
17  import com.melloware.jukes.gui.tool.Resources;
18  
19  /**
20   * Static methods for performing web services calls to FreeDB
21   * 
22   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
23   * @author Emil A. Lefkof III <info@melloware.com>
24   * @version 4.0
25   * AZ Development 2010
26   */
27  
28  @SuppressWarnings("unchecked")
29  public final class FreeDBSearch {
30     private static final Log LOG = LogFactory.getLog(FreeDBSearch.class);
31  
32     /**
33      * Default constructor. Private so no instantiation.
34      */
35     private FreeDBSearch() {
36        super();
37     }
38    
39  
40     /**
41      * Finds a collection of albums at FreeDB
42      * <p>
43      * @param  float[] trackLength - a set of track lengths to search at FreeDB for.
44      * @return a collection of items matching the search criteria
45      * @throws WebServiceException if any error occurs querying the FreeDB service
46      */
47     public static Collection findItemsAtFreeDB(float[] trackLength)
48              throws WebServiceException {
49        if (LOG.isDebugEnabled()) {
50           LOG.debug("FreeDB search");
51        }
52        Collection collection = null;
53        Freedb freedb;
54        //Create default FreeDB settings
55        FreedbSettings fdbs = new FreedbSettings();
56        try {
57      	freedb = new Freedb(fdbs);
58  	  
59  		FreedbQueryResult[] fdbqr = freedb.query(trackLength);
60          if (fdbqr.length ==0) {
61        	  MessageUtil.showInformation(null, Resources.getString("messages.NoItemsFound"));  
62          }
63          	
64  		collection = loadItems(freedb, fdbqr);
65        } catch (FreedbException e) {
66            LOG.error(e.getMessage(), e);
67            MessageUtil.showInformation(null, "FreeDB: " + e.getMessage());
68        } catch (Exception ex) {
69           LOG.error(ex.getMessage(), ex);
70           throw new WebServiceException(ex);
71        } 
72  
73        return collection;
74     }
75  
76     /**
77      * Loads a collection with FreeDB Items from a list of FreeDB-response.
78      * <p>
79      * @param Freedb aFreeDB 
80      * @param FreedbQueryResult[] aFreeDBReaults the list of FreeDB query results
81      * @return a collection of FreeDBItem objects or an empty collection
82      * @throws WebServiceException 
83      */
84     private static Collection loadItems(Freedb aFreeDB, FreedbQueryResult[] aFreeDBResults) throws WebServiceException {
85        ArrayList collection = new ArrayList();
86      if (aFreeDBResults != null) {
87      	try {
88          for (int i=0; i<aFreeDBResults.length; i=i+1){
89          	FreedbReadResult freedbResult = aFreeDB.read(aFreeDBResults[i]);  
90              collection.add(new FreeDBItem(freedbResult));
91           }      
92      	} catch (FreedbException ex){
93              LOG.error(ex.getMessage(), ex);
94              throw new WebServiceException(ex);	
95      	}
96         } 
97        return collection;
98     }
99  
100 }