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

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

Introduction

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

Prototype

SimplePropertyHandler

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(),//from  ww  w. ja v a2 s.  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.
 *//*www.  j a  va2s  .  c  om*/
@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.springframework.data.rest.webmvc.json.DomainObjectReader.java

/**
 * Reads the given source node onto the given target object and applies PUT semantics, i.e. explicitly
 * /*from w ww .  j a  va  2 s  .c o m*/
 * @param source must not be {@literal null}.
 * @param target must not be {@literal null}.
 * @param mapper
 * @return
 */
public <T> T readPut(final ObjectNode source, T target, final ObjectMapper mapper) {

    Assert.notNull(source, "ObjectNode must not be null!");
    Assert.notNull(target, "Existing object instance must not be null!");
    Assert.notNull(mapper, "ObjectMapper must not be null!");

    Class<? extends Object> type = target.getClass();

    final PersistentEntity<?, ?> entity = entities.getPersistentEntity(type);

    Assert.notNull(entity, "No PersistentEntity found for ".concat(type.getName()).concat("!"));

    final MappedProperties properties = getJacksonProperties(entity, mapper);

    entity.doWithProperties(new SimplePropertyHandler() {

        /*
         * (non-Javadoc)
         * @see org.springframework.data.mapping.SimplePropertyHandler#doWithPersistentProperty(org.springframework.data.mapping.PersistentProperty)
         */
        @Override
        public void doWithPersistentProperty(PersistentProperty<?> property) {

            if (property.isIdProperty() || property.isVersionProperty()) {
                return;
            }

            String mappedName = properties.getMappedName(property);

            boolean isMappedProperty = mappedName != null;
            boolean noValueInSource = !source.has(mappedName);

            if (isMappedProperty && noValueInSource) {
                source.putNull(mappedName);
            }
        }
    });

    return merge(source, target, mapper);
}

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

private List<PersistentProperty> findPersistentFileProperties(PersistentEntity persistentEntity) {
    final List<PersistentProperty> result = newArrayList();
    persistentEntity.doWithProperties(new SimplePropertyHandler() {
        @Override/*from w  w  w .  ja  v a  2  s  . c  om*/
        public void doWithPersistentProperty(PersistentProperty<?> property) {
            if (PersistentPropertyType.forPersistentProperty(property) == FILE) {
                result.add(property);
            }
        }
    });
    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/*  w  ww  . ja  v a2 s .  com*/
        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  ww  w.  j  ava2s  .  com
        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();
}