diff options
Diffstat (limited to 'src/uk/org/ury/library/LibraryUtils.java')
-rw-r--r-- | src/uk/org/ury/library/LibraryUtils.java | 112 |
1 files changed, 105 insertions, 7 deletions
diff --git a/src/uk/org/ury/library/LibraryUtils.java b/src/uk/org/ury/library/LibraryUtils.java index a280738..8a71285 100644 --- a/src/uk/org/ury/library/LibraryUtils.java +++ b/src/uk/org/ury/library/LibraryUtils.java @@ -8,12 +8,16 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.Map; import uk.org.ury.database.DatabaseDriver; import uk.org.ury.database.exceptions.QueryFailureException; -import uk.org.ury.library.LibraryItemProperty; 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.Directive; /** @@ -77,13 +81,9 @@ public class LibraryUtils + " t.artist, recordlabel AS label, status, media AS medium, format," + " datereleased, EXTRACT(EPOCH FROM dateadded) as dateadded," + " EXTRACT(EPOCH FROM datetime_lastedit) AS dateedited," - + " shelfletter, shelfnumber, cdid, memberid_add, memberid_lastedit," - + " digitised, clean," - + " a.fname AS fnameadd, a.sname AS snameadd, b.fname AS fnameedit, b.sname AS snameedit" + + " shelfletter, shelfnumber, cdid, digitised, clean" + " FROM rec_record AS r" + " INNER JOIN rec_track AS t ON (r.recordid = t.recordid)" - + " INNER JOIN member AS a ON (a.memberid = r.memberid_add)" - + " LEFT JOIN member AS b ON (b.memberid = r.memberid_lastedit)" + " WHERE t.title ILIKE ?" + " OR t.artist ILIKE ?" + " OR r.title ILIKE ?" @@ -126,7 +126,7 @@ public class LibraryUtils { // Translate SQL columns into a list of properties. - HashMap<LibraryItemProperty, String> properties = new HashMap<LibraryItemProperty, String> (); + Map<LibraryItemProperty, String> properties = new HashMap<LibraryItemProperty, String> (); for (LibraryItemProperty p : LibraryItemProperty.values ()) { @@ -144,4 +144,102 @@ 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<LibraryItem> + extractItemsFromResponse (List<String> response) + { + List<LibraryItem> result = new ArrayList<LibraryItem> (); + + boolean inItem = false; + List<String> itemBuffer = new ArrayList<String> (); + + 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<String> 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<LibraryItemProperty, String> properties = new HashMap<LibraryItemProperty, String> (); + + 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."); + } } |