com.github.aidensuen.mongo.parsing.DynamicCommandParser Maven / Gradle / Ivy
package com.github.aidensuen.mongo.parsing;
import com.github.aidensuen.mongo.command.OperationType;
import com.github.aidensuen.mongo.core.MongoDaoStatement;
import com.github.aidensuen.mongo.exception.MongoDaoException;
import com.github.aidensuen.mongo.mapping.DynamicCommand;
import com.github.aidensuen.mongo.mapping.TypeAliasRegistry;
import com.github.aidensuen.mongo.reflection.ParamNameResolver;
import com.github.aidensuen.util.StringUtil;
import org.springframework.core.ResolvableType;
import java.util.Collection;
import java.util.List;
import java.util.Map;
public final class DynamicCommandParser {
private static final TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();
private final ParamNameResolver resolver;
private final MongoDaoStatement ms;
public DynamicCommandParser(ParamNameResolver resolver, MongoDaoStatement.Builder builder) {
this.resolver = resolver;
this.ms = builder.build();
}
public DynamicCommand parser() {
DynamicCommand dynamicCommand = null;
String dynamicCommandStr = "";
String[] names = resolver.getNames();
if (names.length == 0) {
if (ms.getOperationType() == OperationType.AGGREGATE) {
dynamicCommandStr = "[]";
} else {
dynamicCommandStr = "{}";
}
} else if (names.length > 1 && ms.getOperationType() != OperationType.AGGREGATE) {
// initial args's length gt 1
dynamicCommandStr = StringUtil.appendStrs(dynamicCommandStr, "{");
for (int i = 0; i < names.length; i++) {
if (i == 0) {
dynamicCommandStr = StringUtil.appendStrs(dynamicCommandStr, "'", names[i], "': #{", names[i], "}");
} else {
dynamicCommandStr = StringUtil.appendStrs(dynamicCommandStr, ", '", names[i], "': #{", names[i], "}");
}
}
dynamicCommandStr = StringUtil.appendStrs(dynamicCommandStr, "}");
} else if (names.length == 1 && ms.getOperationType() != OperationType.AGGREGATE) {
Class parameterType = resolver.getResolvableType(0).resolve();
if (List.class.isAssignableFrom(parameterType)) {
ResolvableType resolvableType = resolver.getResolvableType(0);
Class> elementType = null;
if (resolvableType.hasGenerics()) {
elementType = resolvableType.getGeneric(0).resolve();
}
//java basic type
if (!Object.class.equals(elementType) && typeAliasRegistry.getTypeAliases().containsKey(elementType.getSimpleName().toLowerCase())) {
dynamicCommandStr = StringUtil.appendStrs("{'", names[0], "': {'$in': #{list}}}");
} else {
dynamicCommandStr = StringUtil.appendStrs("{$or: #{list}}");
}
} else if (Collection.class.isAssignableFrom(parameterType)) {
ResolvableType resolvableType = resolver.getResolvableType(0);
Class> elementType = null;
if (resolvableType.hasGenerics()) {
elementType = resolvableType.getGeneric(0).resolve();
}
//java basic type
if (!Object.class.equals(elementType) && typeAliasRegistry.getTypeAliases().containsKey(elementType.getSimpleName().toLowerCase())) {
dynamicCommandStr = StringUtil.appendStrs("{'", names[0], "': {'$in': #{collection}}}");
} else {
dynamicCommandStr = StringUtil.appendStrs("{$or: #{collection}}");
}
} else if (parameterType.isArray()) {
ResolvableType resolvableType = resolver.getResolvableType(0);
Class> elementType = resolvableType.getComponentType().resolve();
//java basic type
if (!Object.class.equals(elementType) && typeAliasRegistry.getTypeAliases().containsKey(elementType.getSimpleName().toLowerCase())) {
dynamicCommandStr = StringUtil.appendStrs("{'", names[0], "': {'$in': #{array}}}");
} else {
dynamicCommandStr = StringUtil.appendStrs("{$or: #{array}}");
}
} else if (Map.class.isAssignableFrom(parameterType)) {
// ordinary map
dynamicCommandStr = StringUtil.appendStrs("#{@value}");
} else if (!Object.class.equals(parameterType) && typeAliasRegistry.getTypeAliases().containsKey(parameterType.getSimpleName().toLowerCase())) {
dynamicCommandStr = StringUtil.appendStrs("{'", names[0], "': #{", names[0], "}}");
} else {
// other java type
dynamicCommandStr = StringUtil.appendStrs("#{@value}");
}
} else {
throw new MongoDaoException("can't parse dynamic command by param names in method, please add @Query or @Pipeline");
}
dynamicCommand = new DynamicCommand(dynamicCommandStr);
return dynamicCommand;
}
}