List of usage examples for org.apache.commons.beanutils PropertyUtils setProperty
public static void setProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
Set the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.
For more details see PropertyUtilsBean
.
From source file:com.mycollab.db.persistence.service.DefaultCrudService.java
@Override public Integer updateWithSession(T record, String username) { try {/*from ww w . j av a 2 s. c om*/ PropertyUtils.setProperty(record, "lastupdatedtime", new GregorianCalendar().getTime()); } catch (Exception e) { } if (cacheUpdateMethod == null) { findCacheUpdateMethod(); } try { cacheUpdateMethod.invoke(getCrudMapper(), record); return 1; } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new MyCollabException(e); } }
From source file:com.blackbear.flatworm.ParseUtils.java
/** * Invoke a setter for a the given {@code propertyName}. * * @param target The {@code Object} that contains the setter property to be invoked. * @param propertyName The name of the property. * @param toAdd The value to pass to the property. * @throws FlatwormParserException should invoking the setter method fail for any reason. *//*from w ww. ja v a 2s .co m*/ public static void setProperty(Object target, String propertyName, Object toAdd) throws FlatwormParserException { try { PropertyUtils.setProperty(target, propertyName, toAdd); } catch (Exception e) { throw new FlatwormParserException(e.getMessage(), e); } }
From source file:com.tonbeller.wcf.convert.SelectMultipleConverter.java
/** * @see com.tonbeller.wcf.convert.SelectConverterBase#updateModelReference(Formatter, Map, Element, Object) *//*from ww w. j ava 2 s. c o m*/ protected void updateModelReference(Formatter fmt, Element elem, Object bean) throws FormatException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { String model = SelectMultiple.getModelReference(elem); if (model.length() == 0) return; String type = SelectMultiple.getType(elem); String formatString = SelectMultiple.getFormatString(elem); FormatHandler parser = fmt.getHandler(type); if (parser == null) throw new FormatException("no handler found for type: " + type); List items = SelectMultiple.getSelectedItems(elem); checkRequired(fmt.getLocale(), elem, items.size() == 0); List values = new ArrayList(); for (Iterator it = items.iterator(); it.hasNext();) { Element item = (Element) it.next(); String valueString = Item.getValue(item); Object value = parser.parse(valueString, formatString); values.add(value); } PropertyUtils.setProperty(bean, model, parser.toNativeArray(values)); }
From source file:de.erdesignerng.dialect.ModelItemProperties.java
public void initializeFrom(T aObject) { ModelProperties theProperties = aObject.getProperties(); try {/*from ww w.j av a2s. co m*/ for (PropertyDescriptor theDescriptor : PropertyUtils.getPropertyDescriptors(this)) { if (theDescriptor.getReadMethod() != null && theDescriptor.getWriteMethod() != null) { String theValue = theProperties.getProperty(theDescriptor.getName()); if (!StringUtils.isEmpty(theValue)) { Class theType = theDescriptor.getPropertyType(); if (theType.isEnum()) { PropertyUtils.setProperty(this, theDescriptor.getName(), Enum.valueOf(theType, theValue)); } if (String.class.equals(theType)) { PropertyUtils.setProperty(this, theDescriptor.getName(), theValue); } if (Long.class.equals(theType) || long.class.equals(theType)) { PropertyUtils.setProperty(this, theDescriptor.getName(), Long.parseLong(theValue)); } if (Integer.class.equals(theType) || int.class.equals(theType)) { PropertyUtils.setProperty(this, theDescriptor.getName(), Integer.parseInt(theValue)); } if (Boolean.class.equals(theType) || boolean.class.equals(theType)) { PropertyUtils.setProperty(this, theDescriptor.getName(), Boolean.parseBoolean(theValue)); } } } } } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.esofthead.mycollab.core.persistence.service.DefaultCrudService.java
@Override public int updateWithSession(T record, String username) { try {//ww w . ja v a 2 s.com PropertyUtils.setProperty(record, "lastupdatedtime", new GregorianCalendar().getTime()); } catch (Exception e) { } if (cacheUpdateMethod == null) { findCacheUpdateMethod(); } try { cacheUpdateMethod.invoke(getCrudMapper(), record); return 1; } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new MyCollabException(e); } }
From source file:com.afeng.common.dao.orm.HibernateWebUtils.java
/** * ?ID?,???.//from w ww. j a v a2 s. c om * <p/> * ??????id,??????id?????. * ???id??,??id??. * ?ID, ??cascade-save-or-update. * * @param srcObjects ??,. * @param checkedIds ?,ID. * @param clazz ? * @param idName ?? */ public static <T, ID> void mergeByCheckedIds(final Collection<T> srcObjects, final Collection<ID> checkedIds, final Class<T> clazz, final String idName) { //? Assert.notNull(srcObjects, "scrObjects?"); Assert.hasText(idName, "idName?"); Assert.notNull(clazz, "clazz?"); //?,???. if (checkedIds == null) { srcObjects.clear(); return; } //????,id?ID?,. //?,???id,?id???id. Iterator<T> srcIterator = srcObjects.iterator(); try { while (srcIterator.hasNext()) { T element = srcIterator.next(); Object id; id = PropertyUtils.getProperty(element, idName); if (!checkedIds.contains(id)) { srcIterator.remove(); } else { checkedIds.remove(id); } } //ID??id????,,id??. for (ID id : checkedIds) { T obj = clazz.newInstance(); PropertyUtils.setProperty(obj, idName, id); srcObjects.add(obj); } } catch (Exception e) { throw ReflectionUtils.convertReflectionExceptionToUnchecked(e); } }
From source file:com.esofthead.mycollab.module.crm.ui.components.RelatedItemSelectionWindow.java
public RelatedItemSelectionWindow(String title, RelatedListComp2 relatedList) { super(title); center();// w w w . j a v a 2s. c o m bodyContent = new MVerticalLayout(); this.setContent(bodyContent); this.relatedListComp = relatedList; this.setModal(true); this.setResizable(false); initUI(); tableItem.addTableListener(new TableClickListener() { private static final long serialVersionUID = 1L; @SuppressWarnings("unchecked") @Override public void itemClick(TableClickEvent event) { try { Object rowItem = event.getData(); Boolean selectedVal = (Boolean) PropertyUtils.getProperty(rowItem, selectedFieldName); if (selectedVal) { selectedItems.remove(rowItem); PropertyUtils.setProperty(rowItem, selectedFieldName, false); } else { selectedItems.add(rowItem); PropertyUtils.setProperty(rowItem, selectedFieldName, true); } } catch (Exception ex) { throw new MyCollabException(ex); } } }); }
From source file:com.krawler.br.utils.Helper.java
public void assign(Object container, String key, Object val) throws ProcessException { if (container == null) { throw new ProcessException("container not found for key: " + key); }//from w w w.ja v a 2 s . c om if (container instanceof java.util.Map) { ((java.util.Map) container).put(key, val); } else if (container instanceof JSONObject) { try { ((JSONObject) container).put(key, val); } catch (JSONException ex) { throw new ProcessException(ex); } } else { try { PropertyUtils.setProperty(container, key, val); } catch (Exception ex) { throw new ProcessException("property not writable: " + key, ex); } } }
From source file:com.tonbeller.wcf.convert.EditCtrlConverter.java
/** * updates a bean property as well as the value attribute of element *///from w w w .j a v a2 s. co m public void convert(Formatter formatter, Map param, Map fileSource, Element element, Object bean) throws FormatException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { // disabled = true? return if (TypedCtrl.isDisabled(element)) return; String id = EditCtrl.getId(element); String[] inputValue = (String[]) param.get(id); // input available if (inputValue != null && inputValue.length > 0) { XoplonNS.removeAttribute(element, "error"); // parse input String formatString = EditCtrl.getFormatString(element); try { checkRequired(formatter.getLocale(), element, inputValue[0].trim().length() == 0); String type = EditCtrl.getType(element); FormatHandler handler = formatter.getHandler(type); if (handler == null) throw new FormatException("no handler found for type: " + type); Object newValue = handler.parse(inputValue[0], formatString); String strValue = handler.format(newValue, formatString); EditCtrl.setValue(element, strValue); String model = EditCtrl.getModelReference(element); if (bean != null && model.length() > 0) { PropertyUtils.setProperty(bean, model, newValue); } } catch (IllegalAccessException e) { logger.info("exception caught", e); XoplonNS.setAttribute(element, "error", e.getMessage()); XoplonNS.setAttribute(element, "value", inputValue[0]); throw e; } catch (NoSuchMethodException e) { logger.info("exception caught", e); XoplonNS.setAttribute(element, "error", e.getMessage()); XoplonNS.setAttribute(element, "value", inputValue[0]); throw e; } catch (InvocationTargetException e) { logger.info("exception caught", e); XoplonNS.setAttribute(element, "error", e.getMessage()); XoplonNS.setAttribute(element, "value", inputValue[0]); throw e; } catch (FormatException e) { logger.info("invalid user input: " + e.getMessage()); XoplonNS.setAttribute(element, "error", e.getMessage()); XoplonNS.setAttribute(element, "value", inputValue[0]); throw e; } } }
From source file:net.mojodna.sprout.support.SproutUtils.java
/** * Bean initialization method. Uses reflection to determine properties * for which auto-wiring may be appropriate. Subsequently attempts to * retrieve appropriate beans from the WebApplicationContext and set them * locally.// w ww . j a va2 s.co m * * @param bean Bean to initialize. * @param context WebApplicationContext containing Spring beans. * @param clazz Type of Sprout. This is used to determine which declared * methods are candidates for auto-wiring. */ public static void initialize(final Object bean, final WebApplicationContext context, final Class clazz) { final Collection<Method> methods = SproutUtils.getDeclaredMethods(bean.getClass(), clazz); final PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(bean.getClass()); for (final PropertyDescriptor descriptor : descriptors) { final Class type = descriptor.getPropertyType(); // beans should never be of type String // there must be a write method present // the write method must exist within the relevant subset of declared methods if (!type.equals(String.class) && null != descriptor.getWriteMethod() && methods.contains(descriptor.getWriteMethod())) { final Object serviceBean = context.getBean(descriptor.getName()); if (null != serviceBean) { try { log.debug("Wiring property '" + descriptor.getName() + "' with bean of type " + serviceBean.getClass().getName()); PropertyUtils.setProperty(bean, descriptor.getName(), serviceBean); } catch (final IllegalAccessException e) { throw new RuntimeException(e); } catch (final InvocationTargetException e) { throw new RuntimeException(e); } catch (final NoSuchMethodException e) { throw new RuntimeException(e); } } } } /** * TODO additional lifecycle interface callbacks as defined in BeanFactory * should be implemented here * @see org.springframework.beans.factory.BeanFactory */ // InitializingBean callback if (bean instanceof InitializingBean) { try { ((InitializingBean) bean).afterPropertiesSet(); } catch (final Exception e) { log.warn("Exception while running afterPropertiesSet() on an InitializingBean: " + e.getMessage(), e); } } }