/** * Try to create multiple entities in one transaction. If one fails all fails. * * @param entityName name of the entity where the entities are going to be added. * @param request EntityCollectionCreateRequestV2 * @param response HttpServletResponse * @return EntityCollectionCreateResponseBodyV2 * @throws Exception */ @RequestMapping(value = "/{entityName}", method = POST, produces = APPLICATION_JSON_VALUE) @ResponseBody public EntityCollectionBatchCreateResponseBodyV2 createEntities( @PathVariable("entityName") String entityName, @RequestBody @Valid EntityCollectionBatchRequestV2 request, HttpServletResponse response) throws Exception { final EntityMetaData meta = dataService.getEntityMetaData(entityName); if (meta == null) { throw createUnknownEntityException(entityName); } try { final List<Entity> entities = request .getEntities() .stream() .map(e -> this.restService.toEntity(meta, e)) .collect(Collectors.toList()); final EntityCollectionBatchCreateResponseBodyV2 responseBody = new EntityCollectionBatchCreateResponseBodyV2(); final List<String> ids = new ArrayList<String>(); // Add all entities this.dataService.add(entityName, entities.stream()); entities.forEach( entity -> { String id = entity.getIdValue().toString(); ids.add(id.toString()); responseBody .getResources() .add( new AutoValue_ResourcesResponseV2( Href.concatEntityHref(RestControllerV2.BASE_URI, entityName, id))); }); responseBody.setLocation( Href.concatEntityCollectionHref( RestControllerV2.BASE_URI, entityName, meta.getIdAttribute().getName(), ids)); response.setStatus(HttpServletResponse.SC_CREATED); return responseBody; } catch (Exception e) { response.setStatus(HttpServletResponse.SC_NO_CONTENT); throw e; } }