Example usage for org.springframework.web.multipart MultipartFile isEmpty

List of usage examples for org.springframework.web.multipart MultipartFile isEmpty

Introduction

In this page you can find the example usage for org.springframework.web.multipart MultipartFile isEmpty.

Prototype

boolean isEmpty();

Source Link

Document

Return whether the uploaded file is empty, that is, either no file has been chosen in the multipart form or the chosen file has no content.

Usage

From source file:uk.ac.ebi.metabolights.controller.SubmissionQueueController.java

@RequestMapping(value = "/queueExperiment", method = RequestMethod.POST)
public ModelAndView queueExperiment(@RequestParam(required = true, value = "file") MultipartFile file,
        @RequestParam(required = true, value = "pickdate") String publicDate,
        @RequestParam(required = false, value = "study") String study,
        @RequestParam(required = false, value = "owner") String ownerId,
        @RequestParam(required = false, value = "validated", defaultValue = "false") boolean validated,
        HttpServletRequest request) throws Exception {

    //Start the submission process...
    logger.info("Queue Experiment. Start");

    StringBuffer messageBody = new StringBuffer();
    String hostName = java.net.InetAddress.getLocalHost().getHostName();
    messageBody.append("Study submission started from machine " + hostName);

    // Get the actual user (could be a curator).
    MetabolightsUser actualUser = (MetabolightsUser) (SecurityContextHolder.getContext().getAuthentication()
            .getPrincipal());//from  ww w  .java  2s .c o m

    // The submitter, by default is the same user
    MetabolightsUser submitter = actualUser;

    // If the user is a curator but there is an owner...
    if (ownerId != null && actualUser.isCurator()) {

        // Overwrite the submitter with the owner...
        submitter = userService.lookupByUserName(ownerId);
    }

    try {

        if (file.isEmpty())
            throw new BIIException(PropertyLookup.getMessage("BIISubmit.fileEmpty"));

        if (publicDate.isEmpty())
            throw new BIIException(PropertyLookup.getMessage("BIISubmit.dateEmpty"));

        if (!file.getOriginalFilename().toLowerCase().endsWith("zip"))
            throw new BIIException(PropertyLookup.getMessage("BIISubmit.fileExtension"));

        if (!validated)
            throw new BIIException(PropertyLookup.getMessage("BIISubmit.notValidated"));

        Date publicDateD;
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
        publicDateD = sdf.parse(publicDate); //Date from the form

        // Extend the message...
        messageBody.append("\nFileName: " + file.getOriginalFilename());

        if (submitter.getUserName().equals(actualUser.getUserName())) {
            messageBody.append("\nUser: " + actualUser.getUserName());
        } else {
            messageBody
                    .append("\nUser: " + actualUser.getUserName() + "on behalf of " + submitter.getUserName());
        }
        if (study == null) {
            messageBody.append("\nNEW STUDY");
        } else {
            messageBody.append("\nSTUDY: " + study);
        }
        messageBody.append("\nPublic Release Date: " + publicDate);

        logger.info("Queueing study");
        SubmissionItem si = new SubmissionItem(file, submitter.getApiToken(), publicDateD, study, false);

        // Submit the item to the queue...
        si.submitToQueue();

        messageBody.append("\n\n File Successfully queued.");

        logger.info("Queued study. Adding data to session");
        HttpSession httpSession = request.getSession();
        httpSession.setAttribute("itemQueued", "msg.studyQueueSuccesfully");

        // Cannot load the queue
        emailService.sendQueuedStudyEmail(submitter.getEmail(), si.getOriginalFileName(),
                FileUtils.byteCountToDisplaySize(si.getFileQueued().length()), si.getPublicReleaseDate(),
                hostName, study);

        return new ModelAndView("redirect:itemQueued");

    } catch (BIIException e) {

        ModelAndView mav = AppContext.getMAVFactory().getFrontierMav("submitError");
        logger.error("Submission exception", e);
        mav.addObject("error", e);
        mav.addObject("studyId", study);
        return mav;

    } catch (Exception e) {

        ModelAndView mav = AppContext.getMAVFactory().getFrontierMav("submitError");
        logger.error("Submission exception", e);
        mav.addObject("error", e);
        // Add the study id...
        mav.addObject("studyId", study);

        messageBody.append("\n\nERROR!!!!!\n\n" + e.getMessage());
        emailService.sendSimpleEmail(
                "queueExperiment FAILED in " + hostName + " by " + actualUser.getUserName(),
                messageBody.toString());

        return mav;

    }
}