Skip to content

Commit a59c035

Browse files
committed
some refactoring.
1 parent 43f80c4 commit a59c035

File tree

2 files changed

+65
-53
lines changed

2 files changed

+65
-53
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package fr.fxjavadevblog.aid.utils.jaxrs;
2+
3+
import fr.fxjavadevblog.aid.utils.jaxrs.Filtering.Operation;
4+
import lombok.Builder;
5+
import lombok.Getter;
6+
7+
@Builder
8+
@Getter
9+
public class Filter
10+
{
11+
private String field;
12+
private Operation operation;
13+
private Class<?> type;
14+
private Object value;
15+
16+
@Override
17+
public String toString() {
18+
StringBuilder builder = new StringBuilder();
19+
builder.append(field);
20+
builder.append(" ");
21+
builder.append(operation.getHsqlOperation());
22+
builder.append(" ");
23+
builder.append(":");
24+
builder.append(field);
25+
return builder.toString();
26+
}
27+
}

src/main/java/fr/fxjavadevblog/aid/utils/jaxrs/Filtering.java

Lines changed: 38 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,13 @@ public class Filtering
5252

5353
public enum Operation
5454
{
55-
EQUALS("="), LIKE("LIKE"), GREATER_THAN(">="), LESSER_THAN("<="), BETWEEN("TODO");
55+
EQUALS("="), LIKE("LIKE"), GREATER_THAN(">="), LESSER_THAN("<=");
5656

5757
@Getter
5858
private final String hsqlOperation;
5959

60-
private Operation(String operation) {
60+
private Operation(String operation)
61+
{
6162
this.hsqlOperation = operation;
6263
}
6364
}
@@ -72,31 +73,8 @@ private Operation(String operation) {
7273
operationAliases.put("like", Operation.LIKE);
7374
operationAliases.put("gte", Operation.GREATER_THAN);
7475
operationAliases.put("gte", Operation.GREATER_THAN);
75-
operationAliases.put("between", Operation.BETWEEN);
7676
}
7777

78-
@Builder
79-
@Getter
80-
public static class Filter
81-
{
82-
private String field;
83-
private Operation operation;
84-
private Class<?> type;
85-
private Object value;
86-
87-
@Override
88-
public String toString() {
89-
StringBuilder builder = new StringBuilder();
90-
builder.append(field);
91-
builder.append(" ");
92-
builder.append(operation.getHsqlOperation());
93-
builder.append(" ");
94-
builder.append(":");
95-
builder.append(field);
96-
return builder.toString();
97-
}
98-
}
99-
10078
public void setModelClass(Class <?> clazz)
10179
{
10280
this.modelClass = clazz;
@@ -128,34 +106,8 @@ public Map<String, Object> getParameterMap()
128106
{
129107
try
130108
{
131-
Class <?> targetType = modelClass.getDeclaredField(k).getType();
132-
133-
// inits.
134-
Operation operation = Operation.EQUALS;
135-
String value = v.get(0);
136-
137-
// testing if the value as a specific operation "like:", "gte:", etc.
138-
Matcher matcher = pattern.matcher(value);
139-
if (matcher.matches())
140-
{
141-
String operationValue = matcher.group(1);
142-
operation = operationAliases.get(operationValue);
143-
value = matcher.group(3);
144-
if (operation == Operation.LIKE)
145-
{
146-
value = "%"+value+"%";
147-
}
148-
}
149-
150-
Object convertedValue = ConvertUtils.convert(value, targetType);
151-
152-
Filter f = Filter.builder()
153-
.field(k)
154-
.operation(operation)
155-
.value(convertedValue)
156-
.type(targetType)
157-
.build();
158-
log.info("Adding filter [{}] {} [{}]", f.field, f.operation, f.value);
109+
Filter f = createFilter(k, v.get(0));
110+
log.info("Adding filter [{}] {} [{}]", f.getField(), f.getOperation(), f.getValue());
159111
filters.add(f);
160112
}
161113
catch(NoSuchFieldException ex)
@@ -168,4 +120,37 @@ public Map<String, Object> getParameterMap()
168120
}
169121

170122

123+
124+
private Filter createFilter(String paramName, String paramValue) throws NoSuchFieldException
125+
{
126+
Class <?> targetType = modelClass.getDeclaredField(paramName).getType();
127+
128+
// inits.
129+
Operation operation = Operation.EQUALS;
130+
String value = paramValue;
131+
132+
// testing if the value as a specific operation "like:", "gte:", etc.
133+
Matcher matcher = pattern.matcher(value);
134+
if (matcher.matches())
135+
{
136+
String operationValue = matcher.group(1);
137+
operation = operationAliases.get(operationValue);
138+
value = matcher.group(3);
139+
if (operation == Operation.LIKE)
140+
{
141+
value = "%"+value+"%";
142+
}
143+
}
144+
145+
Object convertedValue = ConvertUtils.convert(value, targetType);
146+
147+
return Filter.builder()
148+
.field(paramName)
149+
.operation(operation)
150+
.value(convertedValue)
151+
.type(targetType)
152+
.build();
153+
}
154+
155+
171156
}

0 commit comments

Comments
 (0)