1 /* 2 * aTunes 3 * Copyright (C) Alex Aranda, Sylvain Gaudard and contributors 4 * 5 * See http://www.atunes.org/wiki/index.php?title=Contributing for information about contributors 6 * 7 * http://www.atunes.org 8 * http://sourceforge.net/projects/atunes 9 * 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public License 12 * as published by the Free Software Foundation; either version 2 13 * of the License, or (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 */ 20 21 package net.sourceforge.atunes.kernel.actions; 22 23 import java.util.Collection; 24 import java.util.List; 25 26 import net.sourceforge.atunes.model.IAlbum; 27 import net.sourceforge.atunes.model.IArtist; 28 import net.sourceforge.atunes.model.IAudioObject; 29 import net.sourceforge.atunes.model.IFileManager; 30 import net.sourceforge.atunes.model.ILocalAudioObject; 31 import net.sourceforge.atunes.model.IPlayListHandler; 32 import net.sourceforge.atunes.model.IRepositoryHandler; 33 import net.sourceforge.atunes.utils.CollectionUtils; 34 35 import org.apache.commons.lang.StringUtils; 36 37 /** 38 * This action enables a telnet client to browse the repository. It enables a 39 * client to view a list of all song path so that a client then can create a 40 * local list of songs and play them when needed. With parameters, it outputs 41 * either all artists or all albums, then when an artist/album name is provided 42 * it can output all songs belonging to him/it. 43 * 44 */ 45 public class PrintSongListRemoteAction extends RemoteAction { 46 47 private static final String PLAYLIST = "playlist"; 48 private static final String ALBUM = "album"; 49 private static final String ARTIST = "artist"; 50 private static final long serialVersionUID = -1450032380215351834L; 51 52 private IRepositoryHandler repositoryHandler; 53 54 private IPlayListHandler playListHandler; 55 56 private IFileManager fileManager; 57 58 /** 59 * @param fileManager 60 */ setFileManager(IFileManager fileManager)61 public void setFileManager(IFileManager fileManager) { 62 this.fileManager = fileManager; 63 } 64 65 /** 66 * @param handler 67 */ setRepositoryHandler(final IRepositoryHandler handler)68 public void setRepositoryHandler(final IRepositoryHandler handler) { 69 this.repositoryHandler = handler; 70 } 71 72 /** 73 * @param handler 74 */ setPlayListHandler(final IPlayListHandler handler)75 public void setPlayListHandler(final IPlayListHandler handler) { 76 this.playListHandler = handler; 77 } 78 79 @Override runCommand(final List<String> parameters)80 public String runCommand(final List<String> parameters) { 81 Collection<ILocalAudioObject> objects = null; 82 83 if (!CollectionUtils.isEmpty(parameters)) { 84 String query = null; 85 if (parameters.size() > 1) { 86 query = StringUtils.join( 87 parameters.subList(1, parameters.size()), ' '); 88 } 89 String firstParameter = parameters.get(0); 90 if (firstParameter.equalsIgnoreCase(ARTIST)) { 91 objects = processArtistParameter(query); 92 if (objects == null) { 93 return "Artist not found."; 94 } 95 } else if (firstParameter.equalsIgnoreCase(ALBUM)) { 96 objects = processAlbumParameter(query); 97 if (objects == null) { 98 return "Album not found"; 99 } 100 } else if (firstParameter.equalsIgnoreCase(PLAYLIST)) { 101 return processPlaylistParameter(); 102 } else { 103 return "Bad command"; 104 } 105 } else { 106 objects = processAllSongs(); 107 } 108 109 StringBuilder sb = new StringBuilder(); 110 if (objects != null) { 111 for (ILocalAudioObject iao : objects) { 112 sb.append(fileManager.getPath(iao)).append("\n"); 113 } 114 } 115 return sb.toString(); 116 } 117 processAllSongs()118 private Collection<ILocalAudioObject> processAllSongs() { 119 return repositoryHandler.getAudioFilesList(); 120 } 121 processPlaylistParameter()122 private String processPlaylistParameter() { 123 Collection<IAudioObject> playlistObjects = playListHandler 124 .getActivePlayList().getAudioObjectsList(); 125 if (playlistObjects.isEmpty()) { 126 return "Playlist is empty"; 127 } else { 128 StringBuilder sb = new StringBuilder(); 129 for (IAudioObject audio : playlistObjects) { 130 sb.append(audio.getTitleOrFileName()).append("\n"); 131 } 132 return sb.toString(); 133 } 134 } 135 136 /** 137 * @param query 138 * @return 139 */ processArtistParameter( final String query)140 private Collection<ILocalAudioObject> processArtistParameter( 141 final String query) { 142 if (net.sourceforge.atunes.utils.StringUtils.isEmpty(query)) { 143 returnArtistList(); 144 return null; 145 } else { 146 IArtist artist = repositoryHandler.getArtist(query); 147 if (artist == null) { 148 return null; 149 } else { 150 return artist.getAudioObjects(); 151 } 152 } 153 } 154 processAlbumParameter( final String query)155 private Collection<ILocalAudioObject> processAlbumParameter( 156 final String query) { 157 if (net.sourceforge.atunes.utils.StringUtils.isEmpty(query)) { 158 returnAlbumList(); 159 return null; 160 } else { 161 List<IAlbum> albums = repositoryHandler.getAlbums(); 162 // Find album 163 IAlbum album = null; 164 for (IAlbum alb : albums) { 165 if (alb.getName().equalsIgnoreCase(query)) { 166 album = alb; 167 break; 168 } 169 } 170 if (album == null) { 171 return null; 172 } else { 173 return album.getAudioObjects(); 174 } 175 } 176 } 177 178 /** 179 * Builds a list of artists 180 */ returnArtistList()181 private String returnArtistList() { 182 StringBuilder sb = new StringBuilder(); 183 for (IArtist artist : repositoryHandler.getArtists()) { 184 sb.append(artist.getName()).append("\n"); 185 } 186 return sb.toString(); 187 } 188 189 /** 190 * Builds a list of albums 191 */ returnAlbumList()192 private String returnAlbumList() { 193 StringBuilder sb = new StringBuilder(); 194 for (IAlbum album : repositoryHandler.getAlbums()) { 195 sb.append(album.getName()).append("\n"); 196 } 197 return sb.toString(); 198 } 199 200 @Override getHelpText()201 protected String getHelpText() { 202 return "Prints out a list of all song files. " 203 + "When parameter artist is used it prints the list of all artists, when the name of an artist is provided, " 204 + "it ouputs all song which belong to the artist"; 205 } 206 207 @Override getOptionalParameters()208 protected String getOptionalParameters() { 209 return "[artist [artistName] | album [albumName]]"; 210 } 211 } 212