List of usage examples for org.apache.commons.beanutils PropertyUtils setSimpleProperty
public static void setSimpleProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
Set the value of the specified simple property of the specified bean, with no type conversions.
For more details see PropertyUtilsBean
.
From source file:org.nmrfx.processor.gui.spectra.DatasetAttributes.java
public void config(String name, Object value) { if (Platform.isFxApplicationThread()) { try {//from w w w.j ava 2 s .c o m PropertyUtils.setSimpleProperty(this, name, value); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { Logger.getLogger(DatasetAttributes.class.getName()).log(Level.SEVERE, null, ex); } } else { Platform.runLater(() -> { try { PropertyUtils.setProperty(this, name, value); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { Logger.getLogger(DatasetAttributes.class.getName()).log(Level.SEVERE, null, ex); } }); } }
From source file:org.nmrfx.processor.gui.spectra.PeakListAttributes.java
public void config(String name, Object value) { ConsoleUtil.runOnFxThread(() -> { try {/*from w w w.ja va 2s . c om*/ switch (name) { case "labelType": setLabelType(value.toString()); break; case "displayType": setDisplayType(value.toString()); break; case "simPeaks": setSimPeaks(Boolean.valueOf(value.toString())); break; case "drawPeaks": setDrawPeaks(Boolean.valueOf(value.toString())); break; default: PropertyUtils.setSimpleProperty(this, name, value); break; } } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { Logger.getLogger(PeakListAttributes.class.getName()).log(Level.SEVERE, null, ex); } }); }
From source file:org.nuclos.common.collect.collectable.AbstractCollectableBean.java
/** * Utility method to set a bean property. * @param sPropertyName// w ww .j av a 2s. c o m * @param oValue * @todo Replace with CollectableElisaValueObject.setPropertyValue implementation and move to BeanUtils. */ protected static void setProperty(Object oBean, String sPropertyName, Object oValue) throws NoSuchMethodException { try { PropertyUtils.setSimpleProperty(oBean, sPropertyName, oValue); /** @todo BeanUtils.setProperty(tBean, sPropertyName, oValue); */ } catch (IllegalAccessException ex) { throw new CommonFatalException(getErrorMessage(sPropertyName, oBean, true), ex); } catch (InvocationTargetException ex) { throw new CommonFatalException(getErrorMessage(sPropertyName, oBean, true), ex.getCause()); } catch (IllegalArgumentException ex) { throw new CommonFatalException(getErrorMessage(sPropertyName, oBean, true), ex); } }
From source file:org.oddjob.framework.WrapDynaBean.java
/** * Set the value of a simple property with the specified name. * * @param name Name of the property whose value is to be set * @param value Value to which this property is to be set * * @exception ConversionException if the specified value cannot be * converted to the type required for this property * @exception IllegalArgumentException if there is no property * of the specified name/*from w w w. j a v a 2s . c o m*/ * @exception NullPointerException if an attempt is made to set a * primitive property to null */ public void set(String name, Object value) { try { PropertyUtils.setSimpleProperty(instance, name, value); } catch (Throwable t) { throw new IllegalArgumentException("Property '" + name + "' has no write method"); } }
From source file:org.ow2.sirocco.cloudmanager.core.utils.QueryHelper.java
public static <E extends Identifiable> QueryResult<E> getEntityList(final EntityManager em, final QueryParamsBuilder params) throws InvalidRequestException { StringBuffer whereClauseSB = new StringBuffer(); if (params.getTenantId() != null) { if (!params.isReturnPublicEntities()) { whereClauseSB.append(" v.tenant.id=:tenantId "); } else {//from www.j a v a 2 s .c om whereClauseSB.append( "( v.tenant.id=:tenantId OR v.visibility = org.ow2.sirocco.cloudmanager.model.cimi.extension.Visibility.PUBLIC) "); } } if (params.getStateToIgnore() != null) { if (whereClauseSB.length() > 0) { whereClauseSB.append(" AND "); } whereClauseSB.append(" v.state<>" + params.getStateToIgnore().getClass().getName() + "." + params.getStateToIgnore().name() + " "); } if (params.isFilterEmbbededTemplate()) { if (whereClauseSB.length() > 0) { whereClauseSB.append(" AND "); } whereClauseSB.append(" v.isEmbeddedInSystemTemplate=false "); } if (params.getFilters() != null) { String filterClause; try { filterClause = QueryHelper.generateFilterClause(params.getFilters(), "v", params.getClazz().getName() + "$State."); } catch (ParseException ex) { throw new InvalidRequestException("Parsing error in filter expression " + ex.getMessage()); } catch (TokenMgrError ex) { throw new InvalidRequestException(ex.getMessage()); } if (!filterClause.isEmpty()) { if (whereClauseSB.length() > 0) { whereClauseSB.append(" AND "); } whereClauseSB.append(filterClause); } } if (params.getMarker() != null) { try { Resource resourceAtMarker = (Resource) em .createQuery("SELECT r FROM " + params.getEntityType() + " r WHERE uuid=:uuid") .setParameter("uuid", params.getMarker()).getSingleResult(); if (whereClauseSB.length() > 0) { whereClauseSB.append(" AND "); } whereClauseSB.append(" v.id>" + resourceAtMarker.getId() + " "); } catch (NoResultException e) { throw new InvalidRequestException("Invalid marker " + params.getMarker()); } } String whereClause = whereClauseSB.toString(); try { int count = ((Number) em .createQuery("SELECT COUNT(v) FROM " + params.getEntityType() + " v WHERE " + whereClause) .setParameter("tenantId", params.getTenantId()).getSingleResult()).intValue(); Query query = em.createQuery( "SELECT v FROM " + params.getEntityType() + " v WHERE " + whereClause + " ORDER BY v.id DESC") .setParameter("tenantId", params.getTenantId()); if (params.getLimit() != null) { query.setMaxResults(params.getLimit()); } else { if (params.getFirst() != null) { query.setFirstResult(params.getFirst()); } if (params.getLast() != null) { if (params.getFirst() != null) { query.setMaxResults(params.getLast() - params.getFirst() + 1); } else { query.setMaxResults(params.getLast() + 1); } } } List<E> queryResult = query.getResultList(); if (params.getAttributes() != null && params.getAttributes().size() != 0) { List<E> items = new ArrayList<E>(); for (E from : queryResult) { E resource = (E) params.getClazz().newInstance(); for (int i = 0; i < params.getAttributes().size(); i++) { try { PropertyUtils.setSimpleProperty(resource, params.getAttributes().get(i), PropertyUtils.getSimpleProperty(from, params.getAttributes().get(i))); } catch (NoSuchMethodException e) { // ignore wrong attribute name } } resource.setUuid(from.getUuid()); if (resource instanceof ICloudProviderResource) { ICloudProviderResource fromResource = (ICloudProviderResource) from; ICloudProviderResource toResource = (ICloudProviderResource) resource; toResource.setLocation(fromResource.getLocation()); toResource.setProviderAssignedId(fromResource.getProviderAssignedId()); toResource.setCloudProviderAccount(fromResource.getCloudProviderAccount()); } else if (resource instanceof IMultiCloudResource) { IMultiCloudResource fromResource = (IMultiCloudResource) from; IMultiCloudResource toResource = (IMultiCloudResource) resource; toResource.setProviderMappings(fromResource.getProviderMappings()); } items.add(resource); } return new QueryResult<E>(count, items); } else { return new QueryResult<E>(count, queryResult); } } catch (IllegalArgumentException ex) { ex.printStackTrace(); throw new InvalidRequestException(ex.getMessage()); } catch (InstantiationException ex) { ex.printStackTrace(); throw new InvalidRequestException(ex.getMessage()); } catch (IllegalAccessException ex) { ex.printStackTrace(); throw new InvalidRequestException(ex.getMessage()); } catch (InvocationTargetException ex) { throw new InvalidRequestException(ex.getMessage()); } }
From source file:org.ow2.sirocco.cloudmanager.core.utils.QueryHelper.java
public static <E> QueryResult<E> getCollectionItemList(final EntityManager em, final QueryParamsBuilder params) throws InvalidRequestException { StringBuffer whereClauseSB = new StringBuffer(); if (params.getTenantId() != null) { whereClauseSB.append(" v.tenant.id=:tenantId "); }//from w w w . j av a2s. c om if (params.getStateToIgnore() != null) { if (whereClauseSB.length() > 0) { whereClauseSB.append(" AND "); } whereClauseSB.append(" vv.state<>" + params.getStateToIgnore().getClass().getName() + "." + params.getStateToIgnore().name() + " "); } if (whereClauseSB.length() > 0) { whereClauseSB.append(" AND "); } whereClauseSB.append("v.uuid=:cid "); if (params.getFilters() != null) { String filterClause; try { filterClause = QueryHelper.generateFilterClause(params.getFilters(), "vv", params.getClazz().getName() + "$State."); } catch (ParseException ex) { throw new InvalidRequestException("Parsing error in filter expression " + ex.getMessage()); } catch (TokenMgrError ex) { throw new InvalidRequestException(ex.getMessage()); } if (!filterClause.isEmpty()) { if (whereClauseSB.length() > 0) { whereClauseSB.append(" AND "); } whereClauseSB.append(filterClause); } } String whereClause = whereClauseSB.toString(); String queryExpression = "SELECT COUNT(vv) FROM " + params.getEntityType() + " vv, " + params.getContainerType() + " v WHERE vv MEMBER OF v." + params.getContainerAttributeName() + " AND " + whereClause; try { int count = ((Number) em.createQuery(queryExpression).setParameter("cid", params.getContainerId()) .setParameter("tenantId", params.getTenantId()).getSingleResult()).intValue(); queryExpression = "SELECT vv FROM " + params.getEntityType() + " vv, " + params.getContainerType() + " v WHERE vv MEMBER OF v." + params.getContainerAttributeName() + " AND " + whereClause + " ORDER BY vv.id"; Query query = em.createQuery(queryExpression).setParameter("cid", params.getContainerId()) .setParameter("tenantId", params.getTenantId()); if (params.getFirst() != null) { query.setFirstResult(params.getFirst()); } if (params.getLast() != null) { if (params.getFirst() != null) { query.setMaxResults(params.getLast() - params.getFirst() + 1); } else { query.setMaxResults(params.getLast() + 1); } } List<E> queryResult = query.getResultList(); if (params.getAttributes() != null && params.getAttributes().size() != 0) { List<E> items = new ArrayList<E>(); for (E from : queryResult) { E resource = (E) params.getClazz().newInstance(); for (int i = 0; i < params.getAttributes().size(); i++) { try { PropertyUtils.setSimpleProperty(resource, params.getAttributes().get(i), PropertyUtils.getSimpleProperty(from, params.getAttributes().get(i))); } catch (NoSuchMethodException e) { // ignore wrong attribute name } } items.add(resource); } return new QueryResult<E>(count, items); } else { return new QueryResult<E>(count, queryResult); } } catch (IllegalArgumentException ex) { ex.printStackTrace(); throw new InvalidRequestException(ex.getMessage()); } catch (InstantiationException ex) { ex.printStackTrace(); throw new InvalidRequestException(ex.getMessage()); } catch (IllegalAccessException ex) { ex.printStackTrace(); throw new InvalidRequestException(ex.getMessage()); } catch (InvocationTargetException ex) { throw new InvalidRequestException(ex.getMessage()); } }
From source file:org.posterita.struts.core.BaseForm.java
protected Object convert(Class type, String key, Object obj, int mode) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NumberFormatException { Object convertedObj = null;//from w ww . ja v a2 s . c om Formatter formatter = getFormatter(key, type); try { switch (mode) { case TO_OBJECT: convertedObj = formatter.unformat(obj); break; case TO_STRING: if (obj == null) convertedObj = (String) defaultStringMap.get(key); else convertedObj = formatter.format(obj); /** * This let's the toDate being converted into endDay, endMonth and endYear * note that the String here are sensitive. */ if (key.equals("toDate")) { JulianDate dateValue = (JulianDate) obj; if (dateValue != null) { PropertyUtils.setSimpleProperty(this, "endDay", "" + dateValue.getDay()); PropertyUtils.setSimpleProperty(this, "endMonth", "" + dateValue.getMonth()); PropertyUtils.setSimpleProperty(this, "endYear", "" + dateValue.getYear()); } else { PropertyUtils.setSimpleProperty(this, "endDay", ""); PropertyUtils.setSimpleProperty(this, "endMonth", ""); PropertyUtils.setSimpleProperty(this, "endYear", ""); } } if (key.equals("fromDate")) { JulianDate dateValue = (JulianDate) obj; if (dateValue != null) { PropertyUtils.setSimpleProperty(this, "startDay", "" + dateValue.getDay()); PropertyUtils.setSimpleProperty(this, "startMonth", "" + dateValue.getMonth()); PropertyUtils.setSimpleProperty(this, "startYear", "" + dateValue.getYear()); } else { PropertyUtils.setSimpleProperty(this, "startDay", ""); PropertyUtils.setSimpleProperty(this, "startMonth", ""); PropertyUtils.setSimpleProperty(this, "startYear", ""); } } break; default: throw new RuntimeException("Unknown mode:" + mode); } } catch (FormattingException e) { e.setFormatter(formatter); throw e; } return convertedObj; }
From source file:org.posterita.struts.core.BaseForm.java
protected ActionErrors populateProperty(Object vo, String key, Object obj, int mode, Map valueMap) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { Object target = (mode == TO_STRING ? this : vo); Class type = PropertyUtils.getPropertyType(vo, key); ActionErrors errors = new ActionErrors(); Object value = null;/*from w w w. j a v a 2 s . co m*/ if (mode == TO_STRING) { value = convert(type, key, obj, mode); } else { try { String errorKey = null; value = convert(type, key, obj, mode); errorKey = validateRequired(key, obj); if (errorKey == null) errorKey = validateRange(key, value); if (errorKey == null) errorKey = validateEmail(key, value); if (errorKey == null) errorKey = validateLength(key, obj); if (errorKey == null) errorKey = validateImageFileExtension(key, obj); if (errorKey == null) errorKey = validateMatchFields(key, value, vo, valueMap); if (errorKey == null) errorKey = validateCustomerNumber(key, value); if (errorKey == null) errorKey = validateMatchFields(key, value, vo, valueMap); if (errorKey == null) errorKey = validateDateFields(key, value, vo, valueMap); if (errorKey == null) errorKey = validateCreditCardExpiryDateFields(key, value, vo, valueMap); if (errorKey != null) errors.add(key, new ActionMessage(errorKey, key)); //This part is placed here because validateCreditCardField method may return an errorKey //As we are reusing the same variable to store an error message //the error is added above in the ActionMessage and then variable errorKey is reused here errorKey = validateCVVField(key, value, vo, valueMap); if (errorKey != null) errors.add(key, new ActionMessage(errorKey, key)); errorKey = validateAccountField(key, value, vo, valueMap); if (errorKey != null) errors.add(key, new ActionMessage(errorKey, key)); errorKey = validateCreditCardField(key, value, vo, valueMap); if (errorKey != null) errors.add(key, new ActionMessage(errorKey, key)); } catch (FormattingException e) { String errorKey = e.getFormatter().getErrorKey(); errors.add(key, new ActionMessage(errorKey, key)); } catch (NumberFormatException nfe) { String errorKey; if (resources.getMessage("error.numberformatexception." + key) == null) errorKey = "error.numberformatexception"; else errorKey = "error.numberformatexception." + key; errors.add(key, new ActionMessage(errorKey, key)); } } PropertyUtils.setSimpleProperty(target, key, value); return errors; }
From source file:org.sakaiproject.component.app.messageforums.PermissionLevelManagerImpl.java
/** * Populates the permission level data for the case when the default permission levels * are being created, not the custom levels * @param name/*from w ww . ja v a2s .com*/ * @param typeUuid * @param mask * @param uuid * @return */ private PermissionLevel createDefaultPermissionLevel(String name, String typeUuid, PermissionsMask mask) { if (LOG.isDebugEnabled()) { LOG.debug("createDefaultPermissionLevel executing(" + name + "," + typeUuid + "," + mask + ")"); } if (name == null || typeUuid == null || mask == null) { throw new IllegalArgumentException("Null Argument"); } PermissionLevel newPermissionLevel = new PermissionLevelImpl(); Date now = new Date(); newPermissionLevel.setName(name); newPermissionLevel.setUuid(idManager.createUuid()); newPermissionLevel.setCreated(now); newPermissionLevel.setCreatedBy("admin"); newPermissionLevel.setModified(now); newPermissionLevel.setModifiedBy("admin"); newPermissionLevel.setTypeUuid(typeUuid); // set permission properties using reflection for (Iterator<Entry<String, Boolean>> i = mask.entrySet().iterator(); i.hasNext();) { Entry<String, Boolean> entry = i.next(); String key = entry.getKey(); Boolean value = entry.getValue(); try { PropertyUtils.setSimpleProperty(newPermissionLevel, key, value); } catch (Exception e) { throw new RuntimeException(e); } } return newPermissionLevel; }
From source file:org.sakaiproject.component.app.messageforums.PermissionLevelManagerImpl.java
public PermissionLevel createPermissionLevel(String name, String typeUuid, PermissionsMask mask) { if (LOG.isDebugEnabled()) { LOG.debug("createPermissionLevel executing(" + name + "," + typeUuid + "," + mask + ")"); }//from ww w . j av a2s .c o m if (name == null || typeUuid == null || mask == null) { throw new IllegalArgumentException("Null Argument"); } PermissionLevel newPermissionLevel = new PermissionLevelImpl(); Date now = new Date(); String currentUser = getCurrentUser(); newPermissionLevel.setName(name); newPermissionLevel.setUuid(idManager.createUuid()); newPermissionLevel.setCreated(now); newPermissionLevel.setCreatedBy(currentUser); newPermissionLevel.setModified(now); newPermissionLevel.setModifiedBy(currentUser); newPermissionLevel.setTypeUuid(typeUuid); // set permission properties using reflection for (Iterator<Entry<String, Boolean>> i = mask.entrySet().iterator(); i.hasNext();) { Entry<String, Boolean> entry = i.next(); String key = entry.getKey(); Boolean value = entry.getValue(); try { PropertyUtils.setSimpleProperty(newPermissionLevel, key, value); } catch (Exception e) { throw new RuntimeException(e); } } return newPermissionLevel; }