diff --git a/src/main/java/sirius/db/mixing/annotations/Numeric.java b/src/main/java/sirius/db/mixing/annotations/Numeric.java index f53646fd..b05415b8 100644 --- a/src/main/java/sirius/db/mixing/annotations/Numeric.java +++ b/src/main/java/sirius/db/mixing/annotations/Numeric.java @@ -17,7 +17,7 @@ /** * Specifies the numeric precision of a NUMBER column. *

- * If a column must sore values with 12 digits and 3 decimal places, the precision would by 15 and scale + * If a column must store values with 12 digits and 3 decimal places, the precision would be 15 and scale * 3. */ @Documented diff --git a/src/main/java/sirius/db/mixing/annotations/RemoveWhitespace.java b/src/main/java/sirius/db/mixing/annotations/RemoveWhitespace.java new file mode 100644 index 00000000..d9918f33 --- /dev/null +++ b/src/main/java/sirius/db/mixing/annotations/RemoveWhitespace.java @@ -0,0 +1,26 @@ +/* + * Made with all the love in the world + * by scireum in Remshalden, Germany + * + * Copyright by scireum GmbH + * http://www.scireum.de - info@scireum.de + */ + +package sirius.db.mixing.annotations; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Marks a string property to remove any white-spaces. + *

+ * All white-spaces of the property's value will be removed before it is written to the database. + */ +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface RemoveWhitespace { +} diff --git a/src/main/java/sirius/db/mixing/annotations/Trim.java b/src/main/java/sirius/db/mixing/annotations/Trim.java index 18b51b47..eeb4191c 100644 --- a/src/main/java/sirius/db/mixing/annotations/Trim.java +++ b/src/main/java/sirius/db/mixing/annotations/Trim.java @@ -17,7 +17,7 @@ /** * Marks a string property as auto trimmed. *

- * The value of this property will be trimmed bevore it is written to the database. + * The value of this property will be trimmed before it is written to the database. */ @Documented @Retention(RetentionPolicy.RUNTIME) diff --git a/src/main/java/sirius/db/mixing/properties/StringProperty.java b/src/main/java/sirius/db/mixing/properties/StringProperty.java index fc5edd66..abca375a 100644 --- a/src/main/java/sirius/db/mixing/properties/StringProperty.java +++ b/src/main/java/sirius/db/mixing/properties/StringProperty.java @@ -26,8 +26,10 @@ import sirius.db.mixing.annotations.DefaultValue; import sirius.db.mixing.annotations.Lob; import sirius.db.mixing.annotations.LowerCase; +import sirius.db.mixing.annotations.RemoveWhitespace; import sirius.db.mixing.annotations.Trim; import sirius.db.mixing.annotations.UpperCase; +import sirius.kernel.commons.StringCleanup; import sirius.kernel.commons.Strings; import sirius.kernel.commons.Value; import sirius.kernel.di.std.Register; @@ -48,6 +50,7 @@ public class StringProperty extends Property implements SQLPropertyInfo, ESPrope private final boolean lowerCase; private final boolean upperCase; private final boolean lob; + private final boolean removeWhiteSpaces; /** * Factory for generating properties based on their field type @@ -75,6 +78,7 @@ protected StringProperty(EntityDescriptor descriptor, AccessPath accessPath, Fie this.lowerCase = field.isAnnotationPresent(LowerCase.class); this.upperCase = field.isAnnotationPresent(UpperCase.class); this.lob = field.isAnnotationPresent(Lob.class); + this.removeWhiteSpaces = field.isAnnotationPresent(RemoveWhitespace.class); } @Override @@ -122,6 +126,14 @@ protected void setValueToField(Object value, Object target) { value = null; } } + if (removeWhiteSpaces) { + if (value != null) { + value = StringCleanup.removeWhitespace(value.toString()); + } + if ("".equals(value)) { + value = null; + } + } super.setValueToField(value, target); } @@ -130,6 +142,13 @@ protected boolean isConsideredNull(Object propertyValue) { if (trim) { return Strings.isEmpty(Strings.trim(propertyValue)); } + if (removeWhiteSpaces) { + if (propertyValue == null) { + return true; + } else { + return Strings.isEmpty(StringCleanup.removeWhitespace(propertyValue.toString())); + } + } return Strings.isEmpty(propertyValue); } @@ -158,7 +177,7 @@ public Object transformValue(Value value) { @Override public void onBeforeSaveChecks(Object entity) { String value = (String) getValue(entity); - if (value != null && (trim || lowerCase || upperCase)) { + if (value != null && hasAnnotationModifications()) { value = applyAnnotationModifications(entity, value); setValue(entity, value); } @@ -176,6 +195,10 @@ public void onBeforeSaveChecks(Object entity) { } } + private boolean hasAnnotationModifications() { + return trim || removeWhiteSpaces || lowerCase || upperCase; + } + @Nullable private String applyAnnotationModifications(Object entity, String value) { String modifiedValue = value; @@ -183,6 +206,9 @@ private String applyAnnotationModifications(Object entity, String value) { if (trim) { modifiedValue = modifiedValue.trim(); } + if (removeWhiteSpaces) { + modifiedValue = StringCleanup.removeWhitespace(modifiedValue); + } if (modifiedValue.isEmpty()) { modifiedValue = null; } else if (lowerCase) { diff --git a/src/test/java/sirius/db/jdbc/StringManipulationTestEntity.java b/src/test/java/sirius/db/jdbc/StringManipulationTestEntity.java index 682d16f3..50066f21 100644 --- a/src/test/java/sirius/db/jdbc/StringManipulationTestEntity.java +++ b/src/test/java/sirius/db/jdbc/StringManipulationTestEntity.java @@ -12,6 +12,7 @@ import sirius.db.mixing.annotations.Length; import sirius.db.mixing.annotations.LowerCase; import sirius.db.mixing.annotations.NullAllowed; +import sirius.db.mixing.annotations.RemoveWhitespace; import sirius.db.mixing.annotations.Trim; import sirius.db.mixing.annotations.UpperCase; @@ -49,6 +50,12 @@ public class StringManipulationTestEntity extends SQLEntity { @Trim private String trimmedUpper; + public static final Mapping REMOVE_WHITE_SPACES = Mapping.named("removeWhiteSpaces"); + @Length(100) + @NullAllowed + @RemoveWhitespace + private String removeWhiteSpaces; + public String getTrimmed() { return trimmed; } @@ -88,4 +95,12 @@ public String getTrimmedUpper() { public void setTrimmedUpper(String trimmedUpper) { this.trimmedUpper = trimmedUpper; } + + public String getRemoveWhiteSpaces() { + return removeWhiteSpaces; + } + + public void setRemoveWhiteSpaces(String removeWhiteSpaces) { + this.removeWhiteSpaces = removeWhiteSpaces; + } } diff --git a/src/test/kotlin/sirius/db/jdbc/StringPropertyTest.kt b/src/test/kotlin/sirius/db/jdbc/StringPropertyTest.kt index 9aab585d..d96bbb1f 100644 --- a/src/test/kotlin/sirius/db/jdbc/StringPropertyTest.kt +++ b/src/test/kotlin/sirius/db/jdbc/StringPropertyTest.kt @@ -40,6 +40,7 @@ class StringPropertyTest { stringManipulationTestEntity.upper = " test " stringManipulationTestEntity.trimmedLower = " TEST " stringManipulationTestEntity.trimmedUpper = " test " + stringManipulationTestEntity.removeWhiteSpaces = " foo\tbar " oma.update(stringManipulationTestEntity) stringManipulationTestEntity = oma.refreshOrFail(stringManipulationTestEntity) @@ -49,6 +50,7 @@ class StringPropertyTest { assertEquals(" TEST ", stringManipulationTestEntity.upper) assertEquals("test", stringManipulationTestEntity.trimmedLower) assertEquals("TEST", stringManipulationTestEntity.trimmedUpper) + assertEquals("foobar", stringManipulationTestEntity.removeWhiteSpaces) } @Test @@ -62,6 +64,7 @@ class StringPropertyTest { assertNull(stringManipulationTestEntity.trimmed) assertNull(stringManipulationTestEntity.lower) assertNull(stringManipulationTestEntity.trimmedLower) + assertNull(stringManipulationTestEntity.removeWhiteSpaces) } companion object {