List of usage examples for org.apache.commons.beanutils PropertyUtils isReadable
public static boolean isReadable(Object bean, String name)
Return true
if the specified property name identifies a readable property on the specified bean; otherwise, return false
.
For more details see PropertyUtilsBean
.
From source file:org.andromda.timetracker.web.timecardsearch.SearchTimecardsFormImpl.java
/** * @param items/*from w ww. j a v a2 s .c o m*/ * @param valueProperty * @param labelProperty */ public void setApproverBackingList(Collection<? extends Object> items, String valueProperty, String labelProperty) { this.approverValueList = null; this.approverLabelList = null; if (items != null) { this.approverValueList = new Object[items.size()]; this.approverLabelList = new Object[items.size()]; try { final List<String> labelProperties = labelProperty == null ? null : new ArrayList<String>(Arrays.asList(labelProperty.split("[\\W&&[^\\.]]+"))); final List<String> labelDelimiters = labelProperty == null ? null : new ArrayList<String>(Arrays.asList(labelProperty.split("[\\w\\.]+"))); int ctr = 0; for (final Iterator<? extends Object> iterator = items.iterator(); iterator.hasNext(); ctr++) { final Object item = iterator.next(); if (item != null) { this.approverValueList[ctr] = valueProperty == null ? item : PropertyUtils.getProperty(item, valueProperty.trim()); if (labelProperties == null) { this.approverLabelList[ctr] = item; } else { final StringBuilder labelText = new StringBuilder(); int ctr2 = 0; do { if (!labelDelimiters.isEmpty()) { labelText.append(labelDelimiters.get(ctr2)); } String property = null; if (ctr2 < labelProperties.size()) { property = labelProperties.get(ctr2); } if (property != null && property.length() > 0) { if (PropertyUtils.isReadable(item, property)) { Object value = PropertyUtils.getProperty(item, property); if (value != null) { if (value instanceof String) { if (((String) value).trim().length() == 0) { value = null; } } if (value != null) { labelText.append(value); } } } else { labelText.append(property); } } ctr2++; } while (ctr2 < labelDelimiters.size()); this.approverLabelList[ctr] = labelText.toString().replaceAll("\\s+", " ").trim(); } } } } catch (final Throwable throwable) { throw new RuntimeException(throwable); } } }
From source file:org.andromda.timetracker.web.timecardsearch.SearchTimecardsFormImpl.java
/** * @param items//from w ww.ja v a 2s . com * @param valueProperty * @param labelProperty */ public void setStatusBackingList(Collection<? extends Object> items, String valueProperty, String labelProperty) { this.statusValueList = null; this.statusLabelList = null; if (items != null) { this.statusValueList = new Object[items.size()]; this.statusLabelList = new Object[items.size()]; try { final List<String> labelProperties = labelProperty == null ? null : new ArrayList<String>(Arrays.asList(labelProperty.split("[\\W&&[^\\.]]+"))); final List<String> labelDelimiters = labelProperty == null ? null : new ArrayList<String>(Arrays.asList(labelProperty.split("[\\w\\.]+"))); int ctr = 0; for (final Iterator<? extends Object> iterator = items.iterator(); iterator.hasNext(); ctr++) { final Object item = iterator.next(); if (item != null) { this.statusValueList[ctr] = valueProperty == null ? item : PropertyUtils.getProperty(item, valueProperty.trim()); if (labelProperties == null) { this.statusLabelList[ctr] = item; } else { final StringBuilder labelText = new StringBuilder(); int ctr2 = 0; do { if (!labelDelimiters.isEmpty()) { labelText.append(labelDelimiters.get(ctr2)); } String property = null; if (ctr2 < labelProperties.size()) { property = labelProperties.get(ctr2); } if (property != null && property.length() > 0) { if (PropertyUtils.isReadable(item, property)) { Object value = PropertyUtils.getProperty(item, property); if (value != null) { if (value instanceof String) { if (((String) value).trim().length() == 0) { value = null; } } if (value != null) { labelText.append(value); } } } else { labelText.append(property); } } ctr2++; } while (ctr2 < labelDelimiters.size()); this.statusLabelList[ctr] = labelText.toString().replaceAll("\\s+", " ").trim(); } } } } catch (final Throwable throwable) { throw new RuntimeException(throwable); } } }
From source file:org.andromda.timetracker.web.timecardsearch.SearchTimecardsFormImpl.java
/** * @param items/* w w w . j a v a 2 s . com*/ * @param valueProperty * @param labelProperty */ public void setStartDateMinimumBackingList(Collection<? extends Object> items, String valueProperty, String labelProperty) { this.startDateMinimumValueList = null; this.startDateMinimumLabelList = null; if (items != null) { this.startDateMinimumValueList = new Object[items.size()]; this.startDateMinimumLabelList = new Object[items.size()]; try { final List<String> labelProperties = labelProperty == null ? null : new ArrayList<String>(Arrays.asList(labelProperty.split("[\\W&&[^\\.]]+"))); final List<String> labelDelimiters = labelProperty == null ? null : new ArrayList<String>(Arrays.asList(labelProperty.split("[\\w\\.]+"))); int ctr = 0; for (final Iterator<? extends Object> iterator = items.iterator(); iterator.hasNext(); ctr++) { final Object item = iterator.next(); if (item != null) { this.startDateMinimumValueList[ctr] = valueProperty == null ? item : PropertyUtils.getProperty(item, valueProperty.trim()); if (labelProperties == null) { this.startDateMinimumLabelList[ctr] = item; } else { final StringBuilder labelText = new StringBuilder(); int ctr2 = 0; do { if (!labelDelimiters.isEmpty()) { labelText.append(labelDelimiters.get(ctr2)); } String property = null; if (ctr2 < labelProperties.size()) { property = labelProperties.get(ctr2); } if (property != null && property.length() > 0) { if (PropertyUtils.isReadable(item, property)) { Object value = PropertyUtils.getProperty(item, property); if (value != null) { if (value instanceof String) { if (((String) value).trim().length() == 0) { value = null; } } if (value != null) { labelText.append(value); } } } else { labelText.append(property); } } ctr2++; } while (ctr2 < labelDelimiters.size()); this.startDateMinimumLabelList[ctr] = labelText.toString().replaceAll("\\s+", " ") .trim(); } } } } catch (final Throwable throwable) { throw new RuntimeException(throwable); } } }
From source file:org.andromda.timetracker.web.timecardsearch.SearchTimecardsFormImpl.java
/** * @param items/* w w w. j a v a 2 s .c om*/ * @param valueProperty * @param labelProperty */ public void setStartDateMaximumBackingList(Collection<? extends Object> items, String valueProperty, String labelProperty) { this.startDateMaximumValueList = null; this.startDateMaximumLabelList = null; if (items != null) { this.startDateMaximumValueList = new Object[items.size()]; this.startDateMaximumLabelList = new Object[items.size()]; try { final List<String> labelProperties = labelProperty == null ? null : new ArrayList<String>(Arrays.asList(labelProperty.split("[\\W&&[^\\.]]+"))); final List<String> labelDelimiters = labelProperty == null ? null : new ArrayList<String>(Arrays.asList(labelProperty.split("[\\w\\.]+"))); int ctr = 0; for (final Iterator<? extends Object> iterator = items.iterator(); iterator.hasNext(); ctr++) { final Object item = iterator.next(); if (item != null) { this.startDateMaximumValueList[ctr] = valueProperty == null ? item : PropertyUtils.getProperty(item, valueProperty.trim()); if (labelProperties == null) { this.startDateMaximumLabelList[ctr] = item; } else { final StringBuilder labelText = new StringBuilder(); int ctr2 = 0; do { if (!labelDelimiters.isEmpty()) { labelText.append(labelDelimiters.get(ctr2)); } String property = null; if (ctr2 < labelProperties.size()) { property = labelProperties.get(ctr2); } if (property != null && property.length() > 0) { if (PropertyUtils.isReadable(item, property)) { Object value = PropertyUtils.getProperty(item, property); if (value != null) { if (value instanceof String) { if (((String) value).trim().length() == 0) { value = null; } } if (value != null) { labelText.append(value); } } } else { labelText.append(property); } } ctr2++; } while (ctr2 < labelDelimiters.size()); this.startDateMaximumLabelList[ctr] = labelText.toString().replaceAll("\\s+", " ") .trim(); } } } } catch (final Throwable throwable) { throw new RuntimeException(throwable); } } }
From source file:org.andromda.timetracker.web.timecardsearch.SearchTimecardsPopulator.java
/** * @param facesContext/* w ww .j a v a2s.c om*/ * @param form * @throws ServletException */ public static void populateFormAndViewVariables(final FacesContext facesContext, Object form) throws ServletException { final HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest(); final HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(true); RequestContext adfContext = RequestContext.getCurrentInstance(); final VariableResolver variableResolver = facesContext.getApplication().getVariableResolver(); if (form == null) { // - first try getting the form from the ADF PageFlowScope form = adfContext.getPageFlowScope().get("form"); // - if the form is null, try getting the current adfContext from the session (and then remove it from the session) if (form == null) { final AdfFacesContextWrapper contextWrapper = (AdfFacesContextWrapper) session .getAttribute("AndroMDAADFContext"); adfContext = contextWrapper != null ? contextWrapper.getCurrentInstance() : null; form = adfContext != null ? adfContext.getPageFlowScope().get("form") : null; if (form == null) { form = session.getAttribute("form"); session.removeAttribute("form"); } // - if the form is still null, see if we can get it from a serialized state if (form == null) { form = JsfUtils.getSerializedForm(facesContext); } if (form != null) { // - add the form to the current process scope since it wasn't in the current one to begin with RequestContext.getCurrentInstance().getPageFlowScope().put("form", form); } } else { // - remove the ADF context in the event that its present session.removeAttribute("AndroMDAADFContext"); } } else { // - since the form argument is not null, set it as the "form" in the PageFlowScope // (to replace the existing "form" attribute) adfContext.getPageFlowScope().put("form", form); } try { // - populate the forms if (form != null) { SearchTimecardsSearchFormImpl searchTimecardsSearchTimecardsSearchForm = (SearchTimecardsSearchFormImpl) variableResolver .resolveVariable(facesContext, "searchTimecardsSearchTimecardsSearchForm"); // - populate the searchTimecardsSearchTimecardsSearchForm with any parameters from the previous form FormPopulator.populateForm(form, searchTimecardsSearchTimecardsSearchForm, false); request.setAttribute("searchTimecardsSearchTimecardsSearchForm", searchTimecardsSearchTimecardsSearchForm); SearchTimecardsDetailsFormImpl searchTimecardsSearchTimecardsDetailsForm = (SearchTimecardsDetailsFormImpl) variableResolver .resolveVariable(facesContext, "searchTimecardsSearchTimecardsDetailsForm"); // - populate the searchTimecardsSearchTimecardsDetailsForm with any parameters from the previous form FormPopulator.populateForm(form, searchTimecardsSearchTimecardsDetailsForm, false); request.setAttribute("searchTimecardsSearchTimecardsDetailsForm", searchTimecardsSearchTimecardsDetailsForm); } // - serialize the form if (form != null) { JsfUtils.serializeForm(facesContext, form); } // - populate the view variables if (form != null) { final boolean timecardSummariesReadable = PropertyUtils.isReadable(form, "timecardSummaries"); if (timecardSummariesReadable) { Boolean propertySet = null; final String isSetPropertyName = "timecardSummariesSet"; if (PropertyUtils.isReadable(form, isSetPropertyName)) { propertySet = (Boolean) PropertyUtils.getProperty(form, isSetPropertyName); } // - only set the property if its been set, or we can't tell if it has if (propertySet == null || propertySet.booleanValue()) { request.setAttribute("timecardSummaries", PropertyUtils.getProperty(form, "timecardSummaries")); } } final boolean submitterBackingListReadable = PropertyUtils.isReadable(form, "submitterBackingList"); if (submitterBackingListReadable) { Object backingList = PropertyUtils.getProperty(form, "submitterBackingList"); request.setAttribute("submitterBackingList", backingList); } final boolean approverBackingListReadable = PropertyUtils.isReadable(form, "approverBackingList"); if (approverBackingListReadable) { Object backingList = PropertyUtils.getProperty(form, "approverBackingList"); request.setAttribute("approverBackingList", backingList); } final boolean statusBackingListReadable = PropertyUtils.isReadable(form, "statusBackingList"); if (statusBackingListReadable) { Object backingList = PropertyUtils.getProperty(form, "statusBackingList"); request.setAttribute("statusBackingList", backingList); } } } catch (final Throwable throwable) { throw new ServletException(throwable); } }
From source file:org.apache.myfaces.config.ManagedBeanBuilder.java
@SuppressWarnings("unchecked") private void initializeProperties(FacesContext facesContext, ManagedBean beanConfiguration, Object bean) { ELResolver elResolver = facesContext.getApplication().getELResolver(); ELContext elContext = facesContext.getELContext(); for (ManagedProperty property : beanConfiguration.getManagedProperties()) { Object value = null;/* w ww.ja v a 2 s . c o m*/ switch (property.getType()) { case ManagedProperty.TYPE_LIST: // JSF 1.1, 5.3.1.3 // Call the property getter, if it exists. // If the getter returns null or doesn't exist, create a java.util.ArrayList, // otherwise use the returned Object ... if (PropertyUtils.isReadable(bean, property.getPropertyName())) { value = elResolver.getValue(elContext, bean, property.getPropertyName()); } value = value == null ? new ArrayList<Object>() : value; if (value instanceof List) { initializeList(facesContext, property.getListEntries(), (List<Object>) value); } else if (value != null && value.getClass().isArray()) { int length = Array.getLength(value); ArrayList<Object> temp = new ArrayList<Object>(length); for (int i = 0; i < length; i++) { temp.add(Array.get(value, i)); } initializeList(facesContext, property.getListEntries(), temp); value = Array.newInstance(value.getClass().getComponentType(), temp.size()); length = temp.size(); for (int i = 0; i < length; i++) { Array.set(value, i, temp.get(i)); } } else { value = new ArrayList<Object>(); initializeList(facesContext, property.getListEntries(), (List<Object>) value); } break; case ManagedProperty.TYPE_MAP: // JSF 1.1, 5.3.1.3 // Call the property getter, if it exists. // If the getter returns null or doesn't exist, create a java.util.HashMap, // otherwise use the returned java.util.Map . if (PropertyUtils.isReadable(bean, property.getPropertyName())) { value = elResolver.getValue(elContext, bean, property.getPropertyName()); } value = value == null ? new HashMap<Object, Object>() : value; if (!(value instanceof Map)) { value = new HashMap<Object, Object>(); } initializeMap(facesContext, property.getMapEntries(), (Map<Object, Object>) value); break; case ManagedProperty.TYPE_NULL: break; case ManagedProperty.TYPE_VALUE: // check for correct scope of a referenced bean if (!isInValidScope(facesContext, property, beanConfiguration)) { throw new FacesException("Property " + property.getPropertyName() + " references object in a scope with shorter lifetime than the target scope " + beanConfiguration.getManagedBeanScope()); } value = property.getRuntimeValue(facesContext); break; default: throw new FacesException("unknown ManagedProperty type: " + property.getType()); } Class<?> propertyClass = null; if (property.getPropertyClass() == null) { propertyClass = elResolver.getType(elContext, bean, property.getPropertyName()); } else { propertyClass = ClassUtils.simpleJavaTypeToClass(property.getPropertyClass()); } if (null == propertyClass) { throw new IllegalArgumentException( "unable to find the type of property " + property.getPropertyName()); } Object coercedValue = coerceToType(facesContext, value, propertyClass); elResolver.setValue(elContext, bean, property.getPropertyName(), coercedValue); } }
From source file:org.apache.myfaces.ov2021.config.ManagedBeanBuilder.java
@SuppressWarnings("unchecked") private void initializeProperties(FacesContext facesContext, ManagedBean beanConfiguration, Object bean) { ELResolver elResolver = facesContext.getApplication().getELResolver(); ELContext elContext = facesContext.getELContext(); for (ManagedProperty property : beanConfiguration.getManagedProperties()) { Object value = null;/* ww w. j a v a 2 s. c o m*/ switch (property.getType()) { case ManagedProperty.TYPE_LIST: // JSF 1.1, 5.3.1.3 // Call the property getter, if it exists. // If the getter returns null or doesn't exist, create a java.util.ArrayList, // otherwise use the returned Object ... if (PropertyUtils.isReadable(bean, property.getPropertyName())) { value = elResolver.getValue(elContext, bean, property.getPropertyName()); } value = value == null ? new ArrayList<Object>() : value; if (value instanceof List) { initializeList(facesContext, property.getListEntries(), (List<Object>) value); } else if (value != null && value.getClass().isArray()) { int length = Array.getLength(value); ArrayList<Object> temp = new ArrayList<Object>(length); for (int i = 0; i < length; i++) { temp.add(Array.get(value, i)); } initializeList(facesContext, property.getListEntries(), temp); value = Array.newInstance(value.getClass().getComponentType(), temp.size()); length = temp.size(); for (int i = 0; i < length; i++) { Array.set(value, i, temp.get(i)); } } else { value = new ArrayList<Object>(); initializeList(facesContext, property.getListEntries(), (List<Object>) value); } break; case ManagedProperty.TYPE_MAP: // JSF 1.1, 5.3.1.3 // Call the property getter, if it exists. // If the getter returns null or doesn't exist, create a java.util.HashMap, // otherwise use the returned java.util.Map . if (PropertyUtils.isReadable(bean, property.getPropertyName())) value = elResolver.getValue(elContext, bean, property.getPropertyName()); value = value == null ? new HashMap<Object, Object>() : value; if (!(value instanceof Map)) { value = new HashMap<Object, Object>(); } initializeMap(facesContext, property.getMapEntries(), (Map<Object, Object>) value); break; case ManagedProperty.TYPE_NULL: break; case ManagedProperty.TYPE_VALUE: // check for correct scope of a referenced bean if (!isInValidScope(facesContext, property, beanConfiguration)) { throw new FacesException("Property " + property.getPropertyName() + " references object in a scope with shorter lifetime than the target scope " + beanConfiguration.getManagedBeanScope()); } value = property.getRuntimeValue(facesContext); break; } Class<?> propertyClass = null; if (property.getPropertyClass() == null) { propertyClass = elResolver.getType(elContext, bean, property.getPropertyName()); } else { propertyClass = ClassUtils.simpleJavaTypeToClass(property.getPropertyClass()); } if (null == propertyClass) { throw new IllegalArgumentException( "unable to find the type of property " + property.getPropertyName()); } Object coercedValue = coerceToType(facesContext, value, propertyClass); elResolver.setValue(elContext, bean, property.getPropertyName(), coercedValue); } }
From source file:org.fornax.cartridges.sculptor.framework.accessimpl.jpa.JpaPopulateAssociationsAccessImpl.java
private Serializable getId(Object domainObject) { if (PropertyUtils.isReadable(domainObject, "id")) { try {//from w ww.ja v a 2s . c o m return (Serializable) PropertyUtils.getProperty(domainObject, "id"); } catch (Exception e) { throw new IllegalArgumentException("Can't get id property of domainObject: " + domainObject); } } else { // no id property, don't know if it is new throw new IllegalArgumentException("No id property in domainObject: " + domainObject); } }
From source file:org.fornax.cartridges.sculptor.framework.accessimpl.mongodb.IdReflectionUtil.java
public static Serializable internalGetId(Object domainObject) { if (domainObject instanceof Identifiable) { return ((Identifiable) domainObject).getId(); }/*from w ww . j a v a 2 s.co m*/ if (PropertyUtils.isReadable(domainObject, "id")) { try { return (Serializable) PropertyUtils.getProperty(domainObject, "id"); } catch (Exception e) { throw new IllegalArgumentException("Can't get id property of domainObject: " + domainObject); } } else { // no id property, don't know if it is new throw new IllegalArgumentException("No id property in domainObject: " + domainObject); } }
From source file:org.hx.rainbow.common.util.JavaBeanUtil.java
/** * ??MapMapKey??/*from w ww . jav a2s .co m*/ * * @param bean * @return * @throws Exception */ @SuppressWarnings({ "rawtypes", "unchecked" }) @Deprecated public static Map getProperties(Object bean) { if (bean == null) { return null; } Map dataMap = new HashMap(); try { PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(bean); for (int i = 0; i < origDescriptors.length; i++) { String name = origDescriptors[i].getName(); if (name.equals("class")) { continue; } if (PropertyUtils.isReadable(bean, name)) { Object obj = PropertyUtils.getProperty(bean, name); if (obj == null) { continue; } obj = convertValue(origDescriptors[i], obj); dataMap.put(name, obj); } } // for end } catch (Exception e) { e.printStackTrace(); throw new AppException(e.getMessage()); } return dataMap; }