@Test public void testMovePage() throws Exception { Page page1 = pageService.createPage(new Page("p1")); Page page2 = pageService.createPage(new Page("p2")); Page page3 = pageService.createPage(new Page("p3")); Playlist playlist = new Playlist("d1"); playlist.addPageReference(new PageReference(page1)); playlist.addPageReference(new PageReference(page2)); playlist.addPageReference(new PageReference(page3)); playlist = playlistService.createPlaylist(playlist); long id = playlist.getId(); assertEquals("p1", getPlaylist(id).getPageReferences().get(0).getPage().getName()); assertEquals("p2", getPlaylist(id).getPageReferences().get(1).getPage().getName()); playlistService.movePage(id, getPlaylist(id).getPageReferences().get(1).getId(), 0); assertEquals(3, getPlaylist(id).getPageReferences().size()); assertEquals("p2", getPlaylist(id).getPageReferences().get(0).getPage().getName()); assertEquals("p1", getPlaylist(id).getPageReferences().get(1).getPage().getName()); playlistService.movePage(id, getPlaylist(id).getPageReferences().get(0).getId(), 1); assertEquals(3, getPlaylist(id).getPageReferences().size()); assertEquals("p1", getPlaylist(id).getPageReferences().get(0).getPage().getName()); assertEquals("p2", getPlaylist(id).getPageReferences().get(1).getPage().getName()); playlistService.movePage(id, getPlaylist(id).getPageReferences().get(0).getId(), 0); assertEquals(3, getPlaylist(id).getPageReferences().size()); assertEquals("p1", getPlaylist(id).getPageReferences().get(0).getPage().getName()); assertEquals("p2", getPlaylist(id).getPageReferences().get(1).getPage().getName()); assertEquals("p3", getPlaylist(id).getPageReferences().get(2).getPage().getName()); Page page4 = pageService.createPage(new Page("p4")); Page page5 = pageService.createPage(new Page("p5")); playlistService.addPage(id, page4); playlistService.addPage(id, page5); assertEquals(5, getPlaylist(id).getPageReferences().size()); assertEquals("p1", getPlaylist(id).getPageReferences().get(0).getPage().getName()); assertEquals("p2", getPlaylist(id).getPageReferences().get(1).getPage().getName()); assertEquals("p3", getPlaylist(id).getPageReferences().get(2).getPage().getName()); assertEquals("p4", getPlaylist(id).getPageReferences().get(3).getPage().getName()); assertEquals("p5", getPlaylist(id).getPageReferences().get(4).getPage().getName()); playlistService.movePage(id, getPlaylist(id).getPageReferences().get(3).getId(), 2); assertEquals(5, getPlaylist(id).getPageReferences().size()); assertEquals("p1", getPlaylist(id).getPageReferences().get(0).getPage().getName()); assertEquals("p2", getPlaylist(id).getPageReferences().get(1).getPage().getName()); assertEquals("p4", getPlaylist(id).getPageReferences().get(2).getPage().getName()); assertEquals("p3", getPlaylist(id).getPageReferences().get(3).getPage().getName()); assertEquals("p5", getPlaylist(id).getPageReferences().get(4).getPage().getName()); }
@POST @Path("/playlists/{playlistId}/pagereferences/{pageReferenceId}/moveto/{idx}") @Produces(MediaType.APPLICATION_JSON) public Playlist movePage(@PathParam("playlistId") long playlistId, @PathParam("pageReferenceId") long pageReferenceId, @PathParam("idx") int newPosition) { Playlist existingPlaylist = getExistingPlaylist(playlistId, true); int newIdx = -1; int oldIdx = -1; int oldPosition = -1; List<PageReference> pageReferences = existingPlaylist.getPageReferences(); for (int i = 0; i < pageReferences.size(); i++) { if (pageReferences.get(i).getId() == pageReferenceId) { oldIdx = i; oldPosition = pageReferences.get(i).getPosition(); } if (pageReferences.get(i).getPosition() == newPosition) { newIdx = i; } } if (oldIdx == newIdx) { return existingPlaylist; } if (oldIdx == -1 || newIdx == -1) { throwNotFound(); } List<PageReference> newPageReferences = new ArrayList<>(); if (oldPosition > newPosition) { if (newIdx > 0) { newPageReferences.addAll(pageReferences.subList(0, newIdx)); } newPageReferences.add(pageReferences.get(oldIdx)); pageReferences.remove(oldIdx); newPageReferences.addAll(pageReferences.subList(newIdx, pageReferences.size())); } else { PageReference tmp = pageReferences.get(oldIdx); pageReferences.remove(oldIdx); if (newIdx > 0) { newPageReferences.addAll(pageReferences.subList(0, newIdx)); } newPageReferences.add(tmp); newPageReferences.addAll(pageReferences.subList(newIdx, pageReferences.size())); } for (int i = 0; i < newPageReferences.size(); i++) { newPageReferences.get(i).setPosition(i); } existingPlaylist.setPageReferences(newPageReferences); return PlaylistDAO.save(existingPlaylist); }
PlaylistService extends BasicService implements OwnerAPISupport<Playlist> { @POST @Path("/playlists/{playlistId}/pagereferences/{pageReferenceId}/moveto/{idx}") @Produces(MediaType.APPLICATION_JSON) public Playlist movePage(@PathParam("playlistId") long playlistId, @PathParam("pageReferenceId") long pageReferenceId, @PathParam("idx") int newPosition) { Playlist existingPlaylist = getExistingPlaylist(playlistId, true); int newIdx = -1; int oldIdx = -1; int oldPosition = -1; List<PageReference> pageReferences = existingPlaylist.getPageReferences(); for (int i = 0; i < pageReferences.size(); i++) { if (pageReferences.get(i).getId() == pageReferenceId) { oldIdx = i; oldPosition = pageReferences.get(i).getPosition(); } if (pageReferences.get(i).getPosition() == newPosition) { newIdx = i; } } if (oldIdx == newIdx) { return existingPlaylist; } if (oldIdx == -1 || newIdx == -1) { throwNotFound(); } List<PageReference> newPageReferences = new ArrayList<>(); if (oldPosition > newPosition) { if (newIdx > 0) { newPageReferences.addAll(pageReferences.subList(0, newIdx)); } newPageReferences.add(pageReferences.get(oldIdx)); pageReferences.remove(oldIdx); newPageReferences.addAll(pageReferences.subList(newIdx, pageReferences.size())); } else { PageReference tmp = pageReferences.get(oldIdx); pageReferences.remove(oldIdx); if (newIdx > 0) { newPageReferences.addAll(pageReferences.subList(0, newIdx)); } newPageReferences.add(tmp); newPageReferences.addAll(pageReferences.subList(newIdx, pageReferences.size())); } for (int i = 0; i < newPageReferences.size(); i++) { newPageReferences.get(i).setPosition(i); } existingPlaylist.setPageReferences(newPageReferences); return PlaylistDAO.save(existingPlaylist); } }
PlaylistService extends BasicService implements OwnerAPISupport<Playlist> { @POST @Path("/playlists/{playlistId}/pagereferences/{pageReferenceId}/moveto/{idx}") @Produces(MediaType.APPLICATION_JSON) public Playlist movePage(@PathParam("playlistId") long playlistId, @PathParam("pageReferenceId") long pageReferenceId, @PathParam("idx") int newPosition) { Playlist existingPlaylist = getExistingPlaylist(playlistId, true); int newIdx = -1; int oldIdx = -1; int oldPosition = -1; List<PageReference> pageReferences = existingPlaylist.getPageReferences(); for (int i = 0; i < pageReferences.size(); i++) { if (pageReferences.get(i).getId() == pageReferenceId) { oldIdx = i; oldPosition = pageReferences.get(i).getPosition(); } if (pageReferences.get(i).getPosition() == newPosition) { newIdx = i; } } if (oldIdx == newIdx) { return existingPlaylist; } if (oldIdx == -1 || newIdx == -1) { throwNotFound(); } List<PageReference> newPageReferences = new ArrayList<>(); if (oldPosition > newPosition) { if (newIdx > 0) { newPageReferences.addAll(pageReferences.subList(0, newIdx)); } newPageReferences.add(pageReferences.get(oldIdx)); pageReferences.remove(oldIdx); newPageReferences.addAll(pageReferences.subList(newIdx, pageReferences.size())); } else { PageReference tmp = pageReferences.get(oldIdx); pageReferences.remove(oldIdx); if (newIdx > 0) { newPageReferences.addAll(pageReferences.subList(0, newIdx)); } newPageReferences.add(tmp); newPageReferences.addAll(pageReferences.subList(newIdx, pageReferences.size())); } for (int i = 0; i < newPageReferences.size(); i++) { newPageReferences.get(i).setPosition(i); } existingPlaylist.setPageReferences(newPageReferences); return PlaylistDAO.save(existingPlaylist); }  }
PlaylistService extends BasicService implements OwnerAPISupport<Playlist> { @POST @Path("/playlists/{playlistId}/pagereferences/{pageReferenceId}/moveto/{idx}") @Produces(MediaType.APPLICATION_JSON) public Playlist movePage(@PathParam("playlistId") long playlistId, @PathParam("pageReferenceId") long pageReferenceId, @PathParam("idx") int newPosition) { Playlist existingPlaylist = getExistingPlaylist(playlistId, true); int newIdx = -1; int oldIdx = -1; int oldPosition = -1; List<PageReference> pageReferences = existingPlaylist.getPageReferences(); for (int i = 0; i < pageReferences.size(); i++) { if (pageReferences.get(i).getId() == pageReferenceId) { oldIdx = i; oldPosition = pageReferences.get(i).getPosition(); } if (pageReferences.get(i).getPosition() == newPosition) { newIdx = i; } } if (oldIdx == newIdx) { return existingPlaylist; } if (oldIdx == -1 || newIdx == -1) { throwNotFound(); } List<PageReference> newPageReferences = new ArrayList<>(); if (oldPosition > newPosition) { if (newIdx > 0) { newPageReferences.addAll(pageReferences.subList(0, newIdx)); } newPageReferences.add(pageReferences.get(oldIdx)); pageReferences.remove(oldIdx); newPageReferences.addAll(pageReferences.subList(newIdx, pageReferences.size())); } else { PageReference tmp = pageReferences.get(oldIdx); pageReferences.remove(oldIdx); if (newIdx > 0) { newPageReferences.addAll(pageReferences.subList(0, newIdx)); } newPageReferences.add(tmp); newPageReferences.addAll(pageReferences.subList(newIdx, pageReferences.size())); } for (int i = 0; i < newPageReferences.size(); i++) { newPageReferences.get(i).setPosition(i); } existingPlaylist.setPageReferences(newPageReferences); return PlaylistDAO.save(existingPlaylist); }  @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); }
PlaylistService extends BasicService implements OwnerAPISupport<Playlist> { @POST @Path("/playlists/{playlistId}/pagereferences/{pageReferenceId}/moveto/{idx}") @Produces(MediaType.APPLICATION_JSON) public Playlist movePage(@PathParam("playlistId") long playlistId, @PathParam("pageReferenceId") long pageReferenceId, @PathParam("idx") int newPosition) { Playlist existingPlaylist = getExistingPlaylist(playlistId, true); int newIdx = -1; int oldIdx = -1; int oldPosition = -1; List<PageReference> pageReferences = existingPlaylist.getPageReferences(); for (int i = 0; i < pageReferences.size(); i++) { if (pageReferences.get(i).getId() == pageReferenceId) { oldIdx = i; oldPosition = pageReferences.get(i).getPosition(); } if (pageReferences.get(i).getPosition() == newPosition) { newIdx = i; } } if (oldIdx == newIdx) { return existingPlaylist; } if (oldIdx == -1 || newIdx == -1) { throwNotFound(); } List<PageReference> newPageReferences = new ArrayList<>(); if (oldPosition > newPosition) { if (newIdx > 0) { newPageReferences.addAll(pageReferences.subList(0, newIdx)); } newPageReferences.add(pageReferences.get(oldIdx)); pageReferences.remove(oldIdx); newPageReferences.addAll(pageReferences.subList(newIdx, pageReferences.size())); } else { PageReference tmp = pageReferences.get(oldIdx); pageReferences.remove(oldIdx); if (newIdx > 0) { newPageReferences.addAll(pageReferences.subList(0, newIdx)); } newPageReferences.add(tmp); newPageReferences.addAll(pageReferences.subList(newIdx, pageReferences.size())); } for (int i = 0; i < newPageReferences.size(); i++) { newPageReferences.get(i).setPosition(i); } existingPlaylist.setPageReferences(newPageReferences); return PlaylistDAO.save(existingPlaylist); }  @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);  }