1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
/*
* LibraryViewer.java
* ------------------
*
* Part of the URY Frontend Platform
*
* V0.00 2011/03/20
*
* (C) 2011 URY Computing
*/
package uk.org.ury.library.viewer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import uk.org.ury.client.Client;
import uk.org.ury.frontend.AbstractFrontendModule;
import uk.org.ury.frontend.FrontendMaster;
import uk.org.ury.frontend.FrontendModulePanel;
import uk.org.ury.frontend.exceptions.UICreationFailureException;
import uk.org.ury.library.exceptions.EmptySearchException;
import uk.org.ury.library.item.LibraryItem;
import uk.org.ury.library.item.LibraryItemProperty;
import uk.org.ury.protocol.Directive;
import uk.org.ury.protocol.ProtocolUtils;
import uk.org.ury.protocol.exceptions.DecodeFailureException;
import uk.org.ury.protocol.exceptions.InvalidMessageException;
/**
* Module for investigating the track library.
*
* @author Matt Windsor
*/
public class LibraryViewer extends AbstractFrontendModule {
/**
*
*/
private static final long serialVersionUID = -2782366476480563739L;
private List<LibraryItem> libraryList;
private LibraryViewerPanel panel;
/**
* Construct a new LibraryViewer as a frontend object.
*/
public LibraryViewer() {
libraryList = new ArrayList<LibraryItem>();
panel = null;
}
/**
* Run the library viewer frontend.
*/
@Override
public FrontendModulePanel runFrontend(FrontendMaster master) {
try {
panel = new LibraryViewerPanel(this, master);
} catch (UICreationFailureException e) {
master.fatalError(e.getMessage());
}
return panel;
}
/**
* Do a library search.
*
* This will update the library list to reflect the results of the search.
*
* @param search
* The string fragment to use in searches. Cannot be empty or
* null.
*
* @throws EmptySearchException
* if the search string is empty or null (from
* LibraryUtils.search).
*
* @throws InvalidMessageException
* if the response from the server is invalid.
*/
public void doSearch(String search) throws EmptySearchException,
InvalidMessageException {
// TODO: fan out?
if (search == null || search == "")
throw new EmptySearchException();
Client cl = new Client();
Map<?, ?> response = null;
libraryList.clear();
try {
response = cl
.get("/library/tracks?search="
+ search);
} catch (DecodeFailureException e) {
throw new InvalidMessageException(e.getMessage());
}
/*
* Check to see if this is Map<String, ?> by looking for the status,
* which should always be in a valid response.
*/
if (ProtocolUtils.responseIsOK(response) == false)
throw new InvalidMessageException(
(String) response.get(Directive.REASON.toString()));
// Should contain a list of items, even if there are no items.
if (response.containsKey(Directive.ITEMS.toString()) == false)
throw new InvalidMessageException("No item set returned.");
if ((response.get(Directive.ITEMS.toString()) instanceof List<?>) == false)
throw new InvalidMessageException("Malformed item list.");
for (Object obj : (List<?>) response.get(Directive.ITEMS.toString())) {
Map<LibraryItemProperty, String> properties = new HashMap<LibraryItemProperty, String>();
if (obj instanceof Map<?, ?> == false)
throw new InvalidMessageException("Malformed item.");
Set<?> keySet = ((Map<?, ?>) obj).keySet();
// Check to make sure this item has only String-String mappings.
for (Object key : keySet) {
if ((key instanceof String && ((Map<?, ?>) obj).get(key) instanceof String) == false)
throw new InvalidMessageException("Not a valid property.");
else if (LibraryItemProperty.valueOf((String) key) == null)
throw new InvalidMessageException("Property type " + key
+ " not recognised.");
else
properties.put(LibraryItemProperty.valueOf((String) key),
(String) ((Map<?, ?>) obj).get(key));
}
libraryList.add(new LibraryItem(properties));
}
}
/**
* @return the current library list.
*/
public List<LibraryItem> getLibraryList() {
return libraryList;
}
}
|