This repository has been archived by the owner on Nov 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #627 from hohwille/feature-spring-data
add advanced spring-data support and make it first class citizen but keep dao support for compatibility and non-spring/JEE contexts.
- Loading branch information
Showing
87 changed files
with
3,068 additions
and
739 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
modules/basic/src/main/java/io/oasp/module/basic/common/api/query/LikePatternSyntax.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package io.oasp.module.basic.common.api.query; | ||
|
||
/** | ||
* Enum defining available syntaxes for a match pattern in a LIKE-clause. While databases typically require {@link #SQL} | ||
* syntax, human user expect {@link #GLOB} syntax in search forms. Therefore this enum also supports | ||
* {@link #convert(String, LikePatternSyntax, boolean) conversion} from one syntax to another. | ||
* | ||
* @since 3.0.0 | ||
*/ | ||
public enum LikePatternSyntax { | ||
|
||
/** | ||
* Glob syntax that is typically expected by end-users and supported by typical search forms. It uses asterisk ('*') | ||
* for {@link #getAny() any wildcard} and question-mark ('?') for {@link #getSingle() single wildcard}. | ||
*/ | ||
GLOB('*', '?'), | ||
|
||
/** | ||
* SQL syntax that is typically required by databases. It uses percent ('%') for {@link #getAny() any wildcard} and | ||
* underscore ('_') for {@link #getSingle() single wildcard}. | ||
*/ | ||
SQL('%', '_'); | ||
|
||
/** The escape character. */ | ||
public static final char ESCAPE = '\\'; | ||
|
||
private final char any; | ||
|
||
private final char single; | ||
|
||
private LikePatternSyntax(char any, char single) { | ||
|
||
this.any = any; | ||
this.single = single; | ||
} | ||
|
||
/** | ||
* @return the wildcard character that matches any string including the {@link String#isEmpty() empty} string. | ||
*/ | ||
public char getAny() { | ||
|
||
return this.any; | ||
} | ||
|
||
/** | ||
* @return the wildcard character that matches exactly one single character. | ||
*/ | ||
public char getSingle() { | ||
|
||
return this.single; | ||
} | ||
|
||
/** | ||
* @param pattern the LIKE pattern in the given {@link LikePatternSyntax}. | ||
* @param syntax the {@link LikePatternSyntax} of the given {@code pattern}. | ||
* @return the given {@code pattern} converted to this {@link LikePatternSyntax}. | ||
*/ | ||
public String convert(String pattern, LikePatternSyntax syntax) { | ||
|
||
return convert(pattern, syntax, false); | ||
} | ||
|
||
/** | ||
* @param pattern the LIKE pattern in the given {@link LikePatternSyntax}. | ||
* @param syntax the {@link LikePatternSyntax} of the given {@code pattern}. | ||
* @param matchSubstring - {@code true} if the given {@code pattern} shall also match substrings, {@code false} | ||
* otherwise. | ||
* @return the given {@code pattern} converted to this {@link LikePatternSyntax}. | ||
*/ | ||
public String convert(String pattern, LikePatternSyntax syntax, boolean matchSubstring) { | ||
|
||
if ((pattern == null) || pattern.isEmpty()) { | ||
if (matchSubstring) { | ||
return Character.toString(this.any); | ||
} else { | ||
return pattern; | ||
} | ||
} | ||
if (this == syntax) { | ||
String result = pattern; | ||
if (matchSubstring) { | ||
if (pattern.charAt(0) != this.any) { | ||
result = this.any + result; | ||
} | ||
int lastIndex = pattern.length() - 1; | ||
if ((pattern.charAt(lastIndex) != this.any) || ((lastIndex > 0) && (pattern.charAt(lastIndex - 1) == ESCAPE))) { | ||
result = result + this.any; | ||
} | ||
} | ||
return result; | ||
} | ||
int length = pattern.length(); | ||
StringBuilder sb = new StringBuilder(length + 8); | ||
boolean lastWildcardAny = false; | ||
for (int i = 0; i < length; i++) { | ||
lastWildcardAny = false; | ||
char c = pattern.charAt(i); | ||
if (c == syntax.any) { | ||
c = this.any; | ||
lastWildcardAny = true; | ||
} else if (c == syntax.single) { | ||
c = this.single; | ||
} else if ((c == this.any) || (c == this.single) || (c == ESCAPE)) { | ||
if ((i == 0) && matchSubstring) { | ||
sb.append(this.any); | ||
} | ||
sb.append(ESCAPE); | ||
} | ||
if (matchSubstring && (i == 0) && !lastWildcardAny) { | ||
sb.append(this.any); | ||
} | ||
sb.append(c); | ||
} | ||
if (matchSubstring && !lastWildcardAny) { | ||
sb.append(this.any); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
/** | ||
* @param pattern the string value that may be a pattern. | ||
* @return the {@link LikePatternSyntax} for the given {@code pattern} or {@code null} if the given {@code pattern} | ||
* does not contain any wildcards. | ||
*/ | ||
public static LikePatternSyntax autoDetect(String pattern) { | ||
|
||
if ((pattern == null) || pattern.isEmpty()) { | ||
return null; | ||
} | ||
for (LikePatternSyntax syntax : values()) { | ||
if (pattern.indexOf(syntax.any) > 0) { | ||
return syntax; | ||
} else if (pattern.indexOf(syntax.single) > 0) { | ||
return syntax; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
} |
113 changes: 113 additions & 0 deletions
113
modules/basic/src/main/java/io/oasp/module/basic/common/api/query/StringSearchConfigTo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package io.oasp.module.basic.common.api.query; | ||
|
||
import io.oasp.module.basic.common.api.to.AbstractTo; | ||
|
||
/** | ||
* {@link AbstractTo TO} for the options to search for a string value. | ||
* | ||
* @since 3.0.0 | ||
*/ | ||
public class StringSearchConfigTo extends AbstractTo { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
private boolean ignoreCase; | ||
|
||
private boolean matchSubstring; | ||
|
||
private LikePatternSyntax likeSyntax; | ||
|
||
private StringSearchOperator operator; | ||
|
||
/** | ||
* @return {@code true} to ignore the case, {@code false} otherwise (to search case-sensitive). | ||
*/ | ||
public boolean isIgnoreCase() { | ||
|
||
return this.ignoreCase; | ||
} | ||
|
||
/** | ||
* @param ignoreCase new value of {@link #isIgnoreCase()}. | ||
*/ | ||
public void setIgnoreCase(boolean ignoreCase) { | ||
|
||
this.ignoreCase = ignoreCase; | ||
} | ||
|
||
/** | ||
* @return matchSubstring {@code true} if search string shall also match substrings of the string values to search on. | ||
*/ | ||
public boolean isMatchSubstring() { | ||
|
||
return this.matchSubstring; | ||
} | ||
|
||
/** | ||
* @param matchSubstring new value of {@link #isMatchSubstring()}. | ||
*/ | ||
public void setMatchSubstring(boolean matchSubstring) { | ||
|
||
this.matchSubstring = matchSubstring; | ||
} | ||
|
||
/** | ||
* @return the {@link LikePatternSyntax} of the search string used to do a LIKE-search, {@code null} for no | ||
* LIKE-search. Shall be {@code null} if {@link #getOperator() operator} is neither {@code null} nor | ||
* {@link StringSearchOperator#LIKE}. | ||
*/ | ||
public LikePatternSyntax getLikeSyntax() { | ||
|
||
return this.likeSyntax; | ||
} | ||
|
||
/** | ||
* @param likeSyntax new value of {@link #getLikeSyntax()}. | ||
*/ | ||
public void setLikeSyntax(LikePatternSyntax likeSyntax) { | ||
|
||
this.likeSyntax = likeSyntax; | ||
} | ||
|
||
/** | ||
* @return operator the {@link StringSearchOperator} used to search. If {@code null} a "magic auto mode" is used where | ||
* {@link StringSearchOperator#LIKE} is used in case the search string contains wildcards and | ||
* {@link StringSearchOperator#EQ} is used otherwise. | ||
*/ | ||
public StringSearchOperator getOperator() { | ||
|
||
return this.operator; | ||
} | ||
|
||
/** | ||
* @param operator new value of {@link #getOperator()}. | ||
*/ | ||
public void setOperator(StringSearchOperator operator) { | ||
|
||
this.operator = operator; | ||
} | ||
|
||
/** | ||
* @param operator the {@link StringSearchOperator}. | ||
* @return a new {@link StringSearchConfigTo} with the given config. | ||
*/ | ||
public static StringSearchConfigTo of(StringSearchOperator operator) { | ||
|
||
StringSearchConfigTo result = new StringSearchConfigTo(); | ||
result.setOperator(operator); | ||
return result; | ||
} | ||
|
||
/** | ||
* @param syntax the {@link LikePatternSyntax}. | ||
* @return a new {@link StringSearchConfigTo} with the given config. | ||
*/ | ||
public static StringSearchConfigTo of(LikePatternSyntax syntax) { | ||
|
||
StringSearchConfigTo result = new StringSearchConfigTo(); | ||
result.setOperator(StringSearchOperator.LIKE); | ||
result.setLikeSyntax(syntax); | ||
return result; | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
modules/basic/src/main/java/io/oasp/module/basic/common/api/query/StringSearchOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package io.oasp.module.basic.common.api.query; | ||
|
||
/** | ||
* Enum defining available operators for a string search or string comparison. | ||
* | ||
* @since 3.0.0 | ||
*/ | ||
public enum StringSearchOperator { | ||
|
||
/** Matches if strings are {@link String#equals(Object) equal}. */ | ||
EQ("=="), | ||
|
||
/** Matches if strings are NOT {@link String#equals(Object) equal}. */ | ||
NE("!="), | ||
|
||
/** Matches if search value is less than search hit(s) in {@link String#compareTo(String) lexicographical order}. */ | ||
LT("<"), | ||
|
||
/** | ||
* Matches if search value is less or equal to search hit(s) in {@link String#compareTo(String) lexicographical | ||
* order}. | ||
*/ | ||
LE("<="), | ||
|
||
/** | ||
* Matches if search value is greater than search hit(s) in {@link String#compareTo(String) lexicographical order}. | ||
*/ | ||
GT(">"), | ||
|
||
/** | ||
* Matches if search value is greater or equal to search hit(s) in {@link String#compareTo(String) lexicographical | ||
* order}. | ||
*/ | ||
GE(">="), | ||
|
||
/** | ||
* Matches if search value as pattern matches search hit(s) in <em>LIKE</em> search. | ||
* | ||
* @see LikePatternSyntax | ||
*/ | ||
LIKE("LIKE"), | ||
|
||
/** | ||
* Matches if search value as pattern does not match search hit(s) in <em>LIKE</em> search. | ||
* | ||
* @see LikePatternSyntax | ||
*/ | ||
NOT_LIKE("NOT LIKE"); | ||
|
||
private final String operator; | ||
|
||
private StringSearchOperator(String operator) { | ||
|
||
this.operator = operator; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
|
||
return this.operator; | ||
} | ||
|
||
} |
Oops, something went wrong.