Refactor RSQL search fields related classes (4) (#1837)

Signed-off-by: Marinov Avgustin <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2024-09-10 12:29:55 +03:00
committed by GitHub
parent dd500b4d53
commit 62734e936a

View File

@@ -45,26 +45,16 @@ public abstract class AbstractRSQLVisitor<A extends Enum<A> & RsqlQueryField> {
} }
protected QuertPath getQuertPath(final ComparisonNode node) { protected QuertPath getQuertPath(final ComparisonNode node) {
final String[] path = node.getSelector().split(RsqlQueryField.SUB_ATTRIBUTE_SPLIT_REGEX); final int firstSeparatorIndex = node.getSelector().indexOf(RsqlQueryField.SUB_ATTRIBUTE_SEPARATOR);
if (path.length == 0) { final String enumName = (firstSeparatorIndex == -1 ? node.getSelector() : node.getSelector().substring(0, firstSeparatorIndex)).toUpperCase();
throw createRSQLParameterUnsupportedException(node, null); log.debug("Get field identifier by name {} of enum type {}", enumName, rsqlQueryFieldType);
}
final String enumName = path[0].toUpperCase();
log.debug("get field identifier by name {} of enum type {}", enumName, rsqlQueryFieldType);
try { try {
final A enumValue = Enum.valueOf(rsqlQueryFieldType, enumName); final A enumValue = Enum.valueOf(rsqlQueryFieldType, enumName);
String[] split = getSplit(enumValue, node.getSelector()); String[] split = getSplit(enumValue, node.getSelector());
// validate // validate
if (enumValue.isMap()) { if (!enumValue.isMap()) {
// <enum>.<key>
if (split.length != 2) {
throw new RSQLParameterUnsupportedFieldException(
"The syntax of the given map search parameter field {" + node.getSelector() + "} is wrong. Syntax is: <enum name>.<key name>");
}
} else {
// sub entity need minimum 1 dot // sub entity need minimum 1 dot
if (!enumValue.getSubEntityAttributes().isEmpty() && split.length < 2) { if (!enumValue.getSubEntityAttributes().isEmpty() && split.length < 2) {
if (enumValue.getSubEntityAttributes().size() == 1) { // single sub attribute - so default if (enumValue.getSubEntityAttributes().size() == 1) { // single sub attribute - so default
@@ -93,20 +83,14 @@ public abstract class AbstractRSQLVisitor<A extends Enum<A> & RsqlQueryField> {
} }
} }
/**
* Returns the sub attributes
*
* @param rsqlFieldName the given field
* @return array consisting of sub attributes
*/
private String[] getSplit(final A enumValue, final String rsqlFieldName) { private String[] getSplit(final A enumValue, final String rsqlFieldName) {
if (enumValue.isMap()) { if (enumValue.isMap()) {
final String[] subAttributes = rsqlFieldName.split(RsqlQueryField.SUB_ATTRIBUTE_SPLIT_REGEX, 2); final String[] split = rsqlFieldName.split(RsqlQueryField.SUB_ATTRIBUTE_SPLIT_REGEX, 2);
// [0] field name | [1] key name (could miss, e.g. for target attributes) if (split.length != 2 || ObjectUtils.isEmpty(split[1])) {
final String mapKeyName = subAttributes.length == 2 ? subAttributes[1] : null; throw new RSQLParameterUnsupportedFieldException(
return ObjectUtils.isEmpty(mapKeyName) ? "The syntax of the given map search parameter field {" + rsqlFieldName + "} is wrong. Syntax is: <enum name>.<key name>");
new String[] { enumValue.getJpaEntityFieldName() } : }
new String[] { enumValue.getJpaEntityFieldName(), mapKeyName }; return split;
} else { } else {
return rsqlFieldName.split(RsqlQueryField.SUB_ATTRIBUTE_SPLIT_REGEX); return rsqlFieldName.split(RsqlQueryField.SUB_ATTRIBUTE_SPLIT_REGEX);
} }