ScreenService extends BasicService implements OwnerAPISupport<Screen> { @POST @Path("/claim/{key}") @Produces(MediaType.APPLICATION_JSON) public Screen claimScreen(@PathParam("key") String key, @DefaultValue("0") @QueryParam("screenId") long screenId) { List<Screen> onboardings = ScreenDAO.getOnboardings(); Screen existingScreen = null; for (Screen screen : onboardings) { if (key.equalsIgnoreCase(screen.getShortKey())) { existingScreen = screen; break; } } if (existingScreen == null) { throwNotFound(); } if (!existingScreen.getOwners().isEmpty()) { throwBadRequest("Screen is already onboarded. Contact the administrators if you think this is incorrect."); } if (screenId > 0) { Screen reuseScreen = new ScreenDAO().getById(screenId); if (reuseScreen == null) { throwNotFound(); } if (!this.isAdmin() && !inAdminOwners(reuseScreen.getOwners())) { throwUnauthorized(); } reuseScreen.setKey(existingScreen.getKey()); new ScreenDAO().deleteById(existingScreen.getId()); return ScreenDAO.save(reuseScreen); } else { User user = userUtil.getLoggedInDBUser(request); Owner owner = new Owner(user); owner.setContact(true); existingScreen.addOwner(owner); existingScreen.setName(user.getFullName() + "'s Screen"); return ScreenDAO.save(existingScreen); } }  @GET @Path("/screens") @Produces(MediaType.APPLICATION_JSON) List<Screen> getScreens(@QueryParam("userId") String userId); @GET @Path("/onboardings") @Produces(MediaType.APPLICATION_JSON) List<Screen> getOnboardings(); @GET @Path("/screens/{id}") @Produces(MediaType.APPLICATION_JSON) Screen getScreen(@PathParam("id") long screenId,
			@DefaultValue("true") @QueryParam("includeMetrics") boolean includeMetrics,
			@DefaultValue("false") @QueryParam("live") boolean live); @GET @Path("/screens/bykey/{key}") @Produces(MediaType.APPLICATION_JSON) Screen getScreenByKey(@PathParam("key") String key,
			@DefaultValue("true") @QueryParam("includeMetrics") boolean includeMetrics,
			@DefaultValue("false") @QueryParam("live") boolean live,
			@DefaultValue("false") @QueryParam("autoOnboard") boolean doOnboarding,
			@QueryParam("owner") String requestedOwner); @POST @Path("/screens") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) Screen createScreen(Screen screen); @PUT @Path("/screens/{id}") @Produces(MediaType.APPLICATION_JSON) Screen updateScreen(@PathParam("id") long screenId, Screen screenData); @PUT @Path("/screens/{id}/statistics") @Produces(MediaType.APPLICATION_JSON) Screen updateScreenStatstics(@PathParam("id") long screenId, Screen screenData); @GET @Path("/screens/{id}/health") @Produces(MediaType.APPLICATION_JSON) Screen.AliveState getScreenHealth(@PathParam("id") long screenId); @GET @Path("/screens/{id}/events") @Produces(MediaType.APPLICATION_JSON) List<EventHistory> getScreenEvents(@PathParam("id") long screenId); @GET @Path("/screens/{id}/eventsperhour") @Produces(MediaType.APPLICATION_JSON) List<BasicNameValuePair> getScreenEventsPerHour(@PathParam("id") long screenId); @GET @Path("/screens/{id}/control") @Produces(MediaType.APPLICATION_JSON) Integer getRemoteKey(@PathParam("id") long screenId); @PUT @Path("/screens/{id}/control/{key}") @Produces(MediaType.APPLICATION_JSON) Screen remoteControlScreen(@PathParam("id") long screenId, Screen screenData, @PathParam("key") int key); @POST @Path("/screens/{id}/copy") @Consumes(MediaType.APPLICATION_JSON) Screen copyScreen(@PathParam("id") long screenId, Screen newScreen); @PUT @Path("/screens/{id}/requestedOwner") @Produces(MediaType.APPLICATION_JSON) Screen updateRequestedOwner(@PathParam("id") long screenId, Screen screenData); @POST @Path("/screens/{id}/onboard") @Produces(MediaType.APPLICATION_JSON) Screen onboardScreen(@PathParam("id") long screenId); @POST @Path("/claim/{key}") @Produces(MediaType.APPLICATION_JSON) Screen claimScreen(@PathParam("key") String key, @DefaultValue("0") @QueryParam("screenId") long screenId); @DELETE @Path("/screens/{screenId}") Response deleteScreen(@PathParam("screenId") long screenId); @Override @POST @Path("/screens/{screenId}/owners") @Consumes(MediaType.APPLICATION_JSON) Screen addOwners(@PathParam("screenId") long screenId, Owner[] ownerData); @Override @DELETE @Path("/screens/{screenId}/owners/{ownerId}") @Produces(MediaType.APPLICATION_JSON) Screen deleteOwner(@PathParam("screenId") long screenId, @PathParam("ownerId") long ownerId); @POST @Path("/screenkey/{key}/appliancestart") @Consumes(MediaType.APPLICATION_JSON) Response registerApplianceStart(@PathParam("key") String key, ApplianceInfo logContent);  }
@Test public void testClaimScreen() throws Exception { Screen screen = screenService.getScreenByKey("KEY1-and-a-lot-of-digits", false, false, true, null); screen = screenService.claimScreen("key1", 0); assertEquals(1, screen.getOwners().size()); assertEquals(simpleUser.getUserId(), screen.getOwners().get(0).getUser().getUserId()); assertTrue(screen.getName().length() > 0); }