use of com.infiniteautomation.mango.rest.latest.patch.PatchVORequestBody in project ma-modules-public by infiniteautomation.
the class PartialUpdateArgumentResolver method resolveArgument.
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
ServletServerHttpRequest req = createInputMessage(webRequest);
PatchVORequestBody patch = parameter.getParameterAnnotation(PatchVORequestBody.class);
Class<?> serviceClass = patch.service();
AbstractVOService<?, ?> service = (AbstractVOService<?, ?>) context.getBean(serviceClass);
PermissionHolder user = Common.getUser();
// Set the source class into the request scope to use if validation fails
webRequest.setAttribute(MangoRequestBodyAdvice.MODEL_CLASS, patch.modelClass(), RequestAttributes.SCOPE_REQUEST);
Object vo;
switch(patch.idType()) {
case ID:
Integer id = Integer.parseInt(getPathVariables(webRequest).get(ID));
vo = service.get(id);
if (vo == null)
throw new NotFoundRestException();
else {
Object model = modelMapper.map(vo, patch.modelClass(), user);
return readJavaType(model, req);
}
case XID:
String xid = getPathVariables(webRequest).get(XID);
vo = service.get(xid);
if (vo == null)
throw new NotFoundRestException();
else {
Object model = modelMapper.map(vo, patch.modelClass(), user);
return readJavaType(model, req);
}
default:
case OTHER:
String other = getPathVariables(webRequest).get(patch.urlPathVariableName());
vo = service.get(other);
if (vo == null)
throw new NotFoundRestException();
else {
Object model = modelMapper.map(vo, patch.modelClass(), user);
return readJavaType(model, req);
}
}
}
Aggregations