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

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

Introduction

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

Prototype

boolean isIdProperty();

Source Link

Document

Returns whether the property is a potential identifier property of the owning PersistentEntity .

Usage

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   www .j  a  v  a2s.  c om
 * @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.json.JsonConfigurationMetadata.java

private JsonConfigurationMetadata.Property newProperty(PersistentProperty persistentProperty, String title) {
    PersistentPropertyType type = PersistentPropertyType.forPersistentProperty(persistentProperty);
    return new JsonConfigurationMetadata.Property(persistentProperty.getName(), title, type, true,
            persistentProperty.isIdProperty());
}

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 www.ja  v a2 s.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.springframework.data.rest.webmvc.json.PersistentEntityToJsonSchemaConverter.java

private List<AbstractJsonSchemaProperty<?>> getPropertiesFor(Class<?> type, final ResourceMetadata metadata,
        final Definitions definitions) {

    final PersistentEntity<?, ?> entity = entities.getPersistentEntity(type);
    final JacksonMetadata jackson = new JacksonMetadata(objectMapper, type);

    if (entity == null) {
        return Collections.<AbstractJsonSchemaProperty<?>>emptyList();
    }/*ww w  .  j av  a2  s.  c om*/

    JsonSchemaPropertyRegistrar registrar = new JsonSchemaPropertyRegistrar(jackson);

    for (BeanPropertyDefinition definition : jackson) {

        PersistentProperty<?> persistentProperty = entity.getPersistentProperty(definition.getInternalName());

        // First pass, early drops to avoid unnecessary calculation
        if (persistentProperty != null) {

            if (persistentProperty.isIdProperty() && !configuration.isIdExposedFor(type)) {
                continue;
            }

            if (persistentProperty.isVersionProperty()) {
                continue;
            }

            if (!definition.couldSerialize()) {
                continue;
            }
        }

        AnnotatedMember primaryMember = definition.getPrimaryMember();

        if (primaryMember == null) {
            continue;
        }

        TypeInformation<?> propertyType = persistentProperty == null
                ? ClassTypeInformation.from(primaryMember.getRawType())
                : persistentProperty.getTypeInformation();
        TypeInformation<?> actualPropertyType = propertyType.getActualType();
        Class<?> rawPropertyType = propertyType.getType();

        JsonSchemaFormat format = configuration.getMetadataConfiguration().getSchemaFormatFor(rawPropertyType);
        ResourceDescription description = persistentProperty == null
                ? jackson.getFallbackDescription(metadata, definition)
                : getDescriptionFor(persistentProperty, metadata);
        JsonSchemaProperty property = getSchemaProperty(definition, propertyType, description);

        boolean isSyntheticProperty = persistentProperty == null;
        boolean isNotWritable = !isSyntheticProperty && !persistentProperty.isWritable();
        boolean isJacksonReadOnly = !isSyntheticProperty && jackson.isReadOnly(persistentProperty);

        if (isSyntheticProperty || isNotWritable || isJacksonReadOnly) {
            property = property.withReadOnly();
        }

        if (format != null) {

            // Types with explicitly registered format -> value object with format
            registrar.register(property.withFormat(format), actualPropertyType);
            continue;
        }

        Pattern pattern = configuration.getMetadataConfiguration().getPatternFor(rawPropertyType);

        if (pattern != null) {
            registrar.register(property.withPattern(pattern), actualPropertyType);
            continue;
        }

        if (jackson.isValueType()) {
            registrar.register(property.with(STRING_TYPE_INFORMATION), actualPropertyType);
            continue;
        }

        if (persistentProperty == null) {
            registrar.register(property, actualPropertyType);
            continue;
        }

        if (configuration.isLookupType(persistentProperty.getActualType())) {
            registrar.register(property.with(propertyType), actualPropertyType);
        } else if (associations.isLinkableAssociation(persistentProperty)) {
            registrar.register(property.asAssociation(), null);
        } else {

            if (persistentProperty.isEntity()) {

                if (!definitions.hasDefinitionFor(propertyType)) {
                    definitions.addDefinition(propertyType,
                            new Item(propertyType, getNestedPropertiesFor(persistentProperty, definitions)));
                }

                registrar.register(property.with(propertyType, Definitions.getReference(propertyType)),
                        actualPropertyType);

            } else {

                registrar.register(property.with(propertyType), actualPropertyType);
            }
        }
    }

    return registrar.getProperties();
}