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

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

Introduction

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

Prototype

Class<T> getType();

Source Link

Document

Returns the resolved Java type of this entity.

Usage

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

/**
 * @see DATAREST-269 (https://jira.spring.io/browse/DATAREST-269)
 */// www .  j  a  v a 2  s.c  om
@Override
public Link getSelfLinkFor(Object instance) {
    Assert.notNull(instance, "Domain object must not be null!");

    Repositories repositories = repositories(this);

    Class instanceType = instance.getClass();
    PersistentEntity<?, ?> entity = repositories.getPersistentEntity(instanceType);

    if (entity == null) {
        throw new IllegalArgumentException(
                String.format("Cannot create self link for %s! No persistent entity found!", instanceType));
    }

    EntityInformation<Object, Serializable> entityInformation = repositories
            .getEntityInformationFor(instanceType);
    Serializable id = entityInformation.getId(instance);

    if (id == null) {
        return entityLinks(this).linkToCollectionResource(entity.getType()).withSelfRel();
    }

    return entityLinks(this).linkToSingleResource(entity.getType(), id).withSelfRel();
}

From source file:org.springframework.data.rest.webmvc.json.PersistentEntityToJsonSchemaConverter.java

@Override
public JsonSchema convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {

    final PersistentEntity<?, ?> persistentEntity = entities.getPersistentEntity((Class<?>) source);
    final ResourceMetadata metadata = associations.getMappings().getMetadataFor(persistentEntity.getType());

    Definitions definitions = new Definitions();
    List<AbstractJsonSchemaProperty<?>> propertiesFor = getPropertiesFor(persistentEntity.getType(), metadata,
            definitions);//w  w w  .  ja v a 2s  .c  om

    String title = resolveMessageWithDefault(new ResolvableType(persistentEntity.getType()));

    return new JsonSchema(title, resolveMessage(metadata.getItemResourceDescription()), propertiesFor,
            definitions);
}

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//  www  . j a v  a 2 s.co  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.web.support.DynamicPersistentEntityResourceProcessor.java

@Override
public PersistentEntityResource<?> process(PersistentEntityResource<?> persistentEntityResource) {
    PersistentEntity persistentEntity = persistentEntityResource.getPersistentEntity();
    Object value = persistentEntityResource.getContent();
    Link[] links = persistentEntityResource.getLinks()
            .toArray(new Link[persistentEntityResource.getLinks().size()]);

    String stringRepresentation = stringRepresentation(value, persistentEntity);
    Link domainLink = domainLink(persistentEntityResource);
    boolean managedDomainType = adminConfiguration.isManagedDomainType(persistentEntity.getType());
    String primaryKey = primaryKey(persistentEntity);

    Map<DomainConfigurationUnitType, Map<String, Object>> dynamicProperties = dynamicPropertiesPerUnit(value,
            persistentEntity);//from   w  ww  . ja v  a2  s.  c om

    PersistentEntityWrapper persistentEntityWrapper = persistentEntity(value, dynamicProperties,
            stringRepresentation, domainLink, managedDomainType, primaryKey);

    return new PersistentEntityResource<Object>(persistentEntity, persistentEntityWrapper, links);
}