List of usage examples for org.apache.commons.beanutils PropertyUtils getSimpleProperty
public static Object getSimpleProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
Return the value of the specified simple property of the specified bean, with no type conversions.
For more details see PropertyUtilsBean
.
From source file:org.kuali.kfs.sys.batch.dataaccess.impl.FiscalYearMakersDaoOjb.java
protected String getKeyString(FiscalYearMaker fiscalYearMaker, List<String> keyFieldNames, FiscalYearBasedBusinessObject businessObject) throws Exception { StringBuilder keyString = new StringBuilder(40); for (String keyFieldName : keyFieldNames) { keyString.append(PropertyUtils.getSimpleProperty(businessObject, keyFieldName)) .append(KEY_STRING_DELIMITER); }/*w w w. j a v a2 s . com*/ return keyString.toString(); }
From source file:org.kuali.kfs.sys.KualiTestAssertionUtils.java
/** * Asserts that the non-null non-BO properties of the expected bean are equal to those of the actual bean. Any null or * BusinessObject expected properties are ignored. Attributes are reflected bean-style via any public no-argument methods * starting with "get" (or "is" for booleans), including inherited methods. The expected and actual beans do not need to be of * the same class.//from ww w .j av a 2s .c om * <p> * Reflection wraps primitives, so differences in primitiveness are ignored. Properties that are BusinessObjects (generally * relations based on foreign key properties) are also ignored, because this method is not testing OJB foreign key resolution * (e.g., via the <code>refresh</code> method), we do not want to have to put all the related BOs into the test fixture * (redundant with the foreign keys), and many (all?) of our BOs implement the <code>equals</code> method in terms of identity * so would fail this assertion anyway. This is a data-oriented assertion, for our data-oriented tests and persistence layer. * * @param message a description of this test assertion * @param expectedBean a java bean containing expected properties * @param actualBean a java bean containing actual properties * @throws InvocationTargetException if a getter method throws an exception (the cause) * @throws NoSuchMethodException if an expected property does not exist in the actualBean */ public static void assertSparselyEqualBean(String message, Object expectedBean, Object actualBean) throws InvocationTargetException, NoSuchMethodException { if (message == null) { message = ""; } else { message = message + " "; } assertNotNull(message + "actual bean is null", actualBean); PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(expectedBean); for (int i = 0; i < descriptors.length; i++) { PropertyDescriptor descriptor = descriptors[i]; if (PropertyUtils.getReadMethod(descriptor) != null) { try { Object expectedValue = PropertyUtils.getSimpleProperty(expectedBean, descriptor.getName()); if (expectedValue != null && !(expectedValue instanceof BusinessObject)) { assertEquals(message + descriptor.getName(), expectedValue, PropertyUtils.getSimpleProperty(actualBean, descriptor.getName())); } } catch (IllegalAccessException e) { throw new AssertionError(e); // can't happen because getReadMethod() returns only public methods } } } }
From source file:org.kuali.ole.sys.batch.dataaccess.impl.FiscalYearMakerImpl.java
/** * Determines if an extension record is mapped up and exists for the current record. If so then updates the version number, * object id, and clears the primary keys so they will be relinked when storing the main record * //from w ww . ja v a 2 s . c o m * @param newFiscalYear fiscal year to set * @param currentRecord main record with possible extension reference */ protected void updateExtensionRecord(Integer newFiscalYear, PersistableBusinessObject currentRecord) throws Exception { // check if reference is mapped up if (!hasExtension()) { return; } // try to retrieve extension record currentRecord.refreshReferenceObject(OLEPropertyConstants.EXTENSION); PersistableBusinessObject extension = currentRecord.getExtension(); // if found then update fields if (ObjectUtils.isNotNull(extension)) { extension = (PersistableBusinessObject) ProxyHelper.getRealObject(extension); extension.setVersionNumber(ONE); extension.setObjectId(java.util.UUID.randomUUID().toString()); // since this could be a new object (no extension object present on the source record) // we need to set the keys // But...we only need to do this if this was a truly new object, which we can tell by checking // the fiscal year field if (((FiscalYearBasedBusinessObject) extension).getUniversityFiscalYear() == null) { for (String pkField : getPrimaryKeyPropertyNames()) { PropertyUtils.setSimpleProperty(extension, pkField, PropertyUtils.getSimpleProperty(currentRecord, pkField)); } } ((FiscalYearBasedBusinessObject) extension).setUniversityFiscalYear(newFiscalYear); } }
From source file:org.kuali.rice.kns.web.struts.action.KualiRequestProcessor.java
/** * Adds more detailed logging for unhandled exceptions * //w ww. j a v a 2 s .co m * @see org.apache.struts.action.RequestProcessor#processException(HttpServletRequest, * HttpServletResponse, Exception, ActionForm, ActionMapping) */ @Override protected ActionForward processException(HttpServletRequest request, HttpServletResponse response, Exception exception, ActionForm form, ActionMapping mapping) throws IOException, ServletException { ActionForward actionForward = null; try { actionForward = super.processException(request, response, exception, form, mapping); } catch (IOException e) { logException(e); throw e; } catch (ServletException e) { // special case, to make OptimisticLockExceptions easier to read Throwable rootCause = e.getRootCause(); if (rootCause instanceof OjbOperationException) { OjbOperationException ooe = (OjbOperationException) rootCause; Throwable subcause = ooe.getCause(); if (subcause != null) { Object sourceObject = null; boolean optLockException = false; if (subcause instanceof javax.persistence.OptimisticLockException) { javax.persistence.OptimisticLockException ole = (javax.persistence.OptimisticLockException) subcause; sourceObject = ole.getEntity(); optLockException = true; } else if (subcause instanceof OptimisticLockingFailureException) { OptimisticLockingFailureException ole = (OptimisticLockingFailureException) subcause; sourceObject = ole.getMessage(); optLockException = true; } else { if (subcause.getClass().getName().equals("org.apache.ojb.broker.OptimisticLockException")) { try { sourceObject = PropertyUtils.getSimpleProperty(subcause, "sourceObject"); } catch (Exception ex) { LOG.warn("Unable to retrieve source object from OJB OptimisticLockException", ex); } optLockException = true; } } if (optLockException) { StringBuilder message = new StringBuilder(e.getMessage()); if (sourceObject != null) { if (sourceObject instanceof String) { message.append(" Embedded Message: ").append(sourceObject); } else { message.append(" (sourceObject is "); message.append(sourceObject.getClass().getName()); message.append(")"); } } e = new ServletException(message.toString(), rootCause); } } } logException(e); throw e; } return actionForward; }
From source file:org.kuali.rice.krad.service.impl.PersistenceServiceImplBase.java
/** * @see org.kuali.rice.krad.service.PersistenceMetadataService#getPrimaryKeyFields(java.lang.Object, * boolean)//w ww . j av a 2 s . c o m */ public Map getPrimaryKeyFieldValues(Object persistableObject, boolean sortFieldNames) { if (persistableObject == null) { throw new IllegalArgumentException("invalid (null) persistableObject"); } Map keyValueMap = null; if (sortFieldNames) { keyValueMap = new TreeMap(); } else { keyValueMap = new HashMap(); } String className = null; String fieldName = null; try { List fields = listPrimaryKeyFieldNames(persistableObject.getClass()); for (Iterator i = fields.iterator(); i.hasNext();) { fieldName = (String) i.next(); className = persistableObject.getClass().getName(); Object fieldValue = PropertyUtils.getSimpleProperty(persistableObject, fieldName); keyValueMap.put(fieldName, fieldValue); } } catch (IllegalAccessException e) { throw new IntrospectionException("problem accessing property '" + className + "." + fieldName + "'", e); } catch (NoSuchMethodException e) { throw new IntrospectionException( "unable to invoke getter for property '" + className + "." + fieldName + "'", e); } catch (InvocationTargetException e) { throw new IntrospectionException( "problem invoking getter for property '" + className + "." + fieldName + "'", e); } return keyValueMap; }
From source file:org.kuali.rice.krad.service.impl.PersistenceServiceJpaImpl.java
/** * @see org.kuali.rice.krad.service.PersistenceService#allForeignKeyValuesPopulatedForReference(org.kuali.rice.krad.bo.PersistableBusinessObject, java.lang.String) *//*from w w w .j a va 2s . c om*/ public boolean allForeignKeyValuesPopulatedForReference(PersistableBusinessObject bo, String referenceName) { boolean allFkeysHaveValues = true; // yelp if nulls were passed in if (bo == null) { throw new IllegalArgumentException("The Class passed in for the BusinessObject argument was null."); } if (StringUtils.isBlank(referenceName)) { throw new IllegalArgumentException( "The String passed in for the referenceName argument was null or empty."); } PropertyDescriptor propertyDescriptor = null; // make sure the attribute exists at all, throw exception if not try { propertyDescriptor = PropertyUtils.getPropertyDescriptor(bo, referenceName); } catch (Exception e) { throw new RuntimeException(e); } if (propertyDescriptor == null) { throw new ReferenceAttributeDoesntExistException("Requested attribute: '" + referenceName + "' does not exist " + "on class: '" + bo.getClass().getName() + "'."); } // get the class of the attribute name Class referenceClass = getBusinessObjectAttributeClass(bo.getClass(), referenceName); if (referenceClass == null) { referenceClass = propertyDescriptor.getPropertyType(); } // make sure the class of the attribute descends from BusinessObject, // otherwise throw an exception if (!PersistableBusinessObject.class.isAssignableFrom(referenceClass)) { throw new ObjectNotABusinessObjectRuntimeException("Attribute requested (" + referenceName + ") is of class: " + "'" + referenceClass.getName() + "' and is not a " + "descendent of BusinessObject. Only descendents of BusinessObject " + "can be used."); } EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(bo.getClass()); ObjectDescriptor objectDescriptor = descriptor.getObjectDescriptorByName(referenceName); if (objectDescriptor == null) { throw new ReferenceAttributeNotAnOjbReferenceException( "Attribute requested (" + referenceName + ") is not listed " + "in OJB as a reference-descriptor for class: '" + bo.getClass().getName() + "'"); } // get the list of the foreign-keys for this reference-descriptor List fkFields = objectDescriptor.getForeignKeyFields(); Iterator fkIterator = fkFields.iterator(); // walk through the list of the foreign keys, get their types while (fkIterator.hasNext()) { // get the field name of the fk & pk field String fkFieldName = (String) fkIterator.next(); // get the value for the fk field Object fkFieldValue = null; try { fkFieldValue = PropertyUtils.getSimpleProperty(bo, fkFieldName); } // if we cant retrieve the field value, then // it doesnt have a value catch (IllegalAccessException e) { return false; } catch (InvocationTargetException e) { return false; } catch (NoSuchMethodException e) { return false; } // test the value if (fkFieldValue == null) { return false; } else if (String.class.isAssignableFrom(fkFieldValue.getClass())) { if (StringUtils.isBlank((String) fkFieldValue)) { return false; } } } return allFkeysHaveValues; }
From source file:org.kuali.rice.krad.service.impl.PersistenceServiceOjbImpl.java
/** * * @see org.kuali.rice.krad.service.PersistenceService#allForeignKeyValuesPopulatedForReference(org.kuali.rice.krad.bo.BusinessObject, * java.lang.String)/*from w w w. j a va 2s .c o m*/ */ @Override public boolean allForeignKeyValuesPopulatedForReference(PersistableBusinessObject bo, String referenceName) { boolean allFkeysHaveValues = true; // yelp if nulls were passed in if (bo == null) { throw new IllegalArgumentException("The Class passed in for the BusinessObject argument was null."); } if (StringUtils.isBlank(referenceName)) { throw new IllegalArgumentException( "The String passed in for the referenceName argument was null or empty."); } PropertyDescriptor propertyDescriptor = null; // make sure the attribute exists at all, throw exception if not try { propertyDescriptor = PropertyUtils.getPropertyDescriptor(bo, referenceName); } catch (Exception e) { throw new RuntimeException(e); } if (propertyDescriptor == null) { throw new ReferenceAttributeDoesntExistException("Requested attribute: '" + referenceName + "' does not exist " + "on class: '" + bo.getClass().getName() + "'."); } // get the class of the attribute name Class referenceClass = getBusinessObjectAttributeClass(bo.getClass(), referenceName); if (referenceClass == null) { referenceClass = propertyDescriptor.getPropertyType(); } // make sure the class of the attribute descends from BusinessObject, // otherwise throw an exception if (!PersistableBusinessObject.class.isAssignableFrom(referenceClass)) { throw new ObjectNotABusinessObjectRuntimeException("Attribute requested (" + referenceName + ") is of class: " + "'" + referenceClass.getName() + "' and is not a " + "descendent of BusinessObject. Only descendents of BusinessObject " + "can be used."); } // make sure the attribute designated is listed as a // reference-descriptor // on the clazz specified, otherwise throw an exception (OJB); ClassDescriptor classDescriptor = getClassDescriptor(bo.getClass()); ObjectReferenceDescriptor referenceDescriptor = classDescriptor .getObjectReferenceDescriptorByName(referenceName); if (referenceDescriptor == null) { throw new ReferenceAttributeNotAnOjbReferenceException( "Attribute requested (" + referenceName + ") is not listed " + "in OJB as a reference-descriptor for class: '" + bo.getClass().getName() + "'"); } // get the list of the foreign-keys for this reference-descriptor // (OJB) Vector fkFields = referenceDescriptor.getForeignKeyFields(); Iterator fkIterator = fkFields.iterator(); // walk through the list of the foreign keys, get their types while (fkIterator.hasNext()) { // get the field name of the fk & pk field String fkFieldName = (String) fkIterator.next(); // get the value for the fk field Object fkFieldValue = null; try { fkFieldValue = PropertyUtils.getSimpleProperty(bo, fkFieldName); } // if we cant retrieve the field value, then // it doesnt have a value catch (IllegalAccessException e) { return false; } catch (InvocationTargetException e) { return false; } catch (NoSuchMethodException e) { return false; } // test the value if (fkFieldValue == null) { return false; } else if (String.class.isAssignableFrom(fkFieldValue.getClass())) { if (StringUtils.isBlank((String) fkFieldValue)) { return false; } } } return allFkeysHaveValues; }
From source file:org.kuali.rice.krad.service.impl.PersistenceStructureServiceJpaImpl.java
/** * @see org.kuali.rice.krad.service.PersistenceService#getForeignKeyFieldsPopulationState(org.kuali.rice.krad.bo.BusinessObject, * java.lang.String)//from w ww .j av a2 s. c o m */ public ForeignKeyFieldsPopulationState getForeignKeyFieldsPopulationState(PersistableBusinessObject bo, String referenceName) { boolean allFieldsPopulated = true; boolean anyFieldsPopulated = false; List<String> unpopulatedFields = new ArrayList<String>(); // yelp if nulls were passed in if (bo == null) { throw new IllegalArgumentException("The Class passed in for the BusinessObject argument was null."); } if (StringUtils.isBlank(referenceName)) { throw new IllegalArgumentException( "The String passed in for the referenceName argument was null or empty."); } PropertyDescriptor propertyDescriptor = null; // make sure the attribute exists at all, throw exception if not try { propertyDescriptor = PropertyUtils.getPropertyDescriptor(bo, referenceName); } catch (Exception e) { throw new RuntimeException(e); } if (propertyDescriptor == null) { throw new ReferenceAttributeDoesntExistException("Requested attribute: '" + referenceName + "' does not exist " + "on class: '" + bo.getClass().getName() + "'."); } // get the class of the attribute name Class referenceClass = propertyDescriptor.getPropertyType(); // make sure the class of the attribute descends from BusinessObject, // otherwise throw an exception if (!PersistableBusinessObject.class.isAssignableFrom(referenceClass)) { throw new ObjectNotABusinessObjectRuntimeException("Attribute requested (" + referenceName + ") is of class: " + "'" + referenceClass.getName() + "' and is not a " + "descendent of BusinessObject. Only descendents of BusinessObject " + "can be used."); } EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(bo.getClass()); ObjectDescriptor objectDescriptor = descriptor.getObjectDescriptorByName(referenceName); if (objectDescriptor == null) { throw new ReferenceAttributeNotAnOjbReferenceException( "Attribute requested (" + referenceName + ") is not listed " + "in OJB as a reference-descriptor for class: '" + bo.getClass().getName() + "'"); } List<String> fkFields = objectDescriptor.getForeignKeyFields(); Iterator fkIterator = fkFields.iterator(); // walk through the list of the foreign keys, get their types while (fkIterator.hasNext()) { // get the field name of the fk & pk field String fkFieldName = (String) fkIterator.next(); // get the value for the fk field Object fkFieldValue = null; try { fkFieldValue = PropertyUtils.getSimpleProperty(bo, fkFieldName); } // abort if the value is not retrievable catch (Exception e) { throw new RuntimeException(e); } // test the value if (fkFieldValue == null) { allFieldsPopulated = false; unpopulatedFields.add(fkFieldName); } else if (fkFieldValue instanceof String) { if (StringUtils.isBlank((String) fkFieldValue)) { allFieldsPopulated = false; unpopulatedFields.add(fkFieldName); } else { anyFieldsPopulated = true; } } else { anyFieldsPopulated = true; } } // sanity check. if the flag for all fields populated is set, then // there should be nothing in the unpopulatedFields list if (allFieldsPopulated) { if (!unpopulatedFields.isEmpty()) { throw new RuntimeException("The flag is set that indicates all fields are populated, but there " + "are fields present in the unpopulatedFields list. This should never happen, and indicates " + "that the logic in this method is broken."); } } return new ForeignKeyFieldsPopulationState(allFieldsPopulated, anyFieldsPopulated, unpopulatedFields); }
From source file:org.kuali.rice.krad.service.impl.PersistenceStructureServiceOjbImpl.java
/** * @see org.kuali.rice.krad.service.PersistenceService#getForeignKeyFieldsPopulationState(org.kuali.rice.krad.bo.BusinessObject, * java.lang.String)//from w w w . j a v a 2s .com */ @Override public ForeignKeyFieldsPopulationState getForeignKeyFieldsPopulationState(PersistableBusinessObject bo, String referenceName) { boolean allFieldsPopulated = true; boolean anyFieldsPopulated = false; List<String> unpopulatedFields = new ArrayList<String>(); // yelp if nulls were passed in if (bo == null) { throw new IllegalArgumentException("The Class passed in for the BusinessObject argument was null."); } if (StringUtils.isBlank(referenceName)) { throw new IllegalArgumentException( "The String passed in for the referenceName argument was null or empty."); } PropertyDescriptor propertyDescriptor = null; // make sure the attribute exists at all, throw exception if not try { propertyDescriptor = PropertyUtils.getPropertyDescriptor(bo, referenceName); } catch (Exception e) { throw new RuntimeException(e); } if (propertyDescriptor == null) { throw new ReferenceAttributeDoesntExistException("Requested attribute: '" + referenceName + "' does not exist " + "on class: '" + bo.getClass().getName() + "'."); } // get the class of the attribute name Class referenceClass = propertyDescriptor.getPropertyType(); // make sure the class of the attribute descends from BusinessObject, // otherwise throw an exception if (!PersistableBusinessObject.class.isAssignableFrom(referenceClass)) { throw new ObjectNotABusinessObjectRuntimeException("Attribute requested (" + referenceName + ") is of class: " + "'" + referenceClass.getName() + "' and is not a " + "descendent of BusinessObject. Only descendents of BusinessObject " + "can be used."); } // make sure the attribute designated is listed as a // reference-descriptor // on the clazz specified, otherwise throw an exception (OJB); ClassDescriptor classDescriptor = getClassDescriptor(bo.getClass()); // This block is a combination of legacy and jpa ObjectReferenceDescriptor referenceDescriptor = classDescriptor .getObjectReferenceDescriptorByName(referenceName); if (referenceDescriptor == null) { throw new ReferenceAttributeNotAnOjbReferenceException( "Attribute requested (" + referenceName + ") is not listed " + "in OJB as a reference-descriptor for class: '" + bo.getClass().getName() + "'"); } // get the list of the foreign-keys for this reference-descriptor Vector fkFieldsLegacy = referenceDescriptor.getForeignKeyFields(); Iterator fkIteratorLegacy = fkFieldsLegacy.iterator(); // walk through the list of the foreign keys, get their types while (fkIteratorLegacy.hasNext()) { // get the field name of the fk & pk field String fkFieldName = (String) fkIteratorLegacy.next(); // get the value for the fk field Object fkFieldValue = null; try { fkFieldValue = PropertyUtils.getSimpleProperty(bo, fkFieldName); } // abort if the value is not retrievable catch (Exception e) { throw new RuntimeException(e); } // test the value if (fkFieldValue == null) { allFieldsPopulated = false; unpopulatedFields.add(fkFieldName); } else if (fkFieldValue instanceof String) { if (StringUtils.isBlank((String) fkFieldValue)) { allFieldsPopulated = false; unpopulatedFields.add(fkFieldName); } else { anyFieldsPopulated = true; } } else { anyFieldsPopulated = true; } } // sanity check. if the flag for all fields populated is set, then // there should be nothing in the unpopulatedFields list if (allFieldsPopulated) { if (!unpopulatedFields.isEmpty()) { throw new RuntimeException("The flag is set that indicates all fields are populated, but there " + "are fields present in the unpopulatedFields list. This should never happen, and indicates " + "that the logic in this method is broken."); } } return new ForeignKeyFieldsPopulationState(allFieldsPopulated, anyFieldsPopulated, unpopulatedFields); }
From source file:org.kuali.rice.krad.service.impl.PostProcessorServiceImpl.java
/** * Logs further details of OptimisticLockExceptions, using the given depth value to limit recursion Just In Case * * @param depth//w w w . j a v a 2 s .c o m * @param t */ private void logOptimisticDetails(int depth, Throwable t) { if ((depth > 0) && (t != null)) { Object sourceObject = null; boolean optLockException = false; if (t instanceof javax.persistence.OptimisticLockException) { sourceObject = ((javax.persistence.OptimisticLockException) t).getEntity(); optLockException = true; } else if (t instanceof OptimisticLockingFailureException) { sourceObject = ((OptimisticLockingFailureException) t).getMessage(); optLockException = true; } else if (t.getClass().getName().equals("org.apache.ojb.broker.OptimisticLockException")) { try { sourceObject = PropertyUtils.getSimpleProperty(t, "sourceObject"); } catch (Exception ex) { LOG.warn("Unable to retrieve source object from OJB OptimisticLockException", ex); } optLockException = true; } if (optLockException) { if (sourceObject != null) { if (sourceObject instanceof String) { LOG.error("source of OptimisticLockException Unknown. Message: " + sourceObject); } else { LOG.error("source of OptimisticLockException = " + sourceObject.getClass().getName() + " ::= " + sourceObject); } } } else { Throwable cause = t.getCause(); if (cause != t) { logOptimisticDetails(--depth, cause); } } } }