Java Code Examples for org.neo4j.driver.Values#value()

The following examples show how to use org.neo4j.driver.Values#value() . 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: Neo4jConversionsIT.java    From sdn-rx with Apache License 2.0 6 votes vote down vote up
static void assertWrite(String label, String attribute, Object t) {

		Value driverValue;
		if (t != null && Collection.class.isAssignableFrom(t.getClass())) {
			Collection<?> sourceCollection = (Collection<?>) t;
			Object[] targetCollection = (sourceCollection).stream().map(element ->
				DEFAULT_CONVERSION_SERVICE.convert(element, Value.class)).toArray();
			driverValue = Values.value(targetCollection);
		} else {
			driverValue = DEFAULT_CONVERSION_SERVICE.convert(t, Value.class);
		}

		try (Session session = neo4jConnectionSupport.getDriver().session()) {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("label", label);
			parameters.put("attribute", attribute);
			parameters.put("v", driverValue);

			long cnt = session
				.run("MATCH (n) WHERE labels(n) = [$label]  AND n[$attribute] = $v RETURN COUNT(n) AS cnt",
					parameters)
				.single().get("cnt").asLong();
			assertThat(cnt).isEqualTo(1L);
		}
	}
 
Example 2
Source File: DefaultNeo4jConverter.java    From sdn-rx with Apache License 2.0 6 votes vote down vote up
@Override
public Value writeValueFromProperty(@Nullable Object value, TypeInformation<?> type) {

	if (value == null) {
		return Values.NULL;
	}

	if (isCollection(type)) {
		Collection<?> sourceCollection = (Collection<?>) value;
		Object[] targetCollection = (sourceCollection).stream().map(element ->
			conversionService.convert(element, Value.class)).toArray();
		return Values.value(targetCollection);
	}

	return conversionService.convert(value, Value.class);
}
 
Example 3
Source File: AdditionalTypes.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {

	if (source == null) {
		return Value.class.isAssignableFrom(targetType.getType()) ? Values.NULL : null;
	}

	if (Value.class.isAssignableFrom(sourceType.getType())) {
		return Enum.valueOf((Class<Enum>) targetType.getType(), ((Value) source).asString());
	} else {
		return Values.value(((Enum) source).name());
	}
}
 
Example 4
Source File: ThingWithCustomTypes.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {

	if (Value.class.isAssignableFrom(sourceType.getType())) {
		return CustomType.of(((Value) source).asString());
	} else {
		return Values.value(((DifferentType) source).getValue());
	}
}
 
Example 5
Source File: DefaultNeo4jConverterTest.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Test
void shouldCatchConversionErrors() {
	Value value = Values.value("Das funktioniert nicht.");

	assertThatExceptionOfType(TypeMismatchDataAccessException.class)
		.isThrownBy(() -> defaultNeo4jConverter.readValueForProperty(value, ClassTypeInformation.from(Date.class)))
		.withMessageStartingWith("Could not convert \"Das funktioniert nicht.\" into java.util.Date;")
		.withCauseInstanceOf(ConversionFailedException.class)
		.withRootCauseInstanceOf(DateTimeParseException.class);
}
 
Example 6
Source File: DefaultNeo4jConverterTest.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Test
void shouldCatchUncoercibleErrors() {
	Value value = Values.value("Das funktioniert nicht.");

	assertThatExceptionOfType(TypeMismatchDataAccessException.class)
		.isThrownBy(() -> defaultNeo4jConverter.readValueForProperty(value, ClassTypeInformation.from(LocalDate.class)))
		.withMessageStartingWith("Could not convert \"Das funktioniert nicht.\" into java.time.LocalDate;")
		.withCauseInstanceOf(ConversionFailedException.class)
		.withRootCauseInstanceOf(Uncoercible.class);
}
 
Example 7
Source File: AdditionalTypes.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
static Value value(short[] aShortArray) {
	if (aShortArray == null) {
		return Values.NULL;
	}

	long[] values = new long[aShortArray.length];
	int i = 0;
	for (short v : aShortArray) {
		values[i++] = v;
	}
	return Values.value(values);
}
 
Example 8
Source File: AdditionalTypes.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
static Value value(float[] aFloatArray) {
	if (aFloatArray == null) {
		return Values.NULL;
	}

	String[] values = new String[aFloatArray.length];
	int i = 0;
	for (float v : aFloatArray) {
		values[i++] = Float.toString(v);
	}
	return Values.value(values);
}
 
Example 9
Source File: AdditionalTypes.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
static Value value(Short aShort) {
	if (aShort == null) {
		return Values.NULL;
	}

	return Values.value(aShort.longValue());
}
 
Example 10
Source File: AdditionalTypes.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
static Value value(Locale locale) {
	if (locale == null) {
		return Values.NULL;
	}

	return Values.value(locale.toString());
}
 
Example 11
Source File: AdditionalTypes.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
static Value value(Float aFloat) {
	if (aFloat == null) {
		return Values.NULL;
	}

	return Values.value(aFloat.toString());
}
 
Example 12
Source File: AdditionalTypes.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
static Value value(Date date) {
	if (date == null) {
		return Values.NULL;
	}

	return Values.value(DATE_TIME_FORMATTER.format(date.toInstant().atZone(ZoneOffset.UTC.normalized())));
}
 
Example 13
Source File: AdditionalTypes.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
static Value value(Byte aByte) {
	if (aByte == null) {
		return Values.NULL;
	}

	return Values.value(new Byte[] { aByte });
}
 
Example 14
Source File: AdditionalTypes.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
static Value value(BigInteger bigInteger) {
	if (bigInteger == null) {
		return Values.NULL;
	}

	return Values.value(bigInteger.toString());
}
 
Example 15
Source File: AdditionalTypes.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
static Value value(BigDecimal bigDecimal) {
	if (bigDecimal == null) {
		return Values.NULL;
	}

	return Values.value(bigDecimal.toString());
}
 
Example 16
Source File: AdditionalTypes.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
static Value value(UUID uuid) {
	if (uuid == null) {
		return Values.NULL;
	}

	return Values.value(uuid.toString());
}
 
Example 17
Source File: DefaultNeo4jConverterTest.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
@Test
void shouldCatchUncoerfcibleErrors() {
	Value value = Values.value("Das funktioniert nicht.");

	assertThatExceptionOfType(TypeMismatchDataAccessException.class)
		.isThrownBy(
			() -> defaultNeo4jConverter.readValueForProperty(value, ClassTypeInformation.from(ReactiveNeo4jClient.class)))
		.withMessageStartingWith(
			"Could not convert \"Das funktioniert nicht.\" into org.neo4j.springframework.data.core.ReactiveNeo4jClient;")
		.withRootCauseInstanceOf(ConverterNotFoundException.class);
}
 
Example 18
Source File: DefaultNeo4jConverter.java    From sdn-rx with Apache License 2.0 5 votes vote down vote up
/**
 * Merges the root node of a query and the remaining record into one map, adding the internal ID of the node, too.
 * Merge happens only when the record contains additional values.
 *
 * @param node   Node whose attributes are about to be merged
 * @param record Record that should be merged
 * @return
 */
private static MapAccessor mergeRootNodeWithRecord(Node node, Record record) {
	Map<String, Object> mergedAttributes = new HashMap<>(node.size() + record.size() + 1);

	mergedAttributes.put(NAME_OF_INTERNAL_ID, node.id());
	mergedAttributes.putAll(node.asMap(Function.identity()));
	mergedAttributes.putAll(record.asMap(Function.identity()));

	return Values.value(mergedAttributes);
}
 
Example 19
Source File: AdditionalTypes.java    From sdn-rx with Apache License 2.0 4 votes vote down vote up
static Value value(TemporalAmount temporalAmount) {
	return Values.value(temporalAmount);
}
 
Example 20
Source File: AdditionalTypes.java    From sdn-rx with Apache License 2.0 4 votes vote down vote up
static Value value(Instant instant) {
	return Values.value(instant.atOffset(ZoneOffset.UTC));
}