Search in sources :

Example 1 with AbstractVOService

use of com.infiniteautomation.mango.spring.service.AbstractVOService 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);
            }
    }
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) NotFoundRestException(com.infiniteautomation.mango.rest.latest.exception.NotFoundRestException) PatchVORequestBody(com.infiniteautomation.mango.rest.latest.patch.PatchVORequestBody) AbstractVOService(com.infiniteautomation.mango.spring.service.AbstractVOService) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder)

Aggregations

NotFoundRestException (com.infiniteautomation.mango.rest.latest.exception.NotFoundRestException)1 PatchVORequestBody (com.infiniteautomation.mango.rest.latest.patch.PatchVORequestBody)1 AbstractVOService (com.infiniteautomation.mango.spring.service.AbstractVOService)1 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)1 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)1