Example usage for org.springframework.data.mapping PersistentEntity isIdProperty

List of usage examples for org.springframework.data.mapping PersistentEntity isIdProperty

Introduction

In this page you can find the example usage for org.springframework.data.mapping PersistentEntity isIdProperty.

Prototype

boolean isIdProperty(PersistentProperty<?> property);

Source Link

Document

Returns whether the given PersistentProperty is the id property of the entity.

Usage

From source file:org.lightadmin.core.persistence.support.DynamicDomainObjectMerger.java

/**
 * Merges the given target object into the source one.
 *
 * @param from       can be {@literal null}.
 * @param target     can be {@literal null}.
 * @param nullPolicy how to handle {@literal null} values in the source object.
 *//*from   w  w  w. j  a va 2s. co m*/
@Override
public void merge(final Object from, final Object target, final NullHandlingPolicy nullPolicy) {
    if (from == null || target == null) {
        return;
    }

    final BeanWrapper fromWrapper = beanWrapper(from);
    final BeanWrapper targetWrapper = beanWrapper(target);

    final DomainTypeAdministrationConfiguration domainTypeAdministrationConfiguration = configuration
            .forManagedDomainType(target.getClass());
    final PersistentEntity<?, ?> entity = domainTypeAdministrationConfiguration.getPersistentEntity();

    entity.doWithProperties(new SimplePropertyHandler() {
        @Override
        public void doWithPersistentProperty(PersistentProperty<?> persistentProperty) {
            Object sourceValue = fromWrapper.getPropertyValue(persistentProperty.getName());
            Object targetValue = targetWrapper.getPropertyValue(persistentProperty.getName());

            if (entity.isIdProperty(persistentProperty)) {
                return;
            }

            if (nullSafeEquals(sourceValue, targetValue)) {
                return;
            }

            if (propertyIsHiddenInFormView(persistentProperty, domainTypeAdministrationConfiguration)) {
                return;
            }

            if (nullPolicy == APPLY_NULLS || sourceValue != null) {
                targetWrapper.setPropertyValue(persistentProperty.getName(), sourceValue);
            }
        }
    });

    entity.doWithAssociations(new SimpleAssociationHandler() {
        @Override
        @SuppressWarnings("unchecked")
        public void doWithAssociation(Association<? extends PersistentProperty<?>> association) {
            PersistentProperty<?> persistentProperty = association.getInverse();

            Object fromValue = fromWrapper.getPropertyValue(persistentProperty.getName());
            Object targetValue = targetWrapper.getPropertyValue(persistentProperty.getName());

            if (propertyIsHiddenInFormView(persistentProperty, domainTypeAdministrationConfiguration)) {
                return;
            }

            if ((fromValue == null && nullPolicy == APPLY_NULLS)) {
                targetWrapper.setPropertyValue(persistentProperty.getName(), fromValue);
            }

            if (persistentProperty.isCollectionLike()) {
                Collection<Object> sourceCollection = (Collection) fromValue;
                Collection<Object> targetCollection = (Collection) targetValue;

                Collection<Object> candidatesForAddition = candidatesForAddition(sourceCollection,
                        targetCollection, persistentProperty);
                Collection<Object> candidatesForRemoval = candidatesForRemoval(sourceCollection,
                        targetCollection, persistentProperty);

                removeReferencedItems(targetCollection, candidatesForRemoval);

                addReferencedItems(targetCollection, candidatesForAddition);

                return;
            }

            if (!nullSafeEquals(fromValue, targetWrapper.getPropertyValue(persistentProperty.getName()))) {
                targetWrapper.setPropertyValue(persistentProperty.getName(), fromValue);
            }
        }
    });
}