Search in sources :

Example 1 with VoAction

use of com.infiniteautomation.mango.rest.latest.bulk.VoAction in project ma-modules-public by infiniteautomation.

the class EventDetectorsRestController method bulkEventDetectorOperationCSV.

@ApiOperation(value = "Bulk get/create/update/delete event detectors", notes = "User must have read permission for the data point or edit permission for the data source", consumes = MediaTypes.CSV_VALUE)
@RequestMapping(method = RequestMethod.POST, value = "/bulk", consumes = MediaTypes.CSV_VALUE)
public ResponseEntity<TemporaryResource<EventDetectorBulkResponse, AbstractRestException>> bulkEventDetectorOperationCSV(@RequestBody List<AbstractEventDetectorModel<? extends AbstractEventDetectorVO>> eds, @ApiParam(value = "Restart the source to load in the changes", required = false, defaultValue = "true", allowMultiple = false) @RequestParam(required = false, defaultValue = "true") boolean restart, UriComponentsBuilder builder) {
    EventDetectorBulkRequest bulkRequest = new EventDetectorBulkRequest();
    bulkRequest.setRequests(eds.stream().map(actionAndModel -> {
        AbstractEventDetectorModel<? extends AbstractEventDetectorVO> ed = actionAndModel;
        VoAction action = actionAndModel.getAction();
        String originalXid = actionAndModel.getOriginalXid();
        if (originalXid == null && ed != null) {
            originalXid = ed.getXid();
        }
        EventDetectorIndividualRequest request = new EventDetectorIndividualRequest();
        request.setAction(action == null ? VoAction.UPDATE : action);
        request.setXid(originalXid);
        request.setBody(ed);
        return request;
    }).collect(Collectors.toList()));
    return this.bulkEventDetectorOperation(bulkRequest, restart, builder);
}
Also used : VoAction(com.infiniteautomation.mango.rest.latest.bulk.VoAction) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with VoAction

use of com.infiniteautomation.mango.rest.latest.bulk.VoAction in project ma-modules-public by infiniteautomation.

the class PublishedPointsRestController method bulkPublishedPointOperation.

@ApiOperation(value = "Bulk get/create/update/delete published points", notes = "User be superadmin")
@RequestMapping(method = RequestMethod.POST, value = "/bulk")
public ResponseEntity<TemporaryResource<PublishedPointBulkResponse, AbstractRestException>> bulkPublishedPointOperation(@RequestBody PublishedPointBulkRequest requestBody, UriComponentsBuilder builder) {
    VoAction defaultAction = requestBody.getAction();
    AbstractPublishedPointModel<?> defaultBody = requestBody.getBody();
    List<PublishedPointIndividualRequest> requests = requestBody.getRequests();
    if (requests == null) {
        throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "requests"));
    } else if (requests.isEmpty()) {
        throw new BadRequestException(new TranslatableMessage("rest.error.cantBeEmpty", "requests"));
    }
    String resourceId = requestBody.getId();
    Long expiration = requestBody.getExpiration();
    Long timeout = requestBody.getTimeout();
    TemporaryResource<PublishedPointBulkResponse, AbstractRestException> responseBody = resourceManager.newTemporaryResource(RESOURCE_TYPE_BULK_PUBLISHED_POINT, resourceId, expiration, timeout, (resource) -> {
        PublishedPointBulkResponse bulkResponse = new PublishedPointBulkResponse();
        int i = 0;
        resource.progressOrSuccess(bulkResponse, i++, requests.size());
        PermissionHolder resourceUser = Common.getUser();
        for (PublishedPointIndividualRequest request : requests) {
            UriComponentsBuilder reqBuilder = UriComponentsBuilder.newInstance();
            PublishedPointIndividualResponse individualResponse = doIndividualRequest(request, defaultAction, defaultBody, resourceUser, reqBuilder);
            bulkResponse.addResponse(individualResponse);
            resource.progressOrSuccess(bulkResponse, i++, requests.size());
        }
        return null;
    });
    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(builder.path("/published-points/bulk/{id}").buildAndExpand(responseBody.getId()).toUri());
    return new ResponseEntity<>(responseBody, headers, HttpStatus.CREATED);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) VoAction(com.infiniteautomation.mango.rest.latest.bulk.VoAction) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) ResponseEntity(org.springframework.http.ResponseEntity) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) AbstractRestException(com.infiniteautomation.mango.rest.latest.exception.AbstractRestException) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with VoAction

use of com.infiniteautomation.mango.rest.latest.bulk.VoAction in project ma-modules-public by infiniteautomation.

the class DataPointRestController method doIndividualRequest.

private DataPointIndividualResponse doIndividualRequest(DataPointIndividualRequest request, VoAction defaultAction, DataPointModel defaultBody, PermissionHolder user, UriComponentsBuilder builder) {
    DataPointIndividualResponse result = new DataPointIndividualResponse();
    try {
        String xid = request.getXid();
        result.setXid(xid);
        VoAction action = request.getAction() == null ? defaultAction : request.getAction();
        if (action == null) {
            throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "action"));
        }
        result.setAction(action);
        DataPointModel body = request.getBody() == null ? defaultBody : request.getBody();
        switch(action) {
            case GET:
                if (xid == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "xid"));
                }
                result.setBody(this.getDataPoint(xid, user));
                break;
            case CREATE:
                if (body == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "body"));
                }
                result.setBody(body);
                result.setBody(this.createDataPoint(body, user, builder).getBody());
                break;
            case UPDATE:
                if (xid == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "xid"));
                }
                if (body == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "body"));
                }
                result.setBody(body);
                result.setBody(this.updateDataPoint(xid, body, user, builder).getBody());
                break;
            case DELETE:
                if (xid == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "xid"));
                }
                result.setBody(this.deleteDataPoint(xid, user));
                break;
        }
    } catch (Exception e) {
        result.exceptionCaught(e);
    }
    return result;
}
Also used : VoAction(com.infiniteautomation.mango.rest.latest.bulk.VoAction) DataPointModel(com.infiniteautomation.mango.rest.latest.model.dataPoint.DataPointModel) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) AbstractRestException(com.infiniteautomation.mango.rest.latest.exception.AbstractRestException)

Example 4 with VoAction

use of com.infiniteautomation.mango.rest.latest.bulk.VoAction in project ma-modules-public by infiniteautomation.

the class UserRestController method bulkUserOperationCSV.

@ApiOperation(value = "Bulk get/create/update/delete users", notes = "User must have read/edit permission for the user", consumes = MediaTypes.CSV_VALUE)
@RequestMapping(method = RequestMethod.POST, value = "/bulk", consumes = MediaTypes.CSV_VALUE)
public ResponseEntity<TemporaryResource<UserBulkResponse, AbstractRestException>> bulkUserOperationCSV(@RequestBody List<UserActionAndModel> users, HttpServletRequest servletRequest, UriComponentsBuilder builder, Authentication authentication) {
    UserBulkRequest bulkRequest = new UserBulkRequest();
    bulkRequest.setRequests(users.stream().map(actionAndModel -> {
        UserModel u = actionAndModel.getModel();
        VoAction action = actionAndModel.getAction();
        String originalUsername = actionAndModel.getOriginalUsername();
        if (originalUsername == null && u != null) {
            originalUsername = u.getUsername();
        }
        UserIndividualRequest request = new UserIndividualRequest();
        request.setAction(action == null ? VoAction.UPDATE : action);
        request.setUsername(originalUsername);
        request.setBody(u);
        return request;
    }).collect(Collectors.toList()));
    return this.bulkUserOperation(bulkRequest, servletRequest, authentication, builder);
}
Also used : UserModel(com.infiniteautomation.mango.rest.latest.model.user.UserModel) VoAction(com.infiniteautomation.mango.rest.latest.bulk.VoAction) UserIndividualRequest(com.infiniteautomation.mango.rest.latest.model.user.UserIndividualRequest) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with VoAction

use of com.infiniteautomation.mango.rest.latest.bulk.VoAction in project ma-modules-public by infiniteautomation.

the class UserRestController method doIndividualRequest.

private UserIndividualResponse doIndividualRequest(UserIndividualRequest request, VoAction defaultAction, UserModel defaultBody, PermissionHolder user, HttpServletRequest servletRequest, Authentication authentication, UriComponentsBuilder builder) {
    UserIndividualResponse result = new UserIndividualResponse();
    try {
        String username = request.getUsername();
        result.setUsername(username);
        VoAction action = request.getAction() == null ? defaultAction : request.getAction();
        if (action == null) {
            throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "action"));
        }
        result.setAction(action);
        UserModel body = request.getBody() == null ? defaultBody : request.getBody();
        switch(action) {
            case GET:
                if (username == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "xid"));
                }
                result.setBody(this.getUser(username));
                break;
            case CREATE:
                if (body == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "body"));
                }
                result.setBody(body);
                result.setBody(this.createUser(body, builder).getBody());
                break;
            case UPDATE:
                if (username == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "xid"));
                }
                if (body == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "body"));
                }
                result.setBody(body);
                result.setBody(this.updateUser(username, body, user, servletRequest, builder, authentication).getBody());
                break;
            case DELETE:
                if (username == null) {
                    throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "xid"));
                }
                result.setBody(this.deleteUser(username));
                break;
        }
    } catch (Exception e) {
        result.exceptionCaught(e);
    }
    return result;
}
Also used : UserModel(com.infiniteautomation.mango.rest.latest.model.user.UserModel) VoAction(com.infiniteautomation.mango.rest.latest.bulk.VoAction) UserIndividualResponse(com.infiniteautomation.mango.rest.latest.model.user.UserIndividualResponse) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) BadRequestException(com.infiniteautomation.mango.rest.latest.exception.BadRequestException) AbstractRestException(com.infiniteautomation.mango.rest.latest.exception.AbstractRestException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException)

Aggregations

VoAction (com.infiniteautomation.mango.rest.latest.bulk.VoAction)11 AbstractRestException (com.infiniteautomation.mango.rest.latest.exception.AbstractRestException)8 BadRequestException (com.infiniteautomation.mango.rest.latest.exception.BadRequestException)8 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)8 ApiOperation (io.swagger.annotations.ApiOperation)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)4 HttpHeaders (org.springframework.http.HttpHeaders)4 ResponseEntity (org.springframework.http.ResponseEntity)4 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)4 DataPointModel (com.infiniteautomation.mango.rest.latest.model.dataPoint.DataPointModel)3 UserModel (com.infiniteautomation.mango.rest.latest.model.user.UserModel)3 UserIndividualRequest (com.infiniteautomation.mango.rest.latest.model.user.UserIndividualRequest)2 UserIndividualResponse (com.infiniteautomation.mango.rest.latest.model.user.UserIndividualResponse)2 NotFoundRestException (com.infiniteautomation.mango.rest.latest.exception.NotFoundRestException)1 TranslatableIllegalStateException (com.infiniteautomation.mango.util.exception.TranslatableIllegalStateException)1 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)1