Search in sources :

Example 1 with RequestParseResult

use of cern.modesti.request.upload.parser.RequestParseResult in project modesti by jlsalmon.

the class UploadService method parseRequestFromExcelSheet.

/**
 * Delegate the parsing of the uploaded Excel sheet to a specific plugin
 * implementation.
 *
 * @param description the description of the uploaded request
 * @param stream      the Excel sheet to be parsed
 * @return the result of the request parse
 */
public RequestParseResult parseRequestFromExcelSheet(String description, InputStream stream) {
    RequestParseResult result = requestParserFactory.parseRequest(stream);
    Request request = result.getRequest();
    if (request.getDescription() == null) {
        request.setDescription(description);
    }
    requestService.insert(request);
    return result;
}
Also used : RequestParseResult(cern.modesti.request.upload.parser.RequestParseResult) Request(cern.modesti.request.Request)

Example 2 with RequestParseResult

use of cern.modesti.request.upload.parser.RequestParseResult in project modesti by jlsalmon.

the class UploadController method handleFileUpload.

@RequestMapping(value = "/api/requests/upload", method = POST)
public ResponseEntity<?> handleFileUpload(@RequestParam("file") MultipartFile file, @RequestParam("description") String description, UriComponentsBuilder b) {
    RequestParseResult result;
    if (!file.isEmpty()) {
        try {
            result = service.parseRequestFromExcelSheet(description, file.getInputStream());
            log.info("successfully uploaded " + file.getOriginalFilename());
        } catch (Exception e) {
            log.info("failed to upload " + file.getOriginalFilename(), e);
            return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
        }
    } else {
        log.info("failed to upload " + file.getOriginalFilename() + " because the file was empty.");
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }
    // Add link to newly created request in Location header
    UriComponents uriComponents = b.path("/requests/{id}").buildAndExpand(result.getRequest().getRequestId());
    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(uriComponents.toUri());
    return new ResponseEntity<>(result.getWarnings(), headers, HttpStatus.CREATED);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) UriComponents(org.springframework.web.util.UriComponents) RequestParseResult(cern.modesti.request.upload.parser.RequestParseResult) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

RequestParseResult (cern.modesti.request.upload.parser.RequestParseResult)2 Request (cern.modesti.request.Request)1 HttpHeaders (org.springframework.http.HttpHeaders)1 ResponseEntity (org.springframework.http.ResponseEntity)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 UriComponents (org.springframework.web.util.UriComponents)1