From 8f0360d9e72843d527b2db28047ae5f0498f3a3e Mon Sep 17 00:00:00 2001 From: Matt Windsor Date: Sat, 19 Mar 2011 17:25:07 +0000 Subject: Library viewer now retrieves tracks from server. Code will need a LOT of cleanup, but this is a triumph. I'm making a note here-- huge success. --- src/uk/org/ury/database/DatabaseItem.java | 2 +- src/uk/org/ury/library/LibraryRequestHandler.java | 2 +- src/uk/org/ury/library/LibraryUtils.java | 98 ---------------------- src/uk/org/ury/library/viewer/LibraryViewer.java | 88 ++++++++++++++++++- .../org/ury/library/viewer/LibraryViewerPanel.java | 6 +- src/uk/org/ury/server/Server.java | 1 - .../ury/server/exceptions/BadRequestException.java | 7 +- 7 files changed, 94 insertions(+), 110 deletions(-) (limited to 'src') diff --git a/src/uk/org/ury/database/DatabaseItem.java b/src/uk/org/ury/database/DatabaseItem.java index a6d929f..54b2ef7 100644 --- a/src/uk/org/ury/database/DatabaseItem.java +++ b/src/uk/org/ury/database/DatabaseItem.java @@ -72,7 +72,7 @@ public abstract class DatabaseItem /** - * Map down the item into a server response body. + * Retrieve a map of string representations of the properties. * * This relies on E and T having meaningful toString methods. * diff --git a/src/uk/org/ury/library/LibraryRequestHandler.java b/src/uk/org/ury/library/LibraryRequestHandler.java index 835e50e..bd7595b 100644 --- a/src/uk/org/ury/library/LibraryRequestHandler.java +++ b/src/uk/org/ury/library/LibraryRequestHandler.java @@ -133,7 +133,7 @@ public class LibraryRequestHandler implements RequestHandler itemArray.add (li.asResponse ()); } - response.put ("items", itemArray); + response.put (Directive.ITEMS.toString (), itemArray); } catch (QueryFailureException e) { diff --git a/src/uk/org/ury/library/LibraryUtils.java b/src/uk/org/ury/library/LibraryUtils.java index 474c58a..9b4471a 100644 --- a/src/uk/org/ury/library/LibraryUtils.java +++ b/src/uk/org/ury/library/LibraryUtils.java @@ -141,102 +141,4 @@ public class LibraryUtils return new LibraryItem (properties); } - - - /** - * Construct items from a server response body. - * - * @param response The list of strings that constitute the response. - * - * @return a list of LibraryItems corresponding to the item - * stanzas in the response. - */ - -/* public static List - extractItemsFromResponse (List response) - { - List result = new ArrayList (); - - boolean inItem = false; - List itemBuffer = new ArrayList (); - - for (String line : response) - { - if (inItem == false) - { - if (line.startsWith (Directive.ITEM_START.toString ())) - { - inItem = true; - itemBuffer.clear (); - } - } - - if (inItem == true) - { - itemBuffer.add (line); - - if (line.startsWith (Directive.ITEM_END.toString ())) - { - inItem = false; - result.add (createItemFromResponse (itemBuffer)); - } - } - } - - return result; - }*/ - - - /** - * Construct a new item from a server response fragment. - * - * @param response The list of strings that constitutes the response. - * The list must begin with Directive.ITEM_START and - * end with Directive.ITEM_END's string - * representations and otherwise contain solely - * Directive.ITEM_PROPERTY lines. - * - * @return a LibraryItem embodying the properties described - * in the response fragment. - * - * @throws IllegalArgumentException if the response is - * malformed or null, or if the instantiation of - * DatabaseItem does not use String as its data type. - */ - - /*public static LibraryItem - createItemFromResponse (List response) - { - // TODO: More appropriate exceptions. - - if (response == null) - throw new IllegalArgumentException ("Response is null."); - else if (response.get (0).equals (Directive.ITEM_START.toString ()) - && response.get (response.size () - 1) - .equals (Directive.ITEM_END.toString ())) - { - // Response of size 2 must be blank. - if (response.size () <= 2) - throw new IllegalArgumentException ("Blank response."); - - Map properties = new HashMap (); - - for (int i = 0; i < response.size () - 1; i++) - { - // TODO: fan out impl. details to separate class - if (response.get (i) - .startsWith (Directive.ITEM_PROPERTY.toString ())) - { - String[] responseTuple = response.get (i).split (":="); - - properties.put (LibraryItemProperty.getFromSQL (responseTuple[1]), - responseTuple[2]); - } - } - - return new LibraryItem (properties); - } - else - throw new IllegalArgumentException ("Malformed response."); - }*/ } diff --git a/src/uk/org/ury/library/viewer/LibraryViewer.java b/src/uk/org/ury/library/viewer/LibraryViewer.java index 9587d5e..3e09594 100644 --- a/src/uk/org/ury/library/viewer/LibraryViewer.java +++ b/src/uk/org/ury/library/viewer/LibraryViewer.java @@ -1,21 +1,42 @@ 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.config.ConfigReader; + import uk.org.ury.database.DatabaseDriver; import uk.org.ury.database.UserClass; + import uk.org.ury.database.exceptions.MissingCredentialsException; import uk.org.ury.database.exceptions.QueryFailureException; + 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.LibraryUtils; 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.server.protocol.DecodeFailureException; +import uk.org.ury.server.protocol.Directive; +import uk.org.ury.server.protocol.Status; + + +/** + * Module for investigating the track library. + * + * @author Matt Windsor + * + */ public class LibraryViewer extends AbstractFrontendModule { @@ -24,7 +45,6 @@ public class LibraryViewer extends AbstractFrontendModule */ private static final long serialVersionUID = -2782366476480563739L; - private DatabaseDriver dd; private List libraryList; private LibraryViewerPanel panel; private ConfigReader config; @@ -107,7 +127,69 @@ public class LibraryViewer extends AbstractFrontendModule doSearch (String search) throws EmptySearchException, QueryFailureException { - libraryList = LibraryUtils.search (dd, search); + // TODO: fan out? + + Client cl = new Client (); + Map response = null; + libraryList.clear (); + + try + { + response = cl.get ("/library/LibraryRequestHandler?function=search&search=" + search); + } + catch (DecodeFailureException e) + { + throw new QueryFailureException (e.getMessage ()); + } + + // Check to see if this is Map by looking for the status, + // which should always be in a valid response. + if (response.containsKey (Directive.STATUS.toString ()) == false + || (response.get (Directive.STATUS.toString ()) instanceof String) == false) + throw new QueryFailureException ("Status not provided."); + + if (((String) response.get (Directive.STATUS.toString ())) + .equals (Status.OK.toString ()) == false) + throw new QueryFailureException ((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 QueryFailureException ("No item set returned."); + + if ((response.get (Directive.ITEMS.toString ()) instanceof List) == false) + throw new QueryFailureException ("Malformed item list."); + + + for (Object obj : (List) response.get (Directive.ITEMS.toString ())) + { + Map properties + = new HashMap (); + + if (obj instanceof Map == false) + throw new QueryFailureException ("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 QueryFailureException ("Not a valid property."); + else if (LibraryItemProperty.valueOf ((String) key) == null) + throw new QueryFailureException ("Property type " + + key + " not recognised."); + else + properties.put (LibraryItemProperty.valueOf ((String) key), + (String) ((Map) obj).get (key)); + + } + + libraryList.add (new LibraryItem (properties)); + } + + //libraryList = LibraryUtils.search (dd, search); } diff --git a/src/uk/org/ury/library/viewer/LibraryViewerPanel.java b/src/uk/org/ury/library/viewer/LibraryViewerPanel.java index 79b5902..e05164e 100644 --- a/src/uk/org/ury/library/viewer/LibraryViewerPanel.java +++ b/src/uk/org/ury/library/viewer/LibraryViewerPanel.java @@ -177,6 +177,7 @@ public class LibraryViewerPanel extends FrontendModulePanel { searchFailureMessage = String.format (rb.getString ("ERR_SEARCH_FAILED"), searchTerm, e.getMessage ()); + return false; } catch (EmptySearchException e) { @@ -206,11 +207,12 @@ public class LibraryViewerPanel extends FrontendModulePanel } catch (InterruptedException e) { - // Ignore + e.printStackTrace (); } catch (ExecutionException e) { - // Ignore + searchFailureMessage = String.format (rb.getString ("ERR_SEARCH_FAILED"), + searchTerm, e.getMessage ()); } diff --git a/src/uk/org/ury/server/Server.java b/src/uk/org/ury/server/Server.java index 75b5bd9..697476e 100644 --- a/src/uk/org/ury/server/Server.java +++ b/src/uk/org/ury/server/Server.java @@ -417,7 +417,6 @@ public class Server serveError (int code, String reason) { // Get the reason string to put in the error response. - // TODO: standards? String statusReason = ""; diff --git a/src/uk/org/ury/server/exceptions/BadRequestException.java b/src/uk/org/ury/server/exceptions/BadRequestException.java index 61aa951..189c7f5 100644 --- a/src/uk/org/ury/server/exceptions/BadRequestException.java +++ b/src/uk/org/ury/server/exceptions/BadRequestException.java @@ -12,12 +12,11 @@ package uk.org.ury.server.exceptions; public class BadRequestException extends HandlingException { - + /** - * TODO: Change this! ---v + * */ - - private static final long serialVersionUID = -397479334359858162L; + private static final long serialVersionUID = 1825771401085225357L; /** -- cgit v1.2.3