Moved MPRIS to a new project
[stereo.git] / DAAPLib / src / util / command / databases / Groups.java
blob98fdb44e8280e27ff238ea816ef37eed37f207f2
1 package util.command.databases;
3 import interfaces.Album;
4 import interfaces.Constants;
5 import interfaces.DJInterface;
7 import java.util.ArrayList;
8 import java.util.Collections;
9 import java.util.Comparator;
10 import java.util.List;
11 import java.util.Map;
13 import util.command.Command;
14 import util.queryparser.ApplyFilter;
15 import util.queryparser.Filter;
16 import util.queryparser.QueryParser;
17 import api.Response;
18 import api.nodes.AlbumNode;
20 public class Groups implements Command {
22 private Map<String, String> args;
24 public void init(Map<String, String> args) {
25 this.args = args;
28 public Response run(DJInterface dj) {
30 Iterable<? extends Album> albums = dj.library().albums();
32 if (args == null) {
33 throw new IllegalArgumentException("no arguments to group query");
36 //don't query, as albums store name as minm not asal
38 List<? extends Album> als;
39 if (args.containsKey("query")) {
40 String query = args.get("query").replace(' ', '_');
41 Filter q = QueryParser.parse(query);
42 System.out.println(q);
43 als = ApplyFilter.filter(q, albums);
45 else {
46 List<Album> as = new ArrayList<Album>();
47 for (Album a: albums) {
48 as.add(a);
50 als = as;
54 meta=all
55 type=music
56 group-type=albums
57 sort=album
58 include-sort-headers=1
59 query=(('com.apple.itunes.mediakind:1','com.apple.itunes.mediakind:32')+'daap.songalbum!:'
62 if (!args.containsKey("group-type") || !args.get("group-type").equals("albums")) {
63 throw new IllegalArgumentException("trying to handle unknown group-type: " + args.get("group-type"));
66 Collections.sort(als, new Comparator<Album>() {
67 public int compare(Album a, Album b) {
68 int c = a.name().compareTo(b.name());
69 if (c == 0) {
70 return a.artist().compareTo(b.artist());
72 return c;
74 });
76 List<AlbumNode> nodes = new ArrayList<AlbumNode>();
77 for (Album a: als) {
78 nodes.add(new AlbumNode(a));
81 System.out.println("returning " + nodes.size() + " elements");
83 return new util.response.databases.Groups<AlbumNode>(Constants.daap_albumgrouping, nodes);