Search in sources :

Example 1 with AbstractDataSourceModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.dataSource.AbstractDataSourceModel in project ma-modules-public by infiniteautomation.

the class DataSourceRestController method copy.

@ApiOperation(value = "Copy data source", notes = "Copy the data source with optional new XID and Name and enable/disable state (default disabled)")
@RequestMapping(method = RequestMethod.PUT, value = "/copy/{xid}", produces = { "application/json" })
public ResponseEntity<AbstractDataSourceModel<?>> copy(@PathVariable String xid, @ApiParam(value = "Copy's new XID", required = false, defaultValue = "null", allowMultiple = false) @RequestParam(required = false, defaultValue = "null") String copyXid, @ApiParam(value = "Copy's name", required = false, defaultValue = "null", allowMultiple = false) @RequestParam(required = false, defaultValue = "null") String copyName, @ApiParam(value = "Enable/disabled state", required = false, defaultValue = "false", allowMultiple = false) @RequestParam(required = false, defaultValue = "false") boolean enabled, UriComponentsBuilder builder, HttpServletRequest request) {
    RestProcessResult<AbstractDataSourceModel<?>> result = new RestProcessResult<AbstractDataSourceModel<?>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        DataSourceVO<?> existing = DataSourceDao.instance.getByXid(xid);
        if (existing == null) {
            result.addRestMessage(getDoesNotExistMessage());
            return result.createResponseEntity();
        }
        // Check permissions
        try {
            if (!Permissions.hasDataSourcePermission(user, existing)) {
                result.addRestMessage(getUnauthorizedMessage());
                return result.createResponseEntity();
            }
        } catch (PermissionException e) {
            LOG.warn(e.getMessage(), e);
            result.addRestMessage(getUnauthorizedMessage());
            return result.createResponseEntity();
        }
        // Determine the new name
        String name;
        if (StringUtils.isEmpty(copyName))
            name = StringUtils.abbreviate(TranslatableMessage.translate(Common.getTranslations(), "common.copyPrefix", existing.getName()), 40);
        else
            name = copyName;
        // Determine the new xid
        String newXid;
        if (StringUtils.isEmpty(copyXid))
            newXid = dao.generateUniqueXid();
        else
            newXid = copyXid;
        // Setup the Copy
        DataSourceVO<?> copy = existing.copy();
        copy.setId(Common.NEW_ID);
        copy.setName(name);
        copy.setXid(newXid);
        copy.setEnabled(enabled);
        ProcessResult validation = new ProcessResult();
        copy.validate(validation);
        AbstractDataSourceModel<?> model = copy.asModel();
        if (model.validate()) {
            Common.runtimeManager.saveDataSource(copy);
            this.dao.copyDataSourcePoints(existing.getId(), copy.getId());
        } else {
            result.addRestMessage(this.getValidationFailedError());
            return result.createResponseEntity(model);
        }
        // Put a link to the updated data in the header?
        URI location = builder.path("/v1/data-sources/{xid}").buildAndExpand(copy.getXid()).toUri();
        result.addRestMessage(getResourceUpdatedMessage(location));
        return result.createResponseEntity(model);
    }
    // Not logged in
    return result.createResponseEntity();
}
Also used : AbstractDataSourceModel(com.serotonin.m2m2.web.mvc.rest.v1.model.dataSource.AbstractDataSourceModel) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) URI(java.net.URI) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with AbstractDataSourceModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.dataSource.AbstractDataSourceModel in project ma-modules-public by infiniteautomation.

the class DataSourceRestController method updateDataSource.

/**
 * Put a data source into the system
 * @param xid
 * @param model
 * @param builder
 * @param request
 * @return
 */
@ApiOperation(value = "Update data source")
@RequestMapping(method = RequestMethod.PUT, value = "/{xid}", produces = { "application/json" })
public ResponseEntity<AbstractDataSourceModel<?>> updateDataSource(@PathVariable String xid, @RequestBody(required = true) AbstractDataSourceModel<?> model, UriComponentsBuilder builder, HttpServletRequest request) {
    RestProcessResult<AbstractDataSourceModel<?>> result = new RestProcessResult<AbstractDataSourceModel<?>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        DataSourceVO<?> vo = model.getData();
        DataSourceVO<?> existing = DataSourceDao.instance.getByXid(xid);
        if (existing == null) {
            result.addRestMessage(getDoesNotExistMessage());
            return result.createResponseEntity();
        }
        // Check permissions
        try {
            if (!Permissions.hasDataSourcePermission(user, existing)) {
                result.addRestMessage(getUnauthorizedMessage());
                return result.createResponseEntity();
            }
        } catch (PermissionException e) {
            LOG.warn(e.getMessage(), e);
            result.addRestMessage(getUnauthorizedMessage());
            return result.createResponseEntity();
        }
        vo.setId(existing.getId());
        ProcessResult validation = new ProcessResult();
        vo.validate(validation);
        if (model.validate() && Permissions.hasDataSourcePermission(user, vo)) {
            Common.runtimeManager.saveDataSource(vo);
        } else {
            result.addRestMessage(this.getValidationFailedError());
            return result.createResponseEntity(model);
        }
        // Put a link to the updated data in the header?
        URI location = builder.path("/v1/data-sources/{xid}").buildAndExpand(xid).toUri();
        result.addRestMessage(getResourceUpdatedMessage(location));
        return result.createResponseEntity(model);
    }
    // Not logged in
    return result.createResponseEntity();
}
Also used : AbstractDataSourceModel(com.serotonin.m2m2.web.mvc.rest.v1.model.dataSource.AbstractDataSourceModel) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) URI(java.net.URI) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with AbstractDataSourceModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.dataSource.AbstractDataSourceModel in project ma-modules-public by infiniteautomation.

the class DataSourceRestController method deleteDataSource.

@ApiOperation(value = "Delete data source")
@RequestMapping(method = { RequestMethod.DELETE }, value = { "/{xid}" }, produces = { "application/json" })
public ResponseEntity<AbstractDataSourceModel<?>> deleteDataSource(@PathVariable String xid, UriComponentsBuilder builder, HttpServletRequest request) {
    RestProcessResult<AbstractDataSourceModel<?>> result = new RestProcessResult<AbstractDataSourceModel<?>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        DataSourceVO<?> existing = (DataSourceVO<?>) DataSourceDao.instance.getByXid(xid);
        if (existing == null) {
            result.addRestMessage(this.getDoesNotExistMessage());
            return result.createResponseEntity();
        } else {
            try {
                if (!Permissions.hasDataSourcePermission(user, existing.getId())) {
                    result.addRestMessage(this.getUnauthorizedMessage());
                    return result.createResponseEntity();
                }
            } catch (PermissionException pe) {
                LOG.warn(pe.getMessage(), pe);
                result.addRestMessage(this.getUnauthorizedMessage());
                return result.createResponseEntity();
            }
            Common.runtimeManager.deleteDataSource(existing.getId());
            return result.createResponseEntity(existing.asModel());
        }
    }
    return result.createResponseEntity();
}
Also used : AbstractDataSourceModel(com.serotonin.m2m2.web.mvc.rest.v1.model.dataSource.AbstractDataSourceModel) DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with AbstractDataSourceModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.dataSource.AbstractDataSourceModel in project ma-modules-public by infiniteautomation.

the class DataSourceRestController method getAllDataSources.

@ApiOperation(value = "Get all data sources", notes = "Only returns data sources available to logged in user")
@RequestMapping(method = RequestMethod.GET, produces = { "application/json" }, value = "/list")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<List<AbstractDataSourceModel<?>>> getAllDataSources(HttpServletRequest request) {
    RestProcessResult<List<AbstractDataSourceModel<?>>> result = new RestProcessResult<List<AbstractDataSourceModel<?>>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        List<DataSourceVO<?>> dataSources = DataSourceDao.instance.getAll();
        List<AbstractDataSourceModel<?>> models = new ArrayList<AbstractDataSourceModel<?>>();
        for (DataSourceVO<?> ds : dataSources) {
            try {
                if (Permissions.hasDataSourcePermission(user, ds))
                    models.add(ds.asModel());
            } catch (PermissionException e) {
            // Munch Munch
            }
        }
        return result.createResponseEntity(models);
    }
    return result.createResponseEntity();
}
Also used : AbstractDataSourceModel(com.serotonin.m2m2.web.mvc.rest.v1.model.dataSource.AbstractDataSourceModel) DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException) RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with AbstractDataSourceModel

use of com.serotonin.m2m2.web.mvc.rest.v1.model.dataSource.AbstractDataSourceModel in project ma-modules-public by infiniteautomation.

the class DataSourceFunctionalTests method testGetDataSource.

public void testGetDataSource() {
    User adminUser = UserTestData.adminUser();
    DataSourceVO ds = DataSourceTestData.mockDataSource();
    when(dataSourceDao.getByXid(ds.getXid())).thenReturn(ds);
    try {
        MvcResult result = this.mockMvc.perform(get("/v1/dataSources/" + ds.getXid()).sessionAttr("sessionUser", adminUser).accept(MediaType.APPLICATION_JSON)).andDo(print()).andExpect(status().isOk()).andReturn();
        AbstractDataSourceModel model = this.objectMapper.readValue(result.getResponse().getContentAsString(), MockDataSourceModel.class);
        assertEquals(model.getName(), ds.asModel().getName());
    // Check the size
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : DataSourceVO(com.serotonin.m2m2.vo.dataSource.DataSourceVO) MockDataSourceVO(com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO) AbstractDataSourceModel(com.serotonin.m2m2.web.mvc.rest.v1.model.dataSource.AbstractDataSourceModel) User(com.serotonin.m2m2.vo.User) MvcResult(org.springframework.test.web.servlet.MvcResult)

Aggregations

AbstractDataSourceModel (com.serotonin.m2m2.web.mvc.rest.v1.model.dataSource.AbstractDataSourceModel)9 User (com.serotonin.m2m2.vo.User)8 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)7 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)7 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 DataSourceVO (com.serotonin.m2m2.vo.dataSource.DataSourceVO)4 URI (java.net.URI)3 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)2 ResponseEntity (org.springframework.http.ResponseEntity)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 DataSourceDefinition (com.serotonin.m2m2.module.DataSourceDefinition)1 MockDataSourceVO (com.serotonin.m2m2.vo.dataSource.mock.MockDataSourceVO)1 ModelNotFoundException (com.serotonin.m2m2.web.mvc.rest.v1.exception.ModelNotFoundException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 MvcResult (org.springframework.test.web.servlet.MvcResult)1 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)1