From cee53b021632c95f1b4882664a31ca639a9b0700 Mon Sep 17 00:00:00 2001 From: Matt Windsor Date: Sun, 20 Mar 2011 16:38:51 +0000 Subject: Rewrite of server to use HttpCore example code; beginning of code reformat from GNU style to Java conventions. Code now includes Apache license code. --- src/uk/org/ury/library/viewer/LibraryViewer.java | 275 +++++++++++------------ 1 file changed, 126 insertions(+), 149 deletions(-) (limited to 'src/uk/org/ury/library/viewer/LibraryViewer.java') diff --git a/src/uk/org/ury/library/viewer/LibraryViewer.java b/src/uk/org/ury/library/viewer/LibraryViewer.java index 9e854b7..11496cb 100644 --- a/src/uk/org/ury/library/viewer/LibraryViewer.java +++ b/src/uk/org/ury/library/viewer/LibraryViewer.java @@ -1,3 +1,14 @@ +/* + * 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; @@ -7,168 +18,134 @@ 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 - * + * + * @author Matt Windsor */ - -public class LibraryViewer extends AbstractFrontendModule -{ - /** - * - */ - - private static final long serialVersionUID = -2782366476480563739L; - private List libraryList; - private LibraryViewerPanel panel; - - - /** - * Construct a new LibraryViewer as a frontend object. - */ - - public - LibraryViewer () - { - libraryList = new ArrayList (); - panel = null; - } - - - /** - * Run the library viewer frontend. - */ - - 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/LibraryRequestHandler?function=search&search=" + search); - } - catch (DecodeFailureException e) - { - throw new InvalidMessageException (e.getMessage ()); - } - - // Check to see if this is Map 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 properties - = new HashMap (); - - 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)); - } - - //libraryList = LibraryUtils.search (dd, search); - } - - - /** - * @return the current library list. - */ - - public List - getLibraryList () - { - return libraryList; - } +public class LibraryViewer extends AbstractFrontendModule { + /** + * + */ + private static final long serialVersionUID = -2782366476480563739L; + private List libraryList; + private LibraryViewerPanel panel; + + /** + * Construct a new LibraryViewer as a frontend object. + */ + public LibraryViewer() { + libraryList = new ArrayList(); + 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/LibraryRequestHandler?function=search&search=" + + search); + } catch (DecodeFailureException e) { + throw new InvalidMessageException(e.getMessage()); + } + + /* + * Check to see if this is Map 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 properties = new HashMap(); + + 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 getLibraryList() { + return libraryList; + } } -- cgit v1.2.3