Formatting.

This commit is contained in:
Greg Burd 2017-11-03 13:32:57 -04:00
parent a600c0bd23
commit fe47531984
6 changed files with 61 additions and 9 deletions

View file

@ -235,7 +235,7 @@ public class HelenusSession extends AbstractSessionOperations implements Closeab
} }
} else { } else {
value = valueMap.get(prop.getPropertyName()); value = valueMap.get(prop.getPropertyName());
binder.setValueForProperty(prop, value.toString()); if (value != null) binder.setValueForProperty(prop, value.toString());
} }
} }
if (binder.isBound()) { if (binder.isBound()) {

View file

@ -165,6 +165,14 @@ public final class SchemaUtil {
} }
} }
if (p.size() == 0 && c.size() == 0)
return "{"
+ properties
.stream()
.map(HelenusProperty::getPropertyName)
.collect(Collectors.joining(", "))
+ "}";
return "(" return "("
+ ((p.size() > 1) ? "(" + String.join(", ", p) + ")" : p.get(0)) + ((p.size() > 1) ? "(" + String.join(", ", p) + ")" : p.get(0))
+ ((c.size() > 0) + ((c.size() > 0)

View file

@ -31,6 +31,7 @@ import net.helenus.mapping.annotation.*;
import net.helenus.mapping.validator.DistinctValidator; import net.helenus.mapping.validator.DistinctValidator;
import net.helenus.support.HelenusMappingException; import net.helenus.support.HelenusMappingException;
import org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.StringUtils;
public final class HelenusMappingEntity implements HelenusEntity { public final class HelenusMappingEntity implements HelenusEntity {
@ -128,7 +129,24 @@ public final class HelenusMappingEntity implements HelenusEntity {
for (ConstraintValidator<?, ?> constraint : for (ConstraintValidator<?, ?> constraint :
MappingUtil.getValidators(prop.getGetterMethod())) { MappingUtil.getValidators(prop.getGetterMethod())) {
if (constraint.getClass().isAssignableFrom(DistinctValidator.class)) { if (constraint.getClass().isAssignableFrom(DistinctValidator.class)) {
UnboundFacet facet = new UnboundFacet(prop); DistinctValidator validator = (DistinctValidator) constraint;
String[] values = validator.value();
UnboundFacet facet;
if (values != null && values.length >= 1 && !(StringUtils.isBlank(values[0]))) {
List<HelenusProperty> props = new ArrayList<HelenusProperty>(values.length + 1);
props.add(prop);
for (String value : values) {
for (HelenusProperty p : orderedProps) {
String name = p.getPropertyName();
if (name.equals(value) && !name.equals(prop.getPropertyName())) {
props.add(p);
}
}
}
facet = new UnboundFacet(props);
} else {
facet = new UnboundFacet(prop);
}
facetsBuilder.add(facet); facetsBuilder.add(facet);
break; break;
} }

View file

@ -228,10 +228,11 @@ public final class Constraints {
public @interface Distinct { public @interface Distinct {
/** /**
* User defined Enum to further restrict the items in the set. * User defined list of properties that combine with this one to result in a distinct
* combination in the table.
* *
* @return Java * @return Java
*/ */
Class<? extends Enum> value() default Enum.class; String[] value() default "";
} }
} }

View file

@ -22,13 +22,20 @@ import net.helenus.mapping.annotation.Constraints;
public final class DistinctValidator public final class DistinctValidator
implements ConstraintValidator<Constraints.Distinct, CharSequence> { implements ConstraintValidator<Constraints.Distinct, CharSequence> {
private Constraints.Distinct annotation;
@Override @Override
public void initialize(Constraints.Distinct constraintAnnotation) {} public void initialize(Constraints.Distinct constraintAnnotation) {
annotation = constraintAnnotation;
}
@Override @Override
public boolean isValid(CharSequence value, ConstraintValidatorContext context) { public boolean isValid(CharSequence value, ConstraintValidatorContext context) {
// TODO(gburd): if there is an Enum type supplied, check that value is valid // TODO(gburd): check that the list contains valid property names.
// Enum.name()
return true; return true;
} }
public String[] value() {
return annotation == null ? null : annotation.value();
}
} }

View file

@ -41,8 +41,13 @@ interface Widget {
UUID id(); UUID id();
@Index @Index
@Constraints.Distinct() @Constraints.Distinct
String name(); String name();
@Constraints.Distinct(value = {"b"})
String a();
String b();
} }
public class UnitOfWorkTest extends AbstractEmbeddedCassandraTest { public class UnitOfWorkTest extends AbstractEmbeddedCassandraTest {
@ -74,6 +79,8 @@ public class UnitOfWorkTest extends AbstractEmbeddedCassandraTest {
.<Widget>insert(widget) .<Widget>insert(widget)
.value(widget::id, key) .value(widget::id, key)
.value(widget::name, RandomString.make(20)) .value(widget::name, RandomString.make(20))
.value(widget::a, RandomString.make(10))
.value(widget::b, RandomString.make(10))
.sync(); .sync();
try (UnitOfWork uow = session.begin()) { try (UnitOfWork uow = session.begin()) {
@ -118,6 +125,8 @@ public class UnitOfWorkTest extends AbstractEmbeddedCassandraTest {
.<Widget>insert(widget) .<Widget>insert(widget)
.value(widget::id, key1) .value(widget::id, key1)
.value(widget::name, RandomString.make(20)) .value(widget::name, RandomString.make(20))
.value(widget::a, RandomString.make(10))
.value(widget::b, RandomString.make(10))
.sync(uow1); .sync(uow1);
try (UnitOfWork uow2 = session.begin(uow1)) { try (UnitOfWork uow2 = session.begin(uow1)) {
@ -138,6 +147,8 @@ public class UnitOfWorkTest extends AbstractEmbeddedCassandraTest {
.<Widget>insert(widget) .<Widget>insert(widget)
.value(widget::id, key2) .value(widget::id, key2)
.value(widget::name, RandomString.make(20)) .value(widget::name, RandomString.make(20))
.value(widget::a, RandomString.make(10))
.value(widget::b, RandomString.make(10))
.sync(uow2); .sync(uow2);
uow2.commit() uow2.commit()
@ -151,7 +162,8 @@ public class UnitOfWorkTest extends AbstractEmbeddedCassandraTest {
w4 = w4 =
session session
.<Widget>select(widget) .<Widget>select(widget)
.where(widget::id, eq(key2)) .where(widget::a, eq(w3.a()))
.and(widget::b, eq(w3.b()))
.single() .single()
.sync(uow1) .sync(uow1)
.orElse(null); .orElse(null);
@ -175,6 +187,8 @@ public class UnitOfWorkTest extends AbstractEmbeddedCassandraTest {
.<Widget>insert(widget) .<Widget>insert(widget)
.value(widget::id, key) .value(widget::id, key)
.value(widget::name, RandomString.make(20)) .value(widget::name, RandomString.make(20))
.value(widget::a, RandomString.make(10))
.value(widget::b, RandomString.make(10))
.sync(uow); .sync(uow);
// This should read from the database and return a Widget. // This should read from the database and return a Widget.
@ -209,6 +223,8 @@ public class UnitOfWorkTest extends AbstractEmbeddedCassandraTest {
.<Widget>insert(widget) .<Widget>insert(widget)
.value(widget::id, key) .value(widget::id, key)
.value(widget::name, RandomString.make(20)) .value(widget::name, RandomString.make(20))
.value(widget::a, RandomString.make(10))
.value(widget::b, RandomString.make(10))
.sync(); .sync();
try (UnitOfWork uow = session.begin()) { try (UnitOfWork uow = session.begin()) {
@ -271,6 +287,8 @@ public class UnitOfWorkTest extends AbstractEmbeddedCassandraTest {
.<Widget>insert(widget) .<Widget>insert(widget)
.value(widget::id, key) .value(widget::id, key)
.value(widget::name, RandomString.make(20)) .value(widget::name, RandomString.make(20))
.value(widget::a, RandomString.make(10))
.value(widget::b, RandomString.make(10))
.sync(); .sync();
try (UnitOfWork uow = session.begin()) { try (UnitOfWork uow = session.begin()) {