use of com.serotonin.m2m2.view.text.PlainRenderer in project ma-core-public by infiniteautomation.
the class DataPointImporter method importImpl.
@Override
protected void importImpl() {
String xid = json.getString("xid");
DataPointVO vo = null;
DataSourceVO<?> dsvo = null;
if (StringUtils.isBlank(xid))
xid = ctx.getDataPointDao().generateUniqueXid();
else
vo = ctx.getDataPointDao().getDataPoint(xid);
if (vo == null) {
// Locate the data source for the point.
String dsxid = json.getString("dataSourceXid");
dsvo = ctx.getDataSourceDao().getDataSource(dsxid);
if (dsvo == null)
addFailureMessage("emport.dataPoint.badReference", xid);
else {
vo = new DataPointVO();
vo.setXid(xid);
vo.setDataSourceId(dsvo.getId());
vo.setDataSourceXid(dsxid);
vo.setPointLocator(dsvo.createPointLocator());
vo.setEventDetectors(new ArrayList<AbstractPointEventDetectorVO<?>>(0));
// Not needed as it will be set via the template or JSON or it exists in the DB already: vo.setTextRenderer(new PlainRenderer());
}
}
if (vo != null) {
try {
DataPointPropertiesTemplateVO template = null;
if (json.containsKey("templateXid")) {
String templateXid = json.getString("templateXid");
if (!StringUtils.isEmpty(templateXid))
template = (DataPointPropertiesTemplateVO) TemplateDao.instance.getByXid(templateXid);
}
// Read into the VO to get all properties
ctx.getReader().readInto(vo, json);
// Override the settings if we need to
if (template != null) {
template.updateDataPointVO(vo);
}
// If the name is not provided, default to the XID
if (StringUtils.isBlank(vo.getName()))
vo.setName(xid);
// If the chart colour is null provide default of '' to handle legacy code that sets colour to null
if (vo.getChartColour() == null)
vo.setChartColour("");
// Now validate it. Use a new response object so we can distinguish errors in this vo from
// other errors.
ProcessResult voResponse = new ProcessResult();
vo.validate(voResponse);
if (voResponse.getHasMessages())
setValidationMessages(voResponse, "emport.dataPoint.prefix", xid);
else {
// We will always override the DS Info with the one from the XID Lookup
dsvo = ctx.getDataSourceDao().getDataSource(vo.getDataSourceXid());
if (dsvo == null)
addFailureMessage("emport.dataPoint.badReference", xid);
else {
// Compare this point to the existing point in DB to ensure
// that we aren't moving a point to a different type of Data Source
DataPointVO oldPoint = ctx.getDataPointDao().getDataPoint(vo.getId(), false);
// Does the old point have a different data source?
if (oldPoint != null && (oldPoint.getDataSourceId() != dsvo.getId())) {
vo.setDataSourceId(dsvo.getId());
vo.setDataSourceName(dsvo.getName());
}
}
boolean isNew = vo.isNew();
try {
if (Common.runtimeManager.getState() == RuntimeManager.RUNNING) {
Common.runtimeManager.saveDataPoint(vo);
if (hierarchyList != null && json.containsKey(PATH)) {
String path = json.getString(PATH);
if (StringUtils.isNotEmpty(path))
hierarchyList.add(new DataPointSummaryPathPair(new DataPointSummary(vo), path));
}
addSuccessMessage(isNew, "emport.dataPoint.prefix", xid);
} else {
addFailureMessage(new ProcessMessage("Runtime Manager not running point with xid: " + xid + " not saved."));
}
} catch (LicenseViolatedException e) {
addFailureMessage(new ProcessMessage(e.getErrorMessage()));
}
}
} catch (TranslatableJsonException e) {
addFailureMessage("emport.dataPoint.prefix", xid, e.getMsg());
} catch (JsonException e) {
addFailureMessage("emport.dataPoint.prefix", xid, getJsonExceptionMessage(e));
}
}
}
use of com.serotonin.m2m2.view.text.PlainRenderer in project ma-modules-public by infiniteautomation.
the class TextRendererFactory method updateDataPoint.
/**
* @param data
* @param renderer
*/
public static void updateDataPoint(DataPointVO vo, BaseTextRendererModel<?> baseRendererModel) {
if (baseRendererModel.getType().equals(AnalogRenderer.getDefinition().getName())) {
AnalogTextRendererModel model = (AnalogTextRendererModel) baseRendererModel;
AnalogRenderer r = new AnalogRenderer();
r.setUseUnitAsSuffix(model.isUseUnitAsSuffix());
r.setUnit(UnitUtil.parseLocal(model.getUnit()));
r.setRenderedUnit(UnitUtil.parseLocal(model.getRenderedUnit()));
r.setFormat(model.getFormat());
r.setSuffix(model.getSuffix());
vo.setTextRenderer(r);
} else if (baseRendererModel.getType().equals(BinaryTextRenderer.getDefinition().getName())) {
BinaryTextRendererModel model = (BinaryTextRendererModel) baseRendererModel;
BinaryTextRenderer btr = new BinaryTextRenderer();
btr.setOneColour(model.getOneColour());
btr.setOneLabel(model.getOneLabel());
btr.setZeroColour(model.getZeroColour());
btr.setZeroLabel(model.getZeroLabel());
vo.setTextRenderer(btr);
} else if (baseRendererModel.getType().equals(MultistateRenderer.getDefinition().getName())) {
MultistateRenderer r = new MultistateRenderer();
MultistateTextRendererModel model = (MultistateTextRendererModel) baseRendererModel;
r.setMultistateValues(model.getMultistateValues());
vo.setTextRenderer(r);
} else if (baseRendererModel.getType().equals(NoneRenderer.getDefinition().getName())) {
NoneRenderer r = new NoneRenderer();
vo.setTextRenderer(r);
} else if (baseRendererModel.getType().equals(PlainRenderer.getDefinition().getName())) {
PlainTextRendererModel model = (PlainTextRendererModel) baseRendererModel;
PlainRenderer r = new PlainRenderer();
r.setUseUnitAsSuffix(model.isUseUnitAsSuffix());
r.setUnit(UnitUtil.parseLocal(model.getUnit()));
r.setRenderedUnit(UnitUtil.parseLocal(model.getRenderedUnit()));
r.setSuffix(model.getSuffix());
vo.setTextRenderer(r);
} else if (baseRendererModel.getType().equals(RangeRenderer.getDefinition().getName())) {
RangeTextRendererModel model = (RangeTextRendererModel) baseRendererModel;
RangeRenderer r = new RangeRenderer();
r.setUseUnitAsSuffix(model.isUseUnitAsSuffix());
r.setUnit(UnitUtil.parseLocal(model.getUnit()));
r.setRenderedUnit(UnitUtil.parseLocal(model.getRenderedUnit()));
r.setFormat(model.getFormat());
r.setRangeValues(model.getRangeValues());
vo.setTextRenderer(r);
} else if (baseRendererModel.getType().equals(TimeRenderer.getDefinition().getName())) {
TimeRenderer r = new TimeRenderer();
TimeTextRendererModel model = (TimeTextRendererModel) baseRendererModel;
r.setFormat(model.getFormat());
r.setConversionExponent(model.getConversionExponent());
vo.setTextRenderer(r);
} else {
throw new ShouldNeverHappenException("Unknown Text Renderer: " + baseRendererModel.getType());
}
}
use of com.serotonin.m2m2.view.text.PlainRenderer in project ma-modules-public by infiniteautomation.
the class TextRendererFactory method createModel.
/**
* @param vo
* @return
*/
public static BaseTextRendererModel<?> createModel(DataPointVO vo) {
TextRenderer renderer = vo.getTextRenderer();
// For when a VO is created that has no renderer
if (renderer == null)
return new NoneTextRendererModel();
if (renderer.getTypeName().equals(AnalogRenderer.getDefinition().getName())) {
AnalogRenderer r = (AnalogRenderer) renderer;
AnalogTextRendererModel model = new AnalogTextRendererModel(r.isUseUnitAsSuffix(), UnitUtil.formatLocal(r.getUnit()), UnitUtil.formatLocal(r.getRenderedUnit()), r.getFormat(), r.getSuffix());
return model;
} else if (renderer.getTypeName().equals(BinaryTextRenderer.getDefinition().getName())) {
BinaryTextRendererModel model = new BinaryTextRendererModel();
BinaryTextRenderer btr = (BinaryTextRenderer) renderer;
model.setOneColour(btr.getOneColour());
model.setOneLabel(btr.getOneLabel());
model.setZeroColour(btr.getZeroColour());
model.setZeroLabel(btr.getZeroLabel());
return model;
} else if (renderer.getTypeName().equals(MultistateRenderer.getDefinition().getName())) {
MultistateRenderer r = (MultistateRenderer) renderer;
MultistateTextRendererModel model = new MultistateTextRendererModel(r.getMultistateValues());
return model;
} else if (renderer.getTypeName().equals(NoneRenderer.getDefinition().getName())) {
return new NoneTextRendererModel();
}
if (renderer.getTypeName().equals(PlainRenderer.getDefinition().getName())) {
PlainRenderer r = (PlainRenderer) renderer;
PlainTextRendererModel model = new PlainTextRendererModel(r.isUseUnitAsSuffix(), UnitUtil.formatLocal(r.getUnit()), UnitUtil.formatLocal(r.getRenderedUnit()), r.getSuffix());
return model;
} else if (renderer.getTypeName().equals(RangeRenderer.getDefinition().getName())) {
RangeRenderer r = (RangeRenderer) renderer;
RangeTextRendererModel model = new RangeTextRendererModel(r.isUseUnitAsSuffix(), UnitUtil.formatLocal(r.getUnit()), UnitUtil.formatLocal(r.getRenderedUnit()), r.getFormat(), r.getRangeValues());
return model;
} else if (renderer.getTypeName().equals(TimeRenderer.getDefinition().getName())) {
TimeRenderer r = (TimeRenderer) renderer;
TimeTextRendererModel model = new TimeTextRendererModel(r.getFormat(), r.getConversionExponent());
return model;
} else {
throw new ShouldNeverHappenException("Unknown Text Renderer: " + renderer.getDef().getName());
}
}
use of com.serotonin.m2m2.view.text.PlainRenderer in project ma-modules-public by infiniteautomation.
the class DataPointRestController method updateDataPoint.
/**
* Update a data point in the system
* @param vo
* @param xid
* @param builder
* @param request
* @return
*/
@ApiOperation(value = "Update an existing data point", notes = "Content may be CSV or JSON")
@RequestMapping(method = RequestMethod.PUT, consumes = { "application/json", "text/csv", "application/sero-json" }, produces = { "application/json", "text/csv", "application/sero-json" }, value = "/{xid}")
public ResponseEntity<DataPointModel> updateDataPoint(@PathVariable String xid, @ApiParam(value = "Updated data point model", required = true) @RequestBody(required = true) DataPointModel model, UriComponentsBuilder builder, HttpServletRequest request) {
RestProcessResult<DataPointModel> result = new RestProcessResult<DataPointModel>(HttpStatus.OK);
User user = this.checkUser(request, result);
if (result.isOk()) {
boolean contentTypeCsv = false;
if (request.getContentType().toLowerCase().contains("text/csv"))
contentTypeCsv = true;
DataPointVO vo = model.getData();
DataPointVO existingDp = DataPointDao.instance.getByXid(xid);
if (existingDp == null) {
result.addRestMessage(getDoesNotExistMessage());
return result.createResponseEntity();
}
// We will always override the DS Info with the one from the XID Lookup
DataSourceVO<?> dsvo = DataSourceDao.instance.getDataSource(existingDp.getDataSourceXid());
// TODO this implies that we may need to have a different JSON Converter for data points
// Need to set DataSourceId among other things
vo.setDataSourceId(existingDp.getDataSourceId());
// Check permissions
try {
if (!Permissions.hasDataSourcePermission(user, vo.getDataSourceId())) {
result.addRestMessage(getUnauthorizedMessage());
return result.createResponseEntity();
}
} catch (PermissionException e) {
result.addRestMessage(getUnauthorizedMessage());
return result.createResponseEntity();
}
vo.setId(existingDp.getId());
// Set all properties that are not in the template or the spreadsheet
// Use ID to get detectors
DataPointDao.instance.setEventDetectors(vo);
vo.setPointFolderId(existingDp.getPointFolderId());
if (vo.getTextRenderer() == null) {
vo.setTextRenderer(new PlainRenderer());
}
if (vo.getChartColour() == null) {
vo.setChartColour("");
}
// Check the Template and see if we need to use it
if (model.getTemplateXid() != null) {
DataPointPropertiesTemplateVO template = (DataPointPropertiesTemplateVO) TemplateDao.instance.getByXid(model.getTemplateXid());
if (template != null) {
template.updateDataPointVO(vo);
template.updateDataPointVO(model.getData());
} else {
model.addValidationMessage("validate.invalidReference", RestMessageLevel.ERROR, "templateXid");
result.addRestMessage(new RestMessage(HttpStatus.NOT_ACCEPTABLE, new TranslatableMessage("emport.dataPoint.badReference", model.getTemplateXid())));
}
} else {
if (contentTypeCsv) {
model.addValidationMessage("validate.required", RestMessageLevel.ERROR, "templateXid");
result.addRestMessage(this.getValidationFailedError());
return result.createResponseEntity(model);
}
}
if (!model.validate()) {
result.addRestMessage(this.getValidationFailedError());
return result.createResponseEntity(model);
} else {
if (dsvo == null) {
result.addRestMessage(HttpStatus.NOT_ACCEPTABLE, new TranslatableMessage("emport.dataPoint.badReference", xid));
return result.createResponseEntity();
} else {
// Does the old point have a different data source?
if (existingDp.getDataSourceId() != dsvo.getId()) {
vo.setDataSourceId(dsvo.getId());
vo.setDataSourceName(dsvo.getName());
}
}
Common.runtimeManager.saveDataPoint(vo);
}
// Put a link to the updated data in the header?
URI location = builder.path("/v1/data-points/{xid}").buildAndExpand(vo.getXid()).toUri();
result.addRestMessage(getResourceUpdatedMessage(location));
return result.createResponseEntity(model);
}
// Not logged in
return result.createResponseEntity();
}
use of com.serotonin.m2m2.view.text.PlainRenderer in project ma-modules-public by infiniteautomation.
the class DataPointRestController method saveDataPoint.
@ApiOperation(value = "Create a data point", notes = "Content may be CSV or JSON")
@RequestMapping(method = RequestMethod.POST, consumes = { "application/json", "text/csv", "application/sero-json" }, produces = { "application/json", "text/csv", "application/sero-json" })
public ResponseEntity<DataPointModel> saveDataPoint(@ApiParam(value = "Data point model", required = true) @RequestBody(required = true) DataPointModel model, UriComponentsBuilder builder, HttpServletRequest request) {
RestProcessResult<DataPointModel> result = new RestProcessResult<DataPointModel>(HttpStatus.OK);
User user = this.checkUser(request, result);
if (result.isOk()) {
boolean contentTypeCsv = false;
if (request.getContentType().toLowerCase().contains("text/csv"))
contentTypeCsv = true;
DataPointVO vo = model.getData();
// Check to see if the point already exists
if (!StringUtils.isEmpty(vo.getXid())) {
DataPointVO existing = this.dao.getByXid(vo.getXid());
if (existing != null) {
result.addRestMessage(HttpStatus.CONFLICT, new TranslatableMessage("rest.exception.alreadyExists", model.getXid()));
return result.createResponseEntity();
}
}
// Ensure ds exists
DataSourceVO<?> dataSource = DataSourceDao.instance.getByXid(model.getDataSourceXid());
// We will always override the DS Info with the one from the XID Lookup
if (dataSource == null) {
result.addRestMessage(HttpStatus.NOT_ACCEPTABLE, new TranslatableMessage("emport.dataPoint.badReference", model.getDataSourceXid()));
return result.createResponseEntity();
} else {
vo.setDataSourceId(dataSource.getId());
vo.setDataSourceName(dataSource.getName());
}
// Check permissions
try {
if (!Permissions.hasDataSourcePermission(user, vo.getDataSourceId())) {
result.addRestMessage(getUnauthorizedMessage());
return result.createResponseEntity();
}
} catch (PermissionException e) {
result.addRestMessage(getUnauthorizedMessage());
return result.createResponseEntity();
}
if (vo.getTextRenderer() == null) {
vo.setTextRenderer(new PlainRenderer());
}
if (vo.getChartColour() == null) {
vo.setChartColour("");
}
// Check the Template and see if we need to use it
if (model.getTemplateXid() != null) {
DataPointPropertiesTemplateVO template = (DataPointPropertiesTemplateVO) TemplateDao.instance.getByXid(model.getTemplateXid());
if (template == null) {
model.addValidationMessage("validate.invalidReference", RestMessageLevel.ERROR, "templateXid");
result.addRestMessage(new RestMessage(HttpStatus.NOT_ACCEPTABLE, new TranslatableMessage("emport.dataPoint.badReference", model.getTemplateXid())));
}
} else {
if (contentTypeCsv) {
model.addValidationMessage("validate.required", RestMessageLevel.ERROR, "templateXid");
result.addRestMessage(this.getValidationFailedError());
return result.createResponseEntity(model);
}
}
if (StringUtils.isEmpty(vo.getXid()))
vo.setXid(DataPointDao.instance.generateUniqueXid());
// allow empty string, but if its null use the data source name
if (vo.getDeviceName() == null) {
vo.setDeviceName(dataSource.getName());
}
if (!model.validate()) {
result.addRestMessage(this.getValidationFailedError());
return result.createResponseEntity(model);
} else {
try {
Common.runtimeManager.saveDataPoint(vo);
} catch (LicenseViolatedException e) {
result.addRestMessage(HttpStatus.METHOD_NOT_ALLOWED, e.getErrorMessage());
}
}
// Put a link to the updated data in the header?
URI location = builder.path("/v1/data-points/{xid}").buildAndExpand(vo.getXid()).toUri();
result.addRestMessage(getResourceUpdatedMessage(location));
return result.createResponseEntity(model);
}
// Not logged in
return result.createResponseEntity();
}
Aggregations