Example usage for org.springframework.data.mapping SimpleAssociationHandler SimpleAssociationHandler

List of usage examples for org.springframework.data.mapping SimpleAssociationHandler SimpleAssociationHandler

Introduction

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

Prototype

SimpleAssociationHandler

Source Link

Usage

From source file:org.lightadmin.core.web.json.DomainTypeToJsonMetadataConverter.java

@Override
public JsonConfigurationMetadata convert(PersistentEntity persistentEntity) {
    final JsonConfigurationMetadata jsonConfigurationMetadata = new JsonConfigurationMetadata(
            persistentEntity.getName(),/*w  w w .  jav a  2s .com*/
            globalAdministrationConfiguration.isManagedDomainType(persistentEntity.getType()));

    persistentEntity.doWithProperties(new SimplePropertyHandler() {
        @Override
        public void doWithPersistentProperty(PersistentProperty<?> persistentProperty) {
            jsonConfigurationMetadata.addPersistentProperty(persistentProperty);
        }
    });

    persistentEntity.doWithAssociations(new SimpleAssociationHandler() {
        @Override
        public void doWithAssociation(Association<? extends PersistentProperty<?>> association) {
            jsonConfigurationMetadata.addAssociationProperty(association,
                    associationRestLinkTemplate(association.getInverse()));
        }
    });

    if (!globalAdministrationConfiguration.isManagedDomainType(persistentEntity.getType())) {
        return jsonConfigurationMetadata;
    }

    DomainTypeAdministrationConfiguration configuration = globalAdministrationConfiguration
            .forManagedDomainType(persistentEntity.getType());

    List<DomainConfigurationUnitType> unitTypes = newArrayList(LIST_VIEW, FORM_VIEW, SHOW_VIEW, QUICK_VIEW);

    for (DomainConfigurationUnitType unitType : unitTypes) {
        Set<FieldMetadata> fieldForUnit = configuration.fieldsForUnit(unitType);

        for (FieldMetadata field : fieldForUnit) {
            if (persistentFieldMetadataPredicate().apply(field)) {
                addPersistentProperty((PersistentFieldMetadata) field, unitType.toString(),
                        jsonConfigurationMetadata);
            }

            if (customFieldMetadataPredicate().apply(field)) {
                jsonConfigurationMetadata.addDynamicProperty((CustomFieldMetadata) field, unitType.toString());
            }

            if (transientFieldMetadataPredicate().apply(field)) {
                jsonConfigurationMetadata.addDynamicProperty((TransientFieldMetadata) field,
                        unitType.toString());
            }
        }
    }

    return jsonConfigurationMetadata;
}

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   www.  j a v a  2  s .  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);
            }
        }
    });
}

From source file:org.lightadmin.core.config.bootstrap.GlobalAdministrationConfigurationFactoryBean.java

private void registerAssociationDomainTypeConfigurations(
        DomainTypeAdministrationConfiguration domainTypeAdministrationConfiguration,
        final GlobalAdministrationConfiguration globalAdministrationConfiguration) {
    PersistentEntity persistentEntity = domainTypeAdministrationConfiguration.getPersistentEntity();

    persistentEntity.doWithAssociations(new SimpleAssociationHandler() {
        @Override//from w w w. j  a  v a 2s.c om
        public void doWithAssociation(Association<? extends PersistentProperty<?>> association) {
            Class<?> associationDomainType = association.getInverse().getActualType();

            if (!isManagedEntity(associationDomainType)
                    && repositories.hasRepositoryFor(associationDomainType)) {
                DomainTypeBasicConfiguration associationTypeConfiguration = domainTypeAdministrationConfigurationFactory
                        .createNonManagedDomainTypeConfiguration(associationDomainType);
                globalAdministrationConfiguration
                        .registerNonDomainTypeConfiguration(associationTypeConfiguration);
            }
        }
    });
}

From source file:org.lightadmin.core.web.support.DynamicPersistentEntityResourceProcessor.java

private List<Association> findLinkableAssociations(PersistentEntity persistentEntity) {
    final List<Association> result = newArrayList();
    persistentEntity.doWithAssociations(new SimpleAssociationHandler() {
        @Override//from w  w w . ja v  a2  s.  com
        public void doWithAssociation(Association<? extends PersistentProperty<?>> association) {
            if (associationLinks.isLinkableAssociation(association.getInverse())) {
                result.add(association);
            }
        }
    });
    return result;
}

From source file:org.springframework.data.rest.webmvc.alps.RootResourceInformationToAlpsDescriptorConverter.java

private List<Descriptor> buildPropertyDescriptors(final Class<?> type, String baseRel) {

    final PersistentEntity<?, ?> entity = persistentEntities.getPersistentEntity(type);
    final List<Descriptor> propertyDescriptors = new ArrayList<Descriptor>();
    final JacksonMetadata jackson = new JacksonMetadata(mapper, type);
    final ResourceMetadata metadata = associations.getMetadataFor(entity.getType());

    entity.doWithProperties(new SimplePropertyHandler() {

        @Override/*from  w w w .ja  va  2 s .  c om*/
        public void doWithPersistentProperty(PersistentProperty<?> property) {

            BeanPropertyDefinition propertyDefinition = jackson.getDefinitionFor(property);
            ResourceMapping propertyMapping = metadata.getMappingFor(property);

            if (propertyDefinition != null) {

                if (property.isIdProperty() && !configuration.isIdExposedFor(property.getOwner().getType())) {
                    return;
                }

                propertyDescriptors.add(//
                        descriptor(). //
                type(Type.SEMANTIC).//
                name(propertyDefinition.getName()).//
                doc(getDocFor(propertyMapping.getDescription(), property)).//
                build());
            }
        }
    });

    entity.doWithAssociations(new SimpleAssociationHandler() {

        @Override
        public void doWithAssociation(Association<? extends PersistentProperty<?>> association) {

            PersistentProperty<?> property = association.getInverse();

            if (!jackson.isExported(property) || !associations.isLinkableAssociation(property)) {
                return;
            }

            ResourceMapping mapping = metadata.getMappingFor(property);

            DescriptorBuilder builder = descriptor().//
            name(mapping.getRel()).doc(getDocFor(mapping.getDescription()));

            ResourceMetadata targetTypeMetadata = associations.getMetadataFor(property.getActualType());

            String href = ProfileController.getPath(configuration, targetTypeMetadata) + "#"
                    + getRepresentationDescriptorId(targetTypeMetadata);

            Link link = new Link(href).withSelfRel();

            builder.//
            type(Type.SAFE).//
            rt(link.getHref());

            propertyDescriptors.add(builder.build());
        }
    });

    return propertyDescriptors;
}

From source file:org.lightadmin.core.config.domain.unit.processor.EmptyConfigurationUnitPostProcessor.java

private FieldSetConfigurationUnit fieldSetUnitWithPersistentFields(final Class<?> domainType,
        DomainConfigurationUnitType configurationUnitType) {
    final FieldSetConfigurationUnitBuilder fieldSetConfigurationUnitBuilder = new GenericFieldSetConfigurationUnitBuilder(
            domainType, configurationUnitType);

    PersistentEntity persistentEntity = getPersistentEntity(domainType);

    persistentEntity.doWithProperties(new SimplePropertyHandler() {
        @Override/*from   w  w w .  java  2 s.  co m*/
        public void doWithPersistentProperty(PersistentProperty<?> property) {
            addField(property, fieldSetConfigurationUnitBuilder);
        }
    });

    persistentEntity.doWithAssociations(new SimpleAssociationHandler() {
        @Override
        public void doWithAssociation(Association<? extends PersistentProperty<?>> association) {
            addField(association.getInverse(), fieldSetConfigurationUnitBuilder);
        }
    });

    return fieldSetConfigurationUnitBuilder.build();
}