List of usage examples for org.apache.commons.beanutils ConvertingWrapDynaBean get
public Object get(String name)
From source file:org.ejbca.ui.cli.FieldEditor.java
/** Lists, Gets or sets fields in a Bean. * //from w ww. j a v a 2s. c o m * @param listOnly if true, fields will be listed, and nothing more will happen. * @param getOnly if true (and listOnly is false), will get the value of a field and nothing else will happen * @param name the name of the Bean to be modified * @param field the field name to get or set * @param value the value to set, of we should set a new value * @param obj the Bean to list, get or set fields * @return true if we only listed or got a value, i.e. if nothing was modified, false is we set a value. * @throws FieldNotFoundException if field was not found. */ public boolean listGetOrSet(boolean listOnly, boolean getOnly, final String name, final String field, final String value, final Object obj) throws FieldNotFoundException { if (listOnly) { listSetMethods(obj); } else if (getOnly) { getBeanValue(field, obj); } else { Object val = value; logger.info("Modifying '" + name + "'..."); final ConvertingWrapDynaBean db = new ConvertingWrapDynaBean(obj); DynaProperty prop = db.getDynaClass().getDynaProperty(field); if (prop == null) { throw new FieldNotFoundException("Field '" + field + "' does not exist. Did you use correct case for every character of the field?"); } if (prop.getType().isInterface()) { logger.info("Converting value '" + value + "' to type '" + ArrayList.class + "', "); // If the value can be converted into an integer, we will use an ArrayList<Integer> // Our problem here is that the type of a collection (<Integer>, <String>) is only compile time, it can not be determined in runtime. List<Object> arr = new ArrayList<Object>(); if (StringUtils.isNumeric(value)) { logger.info("using Integer value."); arr.add(Integer.valueOf(value)); } else { // Make it into an array of String logger.info("using String value."); arr.add(value); } val = arr; } final Object gotValue = db.get(field); logger.info("Current value of " + field + " is '" + gotValue + "'."); db.set(field, val); } // return true of we only listed return listOnly || getOnly; }
From source file:org.openhie.openempi.util.TestConversions.java
public static void exploringBeanUtils() { Person person = new Person(); person.setAddress1("2930 Oak Shadow Drive"); person.setCity("Oak Hill"); PersonIdentifier id = new PersonIdentifier(); id.setIdentifier("1234"); IdentifierDomain domain = new IdentifierDomain(); domain.setIdentifierDomainName("testDomain"); domain.setNamespaceIdentifier("testDomain"); id.setIdentifierDomain(domain);/*ww w .ja v a2s . c o m*/ person.addPersonIdentifier(id); Nationality nationality = new Nationality(); nationality.setNationalityCd(100); person.setNationality(nationality); person.setDateOfBirth(new java.util.Date()); ConvertingWrapDynaBean bean = new ConvertingWrapDynaBean(person); System.out.println("Build a dyna bean using my person:"); System.out.println(bean.get("address1")); System.out.println(bean.get("dateOfBirth")); System.out.println("Changing some of the values."); bean.set("givenName", "Odysseas"); bean.set("familyName", "Pentakalos"); System.out.println(bean.get("nationality.nationalityCd")); bean.set("nationality.nationalityCd", "150"); System.out.println("Value " + bean.get("nationality.nationalityCd") + " is of type " + bean.get("nationality.nationalityCd").getClass()); person = (Person) bean.getInstance(); System.out.println(person); List<String> properties = ConvertUtil.extractProperties(person); for (String property : properties) { System.out.println("Property name is: " + property); } // DynaProperty[] properties = bean.getDynaClass().getDynaProperties(); // for (DynaProperty property : properties) { // System.out.println("The map has the property: " + property.getName() + " which is mapped " + property.getType()); // if (property.getType().getName().startsWith("org.openhie")) { // WrapDynaClass dynaClass = WrapDynaClass.createDynaClass(property.getType()); // DynaProperty[] internalProperties = dynaClass.getDynaProperties(); // for (DynaProperty internalProperty : internalProperties) { // System.out.println("The map has the property: " + property.getName() + "." + internalProperty.getName()); // } // } // } // BeanMap beanMap = new BeanMap(person); // Set<String> properties = beanMap.keySet(); // for (String key : properties) { // System.out.println("The map has the property: " + key); // } org.apache.commons.beanutils.converters.DateConverter converter = new org.apache.commons.beanutils.converters.DateConverter(); converter.setPattern("yyyy.MM.dd HH:mm:ss z"); String[] patterns = converter.getPatterns(); for (String pattern : patterns) { System.out.println("Pattern is " + pattern); } Date now = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss z"); System.out.println(sdf.format(now)); ConvertUtils.register(converter, java.util.Date.class); ConvertUtils convertUtils = new ConvertUtils(); System.out.println(convertUtils.convert("2009.03.06 15:13:29 EST", java.util.Date.class)); try { BeanUtils.setProperty(person, "dateOfBirth", "2009.03.06 15:13:29 EST"); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(bean.get("dateOfBirth")); System.out.println(bean.getDynaClass().getDynaProperty("dateOfBirth")); bean.set("dateOfBirth", "2009.03.06 15:13:29 EST"); System.out.println(bean.get("dateOfBirth")); }