View Javadoc
1   package org.kuali.common.util.bind.impl;
2   
3   import java.util.Map;
4   import java.util.Properties;
5   
6   import org.kuali.common.util.PropertyUtils;
7   import org.kuali.common.util.bind.api.Binder;
8   import org.kuali.common.util.spring.binder.BytesFormatAnnotationFormatterFactory;
9   import org.kuali.common.util.spring.binder.TimeFormatAnnotationFormatterFactory;
10  import org.springframework.beans.MutablePropertyValues;
11  import org.springframework.format.support.DefaultFormattingConversionService;
12  import org.springframework.validation.BindingResult;
13  import org.springframework.validation.DataBinder;
14  
15  import com.google.common.collect.Maps;
16  
17  public class DefaultBinder implements Binder {
18  
19  	@Override
20  	public <T> BindingResult bind(T object) {
21  		Properties properties = PropertyUtils.getGlobalProperties();
22  		Map<String, String> map = Maps.newHashMap();
23  		for (String key : properties.stringPropertyNames()) {
24  			map.put(key, properties.getProperty(key));
25  		}
26  		DataBinder binder = new DataBinder(object);
27  		MutablePropertyValues pvs = new MutablePropertyValues(map);
28  		DefaultFormattingConversionService service = new DefaultFormattingConversionService();
29  		service.addFormatterForFieldAnnotation(new BytesFormatAnnotationFormatterFactory());
30  		service.addFormatterForFieldAnnotation(new TimeFormatAnnotationFormatterFactory());
31  		binder.setConversionService(service);
32  		binder.bind(pvs);
33  		return binder.getBindingResult();
34  	}
35  
36  }