List of usage examples for org.apache.commons.beanutils PropertyUtils getProperty
public static Object getProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
Return 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.erudika.para.validation.ValidationUtils.java
/** * Validates objects.//from ww w . j a v a 2 s . co m * @param content an object to be validated * @param app the current app * @return a list of error messages or empty if object is valid */ public static String[] validateObject(App app, ParaObject content) { if (content == null || app == null) { return new String[] { "Object cannot be null." }; } try { String type = content.getType(); boolean isCustomType = (content instanceof Sysprop) && !type.equals(Utils.type(Sysprop.class)); // Validate custom types and user-defined properties if (!app.getValidationConstraints().isEmpty() && isCustomType) { Map<String, Map<String, Map<String, ?>>> fieldsMap = app.getValidationConstraints().get(type); if (fieldsMap != null && !fieldsMap.isEmpty()) { LinkedList<String> errors = new LinkedList<String>(); for (Map.Entry<String, Map<String, Map<String, ?>>> e : fieldsMap.entrySet()) { String field = e.getKey(); Object actualValue = ((Sysprop) content).getProperty(field); // overriding core property validation rules is allowed if (actualValue == null && PropertyUtils.isReadable(content, field)) { actualValue = PropertyUtils.getProperty(content, field); } Map<String, Map<String, ?>> consMap = e.getValue(); for (Map.Entry<String, Map<String, ?>> constraint : consMap.entrySet()) { String consName = constraint.getKey(); Map<String, ?> vals = constraint.getValue(); if (vals == null) { vals = Collections.emptyMap(); } Object val = vals.get("value"); Object min = vals.get("min"); Object max = vals.get("max"); Object in = vals.get("integer"); Object fr = vals.get("fraction"); if ("required".equals(consName) && !required().isValid(actualValue)) { errors.add(Utils.formatMessage("{0} is required.", field)); } else if (matches(Min.class, consName) && !min(val).isValid(actualValue)) { errors.add( Utils.formatMessage("{0} must be a number larger than {1}.", field, val)); } else if (matches(Max.class, consName) && !max(val).isValid(actualValue)) { errors.add( Utils.formatMessage("{0} must be a number smaller than {1}.", field, val)); } else if (matches(Size.class, consName) && !size(min, max).isValid(actualValue)) { errors.add( Utils.formatMessage("{0} must be between {1} and {2}.", field, min, max)); } else if (matches(Email.class, consName) && !email().isValid(actualValue)) { errors.add(Utils.formatMessage("{0} is not a valid email.", field)); } else if (matches(Digits.class, consName) && !digits(in, fr).isValid(actualValue)) { errors.add( Utils.formatMessage("{0} is not a valid number or within range.", field)); } else if (matches(Pattern.class, consName) && !pattern(val).isValid(actualValue)) { errors.add(Utils.formatMessage("{0} doesn't match the pattern {1}.", field, val)); } else if (matches(AssertFalse.class, consName) && !falsy().isValid(actualValue)) { errors.add(Utils.formatMessage("{0} must be false.", field)); } else if (matches(AssertTrue.class, consName) && !truthy().isValid(actualValue)) { errors.add(Utils.formatMessage("{0} must be true.", field)); } else if (matches(Future.class, consName) && !future().isValid(actualValue)) { errors.add(Utils.formatMessage("{0} must be in the future.", field)); } else if (matches(Past.class, consName) && !past().isValid(actualValue)) { errors.add(Utils.formatMessage("{0} must be in the past.", field)); } else if (matches(URL.class, consName) && !url().isValid(actualValue)) { errors.add(Utils.formatMessage("{0} is not a valid URL.", field)); } } } if (!errors.isEmpty()) { return errors.toArray(new String[0]); } } } } catch (Exception ex) { logger.error(null, ex); } return validateObject(content); }
From source file:com.googlecode.jtiger.modules.ecside.core.RetrievalUtils.java
static Collection retrieveNestedCollection(WebContext context, String collection, String scope) throws Exception { String split[] = StringUtils.split(collection, "."); Object obj = RetrievalUtils.retrieve(context, split[0], scope); String collectionToFind = StringUtils.substringAfter(collection, "."); Object value = null;//from w ww .j av a 2s .co m try { // if (ExtremeUtils.isBeanPropertyReadable(bean, property)) { value = PropertyUtils.getProperty(obj, collectionToFind); obj = value; // } } catch (Exception e) { if (logger.isDebugEnabled()) { logger.debug("Could not find the property [" + collectionToFind + "]. Either the bean or property is null"); } } if (!(obj instanceof Collection)) { if (logger.isDebugEnabled()) { logger.debug("The object is not of type Collection."); } return Collections.EMPTY_LIST; } return (Collection) obj; }
From source file:com.stratio.decision.functions.engine.DroolsEngineAction.java
private List<Map<String, Object>> formatDroolsResults(Results results) throws Exception { List<Map<String, Object>> outputList = new ArrayList<>(); for (Object singleResult : results.getResults()) { Map<String, Object> outputMap = null; try {/* w ww. jav a 2 s. c o m*/ Object propertyObject = PropertyUtils.getProperty(singleResult, "result"); //outputMap = PropertyUtils.describe(singleResult); outputMap = PropertyUtils.describe(propertyObject); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { throw new Exception(e); } outputList.add(outputMap); } return outputList; }
From source file:name.martingeisse.common.util.ContextAwareXmlWriter.java
/** * Writes an opening tag with the specified element name and uses the value of the property with the * specified property name of the current context as a new context, pushing the old one on the context stack. * Use leaveElementContext() to return to the old context. * /*from w w w . j a va 2 s. c o m*/ * @param propertyName the name of the property of the current context. The property value must not be null. * @param elementName the name of the element to write the opening tag for */ public void enterElementPropertyContext(String propertyName, String elementName) { try { enterContext(PropertyUtils.getProperty(currentContext, propertyName)); writeOpeningTag(elementName); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.esofthead.mycollab.vaadin.web.ui.MultiSelectComp.java
protected ItemSelectionComp<T> buildItem(final T item) { String itemName = ""; if (!"".equals(propertyDisplayField)) { try {// w ww . j a v a2 s .c o m itemName = (String) PropertyUtils.getProperty(item, propertyDisplayField); } catch (final Exception e) { e.printStackTrace(); } } else { itemName = item.toString(); } final ItemSelectionComp<T> chkItem = new ItemSelectionComp<>(item, itemName); chkItem.setImmediate(true); chkItem.addValueChangeListener(new ValueChangeListener() { @Override public void valueChange(Property.ValueChangeEvent valueChangeEvent) { final Boolean value = chkItem.getValue(); if (value && !selectedItems.contains(item)) { selectedItems.add(item); } else { selectedItems.remove(item); } displaySelectedItems(); } }); return chkItem; }
From source file:gov.nih.nci.ncicb.cadsr.util.BeanPropertyComparator.java
private Object getPropertyValue(Object bean, String property) { Object returnValue = null;/*from ww w . j a v a2s . c o m*/ try { returnValue = PropertyUtils.getProperty(bean, property); } catch (java.lang.IllegalArgumentException e) { returnValue = ""; } catch (InvocationTargetException e) { throw new RuntimeException("Comparator Excception " + e); } catch (IllegalAccessException e) { throw new RuntimeException("Comparator Excception " + e); } catch (java.lang.NoSuchMethodException e) { //this could happen for nested properties returnValue = ""; } return returnValue; }
From source file:com.khubla.cbean.serializer.impl.json.JSONListFieldSerializer.java
@Override public String serialize(Object o, Field field) throws SerializerException { try {//from w ww .j a v a 2s. co m /* * get the parameterized type */ final ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType(); final Class<?> containedType = (Class<?>) parameterizedType.getActualTypeArguments()[0]; /* * get cBean */ final CBean<Object> cBean = CBeanServer.getInstance().getCBean(containedType); /* * get the list */ @SuppressWarnings("unchecked") final List<Object> list = (List<Object>) PropertyUtils.getProperty(o, field.getName()); /* * iterate and save contained objects */ final Property property = field.getAnnotation(Property.class); if (property.cascadeSave() == true) { for (int i = 0; i < list.size(); i++) { cBean.save(list.get(i)); } } /* * iterate and grab the keys */ final List<String> keys = new ArrayList<String>(); for (int i = 0; i < list.size(); i++) { final String key = cBean.getId(list.get(i)); keys.add(key); } final JSONArray jsonArray = new JSONArray(keys); return jsonArray.toString(); } catch (final Exception e) { throw new SerializerException(e); } }
From source file:de.topicmapslab.kuria.runtime.PropertyBinding.java
/** * {@inheritDoc}// w w w . ja v a2 s.c om */ public Object getValue(Object instance) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { return PropertyUtils.getProperty(instance, fieldName); }
From source file:com.mycollab.module.project.view.ticket.TicketRowRenderer.java
@Override public void propertyChanged(PropertyChangedEvent event) { EventBusFactory.getInstance().post(new TicketEvent.HasTicketPropertyChanged(this, event.getBindProperty())); if ("status".equals(event.getBindProperty())) { Object bean = event.getSource(); try {/*from w ww. ja va 2 s . c om*/ String statusValue = (String) PropertyUtils.getProperty(bean, "status"); boolean isClosed = StatusI18nEnum.Closed.name().equals(statusValue) || BugStatus.Verified.name().equals(statusValue); if (isClosed) { toggleTicketField.setClosedTicket(); } else { toggleTicketField.unsetClosedTicket(); } } catch (Exception e) { LOG.error("Error", e); } } TicketDashboardView ticketDashboardView = UIUtils.getRoot(this, TicketDashboardView.class); if (ticketDashboardView != null) { ProjectTicketSearchCriteria criteria = ticketDashboardView.getCriteria(); boolean isSatisfied = AppContextUtil.getSpringBean(ProjectTicketService.class) .isTicketIdSatisfyCriteria(ticket.getType(), ticket.getTypeId(), criteria); if (!isSatisfied) { this.selfRemoved(); } } }
From source file:jp.co.opentone.bsol.linkbinder.view.correspon.attachment.AttachmentUploadAction.java
private UploadedFile getUploadedFileAt(int index) { try {//from w ww.j a va 2s . c o m UploadedFile f = (UploadedFile) PropertyUtils.getProperty(page, String.format(PROP_UPLOADED, index + 1)); return (f != null && f.getFileSize() != null) ? f : null; } catch (IllegalAccessException e) { throw new ApplicationFatalRuntimeException(e); } catch (InvocationTargetException e) { throw new ApplicationFatalRuntimeException(e); } catch (NoSuchMethodException e) { throw new ApplicationFatalRuntimeException(e); } }