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();
}
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();
}
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();
}
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();
}
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());
}
}
Aggregations