Example usage for org.springframework.beans PropertyAccessorUtils getLastNestedPropertySeparatorIndex

List of usage examples for org.springframework.beans PropertyAccessorUtils getLastNestedPropertySeparatorIndex

Introduction

In this page you can find the example usage for org.springframework.beans PropertyAccessorUtils getLastNestedPropertySeparatorIndex.

Prototype

public static int getLastNestedPropertySeparatorIndex(String propertyPath) 

Source Link

Document

Determine the first nested property separator in the given property path, ignoring dots in keys (like "map[my.key]").

Usage

From source file:net.yasion.common.core.bean.wrapper.impl.ExtendedBeanWrapperImpl.java

/**
 * Get the last component of the path. Also works if not nested.
 * /*from w w  w  .  j a  v a  2s .c  o m*/
 * @param bw
 *            BeanWrapper to work on
 * @param nestedPath
 *            property path we know is nested
 * @return last component of the path (the property on the target bean)
 */
private String getFinalPath(BeanWrapper bw, String nestedPath) {
    if (bw == this) {
        return nestedPath;
    }
    return nestedPath.substring(PropertyAccessorUtils.getLastNestedPropertySeparatorIndex(nestedPath) + 1);
}

From source file:org.kuali.rice.kim.impl.identity.PersonServiceImpl.java

private boolean isPersonProperty(Object bo, String propertyName) {
    try {//from www.  jav a  2s .c  om
        if (PropertyAccessorUtils.isNestedOrIndexedProperty(propertyName) // is a nested property
                && !StringUtils.contains(propertyName, "add.")) {// exclude add line properties (due to path parsing problems in PropertyUtils.getPropertyType)
            int lastIndex = PropertyAccessorUtils.getLastNestedPropertySeparatorIndex(propertyName);
            String propertyTypeName = lastIndex != -1 ? StringUtils.substring(propertyName, 0, lastIndex)
                    : StringUtils.EMPTY;
            Class<?> type = PropertyUtils.getPropertyType(bo, propertyTypeName);
            // property type indicates a Person object
            if (type != null) {
                return Person.class.isAssignableFrom(type);
            }
            LOG.warn("Unable to determine type of nested property: " + bo.getClass().getName() + " / "
                    + propertyName);
        }
    } catch (Exception ex) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Unable to determine if property on " + bo.getClass().getName() + " to a person object: "
                    + propertyName, ex);
        }
    }
    return false;
}

From source file:org.kuali.rice.kim.impl.identity.PersonServiceImpl.java

/**
 * @see org.kuali.rice.kim.api.identity.PersonService#resolvePrincipalNamesToPrincipalIds(org.kuali.rice.krad.bo.BusinessObject, java.util.Map)
 *//*from   w  ww  .  j a v  a  2  s  .  com*/
@Override
@SuppressWarnings("unchecked")
public Map<String, String> resolvePrincipalNamesToPrincipalIds(BusinessObject businessObject,
        Map<String, String> fieldValues) {
    if (fieldValues == null) {
        return null;
    }
    if (businessObject == null) {
        return fieldValues;
    }
    StringBuffer resolvedPrincipalIdPropertyName = new StringBuffer();
    // save off all criteria which are not references to Person properties
    // leave person properties out so they can be resolved and replaced by this method
    Map<String, String> processedFieldValues = getNonPersonSearchCriteria(businessObject, fieldValues);
    for (String propertyName : fieldValues.keySet()) {
        if (!StringUtils.isBlank(fieldValues.get(propertyName)) // property has a value
                && isPersonProperty(businessObject, propertyName) // is a property on a Person object
        ) {
            // strip off the prefix on the property
            int lastPropertyIndex = PropertyAccessorUtils.getLastNestedPropertySeparatorIndex(propertyName);
            String personPropertyName = lastPropertyIndex != -1
                    ? StringUtils.substring(propertyName, lastPropertyIndex + 1)
                    : propertyName;
            // special case - the user ID
            if (StringUtils.equals(KIMPropertyConstants.Person.PRINCIPAL_NAME, personPropertyName)) {
                Class targetBusinessObjectClass = null;
                BusinessObject targetBusinessObject = null;
                resolvedPrincipalIdPropertyName.setLength(0); // clear the buffer without requiring a new object allocation on each iteration
                // get the property name up until the ".principalName"
                // this should be a reference to the Person object attached to the BusinessObject
                String personReferenceObjectPropertyName = lastPropertyIndex != -1
                        ? StringUtils.substring(propertyName, 0, lastPropertyIndex)
                        : StringUtils.EMPTY;
                // check if the person was nested within another BO under the master BO.  If so, go up one more level
                // otherwise, use the passed in BO class as the target class
                if (PropertyAccessorUtils.isNestedOrIndexedProperty(personReferenceObjectPropertyName)) {
                    int lastTargetIndex = PropertyAccessorUtils
                            .getLastNestedPropertySeparatorIndex(personReferenceObjectPropertyName);
                    String targetBusinessObjectPropertyName = lastTargetIndex != -1
                            ? StringUtils.substring(personReferenceObjectPropertyName, 0, lastTargetIndex)
                            : StringUtils.EMPTY;
                    DataObjectWrapper<BusinessObject> wrapper = KradDataServiceLocator.getDataObjectService()
                            .wrap(businessObject);
                    targetBusinessObject = (BusinessObject) wrapper
                            .getPropertyValueNullSafe(targetBusinessObjectPropertyName);
                    if (targetBusinessObject != null) {
                        targetBusinessObjectClass = targetBusinessObject.getClass();
                        resolvedPrincipalIdPropertyName.append(targetBusinessObjectPropertyName).append(".");
                    } else {
                        LOG.error("Could not find target property '" + propertyName + "' in class "
                                + businessObject.getClass().getName() + ". Property value was null.");
                    }
                } else { // not a nested Person property
                    targetBusinessObjectClass = businessObject.getClass();
                    targetBusinessObject = businessObject;
                }

                if (targetBusinessObjectClass != null) {
                    // use the relationship metadata in the KNS to determine the property on the
                    // host business object to put back into the map now that the principal ID
                    // (the value stored in application tables) has been resolved
                    int lastIndex = PropertyAccessorUtils
                            .getLastNestedPropertySeparatorIndex(personReferenceObjectPropertyName);
                    String propName = lastIndex != -1
                            ? StringUtils.substring(personReferenceObjectPropertyName, lastIndex + 1)
                            : personReferenceObjectPropertyName;
                    DataObjectRelationship rel = getBusinessObjectMetaDataService()
                            .getBusinessObjectRelationship(targetBusinessObject, propName);
                    if (rel != null) {
                        String sourcePrimitivePropertyName = rel
                                .getParentAttributeForChildAttribute(KIMPropertyConstants.Person.PRINCIPAL_ID);
                        resolvedPrincipalIdPropertyName.append(sourcePrimitivePropertyName);
                        // get the principal - for translation of the principalName to principalId
                        String principalName = fieldValues.get(propertyName);
                        Principal principal = getIdentityService().getPrincipalByPrincipalName(principalName);
                        if (principal != null) {
                            processedFieldValues.put(resolvedPrincipalIdPropertyName.toString(),
                                    principal.getPrincipalId());
                        } else {
                            processedFieldValues.put(resolvedPrincipalIdPropertyName.toString(), null);
                            try {
                                // if the principalName is bad, then we need to clear out the Person object
                                // and base principalId property
                                // so that their values are no longer accidentally used or re-populate
                                // the object
                                KRADUtils.setObjectProperty(targetBusinessObject,
                                        resolvedPrincipalIdPropertyName.toString(), null);
                                KRADUtils.setObjectProperty(targetBusinessObject, propName, null);
                                KRADUtils.setObjectProperty(targetBusinessObject, propName + ".principalName",
                                        principalName);
                            } catch (Exception ex) {
                                LOG.error(
                                        "Unable to blank out the person object after finding that the person with the given principalName does not exist.",
                                        ex);
                            }
                        }
                    } else {
                        LOG.error("Missing relationship for " + propName + " on "
                                + targetBusinessObjectClass.getName());
                    }
                } else { // no target BO class - the code below probably will not work
                    processedFieldValues.put(resolvedPrincipalIdPropertyName.toString(), null);
                }
            }
            // if the property does not seem to match the definition of a Person property but it
            // does end in principalName then...
            // this is to handle the case where the user ID is on an ADD line - a case excluded from isPersonProperty()
        } else if (propertyName.endsWith("." + KIMPropertyConstants.Person.PRINCIPAL_NAME)) {
            // if we're adding to a collection and we've got the principalName; let's populate universalUser
            String principalName = fieldValues.get(propertyName);
            if (StringUtils.isNotEmpty(principalName)) {
                String containerPropertyName = propertyName;
                if (containerPropertyName.startsWith(KRADConstants.MAINTENANCE_ADD_PREFIX)) {
                    containerPropertyName = StringUtils.substringAfter(propertyName,
                            KRADConstants.MAINTENANCE_ADD_PREFIX);
                }
                // get the class of the object that is referenced by the property name
                // if this is not true then there's a principalName collection or primitive attribute
                // directly on the BO on the add line, so we just ignore that since something is wrong here
                if (PropertyAccessorUtils.isNestedOrIndexedProperty(containerPropertyName)) {
                    // the first part of the property is the collection name
                    String collectionName = StringUtils.substringBefore(containerPropertyName, ".");
                    // what is the class held by that collection?
                    // JHK: I don't like this.  This assumes that this method is only used by the maintenance
                    // document service.  If that will always be the case, this method should be moved over there.
                    Class<? extends BusinessObject> collectionBusinessObjectClass = getMaintenanceDocumentDictionaryService()
                            .getCollectionBusinessObjectClass(getMaintenanceDocumentDictionaryService()
                                    .getDocumentTypeName(businessObject.getClass()), collectionName);
                    if (collectionBusinessObjectClass != null) {
                        // we are adding to a collection; get the relationships for that object;
                        // is there one for personUniversalIdentifier?
                        List<DataObjectRelationship> relationships = getBusinessObjectMetaDataService()
                                .getBusinessObjectRelationships(collectionBusinessObjectClass);
                        // JHK: this seems like a hack - looking at all relationships for a BO does not guarantee that we get the right one
                        // JHK: why not inspect the objects like above?  Is it the property path problems because of the .add. portion?
                        for (DataObjectRelationship rel : relationships) {
                            String parentAttribute = rel.getParentAttributeForChildAttribute(
                                    KIMPropertyConstants.Person.PRINCIPAL_ID);
                            if (parentAttribute == null) {
                                continue;
                            }
                            // there is a relationship for personUserIdentifier; use that to find the universal user
                            processedFieldValues.remove(propertyName);
                            String fieldPrefix = StringUtils
                                    .substringBeforeLast(StringUtils.substringBeforeLast(propertyName,
                                            "." + KIMPropertyConstants.Person.PRINCIPAL_NAME), ".");
                            String relatedPrincipalIdPropertyName = fieldPrefix + "." + parentAttribute;
                            // KR-683 Special handling for extension objects
                            if (EXTENSION.equals(StringUtils.substringAfterLast(fieldPrefix, "."))
                                    && EXTENSION.equals(StringUtils.substringBefore(parentAttribute, "."))) {
                                relatedPrincipalIdPropertyName = fieldPrefix + "."
                                        + StringUtils.substringAfter(parentAttribute, ".");
                            }
                            String currRelatedPersonPrincipalId = processedFieldValues
                                    .get(relatedPrincipalIdPropertyName);
                            if (StringUtils.isBlank(currRelatedPersonPrincipalId)) {
                                Principal principal = getIdentityService()
                                        .getPrincipalByPrincipalName(principalName);
                                if (principal != null) {
                                    processedFieldValues.put(relatedPrincipalIdPropertyName,
                                            principal.getPrincipalId());
                                } else {
                                    processedFieldValues.put(relatedPrincipalIdPropertyName, null);
                                }
                            }
                        } // relationship loop
                    } else {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug(
                                    "Unable to determine class for collection referenced as part of property: "
                                            + containerPropertyName + " on "
                                            + businessObject.getClass().getName());
                        }
                    }
                } else {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Non-nested property ending with 'principalName': " + containerPropertyName
                                + " on " + businessObject.getClass().getName());
                    }
                }
            }
        }
    }
    return processedFieldValues;
}

From source file:org.kuali.rice.krad.uif.service.impl.ViewHelperServiceImpl.java

protected void linkAddedLine(Object model, String collectionPath, int addedIndex) {
    int lastSepIndex = PropertyAccessorUtils.getLastNestedPropertySeparatorIndex(collectionPath);
    if (lastSepIndex != -1) {
        String collectionParentPath = collectionPath.substring(0, lastSepIndex);
        Object parent = ObjectPropertyUtils.getPropertyValue(model, collectionParentPath);
        if (parent != null && getDataObjectService().supports(parent.getClass())) {
            DataObjectWrapper<?> wrappedParent = getDataObjectService().wrap(parent);
            String collectionName = collectionPath.substring(lastSepIndex + 1);
            wrappedParent.linkChanges(Sets.newHashSet(collectionName + "[" + addedIndex + "]"));
        }// w  ww  .  java  2 s .  c om
    }
}

From source file:org.kuali.rice.krad.util.KRADUtils.java

/**
 * Returns the prefix of a nested attribute name, or the empty string if the attribute name is not nested.
 *
 * @param attributeName/*from  w ww.  j  a  v a 2  s . com*/
 * @return everything BEFORE the last "." character in attributeName
 */
public static String getNestedAttributePrefix(String attributeName) {
    int lastIndex = PropertyAccessorUtils.getLastNestedPropertySeparatorIndex(attributeName);

    return lastIndex != -1 ? StringUtils.substring(attributeName, 0, lastIndex) : StringUtils.EMPTY;
}

From source file:org.kuali.rice.krad.util.KRADUtils.java

/**
 * Returns the primitive part of an attribute name string.
 *
 * @param attributeName/*from ww  w  .ja va  2 s  .  c  o  m*/
 * @return everything AFTER the last "." character in attributeName
 */
public static String getNestedAttributePrimitive(String attributeName) {
    int lastIndex = PropertyAccessorUtils.getLastNestedPropertySeparatorIndex(attributeName);

    return lastIndex != -1 ? StringUtils.substring(attributeName, lastIndex + 1) : attributeName;
}

From source file:org.springframework.beans.AbstractNestablePropertyAccessor.java

/**
 * Get the last component of the path. Also works if not nested.
 * @param pa property accessor to work on
 * @param nestedPath property path we know is nested
 * @return last component of the path (the property on the target bean)
 *//*from ww w.ja v  a  2 s  . c  o  m*/
protected String getFinalPath(AbstractNestablePropertyAccessor pa, String nestedPath) {
    if (pa == this) {
        return nestedPath;
    }
    return nestedPath.substring(PropertyAccessorUtils.getLastNestedPropertySeparatorIndex(nestedPath) + 1);
}