Search in sources :

Example 26 with DataBinder

use of org.springframework.validation.DataBinder in project spring-boot by spring-projects.

the class PropertySourcesPropertyValuesTests method testFirstCollectionPropertyWinsNestedAttributes.

@Test
public void testFirstCollectionPropertyWinsNestedAttributes() throws Exception {
    ListTestBean target = new ListTestBean();
    DataBinder binder = new DataBinder(target);
    Map<String, Object> first = new LinkedHashMap<>();
    first.put("list[0].description", "another description");
    Map<String, Object> second = new LinkedHashMap<>();
    second.put("list[0].name", "first name");
    second.put("list[0].description", "first description");
    second.put("list[1].name", "second name");
    second.put("list[1].description", "second description");
    this.propertySources.addFirst(new MapPropertySource("s", second));
    this.propertySources.addFirst(new MapPropertySource("f", first));
    binder.bind(new PropertySourcesPropertyValues(this.propertySources));
    assertThat(target.getList()).hasSize(1);
    assertThat(target.getList().get(0).getDescription()).isEqualTo("another description");
    assertThat(target.getList().get(0).getName()).isNull();
}
Also used : MapPropertySource(org.springframework.core.env.MapPropertySource) DataBinder(org.springframework.validation.DataBinder) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 27 with DataBinder

use of org.springframework.validation.DataBinder in project spring-boot by spring-projects.

the class PropertySourcesPropertyValuesTests method testPlaceholdersErrorInNonEnumerable.

@Test
public void testPlaceholdersErrorInNonEnumerable() {
    TestBean target = new TestBean();
    DataBinder binder = new DataBinder(target);
    this.propertySources.addFirst(new PropertySource<Object>("application", "STUFF") {

        @Override
        public Object getProperty(String name) {
            return new Object();
        }
    });
    binder.bind(new PropertySourcesPropertyValues(this.propertySources, (Collection<String>) null, Collections.singleton("name")));
    assertThat(target.getName()).isNull();
}
Also used : Collection(java.util.Collection) DataBinder(org.springframework.validation.DataBinder) Test(org.junit.Test)

Example 28 with DataBinder

use of org.springframework.validation.DataBinder in project spring-boot by spring-projects.

the class PropertySourcesPropertyValuesTests method testFirstCollectionPropertyWins.

@Test
public void testFirstCollectionPropertyWins() throws Exception {
    ListBean target = new ListBean();
    DataBinder binder = new DataBinder(target);
    Map<String, Object> first = new LinkedHashMap<>();
    first.put("list[0]", "f0");
    Map<String, Object> second = new LinkedHashMap<>();
    second.put("list[0]", "s0");
    second.put("list[1]", "s1");
    this.propertySources.addFirst(new MapPropertySource("s", second));
    this.propertySources.addFirst(new MapPropertySource("f", first));
    binder.bind(new PropertySourcesPropertyValues(this.propertySources));
    assertThat(target.getList()).containsExactly("f0");
}
Also used : MapPropertySource(org.springframework.core.env.MapPropertySource) DataBinder(org.springframework.validation.DataBinder) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 29 with DataBinder

use of org.springframework.validation.DataBinder in project spring-boot by spring-projects.

the class PropertySourcesPropertyValuesTests method testCollectionProperty.

@Test
public void testCollectionProperty() throws Exception {
    ListBean target = new ListBean();
    DataBinder binder = new DataBinder(target);
    Map<String, Object> map = new LinkedHashMap<>();
    map.put("list[0]", "v0");
    map.put("list[1]", "v1");
    this.propertySources.addFirst(new MapPropertySource("values", map));
    binder.bind(new PropertySourcesPropertyValues(this.propertySources));
    assertThat(target.getList()).containsExactly("v0", "v1");
}
Also used : MapPropertySource(org.springframework.core.env.MapPropertySource) DataBinder(org.springframework.validation.DataBinder) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 30 with DataBinder

use of org.springframework.validation.DataBinder in project OpenClinica by OpenClinica.

the class OpenRosaSubmissionController method doFieldDeletion.

/**
     * @api {post} /pages/api/v2/editform/:studyOid/fieldsubmission Submit form data
     * @apiName doSubmission
     * @apiPermission admin
     * @apiVersion 3.8.0
     * @apiParam {String} studyOid Study Oid.
     * @apiParam {String} ecid Key that will be used to look up subject context information while processing submission.
     * @apiGroup Form
     * @apiDescription Submits the data from a completed form.
     */
@RequestMapping(value = "/{studyOID}/fieldsubmission", method = RequestMethod.DELETE)
public ResponseEntity<String> doFieldDeletion(HttpServletRequest request, HttpServletResponse response, @PathVariable("studyOID") String studyOID, @RequestParam(FORM_CONTEXT) String ecid) {
    logger.info("Processing xform field deletion.");
    HashMap<String, String> subjectContext = null;
    Locale locale = LocaleResolver.getLocale(request);
    DataBinder dataBinder = new DataBinder(null);
    Errors errors = dataBinder.getBindingResult();
    Study study = studyDao.findByOcOID(studyOID);
    String requestBody = null;
    String instanceId = null;
    HashMap<String, String> map = new HashMap();
    ArrayList<HashMap> listOfUploadFilePaths = new ArrayList();
    try {
        // Verify Study is allowed to submit
        if (!mayProceed(study)) {
            logger.info("Field Deletions to the study not allowed.  Aborting field submission.");
            return new ResponseEntity<String>(HttpStatus.NOT_ACCEPTABLE);
        }
        String dir = getAttachedFilePath(studyOID);
        FileProperties fileProperties = new FileProperties();
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setFileSizeMax(fileProperties.getFileSizeMax());
        List<FileItem> items = upload.parseRequest(request);
        for (FileItem item : items) {
            if (item.getFieldName().equals("instance_id")) {
                instanceId = item.getString();
            } else if (item.getFieldName().equals("xml_submission_fragment_file")) {
                requestBody = item.getString("UTF-8");
            } else if (item.getContentType() != null) {
                if (!new File(dir).exists())
                    new File(dir).mkdirs();
                File file = processUploadedFile(item, dir);
                map.put(item.getFieldName(), file.getPath());
            }
        }
        listOfUploadFilePaths.add(map);
        if (instanceId == null) {
            logger.info("Field Submissions to the study not allowed without a valid instanceId.  Aborting field submission.");
            return new ResponseEntity<String>(HttpStatus.NOT_ACCEPTABLE);
        }
        // Load user context from ecid
        PFormCache cache = PFormCache.getInstance(context);
        subjectContext = cache.getSubjectContext(ecid);
        // Execute save as Hibernate transaction to avoid partial imports
        openRosaSubmissionService.processFieldSubmissionRequest(study, subjectContext, instanceId, requestBody, errors, locale, listOfUploadFilePaths, SubmissionContainer.FieldRequestTypeEnum.DELETE_FIELD);
    } catch (Exception e) {
        logger.error("Exception while processing xform submission.");
        logger.error(e.getMessage());
        logger.error(ExceptionUtils.getStackTrace(e));
        if (!errors.hasErrors()) {
            // Send a failure response
            logger.info("Submission caused internal error.  Sending error response.");
            return new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
    if (!errors.hasErrors()) {
        // JsonLog submission with Participate
        if (isParticipantSubmission(subjectContext))
            notifier.notify(studyOID, subjectContext);
        logger.info("Completed xform field submission. Sending successful response");
        String responseMessage = "<OpenRosaResponse xmlns=\"http://openrosa.org/http/response\">" + "<message>success</message>" + "</OpenRosaResponse>";
        return new ResponseEntity<String>(responseMessage, HttpStatus.CREATED);
    } else {
        logger.info("Field Submission contained errors. Sending error response");
        return new ResponseEntity<String>(HttpStatus.NOT_ACCEPTABLE);
    }
}
Also used : Locale(java.util.Locale) Study(org.akaza.openclinica.domain.datamap.Study) FileProperties(org.akaza.openclinica.bean.rule.FileProperties) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) OpenClinicaSystemException(org.akaza.openclinica.exception.OpenClinicaSystemException) Errors(org.springframework.validation.Errors) FileItem(org.apache.commons.fileupload.FileItem) ResponseEntity(org.springframework.http.ResponseEntity) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) DataBinder(org.springframework.validation.DataBinder) File(java.io.File) PFormCache(org.akaza.openclinica.web.pform.PFormCache) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

DataBinder (org.springframework.validation.DataBinder)36 Test (org.junit.Test)15 Errors (org.springframework.validation.Errors)14 Locale (java.util.Locale)8 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)7 ArrayList (java.util.ArrayList)6 DOMSource (javax.xml.transform.dom.DOMSource)6 OpenClinicaSystemException (org.akaza.openclinica.exception.OpenClinicaSystemException)5 Element (org.w3c.dom.Element)5 HashMap (java.util.HashMap)4 FileItem (org.apache.commons.fileupload.FileItem)4 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)4 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)4 MapPropertySource (org.springframework.core.env.MapPropertySource)4 PayloadRoot (org.springframework.ws.server.endpoint.annotation.PayloadRoot)4 File (java.io.File)3 LinkedHashMap (java.util.LinkedHashMap)3 FileProperties (org.akaza.openclinica.bean.rule.FileProperties)3 Study (org.akaza.openclinica.domain.datamap.Study)3 PFormCache (org.akaza.openclinica.web.pform.PFormCache)3