#18 Period overview: improvements

This commit is contained in:
2021-03-05 20:15:05 +01:00
parent 2aa0f7fa9c
commit 61cd310fb0
11 changed files with 243 additions and 74 deletions

View File

@@ -23,7 +23,7 @@ orderByExpression
regularExpression
: (stringExpression | intExpression | booleanExpression | dateExpression) ;
stringExpression
: field=IDENTIFIER operator=STRING_OPERATOR value=STRING_VALUE ;
: field=IDENTIFIER operator=STRING_OPERATOR value=(STRING_VALUE | ACCOUNT_TYPE_VALUE) ;
intExpression
: field=IDENTIFIER operator=(INT_OPERATOR | STRING_OPERATOR) value=INT_VALUE ;
@@ -96,34 +96,34 @@ fragment K : ('K' | 'k') ;
fragment SPACE : ' ' ;
// Keywords
BETWEEN : B E T W E E N ;
AND : A N D ;
OR : O R ;
ORDER_BY : O R D E R SPACE B Y ;
DESC : D E S C ;
ASC : A S C ;
TRUE : T R U E ;
FALSE : F A L S E ;
LIKE : L I K E ;
IN : I N ;
COMMA : ',' ;
BETWEEN : B E T W E E N ;
AND : A N D ;
OR : O R ;
ORDER_BY : O R D E R SPACE B Y ;
DESC : D E S C ;
ASC : A S C ;
TRUE : T R U E ;
FALSE : F A L S E ;
LIKE : L I K E ;
IN : I N ;
COMMA : ',' ;
// Constant values
CURRENT : C U R R E N T ;
LAST : L A S T ;
YEAR : Y E A R ;
GRAND_TOTAL : G R A N D '_' T O T A L ;
CURRENT_YEAR : CURRENT '_' YEAR ;
LAST_YEAR : LAST '_' YEAR ;
CURRENT : C U R R E N T ;
LAST : L A S T ;
YEAR : Y E A R ;
GRAND_TOTAL : G R A N D '_' T O T A L ;
CURRENT_YEAR : CURRENT '_' YEAR ;
LAST_YEAR : LAST '_' YEAR ;
STRING_OPERATOR : '=' ;
INT_OPERATOR : '>' | '<' | '>=' | '<=' | '!=' ;
L_PAREN : '(' ;
R_PAREN : ')' ;
IDENTIFIER : [a-zA-Z]+ ;
INT_VALUE : [0-9]+ ;
STRING_VALUE : '\'' [a-zA-Z0-9\-/.&%@$ ]+ '\'' ;
DATE_VALUE : [0-9][0-9][0-9][0-9][-][0-9][0-9][-][0-9][0-9] ; // ANTLR does not support regex quantifiers
STRING_OPERATOR : '=' ;
INT_OPERATOR : '>' | '<' | '>=' | '<=' | '!=' ;
L_PAREN : '(' ;
R_PAREN : ')' ;
IDENTIFIER : [a-zA-Z]+ ;
INT_VALUE : [0-9]+ ;
STRING_VALUE : '\'' [a-zA-Z0-9\-/.&%@$ ]+ '\'' ;
DATE_VALUE : [0-9][0-9][0-9][0-9][-][0-9][0-9][-][0-9][0-9] ; // ANTLR does not support regex quantifiers
NEWLINE : ('\r'? '\n' | '\r')+ ;
WHITESPACE : ' ' -> skip;
NEWLINE : ('\r'? '\n' | '\r')+ ;
WHITESPACE : ' ' -> skip;

View File

@@ -7,24 +7,38 @@ import de.financer.model.*;
import java.util.Arrays;
public enum FieldMapping {
AMOUNT("amount", Transaction_.AMOUNT, Transaction.class, NoopJoinHandler.class, JoinKey
.of(Transaction.class), null, IntHandler.class),
AMOUNT("amount", Transaction_.AMOUNT, Transaction.class, NoopJoinHandler.class,
JoinKey.of(Transaction.class), null, IntHandler.class),
PERIOD("period", Period_.ID, Period.class, PeriodJoinHandler.class, JoinKey.of(Period.class), null, PeriodConstHandler.class),
PERIOD("period", Period_.ID, Period.class, PeriodJoinHandler.class,
JoinKey.of(Period.class), null, PeriodConstHandler.class),
FROM_ACCOUNT("fromAccount", Account_.KEY, Account.class, FromAccountJoinHandler.class, JoinKey
.of(Account.class, "FROM"), null, StringHandler.class),
/**
* ONLY FOR INTERNAL USAGE - NOT EXPOSED TO USER
*/
PERIOD_ID("periodId", Period_.ID, Period.class, PeriodJoinHandler.class,
JoinKey.of(Period.class), null, PeriodIdHandler.class),
TO_ACCOUNT("toAccount", Account_.KEY, Account.class, ToAccountJoinHandler.class, JoinKey
.of(Account.class, "TO"), null, StringHandler.class),
FROM_ACCOUNT("fromAccount", Account_.KEY, Account.class, FromAccountJoinHandler.class,
JoinKey.of(Account.class, "FROM"), null, StringHandler.class),
FROM_ACCOUNT_GROUP("fromAccountGroup", AccountGroup_.NAME, AccountGroup.class,
AccountGroupJoinHandler.class, JoinKey.of(AccountGroup.class, "FROM"), FROM_ACCOUNT, StringHandler.class),
TO_ACCOUNT("toAccount", Account_.KEY, Account.class, ToAccountJoinHandler.class,
JoinKey.of(Account.class, "TO"), null, StringHandler.class),
TO_ACCOUNT_GROUP("toAccountGroup", AccountGroup_.NAME, AccountGroup.class,
AccountGroupJoinHandler.class, JoinKey.of(AccountGroup.class, "TO"), TO_ACCOUNT, StringHandler.class),
FROM_ACCOUNT_GROUP("fromAccountGroup", AccountGroup_.NAME, AccountGroup.class, AccountGroupJoinHandler.class,
JoinKey.of(AccountGroup.class, "FROM"), FROM_ACCOUNT, StringHandler.class),
DATE("date", Transaction_.DATE, Transaction.class, NoopJoinHandler.class, JoinKey.of(Transaction.class), null, DateHandler.class),
TO_ACCOUNT_GROUP("toAccountGroup", AccountGroup_.NAME, AccountGroup.class, AccountGroupJoinHandler.class,
JoinKey.of(AccountGroup.class, "TO"), TO_ACCOUNT, StringHandler.class),
FROM_ACCOUNT_TYPE("fromAccountType", Account_.TYPE, Account.class, FromAccountJoinHandler.class,
JoinKey.of(Account.class, "FROM"), null, AccountTypeHandler.class),
TO_ACCOUNT_TYPE("toAccountType", Account_.TYPE, Account.class, ToAccountJoinHandler.class,
JoinKey.of(Account.class, "TO"), null, AccountTypeHandler.class),
DATE("date", Transaction_.DATE, Transaction.class, NoopJoinHandler.class,
JoinKey.of(Transaction.class), null, DateHandler.class),
RECURRING("recurring", Transaction_.RECURRING_TRANSACTION, Transaction.class, NoopJoinHandler.class,
JoinKey.of(Transaction.class), null, NotNullSyntheticHandler.class),
@@ -32,10 +46,11 @@ public enum FieldMapping {
TAX_RELEVANT("taxRelevant", Transaction_.TAX_RELEVANT, Transaction.class, NoopJoinHandler.class,
JoinKey.of(Transaction.class), null, BooleanHandler.class),
HAS_FILE("hasFile", File_.ID, File.class, FileJoinHandler.class, JoinKey.of(File.class), null, NotNullSyntheticHandler.class),
HAS_FILE("hasFile", File_.ID, File.class, FileJoinHandler.class,
JoinKey.of(File.class), null, NotNullSyntheticHandler.class),
DESCRIPTION("description", Transaction_.DESCRIPTION, Transaction.class, NoopJoinHandler.class, JoinKey.of(Transaction.class),
null, StringHandler.class);
DESCRIPTION("description", Transaction_.DESCRIPTION, Transaction.class, NoopJoinHandler.class,
JoinKey.of(Transaction.class), null, StringHandler.class);
private final String fieldName;

View File

@@ -0,0 +1,32 @@
package de.financer.fql.field_handler;
import de.financer.fql.FQLException;
import de.financer.fql.FieldMapping;
import de.financer.fql.join_handler.JoinKey;
import de.financer.model.AccountType;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.From;
import javax.persistence.criteria.Predicate;
import java.util.Map;
public class AccountTypeHandler implements FieldHandler<String> {
@Override
public Predicate apply(FieldMapping fieldMapping, Map<JoinKey, From<?, ?>> froms, CriteriaBuilder criteriaBuilder, String value) {
return criteriaBuilder
.equal(froms.get(fieldMapping.getJoinKey()).get(fieldMapping.getAttributeName()), toAccountType(FieldHandlerUtils.removeQuotes(value)));
}
private static AccountType toAccountType(String value) {
if (value == null) {
throw new FQLException("NULL cannot be solved to AccountType!");
}
try {
return AccountType.valueOf(value.toUpperCase());
}
catch (IllegalArgumentException e) {
throw new FQLException(e);
}
}
}

View File

@@ -0,0 +1,24 @@
package de.financer.fql.field_handler;
import de.financer.fql.FQLException;
import de.financer.fql.FieldMapping;
import de.financer.fql.join_handler.JoinKey;
import org.apache.commons.lang3.math.NumberUtils;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.From;
import javax.persistence.criteria.Predicate;
import java.util.Map;
public class PeriodIdHandler implements FieldHandler<String> {
@Override
public Predicate apply(FieldMapping fieldMapping, Map<JoinKey, From<?, ?>> froms, CriteriaBuilder criteriaBuilder, String value) {
final String unquotedValue = FieldHandlerUtils.removeQuotes(value);
if (!NumberUtils.isCreatable(unquotedValue)) {
throw new FQLException(String.format("Invalid value %s for field periodId - only numbers allowed!", unquotedValue));
}
return criteriaBuilder.equal(froms.get(fieldMapping.getJoinKey()).get(fieldMapping.getAttributeName()), NumberUtils.toLong(unquotedValue));
}
}