Search in sources :

Example 21 with DataBinder

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

the class MoneyFormattingTests method testAmountWithNumberFormat5.

@Test
public void testAmountWithNumberFormat5() {
    FormattedMoneyHolder5 bean = new FormattedMoneyHolder5();
    DataBinder binder = new DataBinder(bean);
    binder.setConversionService(conversionService);
    MutablePropertyValues propertyValues = new MutablePropertyValues();
    propertyValues.add("amount", "USD 10.50");
    binder.bind(propertyValues);
    assertEquals(0, binder.getBindingResult().getErrorCount());
    assertEquals("USD 010.500", binder.getBindingResult().getFieldValue("amount"));
    assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
    assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());
    LocaleContextHolder.setLocale(Locale.CANADA);
    binder.bind(propertyValues);
    LocaleContextHolder.setLocale(Locale.US);
    assertEquals(0, binder.getBindingResult().getErrorCount());
    assertEquals("USD 010.500", binder.getBindingResult().getFieldValue("amount"));
    assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
    assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());
}
Also used : MutablePropertyValues(org.springframework.beans.MutablePropertyValues) DataBinder(org.springframework.validation.DataBinder) Test(org.junit.Test)

Example 22 with DataBinder

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

the class NumberFormattingTests method setUp.

@Before
public void setUp() {
    DefaultConversionService.addDefaultConverters(conversionService);
    conversionService.setEmbeddedValueResolver(new StringValueResolver() {

        @Override
        public String resolveStringValue(String strVal) {
            if ("${pattern}".equals(strVal)) {
                return "#,##.00";
            } else {
                return strVal;
            }
        }
    });
    conversionService.addFormatterForFieldType(Number.class, new NumberStyleFormatter());
    conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory());
    LocaleContextHolder.setLocale(Locale.US);
    binder = new DataBinder(new TestBean());
    binder.setConversionService(conversionService);
}
Also used : StringValueResolver(org.springframework.util.StringValueResolver) DataBinder(org.springframework.validation.DataBinder) Before(org.junit.Before)

Example 23 with DataBinder

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

the class MoneyFormattingTests method testAmountAndUnit.

@Test
public void testAmountAndUnit() {
    MoneyHolder bean = new MoneyHolder();
    DataBinder binder = new DataBinder(bean);
    binder.setConversionService(conversionService);
    MutablePropertyValues propertyValues = new MutablePropertyValues();
    propertyValues.add("amount", "USD 10.50");
    propertyValues.add("unit", "USD");
    binder.bind(propertyValues);
    assertEquals(0, binder.getBindingResult().getErrorCount());
    assertEquals("USD10.50", binder.getBindingResult().getFieldValue("amount"));
    assertEquals("USD", binder.getBindingResult().getFieldValue("unit"));
    assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
    assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());
    LocaleContextHolder.setLocale(Locale.CANADA);
    binder.bind(propertyValues);
    LocaleContextHolder.setLocale(Locale.US);
    assertEquals(0, binder.getBindingResult().getErrorCount());
    assertEquals("USD10.50", binder.getBindingResult().getFieldValue("amount"));
    assertEquals("USD", binder.getBindingResult().getFieldValue("unit"));
    assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d);
    assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());
}
Also used : MutablePropertyValues(org.springframework.beans.MutablePropertyValues) DataBinder(org.springframework.validation.DataBinder) Test(org.junit.Test)

Example 24 with DataBinder

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

the class DateTimeFormattingTests method setUp.

private void setUp(DateTimeFormatterRegistrar registrar) {
    conversionService = new FormattingConversionService();
    DefaultConversionService.addDefaultConverters(conversionService);
    registrar.registerFormatters(conversionService);
    DateTimeBean bean = new DateTimeBean();
    bean.getChildren().add(new DateTimeBean());
    binder = new DataBinder(bean);
    binder.setConversionService(conversionService);
    LocaleContextHolder.setLocale(Locale.US);
    DateTimeContext context = new DateTimeContext();
    context.setTimeZone(ZoneId.of("-05:00"));
    DateTimeContextHolder.setDateTimeContext(context);
}
Also used : DataBinder(org.springframework.validation.DataBinder) FormattingConversionService(org.springframework.format.support.FormattingConversionService)

Example 25 with DataBinder

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

the class CreateXformCRFVersionServlet method processRequest.

// public final String FM_BASEURL = "http://oc.local:8090/api/protocol/";
@Override
protected void processRequest() throws Exception {
    CrfDao crfDao = (CrfDao) SpringServletAccess.getApplicationContext(context).getBean("crfDao");
    XformMetaDataService xformService = (XformMetaDataService) SpringServletAccess.getApplicationContext(context).getBean("xformService");
    Locale locale = LocaleResolver.getLocale(request);
    ResourceBundleProvider.updateLocale(locale);
    resword = ResourceBundleProvider.getWordsBundle(locale);
    // Retrieve submission data from multipart request
    DiskFileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    List<FileItem> items = upload.parseRequest(request);
    String crfName = retrieveFormFieldValue(items, "crfName");
    DataBinder dataBinder = new DataBinder(new FormVersion());
    Errors errors = dataBinder.getBindingResult();
    int crfId = Integer.valueOf(retrieveFormFieldValue(items, "crfId"));
    if (crfId != 0) {
        CrfBean crfBean = crfDao.findByCrfId(crfId);
        crfName = crfBean.getName();
    }
    FormArtifactTransferObj transferObj = getFormArtifactsFromFM(items, currentStudy.getOid(), crfName);
    if (transferObj.getErr().size() != 0) {
        for (ErrorObj er : transferObj.getErr()) {
            errors.rejectValue("name", er.getCode(), er.getMessage());
        }
    } else {
        List<OCodmComplexTypeDefinitionFormLayoutDef> formLayoutDefs = new ArrayList<>();
        OCodmComplexTypeDefinitionFormLayoutDef formLayoutDef;
        for (FormVersion version : transferObj.getForm().getVersions()) {
            formLayoutDef = new OCodmComplexTypeDefinitionFormLayoutDef();
            formLayoutDef.setOID(version.getName());
            formLayoutDef.setURL(version.getArtifactURL());
            formLayoutDefs.add(formLayoutDef);
        }
        ExecuteIndividualCrfObject eicObj = new ExecuteIndividualCrfObject(transferObj.getForm(), formLayoutDefs, errors, currentStudy, ub, false, null);
        xformService.executeIndividualCrf(eicObj);
    }
    if (errors.hasErrors()) {
        request.setAttribute("errorList", errors.getAllErrors());
    }
    forwardPage(Page.CREATE_XFORM_CRF_VERSION_SERVLET);
}
Also used : Locale(java.util.Locale) CrfBean(org.akaza.openclinica.domain.datamap.CrfBean) ExecuteIndividualCrfObject(org.akaza.openclinica.service.crfdata.ExecuteIndividualCrfObject) OCodmComplexTypeDefinitionFormLayoutDef(org.openclinica.ns.odm_ext_v130.v31.OCodmComplexTypeDefinitionFormLayoutDef) ArrayList(java.util.ArrayList) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) CrfDao(org.akaza.openclinica.dao.hibernate.CrfDao) FileItem(org.apache.commons.fileupload.FileItem) Errors(org.springframework.validation.Errors) XformMetaDataService(org.akaza.openclinica.service.crfdata.XformMetaDataService) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FormArtifactTransferObj(org.akaza.openclinica.service.crfdata.FormArtifactTransferObj) DataBinder(org.springframework.validation.DataBinder) FormVersion(org.akaza.openclinica.service.dto.FormVersion) ErrorObj(org.akaza.openclinica.service.crfdata.ErrorObj)

Aggregations

DataBinder (org.springframework.validation.DataBinder)35 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