/** * Can be used to perform a null check for the given field and value. * <p> * This is internally used to check all properties which must not be null * ({@link sirius.search.properties.Property#isNullAllowed()}). If a field accepts a <tt>null</tt> value but * still must be field, this method can be called in {@link #beforeSaveChecks()}. * * @param previousError Can be used to signal that an error was already found. In this case the given exception * will be returned as result even if the value was <tt>null</tt>. In most cases this * parameter will be <tt>null</tt>. * @param property the field to check * @param value the value to check. If the value is <tt>null</tt> an error will be generated. * @return an error if either the given <tt>previousError</tt> was non null or if the given value was <tt>null</tt> */ @Nullable protected HandledException checkNullability(@Nullable HandledException previousError, @Nonnull Property property, @Nullable Object value) { if (Strings.isEmpty(value)) { UserContext.setFieldError(property.getName(), null); if (previousError == null) { return Exceptions.createHandled() .withNLSKey("Entity.fieldMustBeFilled") .set("field", property.getFieldTitle()) .handle(); } } return previousError; }
/** * Extracts and converts the value from the given request. * * @param name name of the parameter to read * @param ctx the request to read the data from * @return the converted value which can be assigned to the field */ protected Object transformFromRequest(String name, WebContext ctx) { Value value = ctx.get(name); if (value.isEmptyString() && !field.getType().isPrimitive()) { return null; } Object result = value.coerce(field.getType(), null); if (result == null) { UserContext.setFieldError(name, value.get()); throw Exceptions.createHandled() .withNLSKey("Property.invalidInput") .set("field", NLS.get(field.getDeclaringClass().getSimpleName() + "." + name)) .set("value", value.asString()) .handle(); } return result; }
@Override protected Object transformToDatasource(Class<? extends BaseMapper<?, ?, ?>> mapperType, Object object) { if (length > 0 && !lob && object != null && ((String) object).length() > length) { throw Exceptions.handle() .to(Mixing.LOG) .withNLSKey("StringProperty.dataTruncation") .set("value", Strings.limit(object, 30)) .set("field", getLabel()) .set("length", ((String) object).length()) .set("maxLength", length) .handle(); } return object; }
this.queryString = detectLogging(query); if (query.length() > MAX_QUERY_LENGTH) { throw Exceptions.createHandled().withNLSKey("Query.queryTooLong").handle();
protected void onDeleteReject(Object e) { BaseEntity<?> referenceInstance = (BaseEntity<?>) getDescriptor().getReferenceInstance(); long count = referenceInstance.getMapper() .select(referenceInstance.getClass()) .eq(nameAsMapping, ((BaseEntity<?>) e).getId()) .count(); if (count == 1) { throw Exceptions.createHandled() .withNLSKey("BaseEntityRefProperty.cannotDeleteEntityWithChild") .set("field", getLabel()) .set("type", getReferencedDescriptor().getLabel()) .set("source", getDescriptor().getLabel()) .handle(); } if (count > 1) { throw Exceptions.createHandled() .withNLSKey("BaseEntityRefProperty.cannotDeleteEntityWithChildren") .set("count", count) .set("field", getLabel()) .set("type", getReferencedDescriptor().getLabel()) .set("source", getDescriptor().getLabel()) .handle(); } } }
protected void onDeleteReject(Object e) { BaseEntity<?> referenceInstance = (BaseEntity<?>) getDescriptor().getReferenceInstance(); long count = referenceInstance.getMapper() .select(referenceInstance.getClass()) .eq(nameAsMapping, ((BaseEntity<?>) e).getId()) .count(); if (count == 1) { throw Exceptions.createHandled() .withNLSKey("BaseEntityRefProperty.cannotDeleteEntityWithChild") .set("field", getLabel()) .set("type", getReferencedDescriptor().getLabel()) .set("source", getDescriptor().getLabel()) .handle(); } if (count > 1) { throw Exceptions.createHandled() .withNLSKey("BaseEntityRefProperty.cannotDeleteEntityWithChildren") .set("count", count) .set("field", getLabel()) .set("type", getReferencedDescriptor().getLabel()) .set("source", getDescriptor().getLabel()) .handle(); } } }
protected HandledException checkUniqueness(HandledException previousError, EntityDescriptor descriptor, Property p, Object value) { Query<?> qry = index.select(getClass()).eq(p.getName(), value); if (!isNew()) { qry.notEq(IndexAccess.ID_FIELD, id); } Unique unique = p.getField().getAnnotation(Unique.class); setupRoutingForUniquenessCheck(descriptor, qry, unique); if (qry.exists()) { UserContext.setFieldError(p.getName(), NLS.toUserString(value)); if (previousError == null) { try { return Exceptions.createHandled() .withNLSKey("Entity.fieldMustBeUnique") .set("field", p.getFieldTitle()) .set("value", NLS.toUserString(p.getField().get(this))) .handle(); } catch (Exception e) { Exceptions.handle(e); } } } return previousError; }
@Override protected Object transformFromRequest(String name, WebContext ctx) { Value value = ctx.get(name); if (value.isEmptyString()) { return null; } try { return NLS.parseUserString(LocalDateTime.class, value.getString()); } catch (IllegalArgumentException e) { Exceptions.ignore(e); UserContext.setFieldError(name, value.get()); throw Exceptions.createHandled() .withNLSKey("Property.invalidInput") .set("field", NLS.get(getField().getDeclaringClass().getSimpleName() + "." + name)) .set("value", value.asString()) .handle(); } }
/** * Ensures that the given value in the given field is unique within the given side constraints. * * @param field the field to check * @param value the value to be unique * @param within the side constraints within the value must be unique * @throws sirius.kernel.health.HandledException if the value isn't unique */ public void assertUnique(Mapping field, Object value, Mapping... within) { if (!isUnique(field, value, within)) { throw Exceptions.createHandled() .withNLSKey("Property.fieldNotUnique") .set("field", getDescriptor().getProperty(field).getLabel()) .set("value", NLS.toUserString(value)) .handle(); } }
@Override protected Object transformFromRequest(String name, WebContext ctx) { Value value = ctx.get(name); if (value.isEmptyString()) { return null; } try { return NLS.parseUserString(LocalDate.class, value.getString()); } catch (IllegalArgumentException e) { Exceptions.ignore(e); UserContext.setFieldError(name, value.get()); throw Exceptions.createHandled() .withNLSKey("Property.invalidInput") .set("field", NLS.get(getField().getDeclaringClass().getSimpleName() + "." + name)) .set("value", value.asString()) .handle(); } }
/** * Checks if the given entity can be consistently deleted. * * @param entity the entity (which must be of type {@link #getReferencedClass()}) which is the be deleted. */ public void checkDelete(final Entity entity) { if (refType.cascade() == Cascade.REJECT && index.select(getLocalClass()) .eq(field.getName(), entity.getId()) .autoRoute(field.getName(), entity.getId()) .exists()) { throw Exceptions.createHandled() .withNLSKey(Strings.isFilled(refType.onDeleteErrorMsg()) ? refType.onDeleteErrorMsg() : "ForeignKey.restricted") .handle(); } }
/** * Parses the given value and applies it to the given entity if possible. * * @param e the entity to receive the parsed value * @param value the value to parse and apply */ public void parseValue(Object e, Value value) { try { setValue(e, transformValue(value)); } catch (IllegalArgumentException exception) { throw Exceptions.createHandled() .withNLSKey("Property.parseValueErrorMessage") .set("message", exception.getMessage()) .error(exception) .handle(); } }
/** * Creates an exception which represents an illegal value for this property * * @param value the illegal value itself * @return an exception filled with an appropriate message */ protected HandledException illegalFieldValue(Value value) { return Exceptions.createHandled() .withNLSKey("Property.illegalValue") .set("property", getLabel()) .set("value", NLS.toUserString(value.get())) .handle(); }
private void rejectDeleteIfNecessary(Entity entity) { if (index.select(getLocalClass()) .eq(getName(), entity.getId()) .autoRoute(field.getName(), entity.getId()) .exists()) { throw Exceptions.createHandled() .withNLSKey(Strings.isFilled(refType.onDeleteErrorMsg()) ? refType.onDeleteErrorMsg() : "ForeignKey.restricted") .handle(); } }
@Override protected void checkNullability(Object propertyValue) { super.checkNullability(propertyValue); if (!isNullable() && ((Amount) propertyValue).isEmpty()) { throw Exceptions.createHandled().withNLSKey("Property.fieldNotNullable").set("field", getLabel()).handle(); } }
/** * Checks if the value is non-null or the property accepts null values. * * @param propertyValue the value to check */ protected void checkNullability(Object propertyValue) { if (!isNullable() && propertyValue == null) { throw Exceptions.createHandled().withNLSKey("Property.fieldNotNullable").set("field", getLabel()).handle(); } }