use of org.webpieces.router.api.extensions.ParamMeta in project webpieces by deanhiller.
the class HibernateLookup method findEntityImpl.
private <T> T findEntityImpl(ParamMeta paramMeta, ParamTreeNode tree, Function<Class<T>, T> beanCreate, EntityManager entityManager) {
ParamMeta m = paramMeta;
Class<T> paramTypeToCreate = (Class<T>) m.getFieldClass();
Metamodel metamodel = entityManager.getMetamodel();
ManagedType<T> managedType = metamodel.managedType(paramTypeToCreate);
IdentifiableType<T> entityType = (IdentifiableType<T>) managedType;
Class<?> idClazz = entityType.getIdType().getJavaType();
SingularAttribute<? super T, ?> idAttribute = entityType.getId(idClazz);
String name = idAttribute.getName();
ParamNode paramNode = tree.get(name);
String value = null;
if (paramNode != null) {
if (!(paramNode instanceof ValueNode))
throw new IllegalStateException("The id field in the hibernate entity should have matched to a " + "ValueNode on incoming data and did not. node=" + paramNode + ". bad multipart form? (Please " + "let us know so we can pair with you on this and I can add better error messaging)");
ValueNode node = (ValueNode) paramNode;
value = node.getValue();
}
if (value == null)
return beanCreate.apply(paramTypeToCreate);
@SuppressWarnings("rawtypes") ObjectStringConverter unmarshaller = translator.getConverter(idClazz);
Object id = unmarshaller.stringToObject(value);
UseQuery namedQuery = fetchUseQuery(m.getAnnotations());
if (namedQuery == null)
return entityManager.find(paramTypeToCreate, id);
Query query = entityManager.createNamedQuery(namedQuery.value());
query.setParameter(namedQuery.id(), id);
return (T) query.getSingleResult();
}
use of org.webpieces.router.api.extensions.ParamMeta in project webpieces by deanhiller.
the class ParamToObjectTranslatorImpl method createArgsImpl.
protected XFuture<List<Object>> createArgsImpl(Method method, RequestContext ctx, BodyContentBinder binder) {
RouterRequest req = ctx.getRequest();
Parameter[] paramMetas = method.getParameters();
Annotation[][] paramAnnotations = method.getParameterAnnotations();
ParamTreeNode paramTree = new ParamTreeNode();
// For multipart AND for query params such as ?var=xxx&var=yyy&var2=xxx AND for url path params /mypath/{var1}/account/{id}
// query params first
Map<String, String> queryParams = translate(req.queryParams);
treeCreator.createTree(paramTree, queryParams, FromEnum.QUERY_PARAM);
// next multi-part params
Map<String, String> multiPartParams = translate(req.multiPartFields);
treeCreator.createTree(paramTree, multiPartParams, FromEnum.FORM_MULTIPART);
// lastly path params
treeCreator.createTree(paramTree, ctx.getPathParams(), FromEnum.URL_PATH);
List<Object> results = new ArrayList<>();
XFuture<List<Object>> future = XFuture.completedFuture(results);
for (int i = 0; i < paramMetas.length; i++) {
Parameter paramMeta = paramMetas[i];
Annotation[] annotations = paramAnnotations[i];
ParamMeta fieldMeta = new ParamMeta(method, paramMeta, annotations);
String name = fieldMeta.getName();
ParamNode paramNode = paramTree.get(name);
XFuture<Object> beanFuture;
if (binder != null && isManagedBy(binder, fieldMeta)) {
Object bean = binder.unmarshal(ctx, fieldMeta, req.body.createByteArray());
beanFuture = XFuture.completedFuture(bean);
} else {
beanFuture = translate(req, method, paramNode, fieldMeta, ctx.getValidation());
}
future = future.thenCompose(list -> {
return beanFuture.thenApply(bean -> {
list.add(bean);
return list;
});
});
}
return future;
}
use of org.webpieces.router.api.extensions.ParamMeta in project webpieces by deanhiller.
the class DtoLookup method find.
@SuppressWarnings("unchecked")
@Override
public <T> XFuture<T> find(Meta paramMeta, ParamTreeNode tree, Function<Class<T>, T> beanCreate) {
if (!(paramMeta instanceof ParamMeta))
throw new UnsupportedOperationException("this plugin does not support type=" + paramMeta.getClass());
ParamMeta m = (ParamMeta) paramMeta;
Class<T> paramTypeToCreate = (Class<T>) m.getFieldClass();
Dto annotation = paramTypeToCreate.getAnnotation(Dto.class);
// EntityManager entityManager = Em.get();
// Metamodel metamodel = entityManager.getMetamodel();
// ManagedType<T> managedType = metamodel.managedType(paramTypeToCreate);
// IdentifiableType<T> entityType = (IdentifiableType<T>) managedType;
Method method;
try {
method = paramTypeToCreate.getMethod("getId");
} catch (NoSuchMethodException | SecurityException e) {
throw new IllegalStateException("Class=" + paramTypeToCreate.getName() + " has an annotation @Dto and this requires that dto to have a method getId, but we could not find that method", e);
}
Class<?> idClazz = method.getReturnType();
// SingularAttribute<? super T, ?> idAttribute = entityType.getId(idClazz);
// String name = idAttribute.getName();
String name = "id";
ParamNode paramNode = tree.get(name);
String value = null;
if (paramNode != null) {
if (!(paramNode instanceof ValueNode))
throw new IllegalStateException("The id field in the DTO should have matched to a " + "ValueNode on incoming data and did not. node=" + paramNode + ". bad multipart form? (Please " + "let us know so we can pair with you on this and I can add better error messaging)");
ValueNode node = (ValueNode) paramNode;
value = node.getValue();
}
if (value == null) {
T theBean = beanCreate.apply(paramTypeToCreate);
return XFuture.completedFuture(theBean);
}
@SuppressWarnings("rawtypes") ObjectStringConverter unmarshaller = translator.getConverter(idClazz);
Object id = unmarshaller.stringToObject(value);
Class lookupClass = annotation.lookupClass();
String function = annotation.function();
Object instance = webInjector.getCurrentInjector().getInstance(lookupClass);
Method lookupMethod;
try {
lookupMethod = lookupClass.getMethod(function, idClazz);
} catch (NoSuchMethodException | SecurityException e) {
throw new IllegalArgumentException("Your function in @Dto='XFuture<" + paramTypeToCreate + "> " + function + "(" + idClazz.getName() + ")' on class=" + lookupClass.getName() + " cannot be found. We also did not find method='" + paramTypeToCreate + " " + function + "(" + idClazz.getName() + ")'");
}
Object result;
try {
result = lookupMethod.invoke(instance, id);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new RuntimeException("Exception invoking lookup method", e);
}
if (result instanceof XFuture) {
return (XFuture<T>) result;
}
return (XFuture<T>) XFuture.completedFuture(result);
}
Aggregations