/** * Performs an action on each Subject in the provided set. * * <p>Subjects are loaded, supplied to the consumer, and then allowed to be * uncached by the implementation.</p> * * <p>This should be used to apply bulk changes or gather data about all * Subjects in the collection. The provided consumer will be supplied * asynchronously. Acting upon a large collection may be particularly * resource intensive.</p> * * <p>Implementations may choose to load and process subjects in * parallel.</p> * * @param action The action to perform on each subject * @param identifiers a set of identifiers to apply the action to * @return A future which will complete when the operation has finished */ default CompletableFuture<Void> applyToAll(Consumer<Subject> action, Set<String> identifiers) { Preconditions.checkNotNull(action, "action"); Preconditions.checkNotNull(identifiers, "identifiers"); return CompletableFuture.runAsync(() -> { for (String id : identifiers) { Subject subject = loadSubject(id).join(); action.accept(subject); suggestUnload(subject.getIdentifier()); } }); }
@GET @Path("/collection/{id}/subject/{subId}") @Permission({ "collection", "subject", "one" }) @ApiOperation( value = "Get subject", notes = "Gets one specific subject belonging to a certain collection") public Subject getSubject(@PathParam("id") String id, @PathParam("subId") String subId) throws NotFoundException { PermissionService srv = getPermissionService(); try { if (!srv.hasCollection(id).get()) throw new NotFoundException("Collection with id " + id + " could not be found"); SubjectCollection coll = srv.loadCollection(id).get(); if (!coll.hasSubject(subId).get()) throw new NotFoundException("Subject with id " + id + " could not be found"); return coll.loadSubject(subId).get(); } catch (InterruptedException e) { throw new ClientErrorException(e.getMessage(), Response.Status.REQUEST_TIMEOUT); } catch (ExecutionException e) { throw new InternalServerErrorException(e.getMessage()); } }
@GET @Path("/collection/{id}/subject") @Permission({ "collection", "subject", "list" }) @ExplicitDetails @ApiOperation( value = "List subjects", notes = "List all subjects belonging to a certain collection") public Set<Subject> listSubjects(@PathParam("id") String id) throws NotFoundException { PermissionService srv = getPermissionService(); try { if (!srv.hasCollection(id).get()) throw new NotFoundException("Collection with id " + id + " could not be found"); Set<Subject> subjects = new HashSet<>(); SubjectCollection coll = srv.loadCollection(id).get(); for (String subId : coll.getAllIdentifiers().get()) { subjects.add(coll.loadSubject(subId).get()); } return subjects; } catch (InterruptedException e) { throw new ClientErrorException(e.getMessage(), Response.Status.REQUEST_TIMEOUT); } catch (ExecutionException e) { throw new InternalServerErrorException(e.getMessage()); } }