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:org.firebrandocm.dao.impl.hector.HectorPersistenceFactory.java
/** * Private helper that hidrates an entity from a list of columns in the datastore * * @param key the key/* w ww . j a v a2 s .c o m*/ * @param metadata the entity metadata * @param entityClass the entity class * @param columns the list of columns to set in the entity proeprties * @param instance the entity instance * @param ignoreLazyFlags whether lazy flags in @Column annotations should be ignored for this operation * @param <T> the entity type * @return the hidrated entity */ private <T> T serializeColumns(String key, ClassMetadata<?> metadata, Class<T> entityClass, List<HColumn<String, Object>> columns, T instance, boolean ignoreLazyFlags) throws Exception { instance = getInstance(entityClass, instance); for (HColumn<String, Object> column : columns) { String name = column.getName(); serializeColumn(metadata, instance, name, column, ignoreLazyFlags); } PropertyUtils.setProperty(instance, metadata.getKeyProperty(), key); return instance; }
From source file:org.firebrandocm.dao.impl.hector.HectorPersistenceFactory.java
/** * Private helper that serializes a column to an entity property * * @param metadata the class metadata * @param instance the entity instance * @param name the property name * @param column the column object * @param ignoreLazyFlags whether lazy flags in @Column annotations should be ignored for this operation *///w w w. j ava2 s .c om protected void serializeColumn(ClassMetadata<?> metadata, Object instance, String name, HColumn<String, Object> column, boolean ignoreLazyFlags) throws Exception { if (!name.equals(CLASS_PROPERTY) && metadata.getSelectionProperties().contains(name)) { //ignore the class type property while deserializing if (ignoreLazyFlags || !metadata.isLazyProperty(name)) { if ("KEY".equals(name)) { name = metadata.getKeyProperty(); } Object value = loadProperty(metadata, name, column); try { instantiateContainersIfNecessary(metadata, instance, name); PropertyUtils.setProperty(instance, name, value); } catch (Throwable e) { throw new UnsupportedOperationException(e); } } } }
From source file:org.firebrandocm.tests.PersistenceOperationTest.java
private void testIndexedPropertyEQ(Class<?> entityClass, Map<String, Object> params) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException { Object entity = factory.getInstance(entityClass); List<Predicate> predicates = new ArrayList<Predicate>(); for (Map.Entry<String, Object> entry : params.entrySet()) { String property = entry.getKey(); Object value = entry.getValue(); PropertyUtils.setProperty(entity, property, value); predicates.add(eq(property, value)); }//from w w w .j a v a 2s . c o m factory.persist(entity); Object loadedEntity = factory.getSingleResult(entityClass, Query.get(select(allColumns(), from(entityClass), where(predicates.toArray(new Predicate[predicates.size()]))))); assertEquals(entity, loadedEntity); }
From source file:org.firebrandocm.tests.PersistenceOperationTest.java
private void testIndexedPropertyRanges(Class<?> entityClass, String property, Object value, Object lesser, Object upper) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException { Object entity = factory.getInstance(entityClass); PropertyUtils.setProperty(entity, property, value); factory.persist(entity);/*from w w w . j a v a 2 s . com*/ Object loadedEntity = factory.getSingleResult(entityClass, Query.get( select(allColumns(), from(entityClass), where(type(FirstEntity.class), gt(property, lesser))))); assertEquals(entity, loadedEntity); loadedEntity = factory.getSingleResult(entityClass, Query.get( select(allColumns(), from(entityClass), where(type(FirstEntity.class), gte(property, lesser))))); assertEquals(entity, loadedEntity); loadedEntity = factory.getSingleResult(entityClass, Query.get( select(allColumns(), from(entityClass), where(type(FirstEntity.class), gte(property, value))))); assertEquals(entity, loadedEntity); loadedEntity = factory.getSingleResult(entityClass, Query .get(select(allColumns(), from(entityClass), where(type(FirstEntity.class), lt(property, upper))))); assertEquals(entity, loadedEntity); loadedEntity = factory.getSingleResult(entityClass, Query.get( select(allColumns(), from(entityClass), where(type(FirstEntity.class), lte(property, upper))))); assertEquals(entity, loadedEntity); loadedEntity = factory.getSingleResult(entityClass, Query.get( select(allColumns(), from(entityClass), where(type(FirstEntity.class), lte(property, value))))); assertEquals(entity, loadedEntity); loadedEntity = factory.getSingleResult(entityClass, Query.get(select(allColumns(), from(entityClass), where(type(FirstEntity.class), between(property, lesser, upper))))); assertEquals(entity, loadedEntity); }
From source file:org.gbif.portal.web.controller.registration.RegistrationController.java
/** * Refreshes the details for the supplied dataset using the latest available for its metadata * request./*from w w w . ja v a 2 s . com*/ * * @return */ public ModelAndView refreshMetadataDetails(HttpServletRequest request, HttpServletResponse response) throws Exception { // get the provider from UDDI if necessary ProviderDetail provider = retrieveProviderFromUDDI(request); if (provider == null) { return new ModelAndView("registrationBadBusinessKey"); } // retrieve resource details ResourceDetail resource = retrieveDataResource(request, provider); // retrieve resources from access point List<ResourceDetail> resources = null; try { resources = contactProviderForMetadata(resource.getAccessPoint()); } catch (Exception e) { logger.error(e.getMessage(), e); ModelAndView mav = new ModelAndView("refreshMetadataFailure"); mav.addObject("resource", resource); return mav; } ResourceDetail metadataResource = null; if (resources.size() == 1) { logger.debug("Only one resource found at end point. Assuming same resource."); metadataResource = resources.get(0); } else { // use name to retrieve the properties for (ResourceDetail retrievedResource : resources) { if (resource.getName().equals(retrievedResource.getName())) { metadataResource = retrievedResource; break; } } } // store the updated properties List<String> updatedProperties = new ArrayList<String>(); // find the list of metadata supplied properties List<String> readonlyProperties = null; boolean refreshFailure = false; // if not null, update properties available from metadata if (metadataResource != null) { readonlyProperties = retrieveReadonlyPropertiesForResource(metadataResource); for (String readonlyProperty : readonlyProperties) { Object newValue = PropertyUtils.getProperty(metadataResource, readonlyProperty); Object oldValue = PropertyUtils.getProperty(resource, readonlyProperty); if (newValue != null && !newValue.equals(oldValue)) { PropertyUtils.setProperty(resource, readonlyProperty, newValue); updatedProperties.add(readonlyProperty); } } } else { refreshFailure = true; } // uddiUtils.updateResource(resource, provider.getBusinessKey()); ModelAndView mav = new ModelAndView("registrationResourceDetail"); mav.addAllObjects(referenceDataForResource(request, resource)); mav.addObject(RegistrationController.REQUEST_PROVIDER_DETAIL, provider); mav.addObject(RegistrationController.REQUEST_RESOURCE, resource); mav.addObject("readonlyProperties", readonlyProperties); mav.addObject("updatedProperties", updatedProperties); mav.addObject("refreshFailure", refreshFailure); mav.addObject("metadataRefresh", true); return mav; }
From source file:org.hephaestus.fixedformat.impl.BeanPopulator.java
public final void populateValue(Object objectToPopulate, String propertyName, Object value) { if (objectToPopulate == null) { throw new IllegalArgumentException("Object to populate must be non-null"); }/* w w w . ja v a 2 s. c o m*/ if (TextUtils.isBlank(propertyName)) { throw new IllegalArgumentException("Property name must be non-null and not empty"); } try { PropertyUtils.setProperty(objectToPopulate, propertyName, value); } catch (Exception e) { throw new RuntimeException("Error setting property value", e); } }
From source file:org.initialize4j.util.InitializationUtil.java
/** * Sets a field value for the given source object and provided {@link Field} * instance.//ww w . j a v a 2 s .c o m * * @param src The object to to use. * @param fieldName The field name to set. * @param value The value to set for field. * @throws InitializeException If an error occurs. */ public static void setProperty(Object src, String fieldName, Object value) { try { PropertyUtils.setProperty(src, fieldName, value); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.intermine.task.DynamicAttributeTask.java
/** * Look at set methods on a target object and lookup values in project * properties. If no value found property will not be set but no error * will be thrown./*from w w w . jav a 2s. c o m*/ * @param bean an object to search for setter methods */ protected void configureDynamicAttributes(Object bean) { Project antProject = getProject(); Hashtable<?, ?> projectProps = antProject.getProperties(); PropertyDescriptor[] props = PropertyUtils.getPropertyDescriptors(bean); for (int i = 0; i < props.length; i++) { PropertyDescriptor desc = props[i]; Method setter = desc.getWriteMethod(); if (setter != null) { Class<?> propType = desc.getPropertyType(); String propName = setter.getName().substring(3).toLowerCase(); Object propValue = projectProps.get(propName); if (propValue == null) { // there is not all-lowercase property in projectProps, so try the camelCase // version String setterName = setter.getName(); String camelCasePropName = setterName.substring(3, 4).toLowerCase() + setterName.substring(4); propName = camelCasePropName; propValue = projectProps.get(camelCasePropName); } if (propValue == null) { // still not found, try replacing each capital (after first) in camelCase // to be a dot - i.e. setSrcDataDir -> src.data.dir String setterName = setter.getName(); String camelCasePropName = setterName.substring(3, 4).toLowerCase() + setterName.substring(4); String dotName = ""; for (int j = 0; j < camelCasePropName.length(); j++) { if (Character.isUpperCase(camelCasePropName.charAt(j))) { dotName += "." + camelCasePropName.substring(j, j + 1).toLowerCase(); } else { dotName += camelCasePropName.substring(j, j + 1); } } propValue = projectProps.get(dotName); } if (propValue != null) { try { if (propType.equals(File.class)) { String filePropValue = (String) propValue; //First check to see if we were given a complete file path, if so then // we can use it directly instead of trying to search for it. File maybeFile = new File(filePropValue); if (maybeFile.exists()) { propValue = maybeFile; LOG.info("Configuring task to use file:" + filePropValue); } else { propValue = getProject().resolveFile(filePropValue); } } PropertyUtils.setProperty(bean, propName, propValue); } catch (Exception e) { throw new BuildException( "failed to set value for " + propName + " to " + propValue + " in " + bean, e); } } } } }
From source file:org.isatools.isatab.mapping.attributes.FreeTextSimpleMappingHelper.java
/** * By default attempts to use set<propertyName>() *//*from w ww . j a v a 2s . co m*/ @Override public void setProperty(T mappedObject, PT propertyValue) { if (propertyValue == null) { return; } try { PropertyUtils.setProperty(mappedObject, getPropertyName(), propertyValue); } catch (Exception ex) { throw new TabInternalErrorException( String.format("Problem while mapping property '%s' from class '%s': %s", getPropertyName(), mappedObject.getClass().getSimpleName(), ex.getMessage()), ex); } log.trace("Property " + getFieldName() + "/'" + propertyValue + "' mapped to object " + mappedObject); }
From source file:org.isatools.tablib.mapping.properties.DatePropertyMappingHelper.java
/** * Maps a value in the TAB into the target property of this helper, fills the object with the * mapped value.// ww w. j a v a 2 s. c o m * <p/> * This version assumes a standard setter exists, i.e.: * mappedObject.set<{@link #getPropertyName()}>. * It does not map anything if propertyValue is null. */ public void setProperty(T mappedObject, Date propertyValue) { if (propertyValue == null) { return; } try { PropertyUtils.setProperty(mappedObject, getPropertyName(), propertyValue); } catch (Exception ex) { throw new TabInternalErrorException( String.format("Problem while mapping property '%s' from class '%s': %s", getPropertyName(), mappedObject.getClass().getSimpleName(), ex.getMessage()), ex); } log.trace("Property " + getFieldName() + "/'" + propertyValue + "' mapped to object " + mappedObject); }