PlaylistService extends BasicService implements OwnerAPISupport<Playlist> { @GET @Path("/playlists/{playlistId}/references") @Produces(MediaType.APPLICATION_JSON) public PlaylistReferences getReferences(@PathParam("playlistId") long playlistId) { Playlist existingPlaylist = getExistingPlaylist(playlistId, false); if (!existingPlaylist.isListPublicly() && !this.isAdmin() && !inAdminOwners(existingPlaylist.getOwners())) { throwUnauthorized(); } PlaylistReferences refs = new PlaylistReferences(); List<Screen> screens = ScreenDAO.getScreensByPlaylist(existingPlaylist); for (Screen screen : screens) { if (this.isAdmin() || inAdminOwners(screen.getOwners())) { refs.addUserScreen(screen); } else { refs.setForeignScreens(refs.getForeignScreens() + 1); } } List<Playlist> playlists = PlaylistDAO.getPlaylistsByPlaylist(existingPlaylist); for (Playlist playlist : playlists) { if (this.isAdmin() || inAdminOwners(playlist.getOwners())) { refs.addUserPlaylist(playlist); } else { refs.setForeignPlaylists(refs.getForeignPlaylists() + 1); } } return refs; }  @GET @Path("/playlists") @Produces(MediaType.APPLICATION_JSON) List<Playlist> getPlaylists(@DefaultValue("false") @QueryParam("expand") boolean expand,
			@QueryParam("userId") String userId); @GET @Path("/catalog") @Produces(MediaType.APPLICATION_JSON) List<Playlist> getPlaylistCatalog(@DefaultValue("0") @QueryParam("exclude") long excludeId); @GET @Path("/playlists/{playlistId}") @Produces(MediaType.APPLICATION_JSON) Playlist getPlaylist(@PathParam("playlistId") long playlistId,
			@DefaultValue("false") @QueryParam("visibleOnly") boolean visibleOnly,
			@DefaultValue("false") @QueryParam("expand") boolean expand,
			@DefaultValue("true") @QueryParam("includeMetrics") boolean includeMetrics); @PUT @Path("/playlists/{playlistId}") @Produces(MediaType.APPLICATION_JSON) Playlist updatePlaylist(@PathParam("playlistId") long playlistId, Playlist playlistData); @POST @Path("/playlists") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) Playlist createPlaylist(Playlist playlist); @POST @Path("/playlists/{playlistId}/pages") @Consumes(MediaType.APPLICATION_JSON) Playlist addPage(@PathParam("playlistId") long playlistId, Page page); @POST @Path("/playlists/{playlistId}/playlists") @Consumes(MediaType.APPLICATION_JSON) Playlist addPlaylist(@PathParam("playlistId") long playlistId, Playlist playlist); @POST @Path("/playlists/{playlistId}/files") @Consumes(MediaType.APPLICATION_JSON) Playlist addFile(@PathParam("playlistId") long playlistId, File file); @PUT @Path("/playlists/{playlistId}/pagereferences/{pageRefId}") @Produces(MediaType.APPLICATION_JSON) PageReference updatePageReference(@PathParam("pageRefId") long pageRefId, PageReference pageRef); @POST @Path("/playlists/{playlistId}/copy") @Consumes(MediaType.APPLICATION_JSON) Playlist copyPlaylist(@PathParam("playlistId") long playlistId, Playlist newPlaylist); @DELETE @Path("/playlists/{playlistId}") Response deletePlaylist(@PathParam("playlistId") long playlistId); @DELETE @Path("/playlists/{playlistId}/pagereferences/{pageReferenceId}") @Produces(MediaType.APPLICATION_JSON) Playlist deletePage(@PathParam("playlistId") long playlistId,
			@PathParam("pageReferenceId") long pageReferenceId); @POST @Path("/playlists/{playlistId}/pagereferences/{pageReferenceId}/moveto/{idx}") @Produces(MediaType.APPLICATION_JSON) Playlist movePage(@PathParam("playlistId") long playlistId,
			@PathParam("pageReferenceId") long pageReferenceId, @PathParam("idx") int newPosition); @GET @Path("/playlists/{playlistId}/references") @Produces(MediaType.APPLICATION_JSON) PlaylistReferences getReferences(@PathParam("playlistId") long playlistId); @Override @POST @Path("/playlists/{playlistId}/owners") @Consumes(MediaType.APPLICATION_JSON) Playlist addOwners(@PathParam("playlistId") long playlistId, Owner[] ownerData); @Override @DELETE @Path("/playlists/{playlistId}/owners/{ownerId}") @Produces(MediaType.APPLICATION_JSON) Playlist deleteOwner(@PathParam("playlistId") long playlistId, @PathParam("ownerId") long ownerId);  }
@Test public void testGetReferences() throws Exception { Playlist playlist1 = new Playlist("d1"); playlist1 = playlistService.createPlaylist(playlist1); Playlist playlist2 = new Playlist("d2"); playlist2 = playlistService.createPlaylist(playlist2); Playlist playlist3 = new Playlist("d3"); playlist3 = playlistService.createPlaylist(playlist3); playlist3 = playlistService.addPlaylist(playlist3.getId(), playlist2); Playlist playlist4 = new Playlist("d4"); playlist4 = playlistService.createPlaylist(playlist4); playlist4 = playlistService.addPlaylist(playlist4.getId(), playlist2); Playlist playlist5 = new Playlist("d5"); playlist5 = playlistService.createPlaylist(playlist5); screenService.createScreen(new Screen("s1", playlist1)); screenService.createScreen(new Screen("s2", playlist2)); screenService.createScreen(new Screen("s3", playlist2)); Screen screen4 = screenService.createScreen(new Screen("s4", playlist1)); screenService.createScreen(new Screen("s5")); screen4.getOwners().clear(); screen4 = ScreenDAO.save(screen4); playlist4.getOwners().clear(); playlist4 = PlaylistDAO.save(playlist4); PlaylistReferences refs = playlistService.getReferences(playlist1.getId()); assertEquals(1, refs.getUserScreens().size()); assertEquals(1, refs.getForeignScreens()); assertEquals(0, refs.getUserPlaylists().size()); assertEquals(0, refs.getForeignPlaylists()); refs = playlistService.getReferences(playlist2.getId()); assertEquals(2, refs.getUserScreens().size()); assertEquals(0, refs.getForeignScreens()); assertEquals(1, refs.getUserPlaylists().size()); assertEquals(1, refs.getForeignPlaylists()); refs = playlistService.getReferences(playlist5.getId()); assertEquals(0, refs.getUserScreens().size()); assertEquals(0, refs.getForeignScreens()); assertEquals(0, refs.getUserPlaylists().size()); assertEquals(0, refs.getForeignPlaylists()); }
@Test(expected = WebApplicationException.class) public void testGetReferencesForNonExistingPlaylist() throws Exception { playlistService.getReferences(-1); }