Example usage for org.springframework.data.mapping Association getInverse

List of usage examples for org.springframework.data.mapping Association getInverse

Introduction

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

Prototype

public P getInverse() 

Source Link

Usage

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

private List<PersistentEntityWrapper> associatedPersistentEntities(Association association, Object instance) {
    PersistentProperty persistentProperty = association.getInverse();

    Object associationValue = beanWrapper(instance).getPropertyValue(persistentProperty.getName());
    if (associationValue == null) {
        return null;
    }/*w w  w . ja va 2  s.  c om*/

    List<PersistentEntityWrapper> result = newArrayList();

    if (persistentProperty.isArray()) {
        for (Object item : (Object[]) associationValue) {
            result.add(associatedPersistentEntity(persistentProperty, item));
        }
        return result;
    }

    for (Object item : (Iterable<Object>) associationValue) {
        result.add(associatedPersistentEntity(persistentProperty, item));
    }
    return result;
}

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

private Map<DomainConfigurationUnitType, Map<String, Object>> dynamicPropertiesPerUnit(Object value,
        PersistentEntity persistentEntity) {
    if (!adminConfiguration.isManagedDomainType(persistentEntity.getType())) {
        return Collections.emptyMap();
    }//from  w  w w .j  a v  a  2 s .c  o  m

    DomainTypeAdministrationConfiguration managedDomainTypeConfiguration = adminConfiguration
            .forManagedDomainType(persistentEntity.getType());

    List<DomainConfigurationUnitType> units = newArrayList(LIST_VIEW, FORM_VIEW, SHOW_VIEW, QUICK_VIEW);

    List<PersistentProperty> persistentProperties = findPersistentFileProperties(persistentEntity);
    List<Association> associations = findLinkableAssociations(persistentEntity);

    Map<DomainConfigurationUnitType, Map<String, Object>> dynamicPropertiesPerUnit = newHashMap();
    for (DomainConfigurationUnitType unit : units) {
        Map<String, Object> dynamicProperties = newLinkedHashMap();
        for (PersistentProperty persistentProperty : persistentProperties) {
            dynamicProperties.put(persistentProperty.getName(), filePropertyValue(persistentProperty, value));
        }
        for (Association association : associations) {
            dynamicProperties.put(association.getInverse().getName(),
                    associationPropertyValue(association, value));
        }
        for (FieldMetadata customField : customFields(managedDomainTypeConfiguration.fieldsForUnit(unit))) {
            dynamicProperties.put(customField.getUuid(), customField.getValue(value));
        }
        for (FieldMetadata transientField : transientFields(
                managedDomainTypeConfiguration.fieldsForUnit(unit))) {
            dynamicProperties.put(transientField.getUuid(), transientField.getValue(value));
        }
        dynamicPropertiesPerUnit.put(unit, dynamicProperties);
    }
    return dynamicPropertiesPerUnit;
}

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

private List<Association> findLinkableAssociations(PersistentEntity persistentEntity) {
    final List<Association> result = newArrayList();
    persistentEntity.doWithAssociations(new SimpleAssociationHandler() {
        @Override/*from www  .ja v  a  2  s.com*/
        public void doWithAssociation(Association<? extends PersistentProperty<?>> association) {
            if (associationLinks.isLinkableAssociation(association.getInverse())) {
                result.add(association);
            }
        }
    });
    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/*from   w  w  w . j  ava2  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:io.twipple.springframework.data.clusterpoint.convert.DefaultClusterpointEntityConverter.java

/**
 * Read an incoming {@link ClusterpointDocument} into the target entity.
 *
 * @param entity the target entity./*  w  w w. j a v a2  s. co m*/
 * @param source the document to convert.
 * @param parent an optional parent object.
 * @param <R>    the entity type.
 * @return the converted entity.
 */
@NotNull
@SuppressWarnings("unchecked")
protected <R> R read(@NotNull final ClusterpointPersistentEntity<R> entity,
        @NotNull final ClusterpointDocument source, Object parent) {
    Assert.notNull(entity);
    Assert.notNull(source);

    final DefaultSpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator(source, spELContext);
    ParameterValueProvider<ClusterpointPersistentProperty> parameterValueProvider = getParameterValueProvider(
            entity, source, evaluator, parent);
    final PersistentPropertyAccessor propertyAccessor = getPersistentPropertyAccessor(entity,
            parameterValueProvider);
    final PropertyValueProvider<ClusterpointPersistentProperty> propertyValueProvider = getPropertyValueProvider(
            source, propertyAccessor);

    entity.doWithProperties(new PropertyHandler<ClusterpointPersistentProperty>() {
        @Override
        public void doWithPersistentProperty(@NotNull ClusterpointPersistentProperty property) {

            Assert.notNull(property);

            if (!source.doesPropertyExist(property) || entity.isConstructorArgument(property)) {
                return;
            }

            Object obj = propertyValueProvider.getPropertyValue(property);
            setPropertyInternal(propertyAccessor, property, obj);
        }
    });

    entity.doWithAssociations(new AssociationHandler<ClusterpointPersistentProperty>() {
        @Override
        public void doWithAssociation(@NotNull Association<ClusterpointPersistentProperty> association) {

            Assert.notNull(association);

            ClusterpointPersistentProperty inverseProperty = association.getInverse();
            Object obj = propertyValueProvider.getPropertyValue(inverseProperty);
            setPropertyInternal(propertyAccessor, inverseProperty, obj);
        }
    });

    return (R) propertyAccessor.getBean();
}

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//w  w  w  .j  a  v a 2s. co  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();
}

From source file:org.springframework.data.mongodb.core.convert.MappingMongoConverter.java

private <S extends Object> S read(final MongoPersistentEntity<S> entity, final DBObject dbo) {

    final StandardEvaluationContext spelCtx = new StandardEvaluationContext(dbo);
    spelCtx.addPropertyAccessor(DBObjectPropertyAccessor.INSTANCE);

    if (applicationContext != null) {
        spelCtx.setBeanResolver(new BeanFactoryResolver(applicationContext));
    }//from  www . j  a v  a2s  .co m

    final MappedConstructor constructor = new MappedConstructor(entity, mappingContext);

    SpELAwareParameterValueProvider delegate = new SpELAwareParameterValueProvider(spelExpressionParser,
            spelCtx);
    ParameterValueProvider provider = new DelegatingParameterValueProvider(constructor, dbo, delegate);

    final BeanWrapper<MongoPersistentEntity<S>, S> wrapper = BeanWrapper.create(entity, provider,
            conversionService);

    // Set properties not already set in the constructor
    entity.doWithProperties(new PropertyHandler<MongoPersistentProperty>() {
        public void doWithPersistentProperty(MongoPersistentProperty prop) {

            boolean isConstructorProperty = constructor.isConstructorParameter(prop);
            boolean hasValueForProperty = dbo.containsField(prop.getFieldName());

            if (!hasValueForProperty || isConstructorProperty) {
                return;
            }

            Object obj = getValueInternal(prop, dbo, spelCtx, prop.getSpelExpression());
            wrapper.setProperty(prop, obj, useFieldAccessOnly);
        }
    });

    // Handle associations
    entity.doWithAssociations(new AssociationHandler<MongoPersistentProperty>() {
        public void doWithAssociation(Association<MongoPersistentProperty> association) {
            MongoPersistentProperty inverseProp = association.getInverse();
            Object obj = getValueInternal(inverseProp, dbo, spelCtx, inverseProp.getSpelExpression());
            try {
                wrapper.setProperty(inverseProp, obj);
            } catch (IllegalAccessException e) {
                throw new MappingException(e.getMessage(), e);
            } catch (InvocationTargetException e) {
                throw new MappingException(e.getMessage(), e);
            }
        }
    });

    return wrapper.getBean();
}

From source file:org.springframework.data.mongodb.core.convert.MappingMongoConverter.java

protected void writeInternal(Object obj, final DBObject dbo, MongoPersistentEntity<?> entity) {

    if (obj == null) {
        return;/*  w ww.  j av  a 2  s .c om*/
    }

    if (null == entity) {
        throw new MappingException("No mapping metadata found for entity of type " + obj.getClass().getName());
    }

    final BeanWrapper<MongoPersistentEntity<Object>, Object> wrapper = BeanWrapper.create(obj,
            conversionService);

    // Write the ID
    final MongoPersistentProperty idProperty = entity.getIdProperty();
    if (!dbo.containsField("_id") && null != idProperty) {

        try {
            Object id = wrapper.getProperty(idProperty, Object.class, useFieldAccessOnly);
            dbo.put("_id", idMapper.convertId(id));
        } catch (ConversionException ignored) {
        }
    }

    // Write the properties
    entity.doWithProperties(new PropertyHandler<MongoPersistentProperty>() {
        public void doWithPersistentProperty(MongoPersistentProperty prop) {

            if (prop.equals(idProperty)) {
                return;
            }

            Object propertyObj = wrapper.getProperty(prop, prop.getType(), useFieldAccessOnly);

            if (null != propertyObj) {
                if (!conversions.isSimpleType(propertyObj.getClass())) {
                    writePropertyInternal(propertyObj, dbo, prop);
                } else {
                    writeSimpleInternal(propertyObj, dbo, prop.getFieldName());
                }
            }
        }
    });

    entity.doWithAssociations(new AssociationHandler<MongoPersistentProperty>() {
        public void doWithAssociation(Association<MongoPersistentProperty> association) {
            MongoPersistentProperty inverseProp = association.getInverse();
            Class<?> type = inverseProp.getType();
            Object propertyObj = wrapper.getProperty(inverseProp, type, useFieldAccessOnly);
            if (null != propertyObj) {
                writePropertyInternal(propertyObj, dbo, inverseProp);
            }
        }
    });
}