Skip to content

Commit eda1f96

Browse files
committed
fix: 修正一处 NPE 问题
1 parent 53a68d8 commit eda1f96

File tree

1 file changed

+34
-3
lines changed
  • eaphone-spring-data-query-mongodb/src/main/java/com/eaphonetech/common/datatables/mongodb/repository

1 file changed

+34
-3
lines changed

eaphone-spring-data-query-mongodb/src/main/java/com/eaphonetech/common/datatables/mongodb/repository/QueryUtils.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,19 +245,29 @@ private static String getFieldName(Class<?> javaType, String fieldName) {
245245
*/
246246
private static <T, ID extends Serializable> List<Criteria> getCriteria(final QueryInput input,
247247
MongoEntityInformation<T, ID> entityInformation) {
248+
return getCriteria(input, entityInformation.getJavaType());
249+
}
250+
251+
/**
252+
* Convert a {@link QueryInput} to Criteia
253+
*
254+
* @param input
255+
* @return
256+
*/
257+
private static <T, ID extends Serializable> List<Criteria> getCriteria(final QueryInput input, Class<T> javaType) {
248258
List<Criteria> result = new LinkedList<>();
249259
// check for each searchable column whether a filter value exists
250260
for (final Map.Entry<String, QueryFilter> entry : input.getWhere().entrySet()) {
251261
final QueryFilter filter = entry.getValue();
252262
final String fieldName = entry.getKey();
253-
final ColumnType type = getFieldType(entityInformation.getJavaType(), fieldName);
263+
final ColumnType type = getFieldType(javaType, fieldName);
254264
if (type == null) {
255265
throw new RuntimeException(String.format("field [%s] not exists", fieldName));
256266
}
257267
// handle column.filter
258268
if (filter != null) {
259269
boolean hasValidCrit = false;
260-
final String queryFieldName = getFieldName(entityInformation.getJavaType(), fieldName);
270+
final String queryFieldName = getFieldName(javaType, fieldName);
261271
Criteria c = Criteria.where(queryFieldName);
262272
if (filter.get_eq() != null) {
263273
// $eq takes first place
@@ -436,6 +446,24 @@ public static Pattern getLikeFilterPattern(String filterValue) {
436446
return Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
437447
}
438448

449+
/**
450+
* Convert {@link QueryInput} to {@link AggregationOperation}[], mainly for column searches.
451+
*
452+
* @param input
453+
* @return
454+
*/
455+
private static <T, ID extends Serializable> List<AggregationOperation> toAggregationOperation(Class<T> javaType,
456+
QueryInput input) {
457+
List<AggregationOperation> result = new LinkedList<>();
458+
List<Criteria> criteriaList = getCriteria(input, javaType);
459+
if (criteriaList != null) {
460+
for (final Criteria c : criteriaList) {
461+
result.add(match(c));
462+
}
463+
}
464+
return result;
465+
}
466+
439467
/**
440468
* Convert {@link QueryInput} to {@link AggregationOperation}[], mainly for column searches.
441469
*
@@ -511,7 +539,7 @@ public static <T> TypedAggregation<T> makeAggregation(Class<T> classOfT, QueryIn
511539
opList.addAll(preFilteringOperations);
512540
}
513541

514-
opList.addAll(toAggregationOperation(null, input));
542+
opList.addAll(toAggregationOperation(classOfT, input));
515543

516544
if (postFilteringOperations != null) {
517545
opList.addAll(postFilteringOperations);
@@ -525,6 +553,9 @@ public static <T> TypedAggregation<T> makeAggregation(Class<T> classOfT, QueryIn
525553
opList.add(skip((long) pageable.getOffset()));
526554
opList.add(limit(pageable.getPageSize()));
527555
}
556+
if (opList.isEmpty()) {
557+
return null;
558+
}
528559
return newAggregation(classOfT, opList);
529560
}
530561

0 commit comments

Comments
 (0)