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
21
22
23
24
25
26
27
28 @SuppressWarnings("unchecked")
29 public final class FreeDBSearch {
30 private static final Log LOG = LogFactory.getLog(FreeDBSearch.class);
31
32
33
34
35 private FreeDBSearch() {
36 super();
37 }
38
39
40
41
42
43
44
45
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
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
78
79
80
81
82
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 }