Example usage for org.springframework.data.mapping.context MappingContext getPersistentEntity

List of usage examples for org.springframework.data.mapping.context MappingContext getPersistentEntity

Introduction

In this page you can find the example usage for org.springframework.data.mapping.context MappingContext getPersistentEntity.

Prototype

@Nullable
E getPersistentEntity(P persistentProperty);

Source Link

Document

Returns the PersistentEntity mapped by the given PersistentProperty .

Usage

From source file:io.github.carlomicieli.springbooks.test.AbstractMongoDbTests.java

protected void ensureDocumentId(Object savedObject, Object id) {
    ConversionService conversionService = mongoTemplate.getConverter().getConversionService();
    MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext = mongoTemplate
            .getConverter().getMappingContext();
    MongoPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(savedObject.getClass());

    MongoPersistentProperty idProp = persistentEntity == null ? null : persistentEntity.getIdProperty();
    if (idProp == null) {
        return;//from  w  ww . jav a 2 s .c  om
    }

    BeanWrapper<PersistentEntity<Object, ?>, Object> wrapper = BeanWrapper.create(savedObject,
            conversionService);
    Object idValue = wrapper.getProperty(idProp, idProp.getType(), true);

    if (idValue != null) {
        return;
    }

    wrapper.setProperty(idProp, id);
}

From source file:org.lightadmin.core.config.bootstrap.parsing.validation.PersistentFieldMetadataValidator.java

@Override
@SuppressWarnings("unchecked")
public Collection<? extends DomainConfigurationProblem> validateFieldMetadata(
        PersistentFieldMetadata fieldMetadata, Class<?> domainType,
        DomainConfigurationValidationContext validationContext) {
    MappingContext mappingContext = validationContext.getMappingContext();
    final LightAdminConfiguration lightAdminConfiguration = validationContext.getLightAdminConfiguration();

    final PersistentEntity persistentEntity = mappingContext.getPersistentEntity(domainType);

    PersistentProperty persistentProperty = persistentEntity.getPersistentProperty(fieldMetadata.getField());

    if (persistentProperty == null) {
        return newArrayList(validationContext.notPersistableFieldProblem(fieldMetadata.getName()));
    }/*w  w w  . jav a2s  . c om*/

    if (!isSupportedAttributeType(PersistentPropertyType.forPersistentProperty(persistentProperty))) {
        return newArrayList(validationContext.notSupportedTypeFieldProblem(fieldMetadata.getName()));
    }

    if (!isOfFileReferenceType(persistentProperty)) {
        return emptyList();
    }

    Annotation annotation = persistentProperty.findAnnotation(FileReference.class);

    FileReference fileReference = (FileReference) annotation;

    if (isEmpty(fileReference.baseDirectory())) {
        if (lightAdminConfiguration.getFileStorageDirectory() != null) {
            return emptyList();
        }
        return newArrayList(
                validationContext.missingBaseDirectoryInFileReferenceProblem(fieldMetadata.getName()));
    }

    final File directory = getFile(fileReference.baseDirectory());
    if (directory.exists() && directory.isDirectory()) {
        return emptyList();
    }

    return newArrayList(validationContext.missingBaseDirectoryInFileReferenceProblem(fieldMetadata.getName()));
}

From source file:org.springframework.data.elasticsearch.core.MappingBuilder.java

private static void mapParentField(Class clazz,
        MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext,
        XContentBuilder xContentBuilder) throws IOException {
    ParentId parentId = getParentIdAnnotation(clazz.getDeclaredFields());
    if (parentId != null) {
        ElasticsearchPersistentEntity<?> parentPersistentEntity = mappingContext
                .getPersistentEntity(parentId.type());
        if (parentPersistentEntity != null) {
            xContentBuilder.startObject(PARENT_FIELD)
                    .field(PARENT_FIELD_TYPE, parentPersistentEntity.getIndexType()).endObject();
        }/*w  w w. j a  v  a  2 s . c  o  m*/
    }
}