Java Code Examples for org.springframework.data.mapping.PersistentPropertyAccessor#setProperty()

The following examples show how to use org.springframework.data.mapping.PersistentPropertyAccessor#setProperty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OptimisticLockingBeforeBindCallback.java    From sdn-rx with Apache License 2.0 6 votes vote down vote up
@Override
public Object onBeforeBind(Object entity) {
	Neo4jPersistentEntity<?> neo4jPersistentEntity =
		(Neo4jPersistentEntity<?>) neo4jMappingContext.getRequiredNodeDescription(entity.getClass());

	if (neo4jPersistentEntity.hasVersionProperty()) {
		PersistentPropertyAccessor<Object> propertyAccessor = neo4jPersistentEntity.getPropertyAccessor(entity);
		Neo4jPersistentProperty versionProperty = neo4jPersistentEntity.getRequiredVersionProperty();

		if (!Long.class.isAssignableFrom(versionProperty.getType())) {
			return entity;
		}

		Long versionPropertyValue = (Long) propertyAccessor.getProperty(versionProperty);

		long newVersionValue = 0;
		if (versionPropertyValue != null) {
			newVersionValue = versionPropertyValue + 1;
		}

		propertyAccessor.setProperty(versionProperty, newVersionValue);
	}
	return entity;
}
 
Example 2
Source File: DefaultNeo4jConverter.java    From sdn-rx with Apache License 2.0 6 votes vote down vote up
private PropertyHandler<Neo4jPersistentProperty> populateFrom(
	MapAccessor queryResult,
	PersistentPropertyAccessor<?> propertyAccessor,
	Predicate<Neo4jPersistentProperty> isConstructorParameter,
	Collection<String> surplusLabels
) {
	return property -> {
		if (isConstructorParameter.test(property)) {
			return;
		}

		if (property.isDynamicLabels()) {
			propertyAccessor
				.setProperty(property, createDynamicLabelsProperty(property.getTypeInformation(), surplusLabels));
		} else {
			propertyAccessor.setProperty(property,
				readValueForProperty(extractValueOf(property, queryResult), property.getTypeInformation()));
		}
	};
}
 
Example 3
Source File: MappingVaultConverter.java    From spring-vault with Apache License 2.0 6 votes vote down vote up
private void readProperties(VaultPersistentEntity<?> entity, PersistentPropertyAccessor accessor,
		@Nullable VaultPersistentProperty idProperty, SecretDocumentAccessor documentAccessor,
		VaultPropertyValueProvider valueProvider) {

	for (VaultPersistentProperty prop : entity) {

		// we skip the id property since it was already set
		if (idProperty != null && idProperty.equals(prop)) {
			continue;
		}

		if (entity.isConstructorArgument(prop) || !documentAccessor.hasValue(prop)) {
			continue;
		}

		accessor.setProperty(prop, valueProvider.getPropertyValue(prop));
	}
}
 
Example 4
Source File: TypicalEntityReaderBenchmark.java    From spring-data-dev-tools with Apache License 2.0 6 votes vote down vote up
private void readProperties(Map<String, Object> data, MyPersistentEntity<?> persistentEntity,
		PropertyValueProvider<MyPersistentProperty> valueProvider, PersistentPropertyAccessor<?> accessor) {

	for (MyPersistentProperty prop : persistentEntity) {

		if (prop.isAssociation() && !persistentEntity.isConstructorArgument(prop)) {
			continue;
		}

		// We skip the id property since it was already set

		if (persistentEntity.isIdProperty(prop)) {
			continue;
		}

		if (persistentEntity.isConstructorArgument(prop) || !data.containsKey(persistentEntity.getName())) {
			continue;
		}

		accessor.setProperty(prop, valueProvider.getPropertyValue(prop));
	}
}
 
Example 5
Source File: IdPopulator.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
Object populateIfNecessary(Object entity) {

		Assert.notNull(entity, "Entity may not be null!");

		Neo4jPersistentEntity<?> nodeDescription = neo4jMappingContext.getRequiredPersistentEntity(entity.getClass());
		IdDescription idDescription = nodeDescription.getIdDescription();

		// Filter in two steps to avoid unnecessary object creation.
		if (!idDescription.isExternallyGeneratedId()) {
			return entity;
		}

		PersistentPropertyAccessor propertyAccessor = nodeDescription.getPropertyAccessor(entity);
		Neo4jPersistentProperty idProperty = nodeDescription.getRequiredIdProperty();

		// Check existing ID
		if (propertyAccessor.getProperty(idProperty) != null) {
			return entity;
		}

		IdGenerator<?> idGenerator;

		// Get or create the shared generator
		// Ref has precedence over class
		Optional<String> optionalIdGeneratorRef = idDescription.getIdGeneratorRef();
		if (optionalIdGeneratorRef.isPresent()) {

			idGenerator = neo4jMappingContext
				.getIdGenerator(optionalIdGeneratorRef.get()).orElseThrow(() -> new IllegalStateException(
					"Id generator named " + optionalIdGeneratorRef.get() + " not found!"));
		} else {

			// At this point, the class must be present, so we don't check the optional not anymore
			idGenerator = neo4jMappingContext.getOrCreateIdGeneratorOfType(idDescription.getIdGeneratorClass().get());
		}

		propertyAccessor.setProperty(idProperty, idGenerator.generateId(nodeDescription.getPrimaryLabel(), entity));
		return propertyAccessor.getBean();
	}
 
Example 6
Source File: Neo4jTemplate.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
private <T> T saveImpl(T instance, @Nullable String inDatabase) {

		Neo4jPersistentEntity entityMetaData = neo4jMappingContext.getPersistentEntity(instance.getClass());
		T entityToBeSaved = eventSupport.maybeCallBeforeBind(instance);

		DynamicLabels dynamicLabels = determineDynamicLabels(entityToBeSaved, entityMetaData, inDatabase);

		Optional<Long> optionalInternalId = neo4jClient
			.query(() -> renderer.render(cypherGenerator.prepareSaveOf(entityMetaData, dynamicLabels)))
			.in(inDatabase)
			.bind((T) entityToBeSaved)
			.with(neo4jMappingContext.getRequiredBinderFunctionFor((Class<T>) entityToBeSaved.getClass()))
			.fetchAs(Long.class).one();

		if (entityMetaData.hasVersionProperty() && !optionalInternalId.isPresent()) {
			throw new OptimisticLockingFailureException(OPTIMISTIC_LOCKING_ERROR_MESSAGE);
		}

		PersistentPropertyAccessor<T> propertyAccessor = entityMetaData.getPropertyAccessor(entityToBeSaved);
		if (!entityMetaData.isUsingInternalIds()) {
			processRelations(entityMetaData, entityToBeSaved, inDatabase);
			return entityToBeSaved;
		} else {
			propertyAccessor.setProperty(entityMetaData.getRequiredIdProperty(), optionalInternalId.get());
			processRelations(entityMetaData, entityToBeSaved, inDatabase);

			return propertyAccessor.getBean();
		}
	}
 
Example 7
Source File: DefaultArangoConverter.java    From spring-data with Apache License 2.0 5 votes vote down vote up
private void readProperty(
	final ArangoPersistentEntity<?> entity,
	final String parentId,
	final PersistentPropertyAccessor<?> accessor,
	final VPackSlice source,
	final ArangoPersistentProperty property) {

	final Object propertyValue = readPropertyValue(entity, parentId, source, property);
	if (propertyValue != null || !property.getType().isPrimitive()) {
		accessor.setProperty(property, propertyValue);
	}
}
 
Example 8
Source File: ArangoTemplate.java    From spring-data with Apache License 2.0 5 votes vote down vote up
private void updateDBFields(final Object value, final DocumentEntity documentEntity) {
	final ArangoPersistentEntity<?> entity = converter.getMappingContext().getPersistentEntity(value.getClass());
	final PersistentPropertyAccessor<?> accessor = entity.getPropertyAccessor(value);
	final ArangoPersistentProperty idProperty = entity.getIdProperty();
	if (idProperty != null && !idProperty.isImmutable()) {
		accessor.setProperty(idProperty, documentEntity.getKey());
	}
	entity.getArangoIdProperty().filter(arangoId -> !arangoId.isImmutable())
			.ifPresent(arangoId -> accessor.setProperty(arangoId, documentEntity.getId()));
	entity.getRevProperty().filter(rev -> !rev.isImmutable())
			.ifPresent(rev -> accessor.setProperty(rev, documentEntity.getRev()));
}
 
Example 9
Source File: MappingVaultConverter.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
private <S> S read(VaultPersistentEntity<S> entity, SecretDocument source) {

		ParameterValueProvider<VaultPersistentProperty> provider = getParameterProvider(entity, source);
		EntityInstantiator instantiator = this.instantiators.getInstantiatorFor(entity);
		S instance = instantiator.createInstance(entity, provider);

		PersistentPropertyAccessor accessor = new ConvertingPropertyAccessor(entity.getPropertyAccessor(instance),
				this.conversionService);

		VaultPersistentProperty idProperty = entity.getIdProperty();
		SecretDocumentAccessor documentAccessor = new SecretDocumentAccessor(source);

		// make sure id property is set before all other properties
		Object idValue;

		if (entity.requiresPropertyPopulation()) {
			if (idProperty != null && !entity.isConstructorArgument(idProperty)
					&& documentAccessor.hasValue(idProperty)) {

				idValue = readIdValue(idProperty, documentAccessor);
				accessor.setProperty(idProperty, idValue);
			}

			VaultPropertyValueProvider valueProvider = new VaultPropertyValueProvider(documentAccessor);

			readProperties(entity, accessor, idProperty, documentAccessor, valueProvider);
		}

		return instance;
	}
 
Example 10
Source File: AbstractContentPropertyController.java    From spring-content with Apache License 2.0 5 votes vote down vote up
protected void setContentProperty(Object domainObj, PersistentProperty<?> property,
		String contentId, Object newValue) {

	PersistentPropertyAccessor accessor = property.getOwner()
			.getPropertyAccessor(domainObj);
	Object contentPropertyObject = accessor.getProperty(property);
	if (contentPropertyObject == null)
		return;
	else if (!PersistentEntityUtils.isPropertyMultiValued(property)) {
		accessor.setProperty(property, newValue);
	}
	else {
		// handle multi-valued
		if (property.isArray()) {
			throw new UnsupportedOperationException();
		}
		else if (property.isCollectionLike()
				&& contentPropertyObject instanceof Set) {
			@SuppressWarnings("unchecked")
			Set<Object> contentSet = (Set<Object>) contentPropertyObject;
			Object oldValue = findContentPropertyObjectInSet(contentId, contentSet);
			contentSet.remove(oldValue);
			if (newValue != null)
				contentSet.add(newValue);
		}
	}
}
 
Example 11
Source File: MappingCrateConverter.java    From spring-data-crate with Apache License 2.0 4 votes vote down vote up
/**
 * Read an incoming {@link CrateDocument} into the target entity.
 *
 * @param entity the target entity.
 * @param source the document to convert.
 * @param parent an optional parent object.
 * @param <R> the entity type.
 * @return the converted entity.
 */
@SuppressWarnings("unchecked")
protected <R> R read(final CratePersistentEntity<R> entity, final CrateDocument source, final Object parent) {
	
	final DefaultSpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator(source, spELContext);
	
    ParameterValueProvider<CratePersistentProperty> provider = getParameterProvider(entity, source, evaluator, parent);
    
    EntityInstantiator instantiator = instantiators.getInstantiatorFor(entity);

    R instance = instantiator.createInstance(entity, provider);
    final PersistentPropertyAccessor propertyAccessor = entity.getPropertyAccessor(instance);
    final R result = (R)propertyAccessor.getBean();
    final CratePersistentProperty idProperty = entity.getIdProperty();
    final CratePersistentProperty versionProperty = entity.getVersionProperty();
    
    if(entity.hasIdProperty()) {
    	Object idValue = getValueInternal(idProperty, source, result);
    	propertyAccessor.setProperty(idProperty, idValue);
    }
    
    if(entity.hasVersionProperty()) {
    	Object versionValue = getValueInternal(versionProperty, source, result);
    	propertyAccessor.setProperty(versionProperty, versionValue);
    }
    
    for(CratePersistentProperty property : entity.getPersistentProperties()) {
    	// skip id and version properties as they may have potentially been set above.  
		if((idProperty != null && idProperty.equals(property)) || (versionProperty != null && versionProperty.equals(property))) {
			continue;
		}
		
		if(!source.containsKey(property.getFieldName()) || entity.isConstructorArgument(property)) {
			continue;
		}
		
		propertyAccessor.setProperty(property, getValueInternal(property, source, result));
    }
    
    entity.doWithAssociations(new AssociationHandler<CratePersistentProperty>() {
    	
      @Override
      public void doWithAssociation(final Association<CratePersistentProperty> association) {	    	  
    	  CratePersistentProperty inverseProp = association.getInverse();
    	  Object obj = getValueInternal(inverseProp, source, result);
    	  propertyAccessor.setProperty(inverseProp, obj);
      }	      
    });

    return result;
  }