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

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

Introduction

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

Prototype

void doWithProperties(SimplePropertyHandler handler);

Source Link

Usage

From source file:org.lightadmin.core.persistence.repository.event.FileManipulationRepositoryEventListener.java

@Override
protected void onAfterDelete(final Object entity) {
    PersistentEntity persistentEntity = persistentEntityFor(entity.getClass());

    persistentEntity.doWithProperties(new PersistentPropertyFileDeletionHandler(entity));
}

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.
 *///w  ww  . j ava  2  s. 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.lightadmin.core.persistence.repository.event.FileManipulationRepositoryEventListener.java

@Override
protected void onBeforeSave(Object entity) {
    PersistentEntity persistentEntity = persistentEntityFor(entity.getClass());

    NotEmptyFileReferencePropertiesCollector propertyValueCollector = new NotEmptyFileReferencePropertiesCollector(
            entity);//  www . j  a  v  a 2 s  .c o m
    persistentEntity.doWithProperties(propertyValueCollector);

    FileReferenceProperties fileReferenceProperties = propertyValueCollector.getFilePropertyValues();

    persistentEntity.doWithProperties(new FileReferencePropertiesValueEraser(entity, fileReferenceProperties));

    fileReferencePropertiesContext.set(fileReferenceProperties);
}

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.j a  v a  2s  .c om*/
        public void doWithPersistentProperty(PersistentProperty<?> property) {
            if (PersistentPropertyType.forPersistentProperty(property) == FILE) {
                result.add(property);
            }
        }
    });
    return result;
}

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 .  ja va  2s . c o m
            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.repository.event.FileManipulationRepositoryEventListener.java

@Override
protected void onAfterSave(Object entity) {
    PersistentEntity persistentEntity = persistentEntityFor(entity.getClass());
    DynamicJpaRepository repository = repositoryFor(entity.getClass());

    FileReferenceProperties fileReferenceProperties = fileReferencePropertiesContext.get();
    try {//from  w ww  . jav  a2s .c  o  m
        persistentEntity
                .doWithProperties(new FileReferencePropertiesSaveHandler(entity, fileReferenceProperties));

        repository.save(entity);
    } finally {
        fileReferencePropertiesContext.remove();
    }
}

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  . ja va2s  . 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.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  . j a v  a 2s.  c  o m
        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  ww .  jav  a  2 s  .  c  o  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();
}